web.str2dom.js Maven / Gradle / Ivy
The newest version!
var str2DOMElement = function(html) {
var wrapMap = {
option: [ 1, "" ],
legend: [ 1, "" ],
area: [ 1, "" ],
param: [ 1, "" ],
thead: [ 1, "", "
" ],
tr: [ 2, "", "
" ],
col: [ 2, "", "
" ],
td: [ 3, "", "
" ],
body: [0, "", ""],
_default: [ 1, "", "" ]
};
wrapMap.optgroup = wrapMap.option;
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
wrapMap.th = wrapMap.td;
var match = /<\s*\w.*?>/g.exec(html);
var element = document.createElement('div');
if(match != null) {
var tag = match[0].replace(//g, '').split(' ')[0];
if(tag.toLowerCase() === 'body') {
var dom = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
var body = document.createElement("body");
// keeping the attributes
element.innerHTML = html.replace(//g, '