Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 434a9b4

Browse files
fix(base/trimIndentation): ensure that lines that start at index zero kill the indentation
Previously we were trying to be clever and guess that lines that had zero indentation were actually just rolling over from the previous line and so were ignored. But since we ignore the first line if it has no indentation, we had to rely on the second line having no indentation for the zero indentation to be set. If the second line had indentation then all future zero indentation lines were being ignored. This change now never ignores zero indent lines and so will indent most things properly. If a line is rolling over then it should be indented to the minimum level already for it to be displayed correctly. See angular/angular.js#10101
1 parent f909b9c commit 434a9b4

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

base/services/trimIndentation.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@ function calcIndent(text) {
66
var MAX_INDENT = 9999;
77
var lines = text.split('\n');
88
var minIndent = MAX_INDENT;
9+
var emptyLinesRemoved = false;
10+
11+
// ignore leading empty lines
12+
while(isEmpty(lines[0])) {
13+
lines.shift();
14+
emptyLinesRemoved = true;
15+
}
916

1017
// ignore first line if it has no indentation and there is more than one line
1118
// this is because sometimes our text starts in the middle of a line of other
1219
// text that is indented and so doesn't appear to have an indent when it really does.
1320
var ignoreLine = (lines[0][0] != ' ' && lines.length > 1);
14-
if ( ignoreLine ) {
15-
lines.shift();
16-
}
17-
// ignore leading empty lines
18-
while(isEmpty(lines[0])) {
21+
if ( ignoreLine && !emptyLinesRemoved ) {
1922
lines.shift();
2023
}
2124

2225
lines.forEach(function(line){
23-
var indent = line.match(/^\s*/)[0].length;
24-
// If indent is 0 then we assume that it is just an overflow and not part of the indentation
25-
if (indent > 0 || minIndent == MAX_INDENT) {
26+
if ( !isEmpty(line) ) {
27+
var indent = line.match(/^\s*/)[0].length;
2628
minIndent = Math.min(minIndent, indent);
2729
}
2830
});

base/services/trimIndentation.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ describe("trimIndentation", function() {
2424
expect(trimIndentation('abc\n\n\n')).toEqual('abc');
2525
});
2626

27+
it("should not trim indentation if more than the first line is not indented", function() {
28+
expect(trimIndentation(
29+
'.ng-hide {\n' +
30+
' /* this is just another form of hiding an element */\n' +
31+
' display:block!important;\n' +
32+
' position:absolute;\n' +
33+
' top:-9999px;\n' +
34+
' left:-9999px;\n' +
35+
'}')).toEqual(
36+
'.ng-hide {\n' +
37+
' /* this is just another form of hiding an element */\n' +
38+
' display:block!important;\n' +
39+
' position:absolute;\n' +
40+
' top:-9999px;\n' +
41+
' left:-9999px;\n' +
42+
'}');
43+
});
44+
2745
describe("calcIndent", function() {
2846
it("should calculate simple leading white-space from a single line of text", function() {
2947
expect(trimIndentation.calcIndent(' abc ')).toEqual(3);

0 commit comments

Comments
 (0)