@master/css 2.0.0-rc.57 → 2.0.0-rc.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/functions.cjs +1 -1
- package/dist/config/functions.mjs +1 -1
- package/dist/core.cjs +113 -113
- package/dist/core.d.ts +62 -12
- package/dist/core.mjs +113 -113
- package/dist/factories/with-syntax-layer.cjs +308 -0
- package/dist/factories/with-syntax-layer.d.ts +26 -0
- package/dist/factories/with-syntax-layer.mjs +306 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +1 -0
- package/dist/layer.cjs +12 -79
- package/dist/layer.d.ts +3 -2
- package/dist/layer.mjs +12 -79
- package/dist/non-layer.cjs +3 -4
- package/dist/non-layer.d.ts +1 -1
- package/dist/non-layer.mjs +3 -4
- package/dist/syntax-layer.cjs +2 -288
- package/dist/syntax-layer.d.ts +17 -16
- package/dist/syntax-layer.mjs +2 -288
- package/dist/syntax-rule.cjs +9 -8
- package/dist/syntax-rule.mjs +9 -8
- package/dist/utils/extend-config.cjs +32 -46
- package/dist/utils/extend-config.mjs +32 -46
- package/dist/utils/flatten-object.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { default as SyntaxRuleType } from './syntax-rule-type';
|
|
|
10
10
|
export { default as MasterCSS, default } from './core';
|
|
11
11
|
export * from './types/config';
|
|
12
12
|
export * from './types/syntax';
|
|
13
|
+
export { default as withSyntaxLayer } from './factories/with-syntax-layer';
|
|
13
14
|
export { default as extendConfig } from './utils/extend-config';
|
|
14
15
|
export { default as reorderForReadableClasses } from './utils/reorder-for-readable-classes';
|
|
15
16
|
export { default as areRuleModesEqual } from './utils/are-rule-modes-equal';
|
package/dist/index.mjs
CHANGED
|
@@ -7,6 +7,7 @@ export { default as Layer } from './layer.mjs';
|
|
|
7
7
|
export { default as SyntaxLayer } from './syntax-layer.mjs';
|
|
8
8
|
export { default as NonLayer } from './non-layer.mjs';
|
|
9
9
|
export { default as SyntaxRuleType } from './syntax-rule-type.mjs';
|
|
10
|
+
export { default as withSyntaxLayer } from './factories/with-syntax-layer.mjs';
|
|
10
11
|
export { default as extendConfig } from './utils/extend-config.mjs';
|
|
11
12
|
export { default as reorderForReadableClasses } from './utils/reorder-for-readable-classes.mjs';
|
|
12
13
|
export { default as areRuleModesEqual } from './utils/are-rule-modes-equal.mjs';
|
package/dist/layer.cjs
CHANGED
|
@@ -1,65 +1,26 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var findNativeCssRuleIndex = require('./shared/utils/find-native-css-rule-index.cjs');
|
|
4
|
-
|
|
5
3
|
class Layer {
|
|
6
4
|
name;
|
|
7
5
|
css;
|
|
8
6
|
rules;
|
|
9
|
-
|
|
10
|
-
native;
|
|
7
|
+
tokenCounts;
|
|
11
8
|
constructor(name, css){
|
|
12
9
|
this.name = name;
|
|
13
10
|
this.css = css;
|
|
14
11
|
this.rules = [];
|
|
15
|
-
this.
|
|
16
|
-
|
|
12
|
+
this.tokenCounts = new Map();
|
|
13
|
+
}
|
|
14
|
+
attach() {
|
|
15
|
+
this.css.rules.push(this);
|
|
17
16
|
}
|
|
18
|
-
|
|
17
|
+
detach() {
|
|
18
|
+
this.css.rules.splice(this.css.rules.indexOf(this), 1);
|
|
19
|
+
}
|
|
20
|
+
insert(rule, index = this.rules.length) {
|
|
19
21
|
if (this.rules.find(({ key })=>key === rule.key)) return;
|
|
20
22
|
if (!this.css.rules.includes(this)) {
|
|
21
|
-
this.
|
|
22
|
-
const nativeSheet = this.css.style?.sheet;
|
|
23
|
-
if (nativeSheet && !this.native?.parentStyleSheet) {
|
|
24
|
-
const insertedIndex = nativeSheet.insertRule(this.text, nativeSheet.cssRules.length);
|
|
25
|
-
this.native = nativeSheet.cssRules.item(insertedIndex);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
if (index === undefined) {
|
|
29
|
-
index = this.rules.length;
|
|
30
|
-
}
|
|
31
|
-
if (this.native) {
|
|
32
|
-
let cssRuleIndex = 0;
|
|
33
|
-
const lastCssRule = function getLastCssRule(layer, index) {
|
|
34
|
-
let lastCssRule;
|
|
35
|
-
const previouRule = layer.rules[index];
|
|
36
|
-
if (previouRule && 'nodes' in previouRule) {
|
|
37
|
-
if (!previouRule.nodes.length) return getLastCssRule(layer, index - 1);
|
|
38
|
-
const lastNativeRule = previouRule.nodes[previouRule.nodes.length - 1];
|
|
39
|
-
lastCssRule = lastNativeRule.native;
|
|
40
|
-
}
|
|
41
|
-
return lastCssRule;
|
|
42
|
-
}(this, index - 1);
|
|
43
|
-
if (lastCssRule) {
|
|
44
|
-
for(let i = 0; i < this.native.cssRules.length; i++){
|
|
45
|
-
if (this.native.cssRules[i] === lastCssRule) {
|
|
46
|
-
cssRuleIndex = i + 1;
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
for(let i = 0; i < rule.nodes.length;){
|
|
52
|
-
try {
|
|
53
|
-
const node = rule.nodes[i];
|
|
54
|
-
const insertedIndex = this.native.insertRule(node.text, cssRuleIndex);
|
|
55
|
-
node.native = this.native.cssRules.item(insertedIndex);
|
|
56
|
-
cssRuleIndex++;
|
|
57
|
-
i++;
|
|
58
|
-
} catch (error) {
|
|
59
|
-
console.error(error, rule.nodes[i]);
|
|
60
|
-
rule.nodes.splice(i, 1);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
23
|
+
this.attach();
|
|
63
24
|
}
|
|
64
25
|
this.rules.splice(index, 0, rule);
|
|
65
26
|
return index;
|
|
@@ -68,26 +29,7 @@ class Layer {
|
|
|
68
29
|
const rule = this.rules.find((rule)=>rule.key === key);
|
|
69
30
|
if (!rule) return;
|
|
70
31
|
if (this.rules.length === 1) {
|
|
71
|
-
|
|
72
|
-
this.css.rules.splice(indexOfLayer, 1);
|
|
73
|
-
const nativeSheet = this.css.style?.sheet;
|
|
74
|
-
if (nativeSheet && this.native?.parentStyleSheet) {
|
|
75
|
-
const foundIndex = findNativeCssRuleIndex(nativeSheet.cssRules, this.native);
|
|
76
|
-
if (foundIndex !== -1) {
|
|
77
|
-
nativeSheet.deleteRule(foundIndex);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (this.native?.cssRules && 'nodes' in rule) {
|
|
82
|
-
for (const node of rule.nodes){
|
|
83
|
-
if (node.native) {
|
|
84
|
-
const foundIndex = findNativeCssRuleIndex(this.native.cssRules, node.native);
|
|
85
|
-
if (foundIndex !== -1) {
|
|
86
|
-
// todo: Firefox throw "Uncaught NS_ERROR_FAILURE". Reproduce: Add '@fade|1s @fade|2s' and remove '@fade|1s @fade|2s'
|
|
87
|
-
this.native.deleteRule(foundIndex);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
32
|
+
this.detach();
|
|
91
33
|
}
|
|
92
34
|
this.rules.splice(this.rules.indexOf(rule), 1);
|
|
93
35
|
return rule;
|
|
@@ -98,16 +40,7 @@ class Layer {
|
|
|
98
40
|
if (indexOfLayer !== -1) {
|
|
99
41
|
this.css.rules.splice(indexOfLayer, 1);
|
|
100
42
|
}
|
|
101
|
-
|
|
102
|
-
this.usages = {};
|
|
103
|
-
const nativeSheet = this.css.style?.sheet;
|
|
104
|
-
if (this.native && nativeSheet) {
|
|
105
|
-
const foundIndex = findNativeCssRuleIndex(nativeSheet.cssRules, this.native);
|
|
106
|
-
if (foundIndex !== -1) {
|
|
107
|
-
nativeSheet.deleteRule(foundIndex);
|
|
108
|
-
}
|
|
109
|
-
this.native = null;
|
|
110
|
-
}
|
|
43
|
+
this.tokenCounts.clear();
|
|
111
44
|
}
|
|
112
45
|
get text() {
|
|
113
46
|
return '@layer ' + this.name + '{' + this.rules.map(({ text })=>text).join('') + '}';
|
package/dist/layer.d.ts
CHANGED
|
@@ -4,9 +4,10 @@ export default class Layer {
|
|
|
4
4
|
name: string;
|
|
5
5
|
css: MasterCSS;
|
|
6
6
|
readonly rules: Rule[];
|
|
7
|
-
readonly
|
|
8
|
-
native: CSSLayerBlockRule | null;
|
|
7
|
+
readonly tokenCounts: Map<string, number>;
|
|
9
8
|
constructor(name: string, css: MasterCSS);
|
|
9
|
+
attach(): void;
|
|
10
|
+
detach(): void;
|
|
10
11
|
insert(rule: Rule, index?: number): number | undefined;
|
|
11
12
|
delete(key: string): Rule | undefined;
|
|
12
13
|
reset(): void;
|
package/dist/layer.mjs
CHANGED
|
@@ -1,63 +1,24 @@
|
|
|
1
|
-
import findNativeCSSRuleIndex from './shared/utils/find-native-css-rule-index.mjs';
|
|
2
|
-
|
|
3
1
|
class Layer {
|
|
4
2
|
name;
|
|
5
3
|
css;
|
|
6
4
|
rules;
|
|
7
|
-
|
|
8
|
-
native;
|
|
5
|
+
tokenCounts;
|
|
9
6
|
constructor(name, css){
|
|
10
7
|
this.name = name;
|
|
11
8
|
this.css = css;
|
|
12
9
|
this.rules = [];
|
|
13
|
-
this.
|
|
14
|
-
|
|
10
|
+
this.tokenCounts = new Map();
|
|
11
|
+
}
|
|
12
|
+
attach() {
|
|
13
|
+
this.css.rules.push(this);
|
|
15
14
|
}
|
|
16
|
-
|
|
15
|
+
detach() {
|
|
16
|
+
this.css.rules.splice(this.css.rules.indexOf(this), 1);
|
|
17
|
+
}
|
|
18
|
+
insert(rule, index = this.rules.length) {
|
|
17
19
|
if (this.rules.find(({ key })=>key === rule.key)) return;
|
|
18
20
|
if (!this.css.rules.includes(this)) {
|
|
19
|
-
this.
|
|
20
|
-
const nativeSheet = this.css.style?.sheet;
|
|
21
|
-
if (nativeSheet && !this.native?.parentStyleSheet) {
|
|
22
|
-
const insertedIndex = nativeSheet.insertRule(this.text, nativeSheet.cssRules.length);
|
|
23
|
-
this.native = nativeSheet.cssRules.item(insertedIndex);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (index === undefined) {
|
|
27
|
-
index = this.rules.length;
|
|
28
|
-
}
|
|
29
|
-
if (this.native) {
|
|
30
|
-
let cssRuleIndex = 0;
|
|
31
|
-
const lastCssRule = function getLastCssRule(layer, index) {
|
|
32
|
-
let lastCssRule;
|
|
33
|
-
const previouRule = layer.rules[index];
|
|
34
|
-
if (previouRule && 'nodes' in previouRule) {
|
|
35
|
-
if (!previouRule.nodes.length) return getLastCssRule(layer, index - 1);
|
|
36
|
-
const lastNativeRule = previouRule.nodes[previouRule.nodes.length - 1];
|
|
37
|
-
lastCssRule = lastNativeRule.native;
|
|
38
|
-
}
|
|
39
|
-
return lastCssRule;
|
|
40
|
-
}(this, index - 1);
|
|
41
|
-
if (lastCssRule) {
|
|
42
|
-
for(let i = 0; i < this.native.cssRules.length; i++){
|
|
43
|
-
if (this.native.cssRules[i] === lastCssRule) {
|
|
44
|
-
cssRuleIndex = i + 1;
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
for(let i = 0; i < rule.nodes.length;){
|
|
50
|
-
try {
|
|
51
|
-
const node = rule.nodes[i];
|
|
52
|
-
const insertedIndex = this.native.insertRule(node.text, cssRuleIndex);
|
|
53
|
-
node.native = this.native.cssRules.item(insertedIndex);
|
|
54
|
-
cssRuleIndex++;
|
|
55
|
-
i++;
|
|
56
|
-
} catch (error) {
|
|
57
|
-
console.error(error, rule.nodes[i]);
|
|
58
|
-
rule.nodes.splice(i, 1);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
21
|
+
this.attach();
|
|
61
22
|
}
|
|
62
23
|
this.rules.splice(index, 0, rule);
|
|
63
24
|
return index;
|
|
@@ -66,26 +27,7 @@ class Layer {
|
|
|
66
27
|
const rule = this.rules.find((rule)=>rule.key === key);
|
|
67
28
|
if (!rule) return;
|
|
68
29
|
if (this.rules.length === 1) {
|
|
69
|
-
|
|
70
|
-
this.css.rules.splice(indexOfLayer, 1);
|
|
71
|
-
const nativeSheet = this.css.style?.sheet;
|
|
72
|
-
if (nativeSheet && this.native?.parentStyleSheet) {
|
|
73
|
-
const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
|
|
74
|
-
if (foundIndex !== -1) {
|
|
75
|
-
nativeSheet.deleteRule(foundIndex);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (this.native?.cssRules && 'nodes' in rule) {
|
|
80
|
-
for (const node of rule.nodes){
|
|
81
|
-
if (node.native) {
|
|
82
|
-
const foundIndex = findNativeCSSRuleIndex(this.native.cssRules, node.native);
|
|
83
|
-
if (foundIndex !== -1) {
|
|
84
|
-
// todo: Firefox throw "Uncaught NS_ERROR_FAILURE". Reproduce: Add '@fade|1s @fade|2s' and remove '@fade|1s @fade|2s'
|
|
85
|
-
this.native.deleteRule(foundIndex);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
30
|
+
this.detach();
|
|
89
31
|
}
|
|
90
32
|
this.rules.splice(this.rules.indexOf(rule), 1);
|
|
91
33
|
return rule;
|
|
@@ -96,16 +38,7 @@ class Layer {
|
|
|
96
38
|
if (indexOfLayer !== -1) {
|
|
97
39
|
this.css.rules.splice(indexOfLayer, 1);
|
|
98
40
|
}
|
|
99
|
-
|
|
100
|
-
this.usages = {};
|
|
101
|
-
const nativeSheet = this.css.style?.sheet;
|
|
102
|
-
if (this.native && nativeSheet) {
|
|
103
|
-
const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
|
|
104
|
-
if (foundIndex !== -1) {
|
|
105
|
-
nativeSheet.deleteRule(foundIndex);
|
|
106
|
-
}
|
|
107
|
-
this.native = null;
|
|
108
|
-
}
|
|
41
|
+
this.tokenCounts.clear();
|
|
109
42
|
}
|
|
110
43
|
get text() {
|
|
111
44
|
return '@layer ' + this.name + '{' + this.rules.map(({ text })=>text).join('') + '}';
|
package/dist/non-layer.cjs
CHANGED
|
@@ -5,11 +5,11 @@ var findNativeCssRuleIndex = require('./shared/utils/find-native-css-rule-index.
|
|
|
5
5
|
class NonLayer {
|
|
6
6
|
css;
|
|
7
7
|
rules;
|
|
8
|
-
|
|
8
|
+
tokenCounts;
|
|
9
9
|
constructor(css){
|
|
10
10
|
this.css = css;
|
|
11
11
|
this.rules = [];
|
|
12
|
-
this.
|
|
12
|
+
this.tokenCounts = new Map();
|
|
13
13
|
}
|
|
14
14
|
insert(rule) {
|
|
15
15
|
if (this.rules.includes(rule)) return;
|
|
@@ -57,8 +57,7 @@ class NonLayer {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
this.rules.length = 0;
|
|
60
|
-
|
|
61
|
-
this.usages = {};
|
|
60
|
+
this.tokenCounts.clear();
|
|
62
61
|
}
|
|
63
62
|
}
|
|
64
63
|
|
package/dist/non-layer.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Rule } from './rule';
|
|
|
3
3
|
export default class NonLayer {
|
|
4
4
|
css: MasterCSS;
|
|
5
5
|
readonly rules: Rule[];
|
|
6
|
-
readonly
|
|
6
|
+
readonly tokenCounts: Map<string, number>;
|
|
7
7
|
constructor(css: MasterCSS);
|
|
8
8
|
insert(rule: Rule): void;
|
|
9
9
|
delete(key: string): Rule | undefined;
|
package/dist/non-layer.mjs
CHANGED
|
@@ -3,11 +3,11 @@ import findNativeCSSRuleIndex from './shared/utils/find-native-css-rule-index.mj
|
|
|
3
3
|
class NonLayer {
|
|
4
4
|
css;
|
|
5
5
|
rules;
|
|
6
|
-
|
|
6
|
+
tokenCounts;
|
|
7
7
|
constructor(css){
|
|
8
8
|
this.css = css;
|
|
9
9
|
this.rules = [];
|
|
10
|
-
this.
|
|
10
|
+
this.tokenCounts = new Map();
|
|
11
11
|
}
|
|
12
12
|
insert(rule) {
|
|
13
13
|
if (this.rules.includes(rule)) return;
|
|
@@ -55,8 +55,7 @@ class NonLayer {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
this.rules.length = 0;
|
|
58
|
-
|
|
59
|
-
this.usages = {};
|
|
58
|
+
this.tokenCounts.clear();
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
61
|
|
package/dist/syntax-layer.cjs
CHANGED
|
@@ -1,294 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var withSyntaxLayer = require('./factories/with-syntax-layer.cjs');
|
|
3
4
|
var layer = require('./layer.cjs');
|
|
4
|
-
var rule = require('./rule.cjs');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
rules = [];
|
|
8
|
-
/**
|
|
9
|
-
* normal
|
|
10
|
-
* normal selectors
|
|
11
|
-
* media normal
|
|
12
|
-
* media selectors
|
|
13
|
-
* media width
|
|
14
|
-
* media width selectors
|
|
15
|
-
*/ insert(syntaxRule) {
|
|
16
|
-
if (this.rules.includes(syntaxRule)) return;
|
|
17
|
-
let index;
|
|
18
|
-
/**
|
|
19
|
-
* 必須按斷點值遞增,並透過索引插入,
|
|
20
|
-
* 以實現響應式先後套用的規則
|
|
21
|
-
* @example <1 <2 <3 ALL >=1 >=2 >=3
|
|
22
|
-
* @description
|
|
23
|
-
*/ const endIndex = this.rules.length - 1;
|
|
24
|
-
const { at, atToken, order, priority } = syntaxRule;
|
|
25
|
-
const findIndex = (startIndex, stopCheck, matchCheck)=>{
|
|
26
|
-
let i = startIndex;
|
|
27
|
-
for(; i <= endIndex; i++){
|
|
28
|
-
const eachSyntaxRule = this.rules[i];
|
|
29
|
-
if (stopCheck?.(eachSyntaxRule)) return matchCheck ? -1 : i - 1;
|
|
30
|
-
if (matchCheck?.(eachSyntaxRule)) return i;
|
|
31
|
-
}
|
|
32
|
-
return matchCheck ? -1 : i - 1;
|
|
33
|
-
};
|
|
34
|
-
let matchStartIndex;
|
|
35
|
-
let matchEndIndex;
|
|
36
|
-
if (atToken) {
|
|
37
|
-
const mediaStartIndex = this.rules.findIndex((eachSyntaxRule)=>eachSyntaxRule.at?.media);
|
|
38
|
-
if (mediaStartIndex === -1) {
|
|
39
|
-
index = endIndex + 1;
|
|
40
|
-
} else {
|
|
41
|
-
const maxWidthFeature = at.media?.find(({ name })=>name === 'max-width');
|
|
42
|
-
const minWidthFeature = at.media?.find(({ name })=>name === 'min-width');
|
|
43
|
-
if (maxWidthFeature || minWidthFeature) {
|
|
44
|
-
const mediaWidthStartIndex = this.rules.findIndex((eachSyntaxRule)=>eachSyntaxRule.at?.media?.find(({ name })=>name === 'max-width' || name === 'min-width'));
|
|
45
|
-
if (mediaWidthStartIndex === -1) {
|
|
46
|
-
index = endIndex + 1;
|
|
47
|
-
} else {
|
|
48
|
-
if (maxWidthFeature && minWidthFeature) {
|
|
49
|
-
/**
|
|
50
|
-
* 範圍越小 ( 越限定 越侷限 ) 越優先,
|
|
51
|
-
* 按照範圍 max-width - min-width 遞減排序
|
|
52
|
-
* find 第一個所遇到同樣 feature 且範圍值比自己大的 rule,
|
|
53
|
-
* 並插入在該 rule 之後,讓自己優先被套用
|
|
54
|
-
*/ if (priority === -1) {
|
|
55
|
-
matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width'));
|
|
56
|
-
matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.priority !== -1);
|
|
57
|
-
} else {
|
|
58
|
-
matchStartIndex = findIndex(mediaWidthStartIndex, undefined, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1);
|
|
59
|
-
matchEndIndex = endIndex;
|
|
60
|
-
}
|
|
61
|
-
if (matchStartIndex !== -1) {
|
|
62
|
-
const range = maxWidthFeature.value - minWidthFeature.value;
|
|
63
|
-
let i = matchEndIndex;
|
|
64
|
-
const endI = matchStartIndex;
|
|
65
|
-
matchStartIndex = matchEndIndex + 1;
|
|
66
|
-
for(; i >= endI; i--){
|
|
67
|
-
const eachSyntaxRule = this.rules[i];
|
|
68
|
-
const eachMaxWidthFeature = eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width');
|
|
69
|
-
const eachMinWidthFeature = eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width');
|
|
70
|
-
if (!eachMaxWidthFeature || !eachMinWidthFeature) {
|
|
71
|
-
break;
|
|
72
|
-
} else {
|
|
73
|
-
const eachRange = eachMaxWidthFeature.value - eachMinWidthFeature.value;
|
|
74
|
-
if (eachRange < range) {
|
|
75
|
-
matchEndIndex = i - 1;
|
|
76
|
-
} else if (eachRange === range) {
|
|
77
|
-
matchStartIndex = i;
|
|
78
|
-
} else {
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
} else if (minWidthFeature) {
|
|
85
|
-
/**
|
|
86
|
-
* find 第一個所遇到同樣 feature 且值比自己大的 rule,
|
|
87
|
-
* 並插入在該 rule 之後,讓自己優先被套用
|
|
88
|
-
*/ if (priority === -1) {
|
|
89
|
-
matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>!eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width'));
|
|
90
|
-
matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1);
|
|
91
|
-
} else {
|
|
92
|
-
matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>!eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1);
|
|
93
|
-
matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1);
|
|
94
|
-
}
|
|
95
|
-
if (matchStartIndex !== -1) {
|
|
96
|
-
for(let i = matchEndIndex; i >= matchStartIndex; i--){
|
|
97
|
-
const value = (this.rules[i].at.media?.find(({ name })=>name === 'min-width')).value;
|
|
98
|
-
if (value > minWidthFeature.value) {
|
|
99
|
-
matchEndIndex = i - 1;
|
|
100
|
-
} else if (value < minWidthFeature.value) {
|
|
101
|
-
matchStartIndex = i + 1;
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
} else {
|
|
107
|
-
/**
|
|
108
|
-
* find 第一個所遇到同樣 feature 且值比自己大的 rule,
|
|
109
|
-
* 並插入在該 rule 之後,讓自己優先被套用
|
|
110
|
-
*/ if (priority === -1) {
|
|
111
|
-
matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width'));
|
|
112
|
-
matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1);
|
|
113
|
-
} else {
|
|
114
|
-
matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.priority !== -1);
|
|
115
|
-
matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1);
|
|
116
|
-
}
|
|
117
|
-
if (matchStartIndex !== -1) {
|
|
118
|
-
for(let i = matchEndIndex; i >= matchStartIndex; i--){
|
|
119
|
-
const value = (this.rules[i].at.media?.find(({ name })=>name === 'max-width')).value;
|
|
120
|
-
if (value < maxWidthFeature.value) {
|
|
121
|
-
matchEndIndex = i - 1;
|
|
122
|
-
} else if (value > maxWidthFeature.value) {
|
|
123
|
-
matchStartIndex = i + 1;
|
|
124
|
-
break;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
} else {
|
|
131
|
-
if (priority === -1) {
|
|
132
|
-
matchStartIndex = mediaStartIndex;
|
|
133
|
-
matchEndIndex = findIndex(mediaStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width') || eachSyntaxRule.priority !== -1);
|
|
134
|
-
} else {
|
|
135
|
-
matchStartIndex = findIndex(mediaStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width'), (eachSyntaxRule)=>eachSyntaxRule.priority !== -1);
|
|
136
|
-
matchEndIndex = findIndex(mediaStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width'));
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
} else {
|
|
141
|
-
if (priority === -1) {
|
|
142
|
-
matchStartIndex = 0;
|
|
143
|
-
matchEndIndex = findIndex(0, (eachSyntax)=>eachSyntax.atToken || eachSyntax.priority !== -1);
|
|
144
|
-
} else {
|
|
145
|
-
matchStartIndex = findIndex(0, (eachSyntax)=>eachSyntax.atToken, (eachSyntax)=>eachSyntax.priority !== -1);
|
|
146
|
-
matchEndIndex = findIndex(0, (eachSyntax)=>eachSyntax.atToken);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (index === undefined && matchEndIndex !== undefined && matchStartIndex !== undefined) {
|
|
150
|
-
if (matchStartIndex === -1) {
|
|
151
|
-
index = matchEndIndex + 1;
|
|
152
|
-
} else {
|
|
153
|
-
if (priority === -1) {
|
|
154
|
-
for(let i = matchStartIndex; i <= matchEndIndex; i++){
|
|
155
|
-
const currentSyntaxRule = this.rules[i];
|
|
156
|
-
if (currentSyntaxRule.order >= order) {
|
|
157
|
-
index = i;
|
|
158
|
-
break;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
} else {
|
|
162
|
-
for(let i = matchStartIndex; i <= matchEndIndex; i++){
|
|
163
|
-
const currentSyntaxRule = this.rules[i];
|
|
164
|
-
if (currentSyntaxRule.priority < priority) {
|
|
165
|
-
index = i;
|
|
166
|
-
break;
|
|
167
|
-
} else if (currentSyntaxRule.priority === priority) {
|
|
168
|
-
if (currentSyntaxRule.order >= order) {
|
|
169
|
-
index = i;
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
} else {
|
|
173
|
-
index = i + 1;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
if (index === undefined) {
|
|
178
|
-
index = matchEndIndex + 1;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
super.insert(syntaxRule, index);
|
|
183
|
-
this.insertVariables(syntaxRule);
|
|
184
|
-
this.insertAnimations(syntaxRule);
|
|
185
|
-
syntaxRule.definition.insert?.call(syntaxRule);
|
|
186
|
-
return index;
|
|
187
|
-
}
|
|
188
|
-
delete(key) {
|
|
189
|
-
const syntaxRule = super.delete(key);
|
|
190
|
-
if (!syntaxRule) return;
|
|
191
|
-
if (syntaxRule.variableNames) {
|
|
192
|
-
for (const eachVariableName of syntaxRule.variableNames){
|
|
193
|
-
if (!--this.css.themeLayer.usages[eachVariableName]) {
|
|
194
|
-
this.css.themeLayer.delete(eachVariableName);
|
|
195
|
-
delete this.css.themeLayer.usages[eachVariableName];
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
if (syntaxRule.animationNames) {
|
|
200
|
-
for (const eachKeyframeName of syntaxRule.animationNames){
|
|
201
|
-
if (!--this.css.animationsNonLayer.usages[eachKeyframeName]) {
|
|
202
|
-
this.css.animationsNonLayer.delete(eachKeyframeName);
|
|
203
|
-
delete this.css.animationsNonLayer.usages[eachKeyframeName];
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
syntaxRule.definition.delete?.call(syntaxRule, syntaxRule.name);
|
|
208
|
-
return syntaxRule;
|
|
209
|
-
}
|
|
210
|
-
insertVariables(syntaxRule) {
|
|
211
|
-
if (syntaxRule.variableNames) {
|
|
212
|
-
for (const eachVariableName of syntaxRule.variableNames){
|
|
213
|
-
const variable = this.css.variables[eachVariableName];
|
|
214
|
-
if (this.css.themeLayer.rules.find(({ name })=>name === eachVariableName)) {
|
|
215
|
-
this.css.themeLayer.usages[eachVariableName]++;
|
|
216
|
-
} else {
|
|
217
|
-
const newRule = new rule.Rule(eachVariableName);
|
|
218
|
-
const addNative = (mode, _variable)=>{
|
|
219
|
-
let isDefaultMode = false;
|
|
220
|
-
let preifxCssRuleText;
|
|
221
|
-
let selectorText;
|
|
222
|
-
let endCurlyBracketCount = 1;
|
|
223
|
-
if (mode) {
|
|
224
|
-
switch(this.css.config.modes?.[mode]){
|
|
225
|
-
case 'media':
|
|
226
|
-
selectorText = ':root';
|
|
227
|
-
preifxCssRuleText = `@media(prefers-color-scheme:${mode}){${selectorText}`;
|
|
228
|
-
endCurlyBracketCount++;
|
|
229
|
-
break;
|
|
230
|
-
case 'host':
|
|
231
|
-
preifxCssRuleText = selectorText = `:host(.${mode})`;
|
|
232
|
-
if (!variable.value && this.css.config.defaultMode === mode) {
|
|
233
|
-
preifxCssRuleText += ',:host';
|
|
234
|
-
isDefaultMode = true;
|
|
235
|
-
}
|
|
236
|
-
break;
|
|
237
|
-
case 'class':
|
|
238
|
-
preifxCssRuleText = selectorText = `.${mode}`;
|
|
239
|
-
if (!variable.value && this.css.config.defaultMode === mode) {
|
|
240
|
-
preifxCssRuleText += ',:root';
|
|
241
|
-
isDefaultMode = true;
|
|
242
|
-
}
|
|
243
|
-
break;
|
|
244
|
-
default:
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
} else {
|
|
248
|
-
preifxCssRuleText = selectorText = ':root';
|
|
249
|
-
}
|
|
250
|
-
const cssRuleText = `${preifxCssRuleText}{--${eachVariableName}:${String(_variable.value)}${'}'.repeat(endCurlyBracketCount)}`;
|
|
251
|
-
if (isDefaultMode) {
|
|
252
|
-
newRule.nodes.unshift({
|
|
253
|
-
selectorText,
|
|
254
|
-
text: cssRuleText
|
|
255
|
-
});
|
|
256
|
-
} else {
|
|
257
|
-
newRule.nodes.push({
|
|
258
|
-
text: cssRuleText
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
if (variable.value) {
|
|
263
|
-
addNative('', variable);
|
|
264
|
-
}
|
|
265
|
-
if (variable.modes) {
|
|
266
|
-
for(const mode in variable.modes){
|
|
267
|
-
addNative(mode, variable.modes[mode]);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
this.css.themeLayer.insert(newRule);
|
|
271
|
-
this.css.themeLayer.usages[eachVariableName] = 1;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
insertAnimations(syntaxRule) {
|
|
277
|
-
if (syntaxRule.animationNames) {
|
|
278
|
-
for (const eachAnimationName of syntaxRule.animationNames){
|
|
279
|
-
if (this.css.animationsNonLayer.rules.find(({ name })=>name === eachAnimationName)) {
|
|
280
|
-
this.css.animationsNonLayer.usages[eachAnimationName]++;
|
|
281
|
-
} else {
|
|
282
|
-
this.css.animationsNonLayer.insert(new rule.Rule(eachAnimationName, [
|
|
283
|
-
{
|
|
284
|
-
text: `@keyframes ${eachAnimationName}{` + Object.entries(this.css.animations[eachAnimationName]).map(([key, variables])=>`${key}{${Object.entries(variables).map(([name, value])=>name + ':' + value).join(';')}}`).join('') + '}'
|
|
285
|
-
}
|
|
286
|
-
]));
|
|
287
|
-
this.css.animationsNonLayer.usages[eachAnimationName] = 1;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
}
|
|
6
|
+
const SyntaxLayer = withSyntaxLayer(layer);
|
|
293
7
|
|
|
294
8
|
module.exports = SyntaxLayer;
|
package/dist/syntax-layer.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import Layer from './layer';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
2
|
+
declare const SyntaxLayer: {
|
|
3
|
+
new (...args: any[]): {
|
|
4
|
+
rules: import("./syntax-rule").SyntaxRule[];
|
|
5
|
+
insert(syntaxRule: import("./syntax-rule").SyntaxRule): number | undefined;
|
|
6
|
+
delete(key: string): import("./syntax-rule").SyntaxRule | undefined;
|
|
7
|
+
insertVariables(syntaxRule: import("./syntax-rule").SyntaxRule): void;
|
|
8
|
+
insertAnimations(syntaxRule: import("./syntax-rule").SyntaxRule): void;
|
|
9
|
+
readonly tokenCounts: Map<string, number>;
|
|
10
|
+
name: string;
|
|
11
|
+
css: import("./core").default;
|
|
12
|
+
attach(): void;
|
|
13
|
+
detach(): void;
|
|
14
|
+
reset(): void;
|
|
15
|
+
readonly text: string;
|
|
16
|
+
};
|
|
17
|
+
} & typeof Layer;
|
|
18
|
+
export default SyntaxLayer;
|