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

Preserve CRLF line endings #606

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Comb {
var result = [];
var start = currentLineNumber - 1 - LINES_AROUND;
var end = currentLineNumber + LINES_AROUND;
var lines = text.split(/\r\n|\r|\n/);
var lines = text.split(/\r?\n/);

for (var i = start; i < end; i++) {
var line = lines[i];
Expand Down
5 changes: 3 additions & 2 deletions src/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ CSScomb.detectInString = function detectInString(text, options) {
result = getDetectedOptions(detectedOptions, handlers);

// Handle conflicting options with spaces around braces:
var match;
var blockIndent = result['block-indent'];
var spaceAfterOpeningBrace = result['space-after-opening-brace'];

if (typeof blockIndent === 'string' &&
spaceAfterOpeningBrace &&
spaceAfterOpeningBrace.indexOf('\n') > -1) {
result['space-after-opening-brace'] = '\n';
(match = /\r?\n/.exec(spaceAfterOpeningBrace)) !== null) {
result['space-after-opening-brace'] = match[0];
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/format.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

module.exports = function(string) {
return string.replace(/\n\s+/gm, ' ');
return string.replace(/\r?\n\s+/g, ' ');
};
12 changes: 7 additions & 5 deletions src/options/block-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let option = {
*/
process(ast) {
ast.eachFor('space', function(whitespaceNode, i) {
var spaces = whitespaceNode.content.replace(/\n[ \t]+/gm, '\n');
var spaces = whitespaceNode.content.replace(/(\r?\n)[ \t]+/g, '$1');

if (spaces === '') {
ast.removeChild(i);
Expand Down Expand Up @@ -72,7 +72,7 @@ let option = {
var lastIndex = spaces.lastIndexOf('\n');

// Do not continue if there is no line break:
if (lastIndex < 0) return;
if (lastIndex === -1) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdalton, out of curiosity, there'll be no bug/issue with this line, it's just a nitpick, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry about that. No bug. I usually try to avoid unfocused changes like that.

Let me know if you spot others like it and I’ll revert them.


// Number of spaces from beginning of line:
var spacesLength = spaces.slice(lastIndex + 1).length + 1;
Expand Down Expand Up @@ -114,9 +114,11 @@ let option = {
var value = this.value;

node.eachFor('space', function(whitespaceNode) {
if (whitespaceNode.content === '\n') return;
var spaces = whitespaceNode.content;

var spaces = whitespaceNode.content.replace(/[ \t]/gm, '');
if (spaces === '\n' || spaces === '\r\n') return;

spaces = spaces.replace(/[ \t]/g, '');
spaces += new Array(level + 2).join(value);
whitespaceNode.content = spaces;
});
Expand All @@ -130,7 +132,7 @@ let option = {
var value = this.value;

// Remove all whitespaces and tabs, leave only new lines:
var spaces = node.content.replace(/[ \t]/gm, '');
var spaces = node.content.replace(/[ \t]/g, '');

if (!spaces) return;

Expand Down
5 changes: 3 additions & 2 deletions src/options/eof-newline.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

let gonzales = require('gonzales-pe');
let os = require('os');

let option = {
/**
Expand Down Expand Up @@ -41,8 +42,8 @@ let option = {
ast.content.push(lastChild);
}

lastChild.content = lastChild.content.replace(/\n$/, '');
if (this.value) lastChild.content += '\n';
lastChild.content = lastChild.content.replace(/\r?\n$/, '');
if (this.value) lastChild.content += os.EOL;
},

/**
Expand Down
7 changes: 5 additions & 2 deletions src/options/lines-between-rulesets.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

let gonzales = require('gonzales-pe');
let os = require('os');

let option = {
newLinesString: '',
Expand Down Expand Up @@ -70,7 +71,7 @@ let option = {
numNewLines = Math.round(this.value) + newLinesOffset;

for (var i = 0; i < numNewLines; i++) {
spacing += '\n';
spacing += os.EOL;
}

return spacing;
Expand Down Expand Up @@ -136,7 +137,9 @@ let option = {
if (!node) {
return false;
}
return (node.content === '\n');

var content = node.content;
return (content === '\n' || content === '\r\n');
},

prevLineIsComment(parent, index) {
Expand Down
19 changes: 12 additions & 7 deletions src/options/sort-order.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var gonzales = require('gonzales-pe');
var os = require('os');

module.exports = {
get name() {
Expand Down Expand Up @@ -55,7 +56,9 @@ module.exports = {
let containsOnlyLinebreaks = true;

node.forEach((space) => {
if (!space.is('space') || space.content !== '\n') {
var content = space.content;

if (!space.is('space') || (content !== '\n' && content !== '\r\n')) {
containsOnlyLinebreaks = false;
return null;
}
Expand Down Expand Up @@ -262,7 +265,7 @@ module.exports = {

if (node.syntax === 'sass' && spacesBeforeNode.length) {
let space = spacesBeforeNode[0];
space.content = space.content.replace(/\n/, '');
space.content = space.content.replace(/\r?\n/, '');
}

spacesBeforeNode.reverse().map(this._removeEmptyLines);
Expand All @@ -272,12 +275,14 @@ module.exports = {
// Divide declarations from different groups with
// an empty line:
if (prevNode && currentNode.groupIndex > prevNode.groupIndex) {
let matches;
let space = spacesBeforeNode[0];

if (space && space.is('space') &&
(space.syntax === 'sass' ||
space.content.match(/\n/g) &&
space.content.match(/\n/g).length < 2)) {
space.content = '\n' + space.content;
((matches = space.content.match(/\r?\n/g)) !== null && matches.length === 1))) {
let eol = matches == null ? os.EOL : matches[0]; // jshint ignore:line
space.content = eol + space.content;
}
}

Expand All @@ -292,7 +297,7 @@ module.exports = {
currentNode.node.is('extend'))) {
let delimiter = gonzales.createNode({
type: 'declarationDelimiter',
content: currentNode.node.syntax === 'sass' ? '\n' : ';'
content: currentNode.node.syntax === 'sass' ? os.EOL : ';'
});
node.content.unshift(delimiter);
}
Expand Down Expand Up @@ -338,7 +343,7 @@ module.exports = {
* @param {node} node Space node.
*/
_removeEmptyLines(node) {
node.content = node.content.replace(/\n[\s\t\n\r]*\n/, '\n');
node.content = node.content.replace(/(\r?\n)\s*\1/, '$1');
},

_separateSortablesFromBlock(block) {
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-after-colon.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-after-combinator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-after-opening-brace.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-after-selector-delimiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-before-closing-brace.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = (function() {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-before-colon.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-before-combinator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-before-opening-brace.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = (function() {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-before-selector-delimiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/options/space-between-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = (function() {

accepts: {
number: true,
string: /^[ \t\n]*$/
string: /^(?:[ \t]|\r?\n)*$/
},

/**
Expand Down
8 changes: 4 additions & 4 deletions src/options/strip-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = (function() {
* @returns {String}
*/
function trim(string) {
return string.replace(/[ \t]+\n/g, '\n');
return string.replace(/[ \t]+(\r?\n)/g, '$1');
}

return {
Expand All @@ -29,7 +29,7 @@ module.exports = (function() {
if (lastChild.is('space')) {
lastChild.content = trim(lastChild.content)
.replace(/[ \t]+$/, '')
.replace(/[\n]+/g, '\n');
.replace(/(\r?\n)+/g, '$1');
}

ast.traverseByType('space', function(space) {
Expand All @@ -52,12 +52,12 @@ module.exports = (function() {
var lastChild = ast.last();
if (lastChild.is('space') &&
lastChild.content !== '\n' &&
lastChild.content.match(/^[ \n\t]+$/)) {
lastChild.content.match(/^(?:[ \t]|\r?\n)+$/)) {
detected.push(false);
}

ast.traverseByType('space', function(space) {
if (space.content.match(/[ \t]\n/)) detected.push(false);
if (space.content.match(/[ \t]\r?\n/)) detected.push(false);
});

return detected;
Expand Down
4 changes: 2 additions & 2 deletions src/options/vendor-prefix-align.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ module.exports = (function() {
payload: function(info, i) {
if (node.get(i - 1) && node.get(i - 1).content) {
let nodeLength = node.get(i - 1).content
.replace(/^[ \t]*\n+/, '').length;
.replace(/^[ \t]*(?:\r?\n)+/, '').length;
var sum = nodeLength + info.prefixLength;
getResult({node: node, sum: sum, info: info, i: i});
}
Expand All @@ -452,7 +452,7 @@ module.exports = (function() {

if (node.get(i).get(x - 1)) {
let nodeLength = node.get(i).get(x - 1).content
.replace(/^[ \t]*\n+/, '').length;
.replace(/^[ \t]*(?:\r?\n)+/, '').length;
var sum = nodeLength + info.prefixLength;
getResult({node: node, sum: sum, info: info, i: i});
}
Expand Down