Skip to content

Commit

Permalink
Merge pull request #9 from estrattonbailey/fix-recursion-failure
Browse files Browse the repository at this point in the history
Fix infinite loop in `HtmlParser`
  • Loading branch information
holtwick authored Oct 11, 2023
2 parents e00555f + 9a3b2d7 commit 3669c26
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/htmlparser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe("htmlparser", () => {
<title>Test</title>
</head>
<body>
<pre><code class="lang-jsx">
<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>)}
<pre><code class="lang-jsx">
<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>)}
</code></pre>
</body>
</html>`
Expand Down Expand Up @@ -107,4 +107,24 @@ $('body')
</script>"
`)
})

it("should not recurse on bad fragment", () => {
const tests = [
'<',
'<<',
'<<<',
'<<div',
'<div<div',
'>',
'>>',
'>>>',
'>>div',
'>div>div',
]

for (const t of tests) {
const { textContent: text } = parseHTML(t) as VHTMLDocument
expect(text).toEqual(t)
}
})
})
7 changes: 5 additions & 2 deletions src/htmlparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,22 @@ export class HtmlParser {

if (treatAsChars) {
index = html.indexOf('<')
let offset = index

if (index === 0) {
// First char is a < so find the next one
index = html.substring(1).indexOf('<')
// We're at substring(1) so add 1 to the index
offset = offset + 1
}

if (index === -1) {
characters = html
html = ''
}
else {
characters = html.substring(0, index)
html = html.substring(index)
characters = html.substring(0, offset)
html = html.substring(offset)
}

if (!this.options.ignoreWhitespaceText || !/^\s*$/.test(characters))
Expand Down

0 comments on commit 3669c26

Please sign in to comment.