Skip to content

Instantly share code, notes, and snippets.

@audinue
Created April 25, 2016 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audinue/2c85be08ea5a1d068563c0d04ebc9858 to your computer and use it in GitHub Desktop.
Save audinue/2c85be08ea5a1d068563c0d04ebc9858 to your computer and use it in GitHub Desktop.
var div = document.createElement('div');
var wraps = {
'option': [1, '<select multiple="multiple">'],
'legend': [1, '<fieldset>'],
'thead' : [1, '<table>'],
'tr' : [2, '<table><tbody>'],
'td' : [3, '<table><tbody><tr>'],
'col' : [2, '<table><tbody></tbody><colgroup>'],
'area' : [1, '<map>'],
};
wraps['optgroup'] = wraps['option'];
wraps['tbody'] = wraps['tfoot'] = wraps['colgroup'] = wraps['caption'] = wraps['thead'];
wraps['th'] = wraps['td'];
var keys = Object.keys(wraps);
keys.sort();
var needsToBewrapsped = new RegExp('^\s*<(' + keys.join('|') + ')', 'i');
function parseHTML(html) {
var match = html.match(needsToBewrapsped);
var node;
if(match != null) {
var wrap = wraps[match[1]];
div.innerHTML = wrap[1] + html;
node = div.firstChild;
var deep = wrap[0];
while(deep--) {
node = node.firstChild;
}
} else {
div.innerHTML = html;
node = div.firstChild;
}
return node.parentNode.removeChild(node);
}
console.log(parseHTML('<option value="1"/>'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment