[d565449] | 1 | var TYPE = require('../../tokenizer').TYPE;
|
---|
| 2 |
|
---|
| 3 | var IDENT = TYPE.Ident;
|
---|
| 4 | var STRING = TYPE.String;
|
---|
| 5 | var COLON = TYPE.Colon;
|
---|
| 6 | var LEFTSQUAREBRACKET = TYPE.LeftSquareBracket;
|
---|
| 7 | var RIGHTSQUAREBRACKET = TYPE.RightSquareBracket;
|
---|
| 8 | var DOLLARSIGN = 0x0024; // U+0024 DOLLAR SIGN ($)
|
---|
| 9 | var ASTERISK = 0x002A; // U+002A ASTERISK (*)
|
---|
| 10 | var EQUALSSIGN = 0x003D; // U+003D EQUALS SIGN (=)
|
---|
| 11 | var CIRCUMFLEXACCENT = 0x005E; // U+005E (^)
|
---|
| 12 | var VERTICALLINE = 0x007C; // U+007C VERTICAL LINE (|)
|
---|
| 13 | var TILDE = 0x007E; // U+007E TILDE (~)
|
---|
| 14 |
|
---|
| 15 | function getAttributeName() {
|
---|
| 16 | if (this.scanner.eof) {
|
---|
| 17 | this.error('Unexpected end of input');
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | var start = this.scanner.tokenStart;
|
---|
| 21 | var expectIdent = false;
|
---|
| 22 | var checkColon = true;
|
---|
| 23 |
|
---|
| 24 | if (this.scanner.isDelim(ASTERISK)) {
|
---|
| 25 | expectIdent = true;
|
---|
| 26 | checkColon = false;
|
---|
| 27 | this.scanner.next();
|
---|
| 28 | } else if (!this.scanner.isDelim(VERTICALLINE)) {
|
---|
| 29 | this.eat(IDENT);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | if (this.scanner.isDelim(VERTICALLINE)) {
|
---|
| 33 | if (this.scanner.source.charCodeAt(this.scanner.tokenStart + 1) !== EQUALSSIGN) {
|
---|
| 34 | this.scanner.next();
|
---|
| 35 | this.eat(IDENT);
|
---|
| 36 | } else if (expectIdent) {
|
---|
| 37 | this.error('Identifier is expected', this.scanner.tokenEnd);
|
---|
| 38 | }
|
---|
| 39 | } else if (expectIdent) {
|
---|
| 40 | this.error('Vertical line is expected');
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | if (checkColon && this.scanner.tokenType === COLON) {
|
---|
| 44 | this.scanner.next();
|
---|
| 45 | this.eat(IDENT);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | return {
|
---|
| 49 | type: 'Identifier',
|
---|
| 50 | loc: this.getLocation(start, this.scanner.tokenStart),
|
---|
| 51 | name: this.scanner.substrToCursor(start)
|
---|
| 52 | };
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | function getOperator() {
|
---|
| 56 | var start = this.scanner.tokenStart;
|
---|
| 57 | var code = this.scanner.source.charCodeAt(start);
|
---|
| 58 |
|
---|
| 59 | if (code !== EQUALSSIGN && // =
|
---|
| 60 | code !== TILDE && // ~=
|
---|
| 61 | code !== CIRCUMFLEXACCENT && // ^=
|
---|
| 62 | code !== DOLLARSIGN && // $=
|
---|
| 63 | code !== ASTERISK && // *=
|
---|
| 64 | code !== VERTICALLINE // |=
|
---|
| 65 | ) {
|
---|
| 66 | this.error('Attribute selector (=, ~=, ^=, $=, *=, |=) is expected');
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | this.scanner.next();
|
---|
| 70 |
|
---|
| 71 | if (code !== EQUALSSIGN) {
|
---|
| 72 | if (!this.scanner.isDelim(EQUALSSIGN)) {
|
---|
| 73 | this.error('Equal sign is expected');
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | this.scanner.next();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | return this.scanner.substrToCursor(start);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | // '[' <wq-name> ']'
|
---|
| 83 | // '[' <wq-name> <attr-matcher> [ <string-token> | <ident-token> ] <attr-modifier>? ']'
|
---|
| 84 | module.exports = {
|
---|
| 85 | name: 'AttributeSelector',
|
---|
| 86 | structure: {
|
---|
| 87 | name: 'Identifier',
|
---|
| 88 | matcher: [String, null],
|
---|
| 89 | value: ['String', 'Identifier', null],
|
---|
| 90 | flags: [String, null]
|
---|
| 91 | },
|
---|
| 92 | parse: function() {
|
---|
| 93 | var start = this.scanner.tokenStart;
|
---|
| 94 | var name;
|
---|
| 95 | var matcher = null;
|
---|
| 96 | var value = null;
|
---|
| 97 | var flags = null;
|
---|
| 98 |
|
---|
| 99 | this.eat(LEFTSQUAREBRACKET);
|
---|
| 100 | this.scanner.skipSC();
|
---|
| 101 |
|
---|
| 102 | name = getAttributeName.call(this);
|
---|
| 103 | this.scanner.skipSC();
|
---|
| 104 |
|
---|
| 105 | if (this.scanner.tokenType !== RIGHTSQUAREBRACKET) {
|
---|
| 106 | // avoid case `[name i]`
|
---|
| 107 | if (this.scanner.tokenType !== IDENT) {
|
---|
| 108 | matcher = getOperator.call(this);
|
---|
| 109 |
|
---|
| 110 | this.scanner.skipSC();
|
---|
| 111 |
|
---|
| 112 | value = this.scanner.tokenType === STRING
|
---|
| 113 | ? this.String()
|
---|
| 114 | : this.Identifier();
|
---|
| 115 |
|
---|
| 116 | this.scanner.skipSC();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | // attribute flags
|
---|
| 120 | if (this.scanner.tokenType === IDENT) {
|
---|
| 121 | flags = this.scanner.getTokenValue();
|
---|
| 122 | this.scanner.next();
|
---|
| 123 |
|
---|
| 124 | this.scanner.skipSC();
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | this.eat(RIGHTSQUAREBRACKET);
|
---|
| 129 |
|
---|
| 130 | return {
|
---|
| 131 | type: 'AttributeSelector',
|
---|
| 132 | loc: this.getLocation(start, this.scanner.tokenStart),
|
---|
| 133 | name: name,
|
---|
| 134 | matcher: matcher,
|
---|
| 135 | value: value,
|
---|
| 136 | flags: flags
|
---|
| 137 | };
|
---|
| 138 | },
|
---|
| 139 | generate: function(node) {
|
---|
| 140 | var flagsPrefix = ' ';
|
---|
| 141 |
|
---|
| 142 | this.chunk('[');
|
---|
| 143 | this.node(node.name);
|
---|
| 144 |
|
---|
| 145 | if (node.matcher !== null) {
|
---|
| 146 | this.chunk(node.matcher);
|
---|
| 147 |
|
---|
| 148 | if (node.value !== null) {
|
---|
| 149 | this.node(node.value);
|
---|
| 150 |
|
---|
| 151 | // space between string and flags is not required
|
---|
| 152 | if (node.value.type === 'String') {
|
---|
| 153 | flagsPrefix = '';
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | if (node.flags !== null) {
|
---|
| 159 | this.chunk(flagsPrefix);
|
---|
| 160 | this.chunk(node.flags);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | this.chunk(']');
|
---|
| 164 | }
|
---|
| 165 | };
|
---|