[d565449] | 1 | var isCustomProperty = require('../../utils/names').isCustomProperty;
|
---|
| 2 | var TYPE = require('../../tokenizer').TYPE;
|
---|
| 3 | var rawMode = require('./Raw').mode;
|
---|
| 4 |
|
---|
| 5 | var IDENT = TYPE.Ident;
|
---|
| 6 | var HASH = TYPE.Hash;
|
---|
| 7 | var COLON = TYPE.Colon;
|
---|
| 8 | var SEMICOLON = TYPE.Semicolon;
|
---|
| 9 | var DELIM = TYPE.Delim;
|
---|
| 10 | var WHITESPACE = TYPE.WhiteSpace;
|
---|
| 11 | var EXCLAMATIONMARK = 0x0021; // U+0021 EXCLAMATION MARK (!)
|
---|
| 12 | var NUMBERSIGN = 0x0023; // U+0023 NUMBER SIGN (#)
|
---|
| 13 | var DOLLARSIGN = 0x0024; // U+0024 DOLLAR SIGN ($)
|
---|
| 14 | var AMPERSAND = 0x0026; // U+0026 ANPERSAND (&)
|
---|
| 15 | var ASTERISK = 0x002A; // U+002A ASTERISK (*)
|
---|
| 16 | var PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
|
---|
| 17 | var SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
|
---|
| 18 |
|
---|
| 19 | function consumeValueRaw(startToken) {
|
---|
| 20 | return this.Raw(startToken, rawMode.exclamationMarkOrSemicolon, true);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | function consumeCustomPropertyRaw(startToken) {
|
---|
| 24 | return this.Raw(startToken, rawMode.exclamationMarkOrSemicolon, false);
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | function consumeValue() {
|
---|
| 28 | var startValueToken = this.scanner.tokenIndex;
|
---|
| 29 | var value = this.Value();
|
---|
| 30 |
|
---|
| 31 | if (value.type !== 'Raw' &&
|
---|
| 32 | this.scanner.eof === false &&
|
---|
| 33 | this.scanner.tokenType !== SEMICOLON &&
|
---|
| 34 | this.scanner.isDelim(EXCLAMATIONMARK) === false &&
|
---|
| 35 | this.scanner.isBalanceEdge(startValueToken) === false) {
|
---|
| 36 | this.error();
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | return value;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | module.exports = {
|
---|
| 43 | name: 'Declaration',
|
---|
| 44 | structure: {
|
---|
| 45 | important: [Boolean, String],
|
---|
| 46 | property: String,
|
---|
| 47 | value: ['Value', 'Raw']
|
---|
| 48 | },
|
---|
| 49 | parse: function() {
|
---|
| 50 | var start = this.scanner.tokenStart;
|
---|
| 51 | var startToken = this.scanner.tokenIndex;
|
---|
| 52 | var property = readProperty.call(this);
|
---|
| 53 | var customProperty = isCustomProperty(property);
|
---|
| 54 | var parseValue = customProperty ? this.parseCustomProperty : this.parseValue;
|
---|
| 55 | var consumeRaw = customProperty ? consumeCustomPropertyRaw : consumeValueRaw;
|
---|
| 56 | var important = false;
|
---|
| 57 | var value;
|
---|
| 58 |
|
---|
| 59 | this.scanner.skipSC();
|
---|
| 60 | this.eat(COLON);
|
---|
| 61 |
|
---|
| 62 | const valueStart = this.scanner.tokenIndex;
|
---|
| 63 |
|
---|
| 64 | if (!customProperty) {
|
---|
| 65 | this.scanner.skipSC();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | if (parseValue) {
|
---|
| 69 | value = this.parseWithFallback(consumeValue, consumeRaw);
|
---|
| 70 | } else {
|
---|
| 71 | value = consumeRaw.call(this, this.scanner.tokenIndex);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if (customProperty && value.type === 'Value' && value.children.isEmpty()) {
|
---|
| 75 | for (let offset = valueStart - this.scanner.tokenIndex; offset <= 0; offset++) {
|
---|
| 76 | if (this.scanner.lookupType(offset) === WHITESPACE) {
|
---|
| 77 | value.children.appendData({
|
---|
| 78 | type: 'WhiteSpace',
|
---|
| 79 | loc: null,
|
---|
| 80 | value: ' '
|
---|
| 81 | });
|
---|
| 82 | break;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | if (this.scanner.isDelim(EXCLAMATIONMARK)) {
|
---|
| 88 | important = getImportant.call(this);
|
---|
| 89 | this.scanner.skipSC();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | // Do not include semicolon to range per spec
|
---|
| 93 | // https://drafts.csswg.org/css-syntax/#declaration-diagram
|
---|
| 94 |
|
---|
| 95 | if (this.scanner.eof === false &&
|
---|
| 96 | this.scanner.tokenType !== SEMICOLON &&
|
---|
| 97 | this.scanner.isBalanceEdge(startToken) === false) {
|
---|
| 98 | this.error();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | return {
|
---|
| 102 | type: 'Declaration',
|
---|
| 103 | loc: this.getLocation(start, this.scanner.tokenStart),
|
---|
| 104 | important: important,
|
---|
| 105 | property: property,
|
---|
| 106 | value: value
|
---|
| 107 | };
|
---|
| 108 | },
|
---|
| 109 | generate: function(node) {
|
---|
| 110 | this.chunk(node.property);
|
---|
| 111 | this.chunk(':');
|
---|
| 112 | this.node(node.value);
|
---|
| 113 |
|
---|
| 114 | if (node.important) {
|
---|
| 115 | this.chunk(node.important === true ? '!important' : '!' + node.important);
|
---|
| 116 | }
|
---|
| 117 | },
|
---|
| 118 | walkContext: 'declaration'
|
---|
| 119 | };
|
---|
| 120 |
|
---|
| 121 | function readProperty() {
|
---|
| 122 | var start = this.scanner.tokenStart;
|
---|
| 123 | var prefix = 0;
|
---|
| 124 |
|
---|
| 125 | // hacks
|
---|
| 126 | if (this.scanner.tokenType === DELIM) {
|
---|
| 127 | switch (this.scanner.source.charCodeAt(this.scanner.tokenStart)) {
|
---|
| 128 | case ASTERISK:
|
---|
| 129 | case DOLLARSIGN:
|
---|
| 130 | case PLUSSIGN:
|
---|
| 131 | case NUMBERSIGN:
|
---|
| 132 | case AMPERSAND:
|
---|
| 133 | this.scanner.next();
|
---|
| 134 | break;
|
---|
| 135 |
|
---|
| 136 | // TODO: not sure we should support this hack
|
---|
| 137 | case SOLIDUS:
|
---|
| 138 | this.scanner.next();
|
---|
| 139 | if (this.scanner.isDelim(SOLIDUS)) {
|
---|
| 140 | this.scanner.next();
|
---|
| 141 | }
|
---|
| 142 | break;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | if (prefix) {
|
---|
| 147 | this.scanner.skip(prefix);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | if (this.scanner.tokenType === HASH) {
|
---|
| 151 | this.eat(HASH);
|
---|
| 152 | } else {
|
---|
| 153 | this.eat(IDENT);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | return this.scanner.substrToCursor(start);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | // ! ws* important
|
---|
| 160 | function getImportant() {
|
---|
| 161 | this.eat(DELIM);
|
---|
| 162 | this.scanner.skipSC();
|
---|
| 163 |
|
---|
| 164 | var important = this.consume(IDENT);
|
---|
| 165 |
|
---|
| 166 | // store original value in case it differ from `important`
|
---|
| 167 | // for better original source restoring and hacks like `!ie` support
|
---|
| 168 | return important === 'important' ? true : important;
|
---|
| 169 | }
|
---|