Skip to content

Commit

Permalink
fix: be more permissive with script and style. fixes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Aug 29, 2024
1 parent 1224e46 commit d7f7489
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
36 changes: 20 additions & 16 deletions src/htmlparser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,23 @@ describe('htmlparser', () => {
const html = `<script>
var x = 1 & 4
window.addEventListener('load', function () {
$('body')
.attr('data-spy', 'scroll')
.attr('data-offset', '88')
.attr('data-target', '#outline')
if (x<1)
$('body')
.attr('data-spy', 'scroll')
.attr('data-offset', '88')
.attr('data-target', '#outline')
})
</script>`
const dom = parseHTML(html) as VHTMLDocument
expect(dom.textContent).toMatchInlineSnapshot(`
"
var x = 1 & 4
window.addEventListener('load', function () {
\$('body')
.attr('data-spy', 'scroll')
.attr('data-offset', '88')
.attr('data-target', '#outline')
if (x<1)
$('body')
.attr('data-spy', 'scroll')
.attr('data-offset', '88')
.attr('data-target', '#outline')
})
"
`)
Expand All @@ -77,10 +79,11 @@ $('body')
"_text": "
var x = 1 & 4
window.addEventListener('load', function () {
$('body')
.attr('data-spy', 'scroll')
.attr('data-offset', '88')
.attr('data-target', '#outline')
if (x<1)
$('body')
.attr('data-spy', 'scroll')
.attr('data-offset', '88')
.attr('data-target', '#outline')
})
",
"append": [Function],
Expand All @@ -101,10 +104,11 @@ $('body')
"<script>
var x = 1 & 4
window.addEventListener('load', function () {
\$('body')
.attr('data-spy', 'scroll')
.attr('data-offset', '88')
.attr('data-target', '#outline')
if (x<1)
$('body')
.attr('data-spy', 'scroll')
.attr('data-offset', '88')
.attr('data-target', '#outline')
})
</script>"
`)
Expand Down
17 changes: 15 additions & 2 deletions src/htmlparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class HtmlParser {
treatAsChars = true
}
}

// end tag
else if (html.substring(0, 2) === '</') {
match = this.endTagRe.exec(html)
Expand All @@ -66,13 +67,25 @@ export class HtmlParser {
treatAsChars = true
}
}

// start tag
else if (html.charAt(0) === '<') {
match = this.startTagRe.exec(html)
if (match) {
html = RegExp.rightContext
treatAsChars = false
this.parseStartTag(RegExp.lastMatch, match[1], match)
let tagName = this.parseStartTag(RegExp.lastMatch, match[1], match)
if (tagName === 'script' || tagName === 'style') {
index = html.search(new RegExp(`<\/${tagName}`, 'i'))
if (index !== -1) {
this.scanner.characters(html.substring(0, index))
html = html.substring(index)
treatAsChars = false
}
else {
treatAsChars = true
}
}
}
else {
treatAsChars = true
Expand Down Expand Up @@ -113,9 +126,9 @@ export class HtmlParser {
let attrInput = match[2]
if (isSelfColse)
attrInput = attrInput.replace(/\s*\/\s*$/, '')

const attrs = this.parseAttributes(tagName, attrInput)
this.scanner.startElement(tagName, attrs, isSelfColse, match[0])
return tagName.toLocaleLowerCase()
}

parseEndTag(input: string, tagName: string) {
Expand Down

0 comments on commit d7f7489

Please sign in to comment.