Skip to content

Commit

Permalink
fix html entities escape
Browse files Browse the repository at this point in the history
- decode all attributes on server (fix #31)
- decode text node on default map
  • Loading branch information
pveyes committed Jan 26, 2018
1 parent e495ce8 commit fe3cbae
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
18 changes: 17 additions & 1 deletion src/__tests__/__snapshots__/convert.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,29 @@ exports[`custom component 1`] = `
</p>
`;

exports[`decode html attributes 1`] = `
<a
href="https://www.google.com/?a=b&c=d"
>
test
</a>
`;

exports[`decode html entities on defaultMap 1`] = `
<p>
& and &
</p>
`;

exports[`default mapping 1`] = `
<div>
<span>
</span>
<div>
Default mapping
<span>
Default mapping
</span>
</div>
<span>
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ test('unescape html entities', () => {
testRender(html);
});

test('decode html entities on defaultMap', () => {
const html = '<div class="entities">&amp; and &</div>';
testRender(html, {
map: {
_: (node, props, children) => {
if (typeof props === 'undefined') {
return node;
}

return <p>{children}</p>;
},
},
});
});

test('ignore comment', () => {
const html = '<!-- comment should be ignored--><div>no comment</div>';
testRender(html);
Expand Down Expand Up @@ -168,6 +183,11 @@ test('newline between tags', () => {
testRender(html);
});

test('decode html attributes', () => {
const html = '<a href="https://www.google.com/?a=b&amp;c=d">test</a>';
testRender(html);
});

/**
* Test utilities
*/
Expand Down
5 changes: 2 additions & 3 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ function transform(node, key: number, options: HtmrOptions) {
if (node.nodeType === NodeTypes.COMMENT) {
return null;
} else if (node.nodeType === NodeTypes.TEXT) {
return node.textContent.trim() === ''
? defaultMap ? defaultMap(node.textContent) : node.textContent
: unescape(node.textContent);
const text = unescape(node.textContent);
return defaultMap ? defaultMap(text) : text;
}

// element
Expand Down
13 changes: 9 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ function transform(node: Node, key: string, options: HtmrOptions): ?Element {
if (/^<!--[\s\S]+-->/.test(text)) {
return null;
}
if (text === '') {
return defaultMap ? defaultMap(node) : node;
}

return HtmlEntity.decode(node);
const str = HtmlEntity.decode(node);
return defaultMap ? defaultMap(str) : str;
}

const { tag, attrs, content } = node;
const customElement = options.map[tag];

// decode all attribute value
if (attrs) {
Object.keys(attrs).forEach(key => {
attrs[key] = HtmlEntity.decode(attrs[key]);
});
}

const props = Object.assign(
{},
mapAttribute(attrs),
Expand Down

0 comments on commit fe3cbae

Please sign in to comment.