Skip to content

Fix negative level issue in html-format#8

Open
joelzanden wants to merge 2 commits into
mohd-akram:masterfrom
joelzanden:fix-negative-level
Open

Fix negative level issue in html-format#8
joelzanden wants to merge 2 commits into
mohd-akram:masterfrom
joelzanden:fix-negative-level

Conversation

@joelzanden
Copy link
Copy Markdown

@joelzanden joelzanden commented Jan 12, 2025

When the input html is malformed and the number of end tags exceed the number of opening tags, level will go negative. That will cause spanLevel to become negative, causing indent.repeat(spanLevel) to throw a RangeError.

By making sure level clamps at 0, this i avoided.

Copy link
Copy Markdown
Owner

@mohd-akram mohd-akram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test as well? Also, combine your commits. Thank you.

Comment thread index.js
@@ -132,6 +132,7 @@ function format(/** @type {string} */ html, indent = " ", width = 80) {
if (tagName == specialElement) specialElement = null;
if (!specialElement) {
--level;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the check here instead:

Suggested change
--level;
if (level > 0) --level;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants