Skip to content

Commit

Permalink
Adds support for UTF-8 BOM.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas830316 authored and jakubpawlowicz committed Jan 19, 2023
1 parent d9b4c63 commit cc7b605
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/reader/load-original-sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ function loadOriginalSourceFromLocalUri(relativeUri, loadContext) {
return null;
}

return fs.readFileSync(absoluteUri, 'utf8');
var result = fs.readFileSync(absoluteUri, 'utf8');
if (result.charCodeAt(0) === 65279) {
result = result.substring(1);
}
return result;
}

module.exports = loadOriginalSources;
4 changes: 4 additions & 0 deletions lib/reader/read-sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ function inlineLocalStylesheet(uri, mediaQuery, metadata, inlinerContext) {
? inlinerContext.externalContext.sourcesContent[normalizedPath]
: fs.readFileSync(absoluteUri, 'utf-8');

if (importedStyles.charCodeAt(0) === 65279) {
importedStyles = importedStyles.substring(1);
}

inlinerContext.inlinedStylesheets.push(absoluteUri);
inlinerContext.inline = inlinerContext.externalContext.options.inline;

Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/utf8-bom/utf8-bom-common.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
body {
color: #f00;
}

p,
ul,
li {
margin: 0;
padding: 0;
}

/* UTF8测试文件1 */
11 changes: 11 additions & 0 deletions test/fixtures/utf8-bom/utf8-bom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@import url('utf8-bom-common.css');

h1 {
font-size: 16px;
}

p{
font-size:14px;
}

/* UTF8 BOM 测试文件 */
8 changes: 8 additions & 0 deletions test/module-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ vows.describe('module tests').addBatch({
assert.equal(minified.styles, '.one{color:red}');
}
},
'utf-8 bom': {
'topic': function () {
return new CleanCSS().minify(['test/fixtures/utf8-bom/utf8-bom.css']);
},
'should be processed correctly': function (error, minified) {
assert.equal(minified.styles, 'body{color:red}li,p,ul{margin:0;padding:0}h1{font-size:16px}p{font-size:14px}');
}
},
'options': {
'level 2': {
'topic': function () {
Expand Down

0 comments on commit cc7b605

Please sign in to comment.