Skip to content

Comments

fix: crash in toJSON() due to document property.#147

Merged
ota-meshi merged 3 commits intomasterfrom
issue-146
Jan 13, 2026
Merged

fix: crash in toJSON() due to document property.#147
ota-meshi merged 3 commits intomasterfrom
issue-146

Conversation

@ota-meshi
Copy link
Owner

close #146

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a crash that occurred when calling toJSON() on parsed HTML documents due to a circular reference caused by the document property on root nodes. The fix makes the document property non-enumerable while maintaining backward compatibility.

Changes:

  • Modified lib/html/parse-styles.js to use Object.defineProperty with enumerable: false for the document property
  • Added regression test in test/issue146.js to verify toJSON() no longer crashes

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lib/html/parse-styles.js Changed document property assignment to use Object.defineProperty with enumerable: false to prevent toJSON serialization issues while maintaining backward compatibility
test/issue146.js Added regression test to verify that calling toJSON() on parsed documents no longer throws an error

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

const document = syntax.parse("<style>a {}</style>", {
from: `/test.html`,
});
document.toJSON();
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

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

The test should verify that the document property is still accessible after the fix. This ensures backward compatibility is maintained, as mentioned in the comment on line 170. Consider adding an assertion like expect(document.nodes[0].document).to.equal(document); after calling toJSON() to verify the property still exists and references the correct document.

Copilot uses AI. Check for mistakes.
@ota-meshi ota-meshi merged commit 2dd9943 into master Jan 13, 2026
4 checks passed
@ota-meshi ota-meshi deleted the issue-146 branch January 13, 2026 14:37
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.

Assigning document to Root nodes causes RangeError: Maximum call stack size exceeded when converting to JSON

1 participant