dom-serialize
    Overview
    Documentation
    Tutorials
    Insights
    Code
    Dependencies
    Contributors
    Jobs

dom-serialize

Serializes any DOM node into a String

2.2.1  •  Updated 3 years ago  •  by webmodules  •  MIT License

dom-serialize

Serializes any DOM node into a String

Sauce Test Status

Build Status

It’s like outerHTML, but it works with:

  • DOM elements
  • Text nodes
  • Attributes
  • Comment nodes
  • Documents
  • DocumentFragments
  • Doctypes
  • NodeLists / Arrays

For custom serialization logic, a “serialize” event is dispatched on every Node which event listeners can override the default behavior on by setting the event.detail.serialize property to a String or other Node.

The “serialize” event bubbles, so it could be a good idea to utilize event delegation on a known root node that will be serialized. Check the event.serializeTarget property to check which Node is currently being serialized.

Installation

$ npm install dom-serialize

Example

var serialize = require('dom-serialize');
var node;

// works with Text nodes
node = document.createTextNode('foo & <bar>');
console.log(serialize(node));


// works with DOM elements
node = document.createElement('body');
node.appendChild(document.createElement('strong'));
node.firstChild.appendChild(document.createTextNode('hello'));
console.log(serialize(node));


// custom "serialize" event
node.firstChild.addEventListener('serialize', function (event) {
  event.detail.serialize = 'pwn';
}, false);
console.log(serialize(node));


// you can also just pass a function in for a one-time serializer
console.log(serialize(node, function (event) {
  if (event.serializeTarget === node.firstChild) {
    // for the first child, output an ellipsis to summarize "content"
    event.detail.serialze = '…';
  } else if (event.serializeTarget !== node) {
    // any other child
    event.preventDefault();
  }
}));
foo &amp; &lt;bar&gt;
<body><strong>hello</strong></body>
<body>pwn</body>
<body>…</body>

Popularity

Weekly Downloads
1.3M
Stars
34

Maintenance

Development

Last ver 4 years ago
Created 5 years ago
Last commit 3 years ago
17 days between commits

Technology

Node version: 4.1.2
0 unpacked

Compliance

MIT License
OSI Approved
0 vulnerabilities

Contributors

3 contributors
Nathan Rajlich
Maintainer, 51 commits, 2 merges
Works at zeit
Shinnosuke Watanabe
1 commits, 1 PRs
Thomas Shafer
1 commits, 1 PRs
Works at google
Tony Kovanen
Maintainer
Works at zeit
Guillermo Rauch
Maintainer
Works at https://zeit.co
philipahlberg
1 PRs

Tags

browser
node
dom
serialize
string
Openbase helps developers choose among and use millions of open-source packages, so they can build amazing products faster.
FacebookTwitterLinkedIn
© 2020 Devstore, Inc.