Skip to content

Commit

Permalink
Expose charset option in Node API [closes sass#567]
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhooper committed Aug 20, 2021
1 parent fd7eec9 commit c245be8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ Both `render()` and `renderSync()` support the following options:
* [`sourceMap`](https://github.com/sass/node-sass#sourcemap)
* Only the `"expanded"` and `"compressed"` values of
[`outputStyle`](https://github.com/sass/node-sass#outputstyle) are supported.
* `charset` (`true`, the default, will prefix non-ASCII CSS with `U+FEFF` or
[`@charset "UTF-8";`](https://developer.mozilla.org/en-US/docs/Web/CSS/@charset))

No support is intended for the following options:

Expand Down
4 changes: 4 additions & 0 deletions lib/src/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Future<RenderResult> _renderAsync(RenderOptions options) async {
url: file == null ? 'stdin' : p.toUri(file).toString(),
quietDeps: options.quietDeps ?? false,
verbose: options.verbose ?? false,
charset: options.charset ?? true,
sourceMap: _enableSourceMaps(options));
} else if (file != null) {
result = await compileAsync(file,
Expand All @@ -121,6 +122,7 @@ Future<RenderResult> _renderAsync(RenderOptions options) async {
lineFeed: _parseLineFeed(options.linefeed),
quietDeps: options.quietDeps ?? false,
verbose: options.verbose ?? false,
charset: options.charset ?? true,
sourceMap: _enableSourceMaps(options));
} else {
throw ArgumentError("Either options.data or options.file must be set.");
Expand Down Expand Up @@ -154,6 +156,7 @@ RenderResult _renderSync(RenderOptions options) {
url: file == null ? 'stdin' : p.toUri(file).toString(),
quietDeps: options.quietDeps ?? false,
verbose: options.verbose ?? false,
charset: options.charset ?? true,
sourceMap: _enableSourceMaps(options));
} else if (file != null) {
result = compile(file,
Expand All @@ -166,6 +169,7 @@ RenderResult _renderSync(RenderOptions options) {
lineFeed: _parseLineFeed(options.linefeed),
quietDeps: options.quietDeps ?? false,
verbose: options.verbose ?? false,
charset: options.charset ?? true,
sourceMap: _enableSourceMaps(options));
} else {
throw ArgumentError("Either options.data or options.file must be set.");
Expand Down
4 changes: 3 additions & 1 deletion lib/src/node/render_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class RenderOptions {
external String? get sourceMapRoot;
external bool? get quietDeps;
external bool? get verbose;
external bool? get charset;

external factory RenderOptions(
{String? file,
Expand All @@ -48,5 +49,6 @@ class RenderOptions {
bool? sourceMapEmbed,
String? sourceMapRoot,
bool? quietDeps,
bool? verbose});
bool? verbose,
bool? charset});
}
16 changes: 16 additions & 0 deletions test/node_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ a {
}'''));
});

group("unicode", () {
test("adds @charset by default", () async {
var unicodePath = p.join(sandbox, 'test.scss');
await writeTextFile(unicodePath, 'p { content: "é"; } ');
expect(renderSync(RenderOptions(file: unicodePath)),
equalsIgnoringWhitespace('@charset "UTF-8"; p { content: "é"; } '));
});

test("allows charset=false to hide @charset", () async {
var unicodePath = p.join(sandbox, 'test.scss');
await writeTextFile(unicodePath, 'p { content: "é"; } ');
expect(renderSync(RenderOptions(file: unicodePath, charset: false)),
equalsIgnoringWhitespace('p { content: "é"; } '));
});
});

group("linefeed allows", () {
test("cr", () {
expect(renderSync(RenderOptions(file: sassPath, linefeed: 'cr')),
Expand Down

0 comments on commit c245be8

Please sign in to comment.