Skip to content

fix: parse HTML instead of XML in SvgCanvas2D.convertHtml - #1012

Merged
tbouffard merged 4 commits into
mainfrom
test/SvgCanvas2D_add_test_convertHtml
Feb 27, 2026
Merged

fix: parse HTML instead of XML in SvgCanvas2D.convertHtml#1012
tbouffard merged 4 commits into
mainfrom
test/SvgCanvas2D_add_test_convertHtml

Conversation

@tbouffard

@tbouffard tbouffard commented Feb 25, 2026

Copy link
Copy Markdown
Member

The method was incorrectly using parseXml (XML parser) instead of DOMParser with text/html.
This bug was introduced in commit 2f4bb07 while trying to share code between methods — the code wasn't actually the same.

  • Use DOMParser.parseFromString(val, 'text/html') for correct HTML parsing
  • Add tests for convertHtml covering plain text, nested HTML, malformed
    HTML, empty input, and body tag extraction
  • Improve JSDoc of the text method

Summary by CodeRabbit

  • Tests

    • Added unit tests for HTML-to-SVG conversion covering plain text, body-wrapped HTML, attributes on body, empty input, nested content, malformed HTML correction, and whitespace trimming.
  • Bug Fixes

    • More robust HTML parsing when converting fragments to SVG to handle varied input formats.
  • Documentation

    • Clarified docs on HTML support and added testing guidance with a data-driven testing example.

The method was incorrectly using `parseXml` (XML parser) instead of
`DOMParser` with `text/html`. This bug was introduced in commit 2f4bb07
while trying to share code between methods — the code wasn't actually
the same.

- Use `DOMParser.parseFromString(val, 'text/html')` for correct HTML parsing
- Add tests for `convertHtml` covering plain text, nested HTML, malformed
  HTML, empty input, and body tag extraction
- Improve JSDoc of the `text` method
@coderabbitai

coderabbitai Bot commented Feb 25, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between de64192 and cde3bc8.

📒 Files selected for processing (1)
  • packages/core/__tests__/view/canvas/SvgCanvas2D.test.ts

Walkthrough

Switches SvgCanvas2D.convertHtml to parse HTML using DOMParser('text/html'), adds unit tests for convertHtml across multiple scenarios, and updates minor JSDoc formatting and testing guidance in docs.

Changes

Cohort / File(s) Summary
Tests
packages/core/__tests__/view/canvas/SvgCanvas2D.test.ts
Adds new test suite for SvgCanvas2D.convertHtml covering plaintext passthrough, body-tag extraction, body attributes, empty input, nested HTML preservation, and malformed HTML normalization.
Implementation
packages/core/src/view/canvas/SvgCanvas2D.ts
convertHtml now uses DOMParser with 'text/html' (replacing XML parsing) and simplifies the document null check; JSDoc for text() reformatted only.
Docs / Guidance
CLAUDE.md
Adds a data-driven testing example using test.each/it.each (documentation-only change).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete. It lacks the PR Checklist section, issue reference, and does not follow the template structure required by the repository. Complete the PR checklist, add issue reference (closes #xxxx), confirm tests are added, and follow the full template structure including Notes section if applicable.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: fixing HTML parsing by switching from XML parser to proper HTML parsing in SvgCanvas2D.convertHtml.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@tbouffard tbouffard added chore Build, CI/CD or repository tasks (issues/PR maintenance, environments, ...) refactor Code refactoring and removed chore Build, CI/CD or repository tasks (issues/PR maintenance, environments, ...) labels Feb 25, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c4659cb and 08cebe7.

📒 Files selected for processing (2)
  • packages/core/__tests__/view/canvas/SvgCanvas2D.test.ts
  • packages/core/src/view/canvas/SvgCanvas2D.ts

Comment thread packages/core/__tests__/view/canvas/SvgCanvas2D.test.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
packages/core/__tests__/view/canvas/SvgCanvas2D.test.ts (1)

17-22: Import extension issue from previous review is now resolved.

The barrel import from '../../../src' correctly omits the .js extension, in line with the moduleNameMapper convention for test files.

🧹 Nitpick comments (1)
packages/core/__tests__/view/canvas/SvgCanvas2D.test.ts (1)

41-44: Global .trim() slightly weakens the "plain text unchanged" assertion.

.trim() on every case means the 'plain text unchanged if not valid HTML' test would still pass even if convertHtml returned ' plain text ', making the test name misleading. It's only strictly needed for the whitespace case (Line 40).

Consider scoping the trim to only the cases that require it, or splitting the whitespace case into its own test block:

♻️ Proposed refinement
-  ])('%s', (_description, input, expected) => {
-    const canvas = createSvgCanvas2D();
-    expect(canvas.convertHtml(input).trim()).toBe(expected);
-  });
+  ])('%s', (_description, input, expected) => {
+    const canvas = createSvgCanvas2D();
+    expect(canvas.convertHtml(input)).toBe(expected);
+  });

And update the whitespace test case's expected value to account for leading/trailing whitespace from the parsed result (or keep the trim only in a dedicated test):

test('HTML with whitespace trims surrounding whitespace', () => {
  const canvas = createSvgCanvas2D();
  expect(canvas.convertHtml('   <div> spaced </div>   ').trim()).toBe('<div> spaced </div>');
});

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b2b6799 and de64192.

📒 Files selected for processing (2)
  • CLAUDE.md
  • packages/core/__tests__/view/canvas/SvgCanvas2D.test.ts
✅ Files skipped from review due to trivial changes (1)
  • CLAUDE.md

Comment thread packages/core/__tests__/view/canvas/SvgCanvas2D.test.ts Outdated
@sonarqubecloud

Copy link
Copy Markdown

@tbouffard
tbouffard merged commit 817c96e into main Feb 27, 2026
14 checks passed
@tbouffard
tbouffard deleted the test/SvgCanvas2D_add_test_convertHtml branch February 27, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Code refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant