Skip to content

Commit

Permalink
fix: adjusted block html regex to avoid perf issues
Browse files Browse the repository at this point in the history
Closes #546

Thank you @devbrains-com for contributing the basis of this fix!
  • Loading branch information
quantizor committed Apr 11, 2024
1 parent dcc457e commit f9328cc
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/quiet-boxes-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"markdown-to-jsx": patch
---

Improved block html detection regex to handle certain edge cases that cause extreme slowness. Thank you @devbrains-com for the basis for this fix 馃
148 changes: 148 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3442,6 +3442,154 @@ Item detail
</p>
`)
})

it('#546 perf regression test, self-closing block + block HTML causes exponential degradation', () => {
render(
compiler(
`<span class="oh" data-self-closing="yes" />
You can have anything here. But it's best if the self-closing tag also appears in the document as a pair tag multiple times. We have found it when compiling a table with spans that had a self-closing span at the top.
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
Each span you copy above increases the time it takes by 2. Also, writing text here increases the time.`.trim()
)
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<div>
<span class="oh"
data-self-closing="yes"
>
</span>
<p>
You can have anything here. But it's best if the self-closing tag also appears in the document as a pair tag multiple times. We have found it when compiling a table with spans that had a self-closing span at the top.
</p>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<p>
Each span you copy above increases the time it takes by 2. Also, writing text here increases the time.
</p>
</div>
`)
})
})

describe('horizontal rules', () => {
Expand Down
2 changes: 1 addition & 1 deletion index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ const HEADING_SETEXT_R = /^([^\n]+)\n *(=|-){3,} *(?:\n *)+\n/
* \n*
*/
const HTML_BLOCK_ELEMENT_R =
/^ *(?!<[a-z][^ >/]* ?\/>)<([a-z][^ >/]*) ?([^>]*)>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1\b)[\s\S])*?)<\/\1>(?!<\/\1>)\n*/i
/^ *(?!<[a-z][^ >/]* ?\/>)<([a-z][^ >/]*) ?((?:[^>]*[^/])?)>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1\b)[\s\S])*?)<\/\1>(?!<\/\1>)\n*/i

const HTML_CHAR_CODE_R = /&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});/gi

Expand Down

0 comments on commit f9328cc

Please sign in to comment.