Skip to content

Commit

Permalink
clients(devtools): escape assets based on filetype (#8456)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored and brendankenny committed Apr 25, 2019
1 parent d9e1a32 commit f42a4ea
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions build/build-dt-report-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ const htmlReportAssets = require('../lighthouse-core/report/html/html-report-ass
*/
function convertToAsciiAndWriteFile(name, content) {
assert(content);
// eslint-disable-next-line no-control-regex
const escaped = content.replace(/[^\x00-\x7F]/g, c => '\\\\u' + c.charCodeAt(0).toString(16));

let unicodeEscapePrefix = '\\\\u'; // js
if (name.endsWith('.html')) {
// Can't support unicode characters in inline stylesheets or js, would have to parse
// and apply context-specific escapes. Instead, since no ascii is used in html yet,
// punt and throw if any ascii is found.
// eslint-disable-next-line no-control-regex
assert(!content.match(/[^\x00-\x7F]/));
} else if (name.endsWith('.css')) {
unicodeEscapePrefix = '\\';
}

const escaped =
// eslint-disable-next-line no-control-regex
content.replace(/[^\x00-\x7F]/g, c => unicodeEscapePrefix + c.charCodeAt(0).toString(16));
fs.writeFileSync(`${distDir}/${name}`, escaped);
}

Expand Down

0 comments on commit f42a4ea

Please sign in to comment.