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

Escape double quotes in attribute values in HTMLElement plugin #3797

Merged
merged 2 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions packages/pretty-format/src/__tests__/HTMLElement-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/HTMLElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,21 @@ 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()
.map(attribute => {
return (
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 @@ -106,7 +112,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