Skip to content

Commit

Permalink
Fix parsing minified html
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jan 31, 2023
1 parent 387662a commit ea9fcf0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -68,8 +68,8 @@ var boolAttrs = {
var match, child
, node = this
, doc = node.ownerDocument || node
, tagRe = /<(!--([\s\S]*?)--!?|!\[[\s\S]*?\]|[?!][\s\S]*?)>|<(\/?)([^ \/>]+)([^>]*?)(\/?)>|[^<]+/g
, attrRe = /([^= ]+)(?:\s*=\s*(("|')((?:\\\3|.)*?)\3|[-.:\w]+)|)/g
, tagRe = /<(!--([\s\S]*?)--!?|!\[[\s\S]*?\]|[?!][\s\S]*?)>|<(\/?)([^ \/>]+)((?:("|')(?:\\\6|.)*?\6|[^>])*?)(\/?)>|[^<]+/g
, attrRe = /([^= ]+)(?:\s*=\s*(("|')((?:\\\3|.)*?)\3|[^\s"'`=<>]+)|)/g
, frag = doc.createDocumentFragment()
, tree = frag

Expand All @@ -84,7 +84,7 @@ var boolAttrs = {
match[5].replace(attrRe, setAttr)
}
tree.appendChild(child)
if (!voidElements[child.tagName] && !match[6]) tree = child
if (!voidElements[child.tagName] && !match[7]) tree = child
} else {
tree.appendChild(
match[2] ? doc.createComment(match[2].replace(unescRe, unescFn)) :
Expand Down Expand Up @@ -416,7 +416,7 @@ function XMLSerializer() {}

DOMParser.prototype.parseFromString = function(str, mime) {
var doc = new Document()
doc.contentType = mime
doc.contentType = mime || "text/html"
doc.documentElement.outerHTML = str
return doc
}
Expand Down
4 changes: 3 additions & 1 deletion test/parser.js
Expand Up @@ -20,11 +20,13 @@ describe("parser", function() {
assert.end()
})
test("minify", function (assert) {
var minSamp1
assert.matchSnapshot("./test/samp1.html", function(str) {
var document = new DOM.Document()
document.documentElement.outerHTML = str
return document.toString(true)
return (minSamp1 = document.toString(true))
})
assert.equal(parser.parseFromString(minSamp1).toString(true), minSamp1)
assert.matchSnapshot("./test/samp2.html", function(str) {
var document = new DOM.Document()
document.documentElement.outerHTML = str
Expand Down

0 comments on commit ea9fcf0

Please sign in to comment.