Skip to content

Commit 3322609

Browse files
More consistent named colour variables.
1 parent 8580ff8 commit 3322609

24 files changed

+93
-92
lines changed

lib/less/functions.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -310,21 +310,19 @@ tree.functions = {
310310
percentage: function (n) {
311311
return new(tree.Dimension)(n.value * 100, '%');
312312
},
313-
color: function (n) {
314-
if (n instanceof tree.Quoted) {
315-
var colorCandidate = n.value,
316-
returnColor;
317-
returnColor = tree.Color.fromKeyword(colorCandidate);
318-
if (returnColor) {
319-
return returnColor;
320-
}
321-
if (/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/.test(colorCandidate)) {
322-
return new(tree.Color)(colorCandidate.slice(1));
323-
}
324-
throw { type: "Argument", message: "argument must be a color keyword or 3/6 digit hex e.g. #FFF" };
325-
} else {
326-
throw { type: "Argument", message: "argument must be a string" };
327-
}
313+
color: function(c) {
314+
if ((c instanceof tree.Quoted) &&
315+
(/^#([a-f0-9]{6}|[a-f0-9]{3})$/i.test(c.value))) {
316+
return new(tree.Color)(c.value.slice(1));
317+
}
318+
if ((c instanceof tree.Color) || (c = tree.Color.fromKeyword(c.value))) {
319+
c.keyword = undefined;
320+
return c;
321+
}
322+
throw {
323+
type: "Argument",
324+
message: "argument must be a color keyword or 3/6 digit hex e.g. #FFF"
325+
};
328326
},
329327
iscolor: function (n) {
330328
return this._isa(n, tree.Color);

lib/less/parser.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -793,15 +793,9 @@ less.Parser = function Parser(env) {
793793
// black border-collapse
794794
//
795795
keyword: function () {
796-
var k;
797-
798-
k = $re(/^[_A-Za-z-][_A-Za-z0-9-]*/);
796+
var k = $(/^[_A-Za-z-][_A-Za-z0-9-]*/);
799797
if (k) {
800-
var color = tree.Color.fromKeyword(k);
801-
if (color) {
802-
return color;
803-
}
804-
return new(tree.Keyword)(k);
798+
return tree.Color.fromKeyword(k) || new(tree.Keyword)(k);
805799
}
806800
},
807801

@@ -1960,4 +1954,4 @@ less.Parser.serializeVars = function(vars) {
19601954
}
19611955

19621956
return s;
1963-
};
1957+
};

lib/less/tree/color.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ tree.Color = function (rgb, a) {
2323
this.alpha = typeof(a) === 'number' ? a : 1;
2424
};
2525

26-
var transparentKeyword = "transparent";
27-
2826
tree.Color.prototype = {
2927
type: "Color",
3028
eval: function () { return this; },
@@ -34,35 +32,39 @@ tree.Color.prototype = {
3432
output.add(this.toCSS(env));
3533
},
3634
toCSS: function (env, doNotCompress) {
37-
var compress = env && env.compress && !doNotCompress,
38-
alpha = tree.fround(env, this.alpha);
35+
var compress = env && env.compress && !doNotCompress, color, alpha;
36+
37+
// `keyword` is set if this color was originally
38+
// converted from a named color string so we need
39+
// to respect this and try to output named color too.
40+
if (this.keyword) {
41+
return this.keyword;
42+
}
3943

4044
// If we have some transparency, the only way to represent it
4145
// is via `rgba`. Otherwise, we use the hex representation,
4246
// which has better compatibility with older browsers.
4347
// Values are capped between `0` and `255`, rounded and zero-padded.
48+
alpha = tree.fround(env, this.alpha);
4449
if (alpha < 1) {
45-
if (alpha === 0 && this.isTransparentKeyword) {
46-
return transparentKeyword;
47-
}
4850
return "rgba(" + this.rgb.map(function (c) {
4951
return clamp(Math.round(c), 255);
5052
}).concat(clamp(alpha, 1))
5153
.join(',' + (compress ? '' : ' ')) + ")";
52-
} else {
53-
var color = this.toRGB();
54+
}
55+
56+
color = this.toRGB();
5457

55-
if (compress) {
56-
var splitcolor = color.split('');
58+
if (compress) {
59+
var splitcolor = color.split('');
5760

58-
// Convert color to short format
59-
if (splitcolor[1] === splitcolor[2] && splitcolor[3] === splitcolor[4] && splitcolor[5] === splitcolor[6]) {
60-
color = '#' + splitcolor[1] + splitcolor[3] + splitcolor[5];
61-
}
61+
// Convert color to short format
62+
if (splitcolor[1] === splitcolor[2] && splitcolor[3] === splitcolor[4] && splitcolor[5] === splitcolor[6]) {
63+
color = '#' + splitcolor[1] + splitcolor[3] + splitcolor[5];
6264
}
63-
64-
return color;
6565
}
66+
67+
return color;
6668
},
6769

6870
//
@@ -152,16 +154,17 @@ tree.Color.prototype = {
152154
};
153155

154156
tree.Color.fromKeyword = function(keyword) {
155-
keyword = keyword.toLowerCase();
156-
157-
if (tree.colors.hasOwnProperty(keyword)) {
158-
// detect named color
159-
return new(tree.Color)(tree.colors[keyword].slice(1));
157+
var c, key = keyword.toLowerCase();
158+
if (tree.colors.hasOwnProperty(key)) {
159+
c = new(tree.Color)(tree.colors[key].slice(1));
160+
}
161+
else if (key === "transparent") {
162+
c = new(tree.Color)([0, 0, 0], 0);
160163
}
161-
if (keyword === transparentKeyword) {
162-
var transparent = new(tree.Color)([0, 0, 0], 0);
163-
transparent.isTransparentKeyword = true;
164-
return transparent;
164+
165+
if (c) {
166+
c.keyword = keyword;
167+
return c;
165168
}
166169
};
167170

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.test {
2-
color: #ff0000;
2+
color: red;
33
}

test/browser/css/modify-vars/simple.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
color: gainsboro;
33
}
44
.test {
5-
color1: #008000;
6-
color2: #800080;
5+
color1: green;
6+
color2: purple;
77
scalar: 20;
88
}

test/css/comments.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@
4848
.selector,
4949
.lots,
5050
.comments {
51-
color: #808080, /* blue */ #ffa500;
51+
color: grey, /* blue */ orange;
5252
-webkit-border-radius: 2px /* webkit only */;
5353
-moz-border-radius: 8px /* moz only with operation */;
5454
}
5555
.test {
5656
color: 1px;
5757
}
5858
#last {
59-
color: #0000ff;
59+
color: blue;
6060
}
6161
/* */
6262
/* { */

test/css/css-3.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.comma-delimited {
2-
text-shadow: -1px -1px 1px #ff0000, 6px 5px 5px #ffff00;
2+
text-shadow: -1px -1px 1px red, 6px 5px 5px yellow;
33
-moz-box-shadow: 0pt 0pt 2px rgba(255, 255, 255, 0.4) inset, 0pt 4px 6px rgba(255, 255, 255, 0.4) inset;
44
-webkit-transform: rotate(0deg);
55
}

test/css/css-escapes.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
background: red;
99
}
1010
.\34 04 strong {
11-
color: #ff00ff;
11+
color: fuchsia;
1212
font-weight: bold;
1313
}
1414
.trailingTest\+ {

test/css/css.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ p + h1 {
7171
display: -moz-inline-stack;
7272
width: .1em;
7373
background-color: #009998;
74-
background: -webkit-gradient(linear, left top, left bottom, from(#ff0000), to(#0000ff));
74+
background: -webkit-gradient(linear, left top, left bottom, from(red), to(blue));
7575
margin: ;
7676
filter: alpha(opacity=100);
7777
width: auto\9;

test/css/extract-and-length.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
name-value: name;
1414
string-value: "string";
1515
number-value: 12345678;
16-
color-value: #0000ff;
16+
color-value: blue;
1717
rgba-value: rgba(80, 160, 240, 0.67);
1818
empty-value: ;
1919
name-length: 1;

test/css/functions.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@
8787
max: 3;
8888
max: max(8%, 1cm);
8989
percentage: 20%;
90-
color: #ff0011;
90+
color-quoted-digit: #dda0dd;
91+
color-quoted-keyword: #dda0dd;
92+
color-color: #dda0dd;
93+
color-keyword: #dda0dd;
9194
tint: #898989;
9295
tint-full: #ffffff;
9396
tint-percent: #898989;

test/css/globalVars/simple.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
* Test
33
*/
44
.class {
5-
color: #ff0000;
5+
color: red;
66
}

test/css/import-once.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#import {
2-
color: #ff0000;
2+
color: red;
33
}
44
body {
55
width: 100%;

test/css/import.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@import url("//ha.com/file.css") (min-width: 100px);
44
#import-test {
55
height: 10px;
6-
color: #ff0000;
6+
color: red;
77
width: 10px;
88
height: 30%;
99
}
@@ -13,11 +13,11 @@
1313
}
1414
}
1515
#import {
16-
color: #ff0000;
16+
color: red;
1717
}
1818
.mixin {
1919
height: 10px;
20-
color: #ff0000;
20+
color: red;
2121
}
2222
@media screen and (max-width: 601px) {
2323
#css {

test/css/mixins-args.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
color: blue;
99
width: 10px;
1010
height: 99%;
11-
border: 2px dotted #000000;
11+
border: 2px dotted black;
1212
}
1313
.one-arg {
1414
width: 15px;
@@ -52,7 +52,7 @@ body {
5252
width: 10px;
5353
}
5454
.arguments {
55-
border: 1px solid #000000;
55+
border: 1px solid black;
5656
width: 1px;
5757
}
5858
.arguments2 {

test/css/mixins-guards-default-func.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ guard-default-multi-2-3 {
9696
default-3: 3;
9797
}
9898
guard-default-multi-3-blue {
99-
case-2: #00008b;
99+
case-2: darkblue;
100100
}
101101
guard-default-multi-3-green {
102-
default-color: #008000;
102+
default-color: green;
103103
}
104104
guard-default-multi-3-foo {
105105
case-1: I am 'foo';

test/css/mixins-guards.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@
6262
test: pass;
6363
}
6464
.colorguardtest {
65-
content: is #ff0000;
66-
content: is not #0000ff its #ff0000;
67-
content: is not #0000ff its #800080;
65+
content: is red;
66+
content: is not blue its red;
67+
content: is not blue its purple;
6868
}
6969
.stringguardtest {
7070
content: is theme1;

test/css/parens.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.parens {
2-
border: 2px solid #000000;
2+
border: 2px solid black;
33
margin: 1px 3px 16 3;
44
width: 36;
55
padding: 2px 36px;

test/css/scope.css

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
color: #998899;
33
}
44
.scope1 {
5-
color: #0000ff;
6-
border-color: #000000;
5+
color: blue;
6+
border-color: black;
77
}
88
.scope1 .scope2 {
9-
color: #0000ff;
9+
color: blue;
1010
}
1111
.scope1 .scope2 .scope3 {
12-
color: #ff0000;
13-
border-color: #000000;
14-
background-color: #ffffff;
12+
color: red;
13+
border-color: black;
14+
background-color: white;
1515
}
1616
.scope {
17-
scoped-val: #008000;
17+
scoped-val: green;
1818
}
1919
.heightIsSet {
2020
height: 1024px;

test/css/selectors.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,16 @@ p a span {
102102
background: amber;
103103
}
104104
.other ::fnord {
105-
color: #ff0000;
105+
color: red;
106106
}
107107
.other::fnord {
108-
color: #ff0000;
108+
color: red;
109109
}
110110
.other ::bnord {
111-
color: #ff0000;
111+
color: red;
112112
}
113113
.other::bnord {
114-
color: #ff0000;
114+
color: red;
115115
}
116116
.blood {
117117
color: red;

test/css/strings.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
url5: "http://lesscss.org/54.4px";
3434
}
3535
.mix-mul-class {
36-
color: #0000ff;
37-
color: #ff0000;
38-
color: #000000;
39-
color: #ffa500;
36+
color: blue;
37+
color: red;
38+
color: black;
39+
color: orange;
4040
}
4141
.watermark {
4242
family: Univers, Arial, Verdana, San-Serif;

test/css/whitespace.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
color: white;
2020
}
2121
.no-semi-column {
22-
color: #ffffff;
22+
color: white;
2323
}
2424
.no-semi-column {
2525
color: white;
2626
white-space: pre;
2727
}
2828
.no-semi-column {
29-
border: 2px solid #ffffff;
29+
border: 2px solid white;
3030
}
3131
.newlines {
3232
background: the,

0 commit comments

Comments
 (0)