Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8fe8885

Browse files
stashing state
1 parent 7495b12 commit 8fe8885

File tree

5 files changed

+175
-5
lines changed

5 files changed

+175
-5
lines changed

‎lib/optimize/graph/style-graph.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@
5959
6060
*/
6161
const chalk = require('chalk');
62-
const random = Date.now();
63-
const VALUE_SYMBOL = random + 'VALUE_SYMBOL';
64-
const PATH_SYMBOL = random + 'PATH_SYMBOL';
65-
const SPECIFICITY_SYMBOL = random + 'SPECIFITY_SYMBOL';
66-
const NAME_SYMBOL = random + 'NAME_SYMBOL';
6762

6863
const SELECTOR_SPLIT_SYMBOLS = ',';
6964
const PATH_SPLIT_SYMBOLS = ' ';

‎lib/walkers/css/inverted-tree.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const cssParseUtils = require('../utils/css-parse-utils');
2+
const cssMatchUtils = require('../utils/find-element');
3+
4+
const {
5+
extractSelectors,
6+
extractSegments,
7+
extractSymbols
8+
} = cssParseUtils;
9+
10+
const {
11+
elementMatches
12+
} = cssMatchUtils;
13+
14+
class InvertedTree {
15+
constructor() {
16+
17+
}
18+
19+
matches(element) {
20+
return elementMatches(element, this.symbols);
21+
}
22+
}

‎lib/walkers/utils/css-parse-utils.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const SELECTOR_SPLIT_SYMBOLS = ',';
2+
const PATH_SPLIT_SYMBOLS = ' ';
3+
const PATH_SPLIT_CLEANUP = /\s+/g;
4+
const SPLIT_SYMBOLS = /\./g;
5+
6+
/*
7+
Given the string form of a CSS Selector for a given set of rules,
8+
return an array of unique selector rules.
9+
10+
Example:
11+
12+
```
13+
.fixed, .sidebar > .sticky {
14+
position: fixed;
15+
}
16+
```
17+
18+
becomes
19+
20+
```
21+
['.fixed', '.sidebar > .sticky']
22+
```
23+
24+
@return Array<String> an array of CSS rules
25+
*/
26+
function extractSelectors(selectorString) {
27+
return selectorString.split(SELECTOR_SPLIT_SYMBOLS);
28+
}
29+
30+
/*
31+
Given the string form of a CSS rule, return the individual element
32+
matchers (segments).
33+
34+
Note: `>` is treated as a segment which is special cased elsewhere.
35+
36+
Example:
37+
38+
```
39+
.sidebar.foo > .sticky
40+
```
41+
42+
becomes
43+
44+
```
45+
['.sidebar.foo', '>', '.sticky']
46+
```
47+
48+
@return Array<String> an array of segments.
49+
*/
50+
function extractSegments(selector) {
51+
return selector
52+
.split('>')
53+
.join(' > ')
54+
.replace(PATH_SPLIT_CLEANUP, ' ')
55+
.split(PATH_SPLIT_SYMBOLS)
56+
.reverse();
57+
}
58+
59+
/*
60+
Given the string form of a CSS rule segment, return the individual
61+
selectors that compose it. Because of our constraints, this will
62+
always be constrained to elements, classNames, and their pseudo-variants.
63+
64+
Example:
65+
66+
```
67+
'div.sidebar.foo'
68+
```
69+
70+
Becomes:
71+
72+
```
73+
['div', '.sidebar', '.foo']
74+
```
75+
76+
@return Array<String> an array of symbols.
77+
*/
78+
function extractSymbols(segment) {
79+
return segment
80+
.replace(SPLIT_SYMBOLS, '##.')
81+
.split('##')
82+
.filter(a => a)
83+
.sort()
84+
.reverse();
85+
}
86+
87+
module.exports = {
88+
extractSelectors,
89+
extractSegments,
90+
extractSymbols
91+
};

‎lib/walkers/utils/find-element.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Determine if we should consider this element (elementSymbols)
3+
to match a css rule segment (matchSymbols)
4+
5+
We assume already sorted symbol arrays
6+
7+
@return Boolean true when the given array of symbols for the element matches the symbols in matchSymbols
8+
*/
9+
function elementMatches(elementSymbols, matchSymbols) {
10+
if (elementSymbols.length < matchSymbols.length) { return false; }
11+
12+
for (let i = 0; i < matchSymbols.length; i++) {
13+
let requiredValue = matchSymbols[i];
14+
if (elementSymbols.indexOf(requiredValue) === -1) {
15+
return false;
16+
}
17+
}
18+
19+
return true;
20+
}
21+
22+
function findClosestParent(node, key) {
23+
// console.log('finding closest', key);
24+
let match = null;
25+
while (!match && node.parent) {
26+
node = node.parent;
27+
let symbols = symbolsForNode(node);
28+
if (matchKey(key, symbols)) {
29+
match = node;
30+
}
31+
}
32+
33+
// console.log(match ? 'found a matching parent element' : 'no matching parent element found');
34+
return match;
35+
}
36+
37+
38+
module.exports = {
39+
elementMatches
40+
};

‎lib/walkers/utils/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function padStr(chars, char = '~') {
2+
let ret = '';
3+
while (chars--) {
4+
ret += char;
5+
}
6+
return ret;
7+
}
8+
9+
function symbolsForElement(node) {
10+
const symbols = [node.tag];
11+
12+
let classNames = node.classNames.slice();
13+
14+
symbols.push(...classNames);
15+
16+
return symbols;
17+
}
18+
19+
module.exports = {
20+
padStr,
21+
symbolsForElement
22+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /