Skip to content

Commit

Permalink
add support for flags in attribute selector (fixes #270)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Feb 27, 2016
1 parent 928282e commit 70d3578
Show file tree
Hide file tree
Showing 16 changed files with 385 additions and 207 deletions.
24 changes: 19 additions & 5 deletions lib/compressor/ast/gonzalesToInternal.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,36 @@ var types = {
},
attrib: function(token) {
var offset = 2;
var name;
var operator = null;
var value = null;
var flags = null;

offset = skipSC(token, 2);
var name = convertToInternal(token[offset]);
name = convertToInternal(token[offset]);

offset = skipSC(token, offset + 1);
var operator = token[offset] ? token[offset][2] : null;
if (offset < token.length) {
operator = token[offset][2];

offset = skipSC(token, offset + 1);
var value = convertToInternal(token[offset]);
offset = skipSC(token, offset + 1);
value = convertToInternal(token[offset]);

if (offset < token.length) {
offset = skipSC(token, offset + 1);
if (offset < token.length && token[offset][1] === 'attribFlags') {
flags = token[offset][2];
}
}
}

return {
type: 'Attribute',
info: token[0],
name: name,
operator: operator,
value: value
value: value,
flags: flags
};
},
attrselector: false,
Expand Down
17 changes: 13 additions & 4 deletions lib/compressor/ast/internalToGonzales.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,20 @@ function toGonzales(node) {

result.push([{}, 'ident', node.name.name]);

if (node.operator) {
if (node.operator !== null) {
result.push([{}, 'attrselector', node.operator]);
}
if (node.value) {
result.push(toGonzales(node.value));

if (node.value !== null) {
result.push(toGonzales(node.value));

if (node.flags !== null) {
if (node.value.type !== 'String') {
result.push([{}, 's', ' ']);
}

result.push([{}, 'attribFlags', node.flags]);
}
}
}
return result;

Expand Down
20 changes: 15 additions & 5 deletions lib/compressor/ast/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ function translate(node) {
: each(node.sequence);

case 'Attribute':
return '[' +
translate(node.name) +
(node.operator !== null ? node.operator : '') +
(node.value ? translate(node.value) : '') +
']';
var result = translate(node.name);

if (node.operator !== null) {
result += node.operator;

if (node.value !== null) {
result += translate(node.value);

if (node.flags !== null) {
result += (node.value.type !== 'String' ? ' ' : '') + node.flags;
}
}
}

return '[' + result + ']';

case 'FunctionalPseudo':
return ':' + node.name + '(' + eachDelim(node.arguments, ',') + ')';
Expand Down
20 changes: 15 additions & 5 deletions lib/compressor/ast/translateWithSourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,21 @@ function translate(node) {
: each(node.sequence);

case 'Attribute':
return '[' +
translate(node.name) +
(node.operator !== null ? node.operator : '') +
(node.value ? translate(node.value) : '') +
']';
var result = translate(node.name);

if (node.operator !== null) {
result += node.operator;

if (node.value !== null) {
result += translate(node.value);

if (node.flags !== null) {
result += (node.value.type !== 'String' ? ' ' : '') + node.flags;
}
}
}

return '[' + result + ']';

case 'FunctionalPseudo':
return ':' + node.name + '(' + eachDelim(node.arguments, ',') + ')';
Expand Down
12 changes: 12 additions & 0 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ function getAny() {
return ident;
}

// '[' S* attrib_name ']'
// '[' S* attrib_name attrib_match [ IDENT | STRING ] S* attrib_flags? ']'
function getAttrib() {
var node = [getInfo(pos), NodeType.AttribType];

Expand All @@ -593,6 +595,16 @@ function getAttrib() {
}

readSC(node);

// attribute flags
if (pos < tokens.length && tokens[pos].type === TokenType.Identifier) {
node.push([
getInfo(pos),
'attribFlags',
tokens[pos++].value
]);
readSC(node);
}
}

eat(TokenType.RightSquareBracket);
Expand Down
1 change: 1 addition & 0 deletions lib/utils/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var typeHandlers = {
operator: simple,
raw: simple,
unknown: simple,
attribFlags: simple,

simpleselector: composite,
dimension: composite,
Expand Down
4 changes: 4 additions & 0 deletions test/fixture/compress/attrib.string/1.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[title="test"],
[title="test"i],
[title="test" i ],
[title="123"],
[title="test test"],
[title="test test"i],
[title="test test" i ],
[title="-"],
[title=""]
{
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/compress/attrib.string/1.min.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[title=""],[title="-"],[title="123"],[title="test test"],[title=test]{p:v}
[title=""],[title="-"],[title="123"],[title="test test"],[title="test test"i],[title=test i],[title=test]{p:v}
4 changes: 4 additions & 0 deletions test/fixture/compress/attrib.string/2.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[title='test'],
[title='test'i],
[title='test' i ],
[title='123'],
[title='test test'],
[title='test test'i],
[title='test test' i ],
[title='-'],
[title='']
{
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/compress/attrib.string/2.min.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[title=''],[title='-'],[title='123'],[title='test test'],[title=test]{p:v}
[title=''],[title='-'],[title='123'],[title='test test'],[title='test test'i],[title=test i],[title=test]{p:v}
3 changes: 2 additions & 1 deletion test/fixture/compress/attrib.string/3.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[name=test]
[name=test],
[name=test i]
{
p: v;
}
2 changes: 1 addition & 1 deletion test/fixture/compress/attrib.string/3.min.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[name=test]{p:v}
[name=test i],[name=test]{p:v}
Loading

0 comments on commit 70d3578

Please sign in to comment.