1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
|
3 |
|
|
var posix = require('posix-character-classes');
|
4 |
|
|
|
5 |
|
|
module.exports = function(brackets) {
|
6 |
|
|
brackets.compiler
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Escaped characters
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
|
|
.set('escape', function(node) {
|
13 |
|
|
return this.emit('\\' + node.val.replace(/^\\/, ''), node);
|
14 |
|
|
})
|
15 |
|
|
|
16 |
|
|
/**
|
17 |
|
|
* Text
|
18 |
|
|
*/
|
19 |
|
|
|
20 |
|
|
.set('text', function(node) {
|
21 |
|
|
return this.emit(node.val.replace(/([{}])/g, '\\$1'), node);
|
22 |
|
|
})
|
23 |
|
|
|
24 |
|
|
/**
|
25 |
|
|
* POSIX character classes
|
26 |
|
|
*/
|
27 |
|
|
|
28 |
|
|
.set('posix', function(node) {
|
29 |
|
|
if (node.val === '[::]') {
|
30 |
|
|
return this.emit('\\[::\\]', node);
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
var val = posix[node.inner];
|
34 |
|
|
if (typeof val === 'undefined') {
|
35 |
|
|
val = '[' + node.inner + ']';
|
36 |
|
|
}
|
37 |
|
|
return this.emit(val, node);
|
38 |
|
|
})
|
39 |
|
|
|
40 |
|
|
/**
|
41 |
|
|
* Non-posix brackets
|
42 |
|
|
*/
|
43 |
|
|
|
44 |
|
|
.set('bracket', function(node) {
|
45 |
|
|
return this.mapVisit(node.nodes);
|
46 |
|
|
})
|
47 |
|
|
.set('bracket.open', function(node) {
|
48 |
|
|
return this.emit(node.val, node);
|
49 |
|
|
})
|
50 |
|
|
.set('bracket.inner', function(node) {
|
51 |
|
|
var inner = node.val;
|
52 |
|
|
|
53 |
|
|
if (inner === '[' || inner === ']') {
|
54 |
|
|
return this.emit('\\' + node.val, node);
|
55 |
|
|
}
|
56 |
|
|
if (inner === '^]') {
|
57 |
|
|
return this.emit('^\\]', node);
|
58 |
|
|
}
|
59 |
|
|
if (inner === '^') {
|
60 |
|
|
return this.emit('^', node);
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
if (/-/.test(inner) && !/(\d-\d|\w-\w)/.test(inner)) {
|
64 |
|
|
inner = inner.split('-').join('\\-');
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
var isNegated = inner.charAt(0) === '^';
|
68 |
|
|
// add slashes to negated brackets, per spec
|
69 |
|
|
if (isNegated && inner.indexOf('/') === -1) {
|
70 |
|
|
inner += '/';
|
71 |
|
|
}
|
72 |
|
|
if (isNegated && inner.indexOf('.') === -1) {
|
73 |
|
|
inner += '.';
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
// don't unescape `0` (octal literal)
|
77 |
|
|
inner = inner.replace(/\\([1-9])/g, '$1');
|
78 |
|
|
return this.emit(inner, node);
|
79 |
|
|
})
|
80 |
|
|
.set('bracket.close', function(node) {
|
81 |
|
|
var val = node.val.replace(/^\\/, '');
|
82 |
|
|
if (node.parent.escaped === true) {
|
83 |
|
|
return this.emit('\\' + val, node);
|
84 |
|
|
}
|
85 |
|
|
return this.emit(val, node);
|
86 |
|
|
});
|
87 |
|
|
};
|