Skip to content

Commit 5ff5398

Browse files
authored
Merge pull request #18 from jonathantneal/fix/atword
Fix atword toString
2 parents bbe07e5 + 9fe3753 commit 5ff5398

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lib/atword.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ class AtWord extends Container {
77
super(opts);
88
this.type = 'atword';
99
}
10+
11+
toString () {
12+
let quote = this.quoted ? this.raws.quote : '';
13+
return [
14+
this.raws.before,
15+
'@',
16+
// we can't use String() here because it'll try using itself
17+
// as the constructor
18+
String.prototype.toString.call(this.value),
19+
this.raws.after
20+
].join('');
21+
}
1022
}
1123

1224
Container.registerWalker(AtWord);

test/atrule.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const chai = require('chai');
4+
const shallowDeepEqual = require('chai-shallow-deep-equal');
5+
const Parser = require('../lib/parser');
6+
7+
let expect = chai.expect;
8+
9+
describe('Parser → Atrule', () => {
10+
11+
chai.use(shallowDeepEqual);
12+
13+
let fixtures,
14+
failures;
15+
16+
fixtures = [
17+
{
18+
it: 'should parse atword',
19+
test: ' @word ',
20+
expected: [
21+
{ type: 'atword', value: 'word', raws: { before: ' ', after: ' ' } }
22+
]
23+
},
24+
{
25+
it: 'should not parse escaped @ after @ (@\\@)',
26+
test: ' @\\@word ',
27+
expected: [
28+
{ type: 'atword', value: '\\', raws: { before: ' ', after: '' } }
29+
]
30+
}
31+
];
32+
33+
fixtures.forEach((fixture) => {
34+
it(fixture.it, () => {
35+
let ast = new Parser(fixture.test).parse();
36+
37+
ast.first.walk((node, index) => {
38+
let expected = fixture.expected[index];
39+
40+
if (expected) {
41+
expect(node).to.shallowDeepEqual(expected);
42+
}
43+
});
44+
});
45+
});
46+
47+
failures && failures.forEach((fixture) => {
48+
it(fixture.it, () => {
49+
expect(() => new Parser(fixture.test).parse()).to.throw(Error);
50+
});
51+
});
52+
53+
});

0 commit comments

Comments
 (0)