Skip to content

Commit

Permalink
fix server-side parser for script tag (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
pveyes authored Feb 17, 2020
1 parent 8df9d6a commit aec069d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import mapAttribute from './mapAttribute';
import { HtmrOptions, HTMLTags } from "./types";

type HTMLNode = {
type: 'tag' | 'style',
type: 'tag' | 'style' | 'script',
name: HTMLTags,
attribs: {
[key: string]: any
Expand All @@ -28,6 +28,7 @@ function transform(node: Node, key: string, options: HtmrOptions): ReactNode {
const defaultTransform = options.transform._;

switch (node.type) {
case 'script':
case 'style':
case 'tag': {
const { name, attribs } = node;
Expand Down
13 changes: 13 additions & 0 deletions test/__snapshots__/convert.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ exports[`custom component 1`] = `
</p>
`;

exports[`dangerously render script tag 1`] = `
<script
data-cfasync="false"
type="text/javascript"
>
var gtm4wp_datalayer_name = "dataLayer";
var dataLayer = dataLayer || [];
dataLayer.push({"pagePostType":"post","pagePostType2":"single-post","pageCategory":["kalender-cuti"],"pagePostAuthor":"Candra Alif Irawan"});
</script>
`;

exports[`decode html attributes 1`] = `
<a
href="https://www.google.com/?a=b&c=d"
Expand Down
12 changes: 12 additions & 0 deletions test/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ test('correctly handle boolean attributes', () => {
expect(container.querySelector('iframe').getAttribute('allowfullscreen')).toEqual('');
});

test('dangerously render script tag', () => {
const html = `
<script data-cfasync="false" type="text/javascript">
var gtm4wp_datalayer_name = "dataLayer";
var dataLayer = dataLayer || [];
dataLayer.push({"pagePostType":"post","pagePostType2":"single-post","pageCategory":["kalender-cuti"],"pagePostAuthor":"Candra Alif Irawan"});
</script>
`.trim();

testRender(html);
});

expect.extend({
toRenderConsistently({ server, browser }, html) {
const serverRender = renderer.create(server);
Expand Down

0 comments on commit aec069d

Please sign in to comment.