source: trip-planner-front/node_modules/critters/src/css.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/**
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17import css from 'css';
18
19/**
20 * Parse a textual CSS Stylesheet into a Stylesheet instance.
21 * Stylesheet is a mutable ReworkCSS AST with format similar to CSSOM.
22 * @see https://github.com/reworkcss/css
23 * @private
24 * @param {String} stylesheet
25 * @returns {css.Stylesheet} ast
26 */
27export function parseStylesheet (stylesheet) {
28 return css.parse(stylesheet);
29}
30
31/**
32 * Serialize a ReworkCSS Stylesheet to a String of CSS.
33 * @private
34 * @param {css.Stylesheet} ast A Stylesheet to serialize, such as one returned from `parseStylesheet()`
35 * @param {Object} options Options to pass to `css.stringify()`
36 * @param {Boolean} [options.compress] Compress CSS output (removes comments, whitespace, etc)
37 */
38export function serializeStylesheet (ast, options) {
39 return css.stringify(ast, options);
40}
41
42/**
43 * Converts a walkStyleRules() iterator to mark nodes with `.$$remove=true` instead of actually removing them.
44 * This means they can be removed in a second pass, allowing the first pass to be nondestructive (eg: to preserve mirrored sheets).
45 * @private
46 * @param {Function} iterator Invoked on each node in the tree. Return `false` to remove that node.
47 * @returns {(rule) => void} nonDestructiveIterator
48 */
49export function markOnly (predicate) {
50 return rule => {
51 const sel = rule.selectors;
52 if (predicate(rule) === false) {
53 rule.$$remove = true;
54 }
55 rule.$$markedSelectors = rule.selectors;
56 if (rule._other) {
57 rule._other.$$markedSelectors = rule._other.selectors;
58 }
59 rule.selectors = sel;
60 };
61}
62
63/**
64 * Apply filtered selectors to a rule from a previous markOnly run.
65 * @private
66 * @param {css.Rule} rule The Rule to apply marked selectors to (if they exist).
67*/
68export function applyMarkedSelectors (rule) {
69 if (rule.$$markedSelectors) {
70 rule.selectors = rule.$$markedSelectors;
71 }
72 if (rule._other) {
73 applyMarkedSelectors(rule._other);
74 }
75}
76
77/**
78 * Recursively walk all rules in a stylesheet.
79 * @private
80 * @param {css.Rule} node A Stylesheet or Rule to descend into.
81 * @param {Function} iterator Invoked on each node in the tree. Return `false` to remove that node.
82 */
83export function walkStyleRules (node, iterator) {
84 if (node.stylesheet) return walkStyleRules(node.stylesheet, iterator);
85
86 node.rules = node.rules.filter(rule => {
87 if (rule.rules) {
88 walkStyleRules(rule, iterator);
89 }
90 rule._other = undefined;
91 rule.filterSelectors = filterSelectors;
92 return iterator(rule) !== false;
93 });
94}
95
96/**
97 * Recursively walk all rules in two identical stylesheets, filtering nodes into one or the other based on a predicate.
98 * @private
99 * @param {css.Rule} node A Stylesheet or Rule to descend into.
100 * @param {css.Rule} node2 A second tree identical to `node`
101 * @param {Function} iterator Invoked on each node in the tree. Return `false` to remove that node from the first tree, true to remove it from the second.
102 */
103export function walkStyleRulesWithReverseMirror (node, node2, iterator) {
104 if (node2 === null) return walkStyleRules(node, iterator);
105
106 if (node.stylesheet) return walkStyleRulesWithReverseMirror(node.stylesheet, node2.stylesheet, iterator);
107
108 [node.rules, node2.rules] = splitFilter(node.rules, node2.rules, (rule, index, rules, rules2) => {
109 const rule2 = rules2[index];
110 if (rule.rules) {
111 walkStyleRulesWithReverseMirror(rule, rule2, iterator);
112 }
113 rule._other = rule2;
114 rule.filterSelectors = filterSelectors;
115 return iterator(rule) !== false;
116 });
117}
118
119// Like [].filter(), but applies the opposite filtering result to a second copy of the Array without a second pass.
120// This is just a quicker version of generating the compliment of the set returned from a filter operation.
121function splitFilter (a, b, predicate) {
122 const aOut = [];
123 const bOut = [];
124 for (let index = 0; index < a.length; index++) {
125 if (predicate(a[index], index, a, b)) {
126 aOut.push(a[index]);
127 } else {
128 bOut.push(a[index]);
129 }
130 }
131 return [aOut, bOut];
132}
133
134// can be invoked on a style rule to subset its selectors (with reverse mirroring)
135function filterSelectors (predicate) {
136 if (this._other) {
137 const [a, b] = splitFilter(this.selectors, this._other.selectors, predicate);
138 this.selectors = a;
139 this._other.selectors = b;
140 } else {
141 this.selectors = this.selectors.filter(predicate);
142 }
143}
Note: See TracBrowser for help on using the repository browser.