Nothing Special   »   [go: up one dir, main page]

2.2.1 • Published 9 years ago

dom-serialize v2.2.1

Weekly downloads
1,895,902
License
MIT
Repository
github
Last release
9 years ago

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>
karma@arisageha/react-lazyloadkarma-3@jginsburgn/karmalevibestliblevibestlib2levilibtest19levilibtest24levilibtest25levilibtest26levilibtest27levilibtest28levilibtest29@infinitebrahmanuniverse/nolb-dom-cclibyarntestopea-bootstraap@everything-registry/sub-chunk-1500winx-form-winxwebdatabasetest1webdatabasetest10sajisiptestweblibapitestlib17superhuman-karmavcloudcam-playkit-js-hlsvideo-multi-uploadervue-dev-clonemiguelcostero-ng2-toastyjavascript-serializelevilibtest10levilibtest11levilibtest12levilibtest13levilibtest14levilibtest15levilibtest16levilibtest17levilibtest18levilibtest21levilibtest22levilibtest23levilibtest3levilibtest4levilibtest5levilibtest6levilibtest7levilibtest8levilibtest9levimultiplatform10levimultiplatformtestlevimultitestlevicommontestlevicommontest1levicotestlevikotlinliblevikotlintestleviapitest1mpd-parser-1kotlinapitestkotlinapitest1kotlinapitest2karma-with-custom-context-htmlkarma-yarn-berrykarma-tidykarma-tskarma-scrivitokarma-soyka-flow.jsmynancelucas-karma-boxopea-bootstrapowt-client-javascriptplaykit-js-hls-sondqnewlibtestng-search-dropdownmultiplatformlevimyreuselibrarymyui5librarychenmyui5librarymobonovathore-karma-forkedstock-ticker-react-widgetsitoptio@caplin/karma@cdevine49/react-numeric-inputzeditorwebliblevi3@garlictech/karma@ngxvoice/ngx-voicelistner@fabwcie/ckeditor5-preview@hieuquang2212/form@inesfidalgo/forge@parkcedar/leaflet@seculum/vue-dev-clonecauseway-concrete-stylesapilibtestanotherweblibaxios-yet-another-proxy-fix@thedoctor0/vuex-shared-mutationsestporrofenglin-uploader
2.2.1

9 years ago

2.2.0

10 years ago

2.1.0

10 years ago

2.0.1

10 years ago

2.0.0

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago