You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
varserialize=require('dom-serialize');varnode;// works with Text nodesnode=document.createTextNode('foo & <bar>');console.log(serialize(node));// works with DOM elementsnode=document.createElement('body');node.appendChild(document.createElement('strong'));node.firstChild.appendChild(document.createTextNode('hello'));console.log(serialize(node));// custom "serialize" eventnode.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 serializerconsole.log(serialize(node,function(event){if(event.serializeTarget===node.firstChild){// for the first child, output an ellipsis to summarize "content"event.detail.serialze='…';}elseif(event.serializeTarget!==node){// any other childevent.preventDefault();}}));
foo & <bar>
<body><strong>hello</strong></body>
<body>pwn</body>
<body>…</body>