Skip to content

Commit

Permalink
Expose charset option in Node API [closes #567] (#1446)
Browse files Browse the repository at this point in the history
Co-authored-by: Natalie Weizenbaum <[email protected]>
  • Loading branch information
adamhooper and nex3 authored Sep 2, 2021
1 parent 1615bb1 commit ad886d9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 6 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 1.38.3
## 1.39.0

* No user-visible changes.
### JS API

* Add a `charset` option that controls whether or not Sass emits a
`@charset`/BOM for non-ASCII stylesheets.

## 1.38.2

Expand Down
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});
}
4 changes: 2 additions & 2 deletions pkg/sass_api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: sass_api
# Note: Every time we add a new Sass AST node, we need to bump the *major*
# version because it's a breaking change for anyone who's implementing the
# visitor interface(s).
version: 1.0.0-dev
version: 1.0.0-beta.6
description: Additional APIs for Dart Sass.
homepage: https://github.com/sass/dart-sass

environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
sass: 1.38.2
sass: 1.39.0

dependency_overrides:
sass: {path: ../..}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass
version: 1.38.3-dev
version: 1.39.0
description: A Sass implementation in Dart.
homepage: https://github.com/sass/dart-sass

Expand Down
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 ad886d9

Please sign in to comment.