Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #49 from inikulin/fl-2529
Browse files Browse the repository at this point in the history
FL-2529 Don't escape entities in HTML and CSS content
  • Loading branch information
RReverser committed Sep 17, 2018
2 parents 5a29178 + 4b727d4 commit 4bbf1f9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
7 changes: 4 additions & 3 deletions lib/plugins/parse5-flatten-external-style.js
Expand Up @@ -23,12 +23,13 @@ module.exports = (rewriter, opts) =>
attrs: [],
selfClosing: false
});
rewriter.emitText({
text: await collapseCSS.external({
// NOTE: use emitRaw here to not escape HTML entities in content.
rewriter.emitRaw(
await collapseCSS.external({
fetch: opts.fetch,
resourceLocation: resolve(opts.resourceLocation, href.value)
})
});
);
rewriter.emitEndTag({
tagName: 'style'
});
Expand Down
5 changes: 2 additions & 3 deletions lib/plugins/parse5-flatten-inline-style.js
Expand Up @@ -23,9 +23,8 @@ module.exports = (rewriter, opts) => {
resourceLocation: opts.resourceLocation
});

rewriter.emitText({
text: content
});
// NOTE: use emitRaw here to not escape HTML entities in content.
rewriter.emitRaw(content);

return true;
});
Expand Down
9 changes: 5 additions & 4 deletions lib/plugins/parse5-flatten-script.js
Expand Up @@ -49,7 +49,9 @@ module.exports = (rewriter, opts) => {

// Emit modified start tag and the contents (but not end tag since it's already in the HTML).
rewriter.emitStartTag(tag);
rewriter.emitText({text: content});

// NOTE: use emitRaw here to not escape HTML entities in content.
rewriter.emitRaw(content);

return true;
});
Expand All @@ -63,9 +65,8 @@ module.exports = (rewriter, opts) => {
resourceLocation: '<script>'
});

rewriter.emitText({
text: content
});
// NOTE: use emitRaw here to not escape HTML entities in content.
rewriter.emitRaw(content);

return true;
});
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/parse5-async-rewriter.js
Expand Up @@ -61,7 +61,8 @@ for (const methodName of [
'emitStartTag',
'emitEndTag',
'emitDoctype',
'emitText'
'emitText',
'emitRaw'
]) {
Rewriter.prototype[methodName] = function(...args) {
this._rewriter[methodName](...args);
Expand Down
8 changes: 4 additions & 4 deletions test/plugins/parse5-flatten-script.js
Expand Up @@ -15,8 +15,8 @@ async function test(input, expected, opts) {
describe('posthtml-flatten-script', () => {
it('should flatten inline JavaScript', () => {
return test(
'<script>alert("foo" + "bar");</script>',
'<script>alert("foobar");</script>',
'<script>alert("foo" + "bar"); var a = c < b;</script>',
'<script>alert("foobar");var a=c<b;</script>',
{
fetch() {
assert(false, 'unexpected resource resolution');
Expand Down Expand Up @@ -50,11 +50,11 @@ describe('posthtml-flatten-script', () => {
it('should flatten external JavaScript', () => {
return test(
'<script src="app.js"></script>',
'<script>alert("foobar");</script>',
'<script>alert("foobar");var a=c<b;</script>',
{
async fetch(url) {
assert(url === 'https://example.com/app.js');
return {body: Buffer.from('alert("foo" + "bar");')};
return {body: Buffer.from('alert("foo" + "bar"); var a = c < b;')};
},
resourceLocation: 'https://example.com/'
}
Expand Down
8 changes: 4 additions & 4 deletions test/plugins/parse5-flatten-style.js
Expand Up @@ -19,8 +19,8 @@ async function test(input, expected, opts) {
describe('posthtml-flatten-style', () => {
it('should flatten inline style', () => {
return test(
'<style>body { background: url(gif.gif); }</style>',
'<style>body{background:url(data:image/gif;base64,R0lGODlhAQABAAAAADs=)}</style>',
'<style>body > .test { background: url(gif.gif); }</style>',
'<style>body>.test{background:url(data:image/gif;base64,R0lGODlhAQABAAAAADs=)}</style>',
{
async fetch(url) {
assert(url === 'https://example.com/gif.gif');
Expand Down Expand Up @@ -64,13 +64,13 @@ describe('posthtml-flatten-style', () => {
it('should flatten resources in external stylesheets', () => {
return test(
'<link rel="stylesheet" href="/static/css/app.css">',
'<style>body,html{background:url(data:image/gif;base64,R0lGODlhAQABAAAAADs=)}</style>',
'<style>body>.test{background:url(data:image/gif;base64,R0lGODlhAQABAAAAADs=)}</style>',
{
async fetch(url) {
switch (url) {
case 'https://example.com/static/css/app.css':
return {
body: Buffer.from('html, body { background: url(gif.gif) }')
body: Buffer.from('body > .test { background: url(gif.gif) }')
};

case 'https://example.com/static/css/gif.gif':
Expand Down

0 comments on commit 4bbf1f9

Please sign in to comment.