What is dom-serializer?
The dom-serializer package is used to serialize DOM nodes to a string representation, typically HTML or XML. It is useful for transforming the DOM tree into a textual format that can be saved, transmitted, or manipulated as a string.
What are dom-serializer's main functionalities?
Serializing DOM nodes to HTML
This feature allows you to serialize a DOM node into an HTML string. The code sample demonstrates how to serialize a simple DOM element using dom-serializer.
const serialize = require('dom-serializer');
const dom = require('domhandler');
const root = new dom.Element('div', { class: 'container' });
const serialized = serialize(root);
console.log(serialized); // Outputs: <div class="container"></div>
Custom formatting options
dom-serializer allows for custom formatting options such as xmlMode, decodeEntities, and selfClosingTags. This code sample shows how to serialize a DOM element with XML formatting.
const serialize = require('dom-serializer');
const dom = require('domhandler');
const root = new dom.Element('div', { class: 'container' });
const options = { xmlMode: true };
const serialized = serialize(root, options);
console.log(serialized); // Outputs: <div class="container"/>
Other packages similar to dom-serializer
parse5
parse5 is a fast full-featured HTML parsing and serialization library. It provides a variety of modules for parsing, serializing, and manipulating HTML documents. Compared to dom-serializer, parse5 offers a more comprehensive suite of HTML processing capabilities.
jsdom
jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards. It creates a virtual DOM and is capable of serializing and manipulating it. While dom-serializer focuses on serialization, jsdom offers a broader range of features including DOM emulation, scripting, and event simulation.
htmlparser2
htmlparser2 is a forgiving HTML and XML parser. It can also be used in conjunction with domhandler to create a DOM tree which can then be serialized. It is similar to dom-serializer in that it can be used to serialize DOM structures, but it also includes robust parsing capabilities.
dom-serializer
Renders a domhandler DOM node or an array of domhandler DOM nodes to a string.
import render from "dom-serializer";
const render = require("dom-serializer").default;
API
render
▸ render(node
: Node | Node[], options?
: Options): string
Renders a DOM node or an array of DOM nodes to a string.
Can be thought of as the equivalent of the outerHTML
of the passed node(s).
Parameters:
Name | Type | Default value | Description |
---|
node | Node | Node[] | - | Node to be rendered. |
options | DomSerializerOptions | {} | Changes serialization behavior |
Returns: string
Options
encodeEntities
• Optional
decodeEntities: boolean | "utf8"
Encode characters that are either reserved in HTML or XML.
If xmlMode
is true
or the value not 'utf8'
, characters outside of the utf8 range will be encoded as well.
default
decodeEntities
decodeEntities
• Optional
decodeEntities: boolean
Option inherited from parsing; will be used as the default value for encodeEntities
.
default
true
emptyAttrs
• Optional
emptyAttrs: boolean
Print an empty attribute's value.
default
xmlMode
example
With emptyAttrs: false
: <input checked>
example
With emptyAttrs: true
: <input checked="">
selfClosingTags
• Optional
selfClosingTags: boolean
Print self-closing tags for tags without contents.
default
xmlMode
example
With selfClosingTags: false
: <foo></foo>
example
With selfClosingTags: true
: <foo />
xmlMode
• Optional
xmlMode: boolean | "foreign"
Treat the input as an XML document; enables the emptyAttrs
and selfClosingTags
options.
If the value is "foreign"
, it will try to correct mixed-case attribute names.
default
false
Ecosystem
Name | Description |
---|
htmlparser2 | Fast & forgiving HTML/XML parser |
domhandler | Handler for htmlparser2 that turns documents into a DOM |
domutils | Utilities for working with domhandler's DOM |
css-select | CSS selector engine, compatible with domhandler's DOM |
cheerio | The jQuery API for domhandler's DOM |
dom-serializer | Serializer for domhandler's DOM |
LICENSE: MIT