parse5-utilities
v1.0.0
Published
Low-level parse5 node manipulation utilities. ESM-compatible version of parse5-utils.
Readme
parse5-utilities
Low-level parse5 node manipulation utilities.
API
parse
Parse an HTML string. If
smartis true, returns adocumentordocumentFragmentbased on the input. Otherwise, always parses it as a document.
parse(html: string, smart?: boolean): Document | DocumentFragmentcreateFragment
Parses HTML as a fragment.
createFragment(html: string): DocumentFragmentstringify
Converts an AST node into an HTML string.
stringify(node: Node): stringattributesOf
Get the attributes of a node as an object.
attributesOf(node: Node): Record<string, string>setAttribute
Set an attribute on a node.
setAttribute(node: Node, key: string, value: string): NodegetAttribute
Get an attribute value from a node.
getAttribute(node: Node, key: string): string | undefinedremoveAttribute
Remove an attribute from a node.
removeAttribute(node: Node, key: string): voidtoAttrs
Convert an object of attributes into an array of attribute objects.
toAttrs(attributes: Record<string, string>): Attribute[]createNode
Create a new element node with the specified tag name.
createNode(tagName: string): ElementcreateTextNode
Create a new text node with the specified text content.
createTextNode(text: string): TextNodeprepend
Add a child to a node, making it the first child.
prepend(parent: ParentNode, node: ChildNode): ChildNodeappend
Add a child to a node, making it the last child.
append(parent: ParentNode, node: ChildNode): ChildNodereplace
Replace a node with another node.
replace(originalNode: ChildNode, newNode: ChildNode): ChildNode | undefinedremove
Remove a node from its parent.
remove(node: ChildNode): ChildNodeflatten
Get all the nodes in a tree as a flat array.
flatten(node: Node | Node[]): Node[]textOf
Get the text content of a node. Throws an error if the node has multiple children or non-text children.
textOf(node: ParentNode): stringsetText
Set the text content of a node.
setText(node: ParentNode, text: string): ParentNodeisDocument
Check if a string is likely a complete HTML document.
isDocument(string: string): booleanLicense
MIT License
Copyright (c) 2014 Jonathan Ong [email protected] Copyright (c) 2025 Cezar Augusto [email protected]
