From ad886d97d6de6f2539c4b6df41652b4e6d1b8b1d Mon Sep 17 00:00:00 2001 From: Adam Hooper Date: Wed, 1 Sep 2021 20:48:11 -0400 Subject: [PATCH] Expose `charset` option in Node API [closes #567] (#1446) Co-authored-by: Natalie Weizenbaum --- CHANGELOG.md | 7 +++++-- README.md | 2 ++ lib/src/node.dart | 4 ++++ lib/src/node/render_options.dart | 4 +++- pkg/sass_api/pubspec.yaml | 4 ++-- pubspec.yaml | 2 +- test/node_api_test.dart | 16 ++++++++++++++++ 7 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1575bde4..bb8aaa1a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 7c20d3c31..5e21a48ea 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/src/node.dart b/lib/src/node.dart index 96f1719b0..0595be249 100644 --- a/lib/src/node.dart +++ b/lib/src/node.dart @@ -109,6 +109,7 @@ Future _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, @@ -121,6 +122,7 @@ Future _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."); @@ -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, @@ -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."); diff --git a/lib/src/node/render_options.dart b/lib/src/node/render_options.dart index 603a23cd5..ea1af9ecd 100644 --- a/lib/src/node/render_options.dart +++ b/lib/src/node/render_options.dart @@ -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, @@ -48,5 +49,6 @@ class RenderOptions { bool? sourceMapEmbed, String? sourceMapRoot, bool? quietDeps, - bool? verbose}); + bool? verbose, + bool? charset}); } diff --git a/pkg/sass_api/pubspec.yaml b/pkg/sass_api/pubspec.yaml index 719beaf4e..8bbb3852b 100644 --- a/pkg/sass_api/pubspec.yaml +++ b/pkg/sass_api/pubspec.yaml @@ -2,7 +2,7 @@ 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 @@ -10,7 +10,7 @@ environment: sdk: '>=2.12.0 <3.0.0' dependencies: - sass: 1.38.2 + sass: 1.39.0 dependency_overrides: sass: {path: ../..} diff --git a/pubspec.yaml b/pubspec.yaml index 92e134f8f..1e0a9fc44 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/node_api_test.dart b/test/node_api_test.dart index 65d28a2f7..63a94de87 100644 --- a/test/node_api_test.dart +++ b/test/node_api_test.dart @@ -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')),