Skip to content

Commit e58662f

Browse files
committed
better support for attributes. Allow interpolation inside attributes. Fixes less#1229
1 parent 19405ac commit e58662f

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

lib/less/parser.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,16 +1140,15 @@ less.Parser = function Parser(env) {
11401140

11411141
if (! $('[')) return;
11421142

1143-
if (key = $(/^(?:[_A-Za-z0-9-]|\\.)+/) || $(this.entities.quoted)) {
1144-
if ((op = $(/^[|~*$^]?=/)) &&
1145-
(val = $(this.entities.quoted) || $(/^[\w-]+/))) {
1146-
attr = [key, op, val.toCSS ? val.toCSS() : val].join('');
1147-
} else { attr = key }
1143+
key = expect(/^(?:[_A-Za-z0-9-\*]*\|)?(?:[_A-Za-z0-9-]|\\.)+/);
1144+
1145+
if ((op = $(/^[|~*$^]?=/))) {
1146+
val = $(this.entities.quoted) || $(/^[\w-]+/);
11481147
}
11491148

1150-
if (! $(']')) return;
1149+
expect(']');
11511150

1152-
if (attr) { return "[" + attr + "]" }
1151+
return new(tree.Attribute)(key, op, val);
11531152
},
11541153

11551154
//

lib/less/tree/element.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ tree.Element.prototype = {
3434
}
3535
};
3636

37+
tree.Attribute = function (key, op, value) {
38+
this.key = key;
39+
this.op = op;
40+
this.value = value;
41+
};
42+
tree.Attribute.prototype = {
43+
type: "Attribute",
44+
accept: function (visitor) {
45+
this.value = visitor.visit(this.value);
46+
},
47+
eval: function (env) {
48+
return new(tree.Attribute)(this.key,
49+
this.op, (this.value && this.value.eval) ? this.value.eval(env) : this.value);
50+
},
51+
toCSS: function (env) {
52+
var value = this.key;
53+
54+
if (this.op) {
55+
value += this.op;
56+
value += (this.value.toCSS ? this.value.toCSS(env) : this.value);
57+
}
58+
59+
return '[' + value + ']';
60+
}
61+
};
62+
3763
tree.Combinator = function (value) {
3864
if (value === ' ') {
3965
this.value = ' ';

test/css/selectors.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,11 @@ p a span {
128128
.test:nth-child(odd):not(:nth-child(3)) {
129129
color: #ff0000;
130130
}
131+
[prop],
132+
[prop="value3"],
133+
[prop*="val3"],
134+
[|prop~="val3"],
135+
[*|prop$="val3"],
136+
[ns|prop^="val3"] {
137+
attributes: yes;
138+
}

test/less/selectors.less

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,12 @@ a {
129129
&:nth-child(odd):not(:nth-child(3)) {
130130
color: #ff0000;
131131
}
132+
}
133+
[prop],
134+
[prop="value@{num}"],
135+
[prop*="val@{num}"],
136+
[|prop~="val@{num}"],
137+
[*|prop$="val@{num}"],
138+
[ns|prop^="val@{num}"] {
139+
attributes: yes;
132140
}

0 commit comments

Comments
 (0)