Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1040@minor: Don't add HTML tags when parsing SVGs. #1041

Merged
merged 3 commits into from
Sep 14, 2023
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
30 changes: 22 additions & 8 deletions packages/happy-dom/src/dom-parser/DOMParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,30 @@ export default class DOMParser {
}
}
} else {
const documentElement = newDocument.createElement('html');
const bodyElement = newDocument.createElement('body');
const headElement = newDocument.createElement('head');
switch (mimeType) {
case 'image/svg+xml':
{
for (const node of root._childNodes.slice()) {
newDocument.appendChild(node);
}
}
break;
case 'text/html':
default:
{
const documentElement = newDocument.createElement('html');
const bodyElement = newDocument.createElement('body');
const headElement = newDocument.createElement('head');

documentElement.appendChild(headElement);
documentElement.appendChild(bodyElement);
newDocument.appendChild(documentElement);
documentElement.appendChild(headElement);
documentElement.appendChild(bodyElement);
newDocument.appendChild(documentElement);

for (const node of root._childNodes.slice()) {
bodyElement.appendChild(node);
for (const node of root._childNodes.slice()) {
bodyElement.appendChild(node);
}
}
break;
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/happy-dom/test/dom-parser/DOMParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import XMLSerializer from '../../src/xml-serializer/XMLSerializer.js';
import DOMParser from '../../src/dom-parser/DOMParser.js';
import DOMParserHTML from './data/DOMParserHTML.js';
import { beforeEach, describe, it, expect } from 'vitest';
import DOMParserSVG from './data/DOMParserSVG';

describe('DOMParser', () => {
let domParser: DOMParser;
Expand Down Expand Up @@ -96,5 +97,12 @@ describe('DOMParser', () => {
// Spurious comment `<!--[a-z]/,end:/-->` should be solved
expect(newDocument.body.textContent).toBe('here is some html elástica ');
});

it('parses SVGs', () => {
const newDocument = domParser.parseFromString(DOMParserSVG, 'image/svg+xml');
expect(new XMLSerializer().serializeToString(newDocument).replace(/[\s]/gm, '')).toBe(
DOMParserSVG.replace(/[\s]/gm, '')
);
});
});
});
5 changes: 5 additions & 0 deletions packages/happy-dom/test/dom-parser/data/DOMParserSVG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default `
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M14 6.53333V7.46667H7.46667V14H6.53333V7.46667H0V6.53333H6.53333V0H7.46667V6.53333H14Z" fill="#0078D4"></path>
</svg>
`;