[79a0317] | 1 | var emptyCharacter = '';
|
---|
| 2 |
|
---|
| 3 | var Breaks = require('../options/format').Breaks;
|
---|
| 4 | var Spaces = require('../options/format').Spaces;
|
---|
| 5 |
|
---|
| 6 | var Marker = require('../tokenizer/marker');
|
---|
| 7 | var Token = require('../tokenizer/token');
|
---|
| 8 |
|
---|
| 9 | function supportsAfterClosingBrace(token) {
|
---|
| 10 | return token[1][1] == 'background' || token[1][1] == 'transform' || token[1][1] == 'src';
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | function afterClosingBrace(token, valueIndex) {
|
---|
| 14 | return token[valueIndex][1][token[valueIndex][1].length - 1] == Marker.CLOSE_ROUND_BRACKET;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | function afterComma(token, valueIndex) {
|
---|
| 18 | return token[valueIndex][1] == Marker.COMMA;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | function afterSlash(token, valueIndex) {
|
---|
| 22 | return token[valueIndex][1] == Marker.FORWARD_SLASH;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | function beforeComma(token, valueIndex) {
|
---|
| 26 | return token[valueIndex + 1] && token[valueIndex + 1][1] == Marker.COMMA;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | function beforeSlash(token, valueIndex) {
|
---|
| 30 | return token[valueIndex + 1] && token[valueIndex + 1][1] == Marker.FORWARD_SLASH;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | function inFilter(token) {
|
---|
| 34 | return token[1][1] == 'filter' || token[1][1] == '-ms-filter';
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | function disallowsSpace(context, token, valueIndex) {
|
---|
| 38 | return !context.spaceAfterClosingBrace
|
---|
| 39 | && supportsAfterClosingBrace(token)
|
---|
| 40 | && afterClosingBrace(token, valueIndex)
|
---|
| 41 | || beforeSlash(token, valueIndex)
|
---|
| 42 | || afterSlash(token, valueIndex)
|
---|
| 43 | || beforeComma(token, valueIndex)
|
---|
| 44 | || afterComma(token, valueIndex);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | function rules(context, tokens) {
|
---|
| 48 | var store = context.store;
|
---|
| 49 |
|
---|
| 50 | for (var i = 0, l = tokens.length; i < l; i++) {
|
---|
| 51 | store(context, tokens[i]);
|
---|
| 52 |
|
---|
| 53 | if (i < l - 1) {
|
---|
| 54 | store(context, comma(context));
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | function body(context, tokens) {
|
---|
| 60 | var lastPropertyAt = lastPropertyIndex(tokens);
|
---|
| 61 |
|
---|
| 62 | for (var i = 0, l = tokens.length; i < l; i++) {
|
---|
| 63 | property(context, tokens, i, lastPropertyAt);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | function lastPropertyIndex(tokens) {
|
---|
| 68 | var index = tokens.length - 1;
|
---|
| 69 |
|
---|
| 70 | for (; index >= 0; index--) {
|
---|
| 71 | if (tokens[index][0] != Token.COMMENT) {
|
---|
| 72 | break;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | return index;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | function property(context, tokens, position, lastPropertyAt) {
|
---|
| 80 | var store = context.store;
|
---|
| 81 | var token = tokens[position];
|
---|
| 82 |
|
---|
| 83 | var propertyValue = token[2];
|
---|
| 84 | var isPropertyBlock = propertyValue && propertyValue[0] === Token.PROPERTY_BLOCK;
|
---|
| 85 |
|
---|
| 86 | var needsSemicolon;
|
---|
| 87 | if (context.format) {
|
---|
| 88 | if (context.format.semicolonAfterLastProperty || isPropertyBlock) {
|
---|
| 89 | needsSemicolon = true;
|
---|
| 90 | } else if (position < lastPropertyAt) {
|
---|
| 91 | needsSemicolon = true;
|
---|
| 92 | } else {
|
---|
| 93 | needsSemicolon = false;
|
---|
| 94 | }
|
---|
| 95 | } else {
|
---|
| 96 | needsSemicolon = position < lastPropertyAt || isPropertyBlock;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | var isLast = position === lastPropertyAt;
|
---|
| 100 |
|
---|
| 101 | switch (token[0]) {
|
---|
| 102 | case Token.AT_RULE:
|
---|
| 103 | store(context, token);
|
---|
| 104 | store(context, semicolon(context, Breaks.AfterProperty, false));
|
---|
| 105 | break;
|
---|
| 106 | case Token.AT_RULE_BLOCK:
|
---|
| 107 | rules(context, token[1]);
|
---|
| 108 | store(context, openBrace(context, Breaks.AfterRuleBegins, true));
|
---|
| 109 | body(context, token[2]);
|
---|
| 110 | store(context, closeBrace(context, Breaks.AfterRuleEnds, false, isLast));
|
---|
| 111 | break;
|
---|
| 112 | case Token.COMMENT:
|
---|
| 113 | store(context, token);
|
---|
| 114 | store(context, breakFor(context, Breaks.AfterComment) + context.indentWith);
|
---|
| 115 | break;
|
---|
| 116 | case Token.PROPERTY:
|
---|
| 117 | store(context, token[1]);
|
---|
| 118 | store(context, colon(context));
|
---|
| 119 | if (propertyValue) {
|
---|
| 120 | value(context, token);
|
---|
| 121 | }
|
---|
| 122 | store(
|
---|
| 123 | context,
|
---|
| 124 | needsSemicolon ? semicolon(context, Breaks.AfterProperty, isLast) : emptyCharacter
|
---|
| 125 | );
|
---|
| 126 | break;
|
---|
| 127 | case Token.RAW:
|
---|
| 128 | store(context, token);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | function value(context, token) {
|
---|
| 133 | var store = context.store;
|
---|
| 134 | var j, m;
|
---|
| 135 |
|
---|
| 136 | if (token[2][0] == Token.PROPERTY_BLOCK) {
|
---|
| 137 | store(context, openBrace(context, Breaks.AfterBlockBegins, false));
|
---|
| 138 | body(context, token[2][1]);
|
---|
| 139 | store(context, closeBrace(context, Breaks.AfterBlockEnds, false, true));
|
---|
| 140 | } else {
|
---|
| 141 | for (j = 2, m = token.length; j < m; j++) {
|
---|
| 142 | store(context, token[j]);
|
---|
| 143 |
|
---|
| 144 | if (j < m - 1 && (inFilter(token) || !disallowsSpace(context, token, j))) {
|
---|
| 145 | store(context, Marker.SPACE);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | function breakFor(context, where) {
|
---|
| 152 | return context.format ? context.format.breaks[where] : emptyCharacter;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | function allowsSpace(context, where) {
|
---|
| 156 | return context.format && context.format.spaces[where];
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | function openBrace(context, where, needsPrefixSpace) {
|
---|
| 160 | if (context.format) {
|
---|
| 161 | context.indentBy += context.format.indentBy;
|
---|
| 162 | context.indentWith = context.format.indentWith.repeat(context.indentBy);
|
---|
| 163 | return (
|
---|
| 164 | needsPrefixSpace
|
---|
| 165 | && allowsSpace(context, Spaces.BeforeBlockBegins) ? Marker.SPACE : emptyCharacter
|
---|
| 166 | ) + Marker.OPEN_CURLY_BRACKET
|
---|
| 167 | + breakFor(context, where)
|
---|
| 168 | + context.indentWith;
|
---|
| 169 | }
|
---|
| 170 | return Marker.OPEN_CURLY_BRACKET;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | function closeBrace(context, where, beforeBlockEnd, isLast) {
|
---|
| 174 | if (context.format) {
|
---|
| 175 | context.indentBy -= context.format.indentBy;
|
---|
| 176 | context.indentWith = context.format.indentWith.repeat(context.indentBy);
|
---|
| 177 | return (
|
---|
| 178 | beforeBlockEnd
|
---|
| 179 | ? breakFor(context, Breaks.BeforeBlockEnds)
|
---|
| 180 | : breakFor(context, Breaks.AfterProperty)
|
---|
| 181 | ) + context.indentWith
|
---|
| 182 | + Marker.CLOSE_CURLY_BRACKET
|
---|
| 183 | + (isLast ? emptyCharacter : breakFor(context, where) + context.indentWith);
|
---|
| 184 | }
|
---|
| 185 | return Marker.CLOSE_CURLY_BRACKET;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | function colon(context) {
|
---|
| 189 | return context.format
|
---|
| 190 | ? Marker.COLON + (allowsSpace(context, Spaces.BeforeValue) ? Marker.SPACE : emptyCharacter)
|
---|
| 191 | : Marker.COLON;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | function semicolon(context, where, isLast) {
|
---|
| 195 | return context.format
|
---|
| 196 | ? Marker.SEMICOLON + (isLast ? emptyCharacter : (breakFor(context, where) + context.indentWith))
|
---|
| 197 | : Marker.SEMICOLON;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | function comma(context) {
|
---|
| 201 | return context.format
|
---|
| 202 | ? Marker.COMMA + breakFor(context, Breaks.BetweenSelectors) + context.indentWith
|
---|
| 203 | : Marker.COMMA;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | function all(context, tokens) {
|
---|
| 207 | var store = context.store;
|
---|
| 208 | var token;
|
---|
| 209 | var isLast;
|
---|
| 210 | var i, l;
|
---|
| 211 |
|
---|
| 212 | for (i = 0, l = tokens.length; i < l; i++) {
|
---|
| 213 | token = tokens[i];
|
---|
| 214 | isLast = i == l - 1;
|
---|
| 215 |
|
---|
| 216 | switch (token[0]) {
|
---|
| 217 | case Token.AT_RULE:
|
---|
| 218 | store(context, token);
|
---|
| 219 | store(context, semicolon(context, Breaks.AfterAtRule, isLast));
|
---|
| 220 | break;
|
---|
| 221 | case Token.AT_RULE_BLOCK:
|
---|
| 222 | rules(context, token[1]);
|
---|
| 223 | store(context, openBrace(context, Breaks.AfterRuleBegins, true));
|
---|
| 224 | body(context, token[2]);
|
---|
| 225 | store(context, closeBrace(context, Breaks.AfterRuleEnds, false, isLast));
|
---|
| 226 | break;
|
---|
| 227 | case Token.NESTED_BLOCK:
|
---|
| 228 | rules(context, token[1]);
|
---|
| 229 | store(context, openBrace(context, Breaks.AfterBlockBegins, true));
|
---|
| 230 | all(context, token[2]);
|
---|
| 231 | store(context, closeBrace(context, Breaks.AfterBlockEnds, true, isLast));
|
---|
| 232 | break;
|
---|
| 233 | case Token.COMMENT:
|
---|
| 234 | store(context, token);
|
---|
| 235 | store(context, breakFor(context, Breaks.AfterComment) + context.indentWith);
|
---|
| 236 | break;
|
---|
| 237 | case Token.RAW:
|
---|
| 238 | store(context, token);
|
---|
| 239 | break;
|
---|
| 240 | case Token.RULE:
|
---|
| 241 | rules(context, token[1]);
|
---|
| 242 | store(context, openBrace(context, Breaks.AfterRuleBegins, true));
|
---|
| 243 | body(context, token[2]);
|
---|
| 244 | store(context, closeBrace(context, Breaks.AfterRuleEnds, false, isLast));
|
---|
| 245 | break;
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | module.exports = {
|
---|
| 251 | all: all,
|
---|
| 252 | body: body,
|
---|
| 253 | property: property,
|
---|
| 254 | rules: rules,
|
---|
| 255 | value: value
|
---|
| 256 | };
|
---|