Skip to content

Commit

Permalink
Fix recursion error in HtmlParser
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Oct 10, 2023
1 parent e00555f commit a43ea98
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
19 changes: 17 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,19 @@ $('body')
</script>"
`)
})

it("should not recurse on bad fragment", () => {
const tests = [
'<',
'<<',
'<<<',
'<<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 a43ea98

Please sign in to comment.