Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Internal: replace escapeJSONforHTML(JSON.stringify()) by a single ser…
Browse files Browse the repository at this point in the history
…ialize() method

Ref #397
  • Loading branch information
MoOx committed Apr 29, 2016
1 parent 33a2431 commit 5cc55ec
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 27 deletions.
16 changes: 0 additions & 16 deletions src/_utils/escape-json-for-html/__tests__/index.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/_utils/escape-json-for-html/index.js

This file was deleted.

47 changes: 47 additions & 0 deletions src/_utils/serialize/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import test from "ava"

import serializer from ".."

test("serialize JS for HTML", (t) => {
const escaped = serializer("<script></script><script></script>")
t.is(
escaped,
"\"<script><\\/script><script><\\/script>\"",
"function should escape all end of script tag"
)
t.falsy(
escaped.includes("</script>"),
"result should not contain unescaped end of script tag"
)
})

/* serialize-javascript version
test("serialize JS object", (t) => {
const serialized = serializer("<script></script><script></script>")
t.is(
serialized,
"\"\\u003Cscript\\u003E\\u003C\\u002Fscript\\u003E" +
"\\u003Cscript\\u003E\\u003C\\u002Fscript\\u003E\"",
"function should escape all end of script tag"
)
t.falsy(
serialized.includes("</script>"),
"result should not contain unescaped end of script tag"
)
})
*/

// https://github.com/MoOx/phenomic/issues/397
// blocked by
// https://github.com/MoOx/phenomic/issues/434
// and also
// https://github.com/yahoo/serialize-javascript/pull/16
/*
test("keep date", (t) => {
const ISODate = "2016-04-28T22:02:17.156Z"
t.is(
serializer({ d: new Date(ISODate) }),
`{"d":new Date("${ ISODate }")}`,
)
})
*/
5 changes: 5 additions & 0 deletions src/_utils/serialize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow

export default (value: any): string => {
return JSON.stringify(value).replace(/\<\/script>/g, "<\\/script>")
}
4 changes: 2 additions & 2 deletions src/builder/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import debug from "debug"
import portFinder from "portfinder"

import minifyCollection from "../content-loader/minify"
import escapeJSONforHTML from "../_utils/escape-json-for-html"
import serialize from "../_utils/serialize"

import collection from "../content-loader/cache.js"
import joinUri from "../_utils/join-uri"
Expand Down Expand Up @@ -136,7 +136,7 @@ export default (config) => {
</div>
<script>
window.__COLLECTION__ = ${
escapeJSONforHTML(JSON.stringify(collectionMin))
serialize(collectionMin)
};
</script>
${
Expand Down
6 changes: 3 additions & 3 deletions src/static/to-html/url-as-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import htmlMetas from "../../_utils/html-metas"
import joinUri from "../../_utils/join-uri"
import Html from "./Html"
import PhenomicContextProvider from "../../ContextProvider"
import escapeJSONforHTML from "../../_utils/escape-json-for-html"
import serialize from "../../_utils/serialize"

import minifyCollection from "../../content-loader/minify"

Expand Down Expand Up @@ -107,10 +107,10 @@ export default function(
}
script =
`window.__COLLECTION__ = ${
escapeJSONforHTML(JSON.stringify(collectionMin))
serialize(collectionMin)
};` +
`window.__INITIAL_STATE__ = ${
escapeJSONforHTML(JSON.stringify(initialState))
serialize(initialState)
}`
}

Expand Down

0 comments on commit 5cc55ec

Please sign in to comment.