Skip to content

Commit

Permalink
Add support for comments
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
sindresorhus committed Nov 17, 2023
1 parent ec7233e commit 9c720f9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ function domify(htmlString, document = globalThis.document) {
throw new TypeError('String expected');
}

// Handle comment nodes
const commentMatch = /^<!--(.*?)-->$/s.exec(htmlString);
if (commentMatch) {
return document.createComment(commentMatch[1]);
}

const tagName = /<([\w:]+)/.exec(htmlString)?.[1];

if (!tagName) {
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ test('preserves trailing/leading spaces for textElement', t => {
t.is(element.textContent, ' text goes here ');
});

test('handles comments', t => {
const comment = domify('<!-- mycomment -->');
t.is(comment.nodeType, jsdom.window.Node.COMMENT_NODE);
t.is(comment.data, ' mycomment ');
});

test('handles comments with leading/trailing spaces', t => {
const comment = domify('<!-- mycomment -->');
t.is(comment.nodeType, jsdom.window.Node.COMMENT_NODE);
t.is(comment.data, ' mycomment ');
});

if (globalThis.SVGElement) {
test('svg - supports path tag', t => {
t.true(domify('<path></path>') instanceof globalThis.SVGPathElement);
Expand Down

0 comments on commit 9c720f9

Please sign in to comment.