Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* jQuery 2.1.3's parseHTML (without scripts options).
* Unlike jQuery, this returns a DocumentFragment, which is more convenient to insert into DOM.
* MIT license.
*
* If you only support Edge 13+ then try this:
function parseHTML(html, context) {
var t = (context || document).createElement('template');
t.innerHTML = html;
return t.content.cloneNode(true);
}
*/
var parseHTML = (function() {
var rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi,
rtagName = /<([\w:]+)/,
rhtml = /<|&#?\w+;/,
// We have to close these tags to support XHTML (#13200)
wrapMap = {
// Support: IE9
option: [1, "<select multiple='multiple'>", "</select>"],
thead: [1, "<table>", "</table>"],
col: [2, "<table><colgroup>", "</colgroup></table>"],
tr: [2, "<table><tbody>", "</tbody></table>"],
td: [3, "<table><tbody><tr>", "</tr></tbody></table>"],
_default: [0, "", ""]
};
/**
* @param {String} elem A string containing html
* @param {Document} context
*/
return function parseHTML(elem, context) {
context = context || document;
var tmp, tag, wrap, j,
fragment = context.createDocumentFragment();
if (!rhtml.test(elem)) {
fragment.appendChild(context.createTextNode(elem));
// Convert html into DOM nodes
} else {
tmp = fragment.appendChild(context.createElement("div"));
// Deserialize a standard representation
tag = (rtagName.exec(elem) || ["", ""])[1].toLowerCase();
wrap = wrapMap[tag] || wrapMap._default;
tmp.innerHTML = wrap[1] + elem.replace(rxhtmlTag, "<$1></$2>") + wrap[2];
// Descend through wrappers to the right content
j = wrap[0];
while (j--) {
tmp = tmp.lastChild;
}
// Remove wrappers and append created nodes to fragment
fragment.removeChild(fragment.firstChild);
while (tmp.firstChild) {
fragment.appendChild(tmp.firstChild);
}
}
return fragment;
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.