Skip to content

Commit

Permalink
Support comment nodes in html pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 23, 2017
1 parent 7cba8fb commit e7bc468
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/pretty-format/src/__tests__/HTMLElementPlugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,16 @@ describe('HTMLElement Plugin', () => {
'</div>',
].join('\n'));
});

it('supports comment node', () => {
const parent = document.createElement('div');
parent.innerHTML = 'some <!-- comments -->';

expect(parent).toPrettyPrintTo([
'<div>',
' some ',
' <!-- comments -->',
'</div>',
].join('\n'));
});
});
8 changes: 6 additions & 2 deletions packages/pretty-format/src/plugins/HTMLElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import type {Colors, Indent, Options, Print, Plugin} from '../types.js';

const escapeHTML = require('./lib/escapeHTML');
const HTML_ELEMENT_REGEXP = /(HTML\w*?Element)|Text/;
const HTML_ELEMENT_REGEXP = /(HTML\w*?Element)|Text|Comment/;
const test = isHTMLElement;

function isHTMLElement(value: any) {
return (
value !== undefined &&
value !== null &&
(value.nodeType === 1 || value.nodeType === 3) &&
(value.nodeType === 1 || value.nodeType === 3 || value.nodeType === 8) &&
value.constructor !== undefined &&
value.constructor.name !== undefined &&
HTML_ELEMENT_REGEXP.test(value.constructor.name)
Expand Down Expand Up @@ -67,6 +67,10 @@ const print = (
return element.data;
}

if (element.nodeType === 8) {
return `<!--${element.data}-->`;
}

let result = colors.tag.open + '<';
const elementName = element.tagName.toLowerCase();
result += elementName + colors.tag.close;
Expand Down

0 comments on commit e7bc468

Please sign in to comment.