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