Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,25 @@ Styles are defined as objects conforming to `CellStyle` type. See `packages/core
- Tests are in `packages/core/__tests__/` mirroring `src/` structure
- Use `@swc/jest` for fast TypeScript compilation
- Import paths in tests should omit `.js` extension (handled by moduleNameMapper)
- When multiple tests share the same structure and only differ by input/expected data, use `test.each` (or `it.each`) to avoid repetition and put focus on the tested use cases:

```typescript
// Good - data-driven tests with test.each
test.each([
['description of case 1', input1, expected1],
['description of case 2', input2, expected2],
])('%s', (_description, input, expected) => {
expect(myFunction(input)).toBe(expected);
});

// Bad - repetitive tests with identical structure
test('case 1', () => {
expect(myFunction(input1)).toBe(expected1);
});
test('case 2', () => {
expect(myFunction(input2)).toBe(expected2);
});
```

## Commit Message Style

Expand Down
49 changes: 49 additions & 0 deletions packages/core/__tests__/view/canvas/SvgCanvas2D.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2026-present The maxGraph project Contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { describe, expect, test } from '@jest/globals';
import { SvgCanvas2D, constants } from '../../../src';

function createSvgCanvas2D() {
return new SvgCanvas2D(document.createElementNS(constants.NS_SVG, 'svg'), true);
}

describe('SvgCanvas2D.convertHtml', () => {
test.each([
['plain text unchanged if not valid HTML', 'plain text', 'plain text'],
['HTML with body tag', '<html><body><p>foo</p></body></html>', '<p>foo</p>'],
[
'HTML with attributes on body',
'<html><body style="color:red"><span>bar</span></body></html>',
'<span>bar</span>',
],
['empty string for empty input', '', ''],
[
'nested HTML',
'<div><div><span>deep</span></div></div>',
'<div><div><span>deep</span></div></div>',
],
['malformed HTML gracefully', '<div><b>broken', '<div><b>broken</b></div>'],
[
'HTML with whitespace, trim only leading whitespace',
' <div> spaced </div> ',
'<div> spaced </div> ',
],
])('%s', (_description, input, expected) => {
const canvas = createSvgCanvas2D();
expect(canvas.convertHtml(input)).toBe(expected);
});
});
14 changes: 8 additions & 6 deletions packages/core/src/view/canvas/SvgCanvas2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,10 @@ class SvgCanvas2D extends AbstractCanvas2D {
* Converts the given HTML string to XHTML.
*/
convertHtml(val: string) {
const doc = parseXml(val);
const doc = new DOMParser().parseFromString(val, 'text/html');

if (doc != null) {
// the jsdoc of DOMParser.parseFromString says the returned value is never null, but keep the check (it comes from mxGraph) for now until we get more feedback on this
if (doc) {
val = new XMLSerializer().serializeToString(doc.body);

// Extracts body content from DOM
Expand Down Expand Up @@ -1383,10 +1384,11 @@ class SvgCanvas2D extends AbstractCanvas2D {
}

/**
* Paints the given text. Possible values for format are empty string for plain
* text and html for HTML markup. Note that HTML markup is only supported if
* foreignObject is supported and <foEnabled> is true. (This means IE9 and later
* does currently not support HTML text as part of shapes.)
* Paints the given text.
*
* Possible values for format are empty string for plain text and HTML for HTML markup.
*
* Note that HTML markup is only supported if `foreignObject` is supported and {@link foEnabled} is `true`.
*/
text(
x: number,
Expand Down