Skip to content

Commit

Permalink
Escape double quotes in attribute values in HTMLElement plugin (jestj…
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and cpojer committed Jun 27, 2017
1 parent dbf301e commit aa48deb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/pretty-format/src/__tests__/html_element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ describe('HTMLElement Plugin', () => {
expect(parent).toPrettyPrintTo('<div\n title="title text"\n/>');
});

test('escapes double quote in attribute value', () => {
const parent = document.createElement('div');
parent.setAttribute('title', '"escape"');

expect(parent).toPrettyPrintTo('<div\n title="\\"escape\\""\n/>');
});

it('supports an HTML element with a single attribute', () => {
const parent = document.createElement('div');
parent.setAttribute('class', 'classy');
Expand Down
12 changes: 9 additions & 3 deletions packages/pretty-format/src/plugins/html_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ function printChildren(flatChildren, print, indent, colors, opts) {
.join(opts.edgeSpacing);
}

function printAttributes(attributes: Array<Attribute>, indent, colors, opts) {
function printAttributes(
attributes: Array<Attribute>,
print,
indent,
colors,
opts,
) {
return attributes
.sort(
(attributeA, attributeB) =>
Expand All @@ -80,7 +86,7 @@ function printAttributes(attributes: Array<Attribute>, indent, colors, opts) {
opts.spacing +
indent(colors.prop.open + attribute.name + colors.prop.close + '=') +
colors.value.open +
`"${attribute.value}"` +
print(attribute.value) +
colors.value.close
);
})
Expand Down Expand Up @@ -117,7 +123,7 @@ const print = (
const hasAttributes = element.attributes && element.attributes.length;
if (hasAttributes) {
const attributes = Array.prototype.slice.call(element.attributes);
result += printAttributes(attributes, indent, colors, opts);
result += printAttributes(attributes, print, indent, colors, opts);
}

const flatChildren = Array.prototype.slice.call(element.childNodes);
Expand Down

0 comments on commit aa48deb

Please sign in to comment.