Skip to content

Commit e7bd488

Browse files
committed
Merge pull request TrySound#22 from MohammadYounes/comment-node
Add support for comment node
2 parents bc132d9 + 7ffd255 commit e7bd488

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ parsed.toString(); // #E92D42
7474

7575
Each node is an object with these common properties:
7676

77-
- **type**: The type of node (`word`, `string`, `div`, `space`, or `function`).
77+
- **type**: The type of node (`word`, `string`, `div`, `space`, `comment`, or `function`).
7878
Each type is documented below.
7979
- **value**: Each node has a `value` property; but what exactly `value` means
8080
is specific to the node type. Details are documented for each type below.
@@ -122,6 +122,16 @@ Node-specific properties:
122122

123123
- **value**: The whitespace itself.
124124

125+
### comment
126+
127+
A CSS comment starts with `/*` and ends with `*/`
128+
129+
Node-specific properties:
130+
131+
- **value**: The comment value without `/*` and `*/`
132+
- **unclosed**: True when the comment has no end.
133+
e.g. `/* comment without an end `.
134+
125135
### function
126136

127137
A CSS function, e.g. `rgb(0,0,0)` or `url(foo.bar)`.

lib/parse.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var backslash = '\\'.charCodeAt(0);
66
var slash = '/'.charCodeAt(0);
77
var comma = ','.charCodeAt(0);
88
var colon = ':'.charCodeAt(0);
9+
var star = '*'.charCodeAt(0);
910

1011
module.exports = function (input) {
1112
var tokens = [];
@@ -38,7 +39,7 @@ module.exports = function (input) {
3839
after = token;
3940
} else if (prev && prev.type === 'div') {
4041
prev.after = token;
41-
} else if (code === slash || code === comma || code === colon) {
42+
} else if (code === slash && value.charCodeAt(next + 1) !== star || code === comma || code === colon) {
4243
before = token;
4344
} else {
4445
tokens.push({ type: 'space', sourceIndex: pos, value: token });
@@ -72,6 +73,22 @@ module.exports = function (input) {
7273
pos = next + 1;
7374
code = value.charCodeAt(pos);
7475

76+
//Comments
77+
} else if (code === slash && value.charCodeAt(pos + 1) === star) {
78+
token = { type: 'comment', sourceIndex: pos};
79+
80+
next = value.indexOf('*/', pos);
81+
if (next === -1) {
82+
token.unclosed = true;
83+
next = value.length;
84+
}
85+
86+
token.value = value.slice(pos + 2, next);
87+
tokens.push(token);
88+
89+
pos = next + 2;
90+
code = value.charCodeAt(pos);
91+
7592
// Dividers
7693
} else if (code === slash || code === comma || code === colon) {
7794
token = value[pos];
@@ -93,7 +110,7 @@ module.exports = function (input) {
93110
token = { type: 'function', sourceIndex: pos - name.length, value: name, before: value.slice(pos + 1, next) };
94111
pos = next;
95112

96-
if (name === 'url' && code !== singleQuote && code !== doubleQuote) {
113+
if (name === 'url' && code !== singleQuote && code !== doubleQuote && (code !== slash || value.charCodeAt(pos + 1) !== star)) {
97114
next -= 1;
98115
do {
99116
escape = false;
@@ -164,7 +181,7 @@ module.exports = function (input) {
164181
} while (next < max && !(
165182
code <= 32 ||
166183
code === singleQuote || code === doubleQuote ||
167-
code === slash || code === comma || code === colon ||
184+
code === slash && value.charCodeAt(next + 1) !== star || code === comma || code === colon ||
168185
code === openParentheses || code === closeParentheses && balanced
169186
));
170187
token = value.slice(pos, next);

lib/stringify.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ function stringifyNode(node) {
66
return value;
77
} else if (type === 'string') {
88
return (node.quote || '') + value + (node.quote || '');
9+
} else if (type === 'comment') {
10+
return '/*' + value + (node.unclosed ? '' : '*/');
911
} else if (type === 'div') {
1012
return (node.before || '') + value + (node.after || '');
1113
} else if (Array.isArray(node.nodes)) {

test/parse.js

+35
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,41 @@ var tests = [{
389389
expected: [
390390
{ type: 'function', sourceIndex: 0, value: 'url', before: '', after: '', nodes: [] }
391391
]
392+
}, {
393+
message: 'should parse comments',
394+
fixture: '/*before*/ 1px /*between*/ 1px /*after*/',
395+
expected: [
396+
{ type: 'comment', sourceIndex: 0, value: 'before'},
397+
{ type: 'space', sourceIndex: 10, value: ' ' },
398+
{ type: 'word', sourceIndex: 11, value: '1px' },
399+
{ type: 'space', sourceIndex: 14, value: ' ' },
400+
{ type: 'comment', sourceIndex: 15, value: 'between'},
401+
{ type: 'space', sourceIndex: 26, value: ' ' },
402+
{ type: 'word', sourceIndex: 27, value: '1px' },
403+
{ type: 'space', sourceIndex: 30, value: ' ' },
404+
{ type: 'comment', sourceIndex: 31, value: 'after'},
405+
]
406+
}, {
407+
message: 'should parse comments inside functions',
408+
fixture: 'url( "/demo/bg.png" /*comment*/ )',
409+
expected: [
410+
{ type: 'function', sourceIndex: 0, value: 'url', before: ' ', after: ' ', nodes: [
411+
{ type: 'string', sourceIndex: 5, value: '/demo/bg.png', quote:'"'},
412+
{ type: 'space', sourceIndex: 19, value: ' ' },
413+
{ type: 'comment', sourceIndex: 20, value: 'comment'}
414+
] }
415+
]
416+
}, {
417+
message: 'should parse unclosed comments',
418+
fixture: '/*comment*/ 1px /* unclosed ',
419+
expected: [
420+
{ type: 'comment', sourceIndex: 0, value: 'comment'},
421+
{ type: 'space', sourceIndex: 11, value: ' ' },
422+
{ type: 'word', sourceIndex: 12, value: '1px' },
423+
{ type: 'space', sourceIndex: 15, value: ' ' },
424+
{ type: 'comment', sourceIndex: 16, value: ' unclosed ', unclosed:true}
425+
426+
]
392427
}];
393428

394429
test('Parse', function (t) {

test/stringify.js

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ var tests = [
1818
{
1919
message: 'Should correctly process advanced gradients',
2020
fixture: 'background-image:linear-gradient(45deg,transparent 25%,hsla(0,0%,100%,.2) 25%,hsla(0,0%,100%,.2) 75%,transparent 75%,transparent 25%,hsla(0,0%,100%,.2) 75%,transparent 75%,transparent),linear-gradient(45deg,transparent 25%,hsla(0,0%,100%,.2))'
21+
},
22+
{
23+
message: 'Should correctly add comments',
24+
fixture: '/*comment*/ 1px /* unclosed '
25+
},
26+
{
27+
message: 'Should correctly process comments inside functions',
28+
fixture: '/*before*/ rgb( /*red component*/ 12, 54 /*green component*/, /* blue */ 65)/* after */ '
2129
}
2230
];
2331

0 commit comments

Comments
 (0)