\hypertarget{md_sfcode_node_modules_dom_serialize_README_autotoc_md8258}{}\doxysubsection{Serializes any DOM node into a String}\label{md_sfcode_node_modules_dom_serialize_README_autotoc_md8258}
\href{https://saucelabs.com/u/dom-serialize}{\texttt{ }}

\href{https://travis-ci.org/webmodules/dom-serialize}{\texttt{ }}

It\textquotesingle{}s like {\ttfamily outer\+HTML}, but it works with\+:


\begin{DoxyItemize}
\item DOM elements
\item Text nodes
\item Attributes
\item Comment nodes
\item Documents
\item Document\+Fragments
\item Doctypes
\item Node\+Lists / Arrays
\end{DoxyItemize}

For custom serialization logic, a \char`\"{}serialize\char`\"{} event is dispatched on every {\ttfamily Node} which event listeners can override the default behavior on by setting the {\ttfamily event.\+detail.\+serialize} property to a String or other Node.

The \char`\"{}serialize\char`\"{} event bubbles, so it could be a good idea to utilize event delegation on a known root node that will be serialized. Check the {\ttfamily event.\+serialize\+Target} property to check which {\ttfamily Node} is currently being serialized.\hypertarget{md_sfcode_node_modules_dom_serialize_README_autotoc_md8259}{}\doxysection{Installation}\label{md_sfcode_node_modules_dom_serialize_README_autotoc_md8259}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install dom-\/serialize }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dom_serialize_README_autotoc_md8260}{}\doxysection{Example}\label{md_sfcode_node_modules_dom_serialize_README_autotoc_md8260}

\begin{DoxyCode}{0}
\DoxyCodeLine{var serialize = require('dom-\/serialize');}
\DoxyCodeLine{var node;}
\DoxyCodeLine{}
\DoxyCodeLine{// works with Text nodes}
\DoxyCodeLine{node = document.createTextNode('foo \& <bar>');}
\DoxyCodeLine{console.log(serialize(node));}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{// works with DOM elements}
\DoxyCodeLine{node = document.createElement('body');}
\DoxyCodeLine{node.appendChild(document.createElement('strong'));}
\DoxyCodeLine{node.firstChild.appendChild(document.createTextNode('hello'));}
\DoxyCodeLine{console.log(serialize(node));}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{// custom "{}serialize"{} event}
\DoxyCodeLine{node.firstChild.addEventListener('serialize', function (event) \{}
\DoxyCodeLine{  event.detail.serialize = 'pwn';}
\DoxyCodeLine{\}, false);}
\DoxyCodeLine{console.log(serialize(node));}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{// you can also just pass a function in for a one-\/time serializer}
\DoxyCodeLine{console.log(serialize(node, function (event) \{}
\DoxyCodeLine{  if (event.serializeTarget === node.firstChild) \{}
\DoxyCodeLine{    // for the first child, output an ellipsis to summarize "{}content"{}}
\DoxyCodeLine{    event.detail.serialze = '…';}
\DoxyCodeLine{  \} else if (event.serializeTarget !== node) \{}
\DoxyCodeLine{    // any other child}
\DoxyCodeLine{    event.preventDefault();}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\})); }

\end{DoxyCode}



\begin{DoxyCode}{0}
\DoxyCodeLine{foo \&amp; \&lt;bar\&gt;}
\DoxyCodeLine{<body><strong>hello</strong></body>}
\DoxyCodeLine{<body>pwn</body>}
\DoxyCodeLine{<body>…</body> }

\end{DoxyCode}
 