Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support --linefeed in Dart and Node #153

Merged
merged 11 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.0.0-beta.2

### Node JS API

* Add support for `lineFeed`, `indentWidth`, and `indentType` options to
`render()` and `renderSync()`.

## 1.0.0-beta.1

* Drop support for the reference combinator. This has been removed from the
Expand Down
25 changes: 23 additions & 2 deletions lib/src/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'node/exports.dart';
import 'node/options.dart';
import 'node/result.dart';
import 'render.dart';
import 'visitor/serialize.dart';

/// The entrypoint for Node.js.
///
Expand Down Expand Up @@ -41,8 +42,11 @@ void _render(
var indentWidth = indentWidthValue is int
? indentWidthValue
: int.parse(indentWidthValue.toString());
var lineFeed = _parseLineFeed(options.linefeed);
var result = newNodeResult(render(options.file,
useSpaces: options.indentType == 'space', indentWidth: indentWidth));
useSpaces: options.indentType == 'space',
indentWidth: indentWidth,
lineFeed: lineFeed));
callback(null, result);
} on SassException catch (error) {
// TODO: populate the error more thoroughly if possible.
Expand All @@ -62,10 +66,27 @@ NodeResult _renderSync(NodeOptions options) {
var indentWidth = indentWidthValue is int
? indentWidthValue
: int.parse(indentWidthValue.toString());
var lineFeed = _parseLineFeed(options.linefeed);
return newNodeResult(render(options.file,
useSpaces: options.indentType == 'space', indentWidth: indentWidth));
useSpaces: options.indentType == 'space',
indentWidth: indentWidth,
lineFeed: lineFeed));
} on SassException catch (error) {
// TODO: populate the error more thoroughly if possible.
throw new NodeError(message: error.message);
}
}

/// Parses the name of a line feed type into a [LineFeed].
LineFeed _parseLineFeed(String str) {
switch (str) {
case 'cr':
return LineFeed.cr;
case 'crlf':
return LineFeed.crlf;
case 'lfcr':
return LineFeed.lfcr;
default:
return LineFeed.lf;
}
}
1 change: 1 addition & 0 deletions lib/src/node/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class NodeOptions {
external String get file;
external String get indentType;
external dynamic get indentWidth;
external String get linefeed;
}
8 changes: 4 additions & 4 deletions lib/src/render.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ String render(String path,
{bool color: false,
SyncPackageResolver packageResolver,
bool useSpaces: true,
int indentWidth: 2}) {
RangeError.checkValueInInterval(indentWidth, 0, 10, "indentWidth");

int indentWidth,
LineFeed lineFeed}) {
var contents = readFile(path);
var url = p.toUri(path);
var sassTree = p.extension(path) == '.sass'
? new Stylesheet.parseSass(contents, url: url, color: color)
: new Stylesheet.parseScss(contents, url: url, color: color);
var cssTree =
evaluate(sassTree, color: color, packageResolver: packageResolver);
return toCss(cssTree, useSpaces: useSpaces, indentWidth: indentWidth);
return toCss(cssTree,
useSpaces: useSpaces, indentWidth: indentWidth, lineFeed: lineFeed);
}
61 changes: 50 additions & 11 deletions lib/src/visitor/serialize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ String toCss(CssNode node,
{OutputStyle style,
bool inspect: false,
bool useSpaces: true,
int indentWidth: 2}) {
int indentWidth,
LineFeed lineFeed}) {
indentWidth ??= 2;
var visitor = new _SerializeCssVisitor(
style: style,
inspect: inspect,
useSpaces: useSpaces,
indentWidth: indentWidth);
indentWidth: indentWidth,
lineFeed: lineFeed);
node.accept(visitor);
var result = visitor._buffer.toString();
if (result.codeUnits.any((codeUnit) => codeUnit > 0x7F)) {
Expand Down Expand Up @@ -94,16 +97,23 @@ class _SerializeCssVisitor
/// The number of spaces or tabs to be used for indentation.
final int _indentWidth;

/// The characters to use for a line feed.
final LineFeed _lineFeed;

_SerializeCssVisitor(
{OutputStyle style,
bool inspect: false,
bool quote: true,
bool useSpaces: true,
int indentWidth: 2})
int indentWidth,
LineFeed lineFeed})
: _inspect = inspect,
_quote = quote,
_indentCharacter = useSpaces ? $space : $tab,
_indentWidth = indentWidth;
_indentWidth = indentWidth ?? 2,
_lineFeed = lineFeed ?? LineFeed.lf {
RangeError.checkValueInInterval(_indentWidth, 0, 10, "indentWidth");
}

void visitStylesheet(CssStylesheet node) {
CssNode previous;
Expand All @@ -112,8 +122,8 @@ class _SerializeCssVisitor
if (_isInvisible(child)) continue;

if (previous != null) {
_buffer.writeln();
if (previous.isGroupEnd) _buffer.writeln();
_buffer.write(_lineFeed.text);
if (previous.isGroupEnd) _buffer.write(_lineFeed.text);
}
previous = child;

Expand Down Expand Up @@ -738,7 +748,11 @@ class _SerializeCssVisitor
first = false;
} else {
_buffer.writeCharCode($comma);
_buffer.writeCharCode(complex.lineBreak ? $lf : $space);
if (complex.lineBreak) {
_buffer.write(_lineFeed.text);
} else {
_buffer.writeCharCode($space);
}
}
visitComplexSelector(complex);
}
Expand Down Expand Up @@ -798,23 +812,23 @@ class _SerializeCssVisitor
return;
}

_buffer.writeln();
_buffer.write(_lineFeed.text);
_indent(() {
CssNode previous;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (_isInvisible(child)) continue;

if (previous != null) {
_buffer.writeln();
if (previous.isGroupEnd) _buffer.writeln();
_buffer.write(_lineFeed.text);
if (previous.isGroupEnd) _buffer.write(_lineFeed.text);
}
previous = child;

child.accept(this);
}
});
_buffer.writeln();
_buffer.write(_lineFeed.text);
_writeIndentation();
_buffer.writeCharCode($rbrace);
}
Expand Down Expand Up @@ -919,3 +933,28 @@ class OutputStyle {

String toString() => _name;
}

/// An enum of line feed sequences.
class LineFeed {
/// A single carriage return.
static const cr = const LineFeed._('cr', '\r');

/// A carriage return followed by a line feed.
static const crlf = const LineFeed._('crlf', '\r\n');

/// A single line feed.
static const lf = const LineFeed._('lf', '\n');

/// A line feed followed by a carriage return.
static const lfcr = const LineFeed._('lfcr', '\n\r');

/// The name of this sequence..
final String name;

/// The text to emit for this line feed.
final String text;

const LineFeed._(this.name, this.text);

String toString() => name;
}
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.0.0-beta.1
version: 1.0.0-dev
description: A Sass implementation in Dart.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/sass/dart-sass
Expand Down