source: frontend/node_modules/css-tree/lib/syntax/node/Declaration.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[9af201e]1var isCustomProperty = require('../../utils/names').isCustomProperty;
2var TYPE = require('../../tokenizer').TYPE;
3var rawMode = require('./Raw').mode;
4
5var IDENT = TYPE.Ident;
6var HASH = TYPE.Hash;
7var COLON = TYPE.Colon;
8var SEMICOLON = TYPE.Semicolon;
9var DELIM = TYPE.Delim;
10var EXCLAMATIONMARK = 0x0021; // U+0021 EXCLAMATION MARK (!)
11var NUMBERSIGN = 0x0023; // U+0023 NUMBER SIGN (#)
12var DOLLARSIGN = 0x0024; // U+0024 DOLLAR SIGN ($)
13var AMPERSAND = 0x0026; // U+0026 ANPERSAND (&)
14var ASTERISK = 0x002A; // U+002A ASTERISK (*)
15var PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
16var SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
17
18function consumeValueRaw(startToken) {
19 return this.Raw(startToken, rawMode.exclamationMarkOrSemicolon, true);
20}
21
22function consumeCustomPropertyRaw(startToken) {
23 return this.Raw(startToken, rawMode.exclamationMarkOrSemicolon, false);
24}
25
26function 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
41module.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
105function 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
144function 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}
Note: See TracBrowser for help on using the repository browser.