@master/css 2.0.0-rc.58 → 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/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
- usages;
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.usages = {};
16
- this.native = null;
12
+ this.tokenCounts = new Map();
13
+ }
14
+ attach() {
15
+ this.css.rules.push(this);
17
16
  }
18
- insert(rule, index) {
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.css.rules.push(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
- const indexOfLayer = this.css.rules.indexOf(this);
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
- // @ts-expect-error
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 usages: Record<string, number>;
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
- usages;
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.usages = {};
14
- this.native = null;
10
+ this.tokenCounts = new Map();
11
+ }
12
+ attach() {
13
+ this.css.rules.push(this);
15
14
  }
16
- insert(rule, index) {
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.css.rules.push(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
- const indexOfLayer = this.css.rules.indexOf(this);
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
- // @ts-expect-error
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('') + '}';
@@ -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
- usages;
8
+ tokenCounts;
9
9
  constructor(css){
10
10
  this.css = css;
11
11
  this.rules = [];
12
- this.usages = {};
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
- // @ts-expect-error
61
- this.usages = {};
60
+ this.tokenCounts.clear();
62
61
  }
63
62
  }
64
63
 
@@ -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 usages: Record<string, number>;
6
+ readonly tokenCounts: Map<string, number>;
7
7
  constructor(css: MasterCSS);
8
8
  insert(rule: Rule): void;
9
9
  delete(key: string): Rule | undefined;
@@ -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
- usages;
6
+ tokenCounts;
7
7
  constructor(css){
8
8
  this.css = css;
9
9
  this.rules = [];
10
- this.usages = {};
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
- // @ts-expect-error
59
- this.usages = {};
58
+ this.tokenCounts.clear();
60
59
  }
61
60
  }
62
61
 
@@ -1,296 +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
- class SyntaxLayer extends layer {
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 feature = this.rules[i].at.media?.find(({ name })=>name === 'min-width');
98
- if (!feature) continue;
99
- if (feature.value > minWidthFeature.value) {
100
- matchEndIndex = i - 1;
101
- } else if (feature.value < minWidthFeature.value) {
102
- matchStartIndex = i + 1;
103
- break;
104
- }
105
- }
106
- }
107
- } else {
108
- /**
109
- * find 第一個所遇到同樣 feature 且值比自己大的 rule,
110
- * 並插入在該 rule 之後,讓自己優先被套用
111
- */ if (priority === -1) {
112
- matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width'));
113
- matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1);
114
- } else {
115
- 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);
116
- matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1);
117
- }
118
- if (matchStartIndex !== -1) {
119
- for(let i = matchEndIndex; i >= matchStartIndex; i--){
120
- const feature = this.rules[i].at.media?.find(({ name })=>name === 'max-width');
121
- if (!feature) continue;
122
- if (feature.value < maxWidthFeature.value) {
123
- matchEndIndex = i - 1;
124
- } else if (feature.value > maxWidthFeature.value) {
125
- matchStartIndex = i + 1;
126
- break;
127
- }
128
- }
129
- }
130
- }
131
- }
132
- } else {
133
- if (priority === -1) {
134
- matchStartIndex = mediaStartIndex;
135
- matchEndIndex = findIndex(mediaStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width') || eachSyntaxRule.priority !== -1);
136
- } else {
137
- matchStartIndex = findIndex(mediaStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width'), (eachSyntaxRule)=>eachSyntaxRule.priority !== -1);
138
- matchEndIndex = findIndex(mediaStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width'));
139
- }
140
- }
141
- }
142
- } else {
143
- if (priority === -1) {
144
- matchStartIndex = 0;
145
- matchEndIndex = findIndex(0, (eachSyntax)=>eachSyntax.atToken || eachSyntax.priority !== -1);
146
- } else {
147
- matchStartIndex = findIndex(0, (eachSyntax)=>eachSyntax.atToken, (eachSyntax)=>eachSyntax.priority !== -1);
148
- matchEndIndex = findIndex(0, (eachSyntax)=>eachSyntax.atToken);
149
- }
150
- }
151
- if (index === undefined && matchEndIndex !== undefined && matchStartIndex !== undefined) {
152
- if (matchStartIndex === -1) {
153
- index = matchEndIndex + 1;
154
- } else {
155
- if (priority === -1) {
156
- for(let i = matchStartIndex; i <= matchEndIndex; i++){
157
- const currentSyntaxRule = this.rules[i];
158
- if (currentSyntaxRule.order >= order) {
159
- index = i;
160
- break;
161
- }
162
- }
163
- } else {
164
- for(let i = matchStartIndex; i <= matchEndIndex; i++){
165
- const currentSyntaxRule = this.rules[i];
166
- if (currentSyntaxRule.priority < priority) {
167
- index = i;
168
- break;
169
- } else if (currentSyntaxRule.priority === priority) {
170
- if (currentSyntaxRule.order >= order) {
171
- index = i;
172
- break;
173
- }
174
- } else {
175
- index = i + 1;
176
- }
177
- }
178
- }
179
- if (index === undefined) {
180
- index = matchEndIndex + 1;
181
- }
182
- }
183
- }
184
- super.insert(syntaxRule, index);
185
- this.insertVariables(syntaxRule);
186
- this.insertAnimations(syntaxRule);
187
- syntaxRule.definition.insert?.call(syntaxRule);
188
- return index;
189
- }
190
- delete(key) {
191
- const syntaxRule = super.delete(key);
192
- if (!syntaxRule) return;
193
- if (syntaxRule.variableNames) {
194
- for (const eachVariableName of syntaxRule.variableNames){
195
- if (!--this.css.themeLayer.usages[eachVariableName]) {
196
- this.css.themeLayer.delete(eachVariableName);
197
- delete this.css.themeLayer.usages[eachVariableName];
198
- }
199
- }
200
- }
201
- if (syntaxRule.animationNames) {
202
- for (const eachKeyframeName of syntaxRule.animationNames){
203
- if (!--this.css.animationsNonLayer.usages[eachKeyframeName]) {
204
- this.css.animationsNonLayer.delete(eachKeyframeName);
205
- delete this.css.animationsNonLayer.usages[eachKeyframeName];
206
- }
207
- }
208
- }
209
- syntaxRule.definition.delete?.call(syntaxRule, syntaxRule.name);
210
- return syntaxRule;
211
- }
212
- insertVariables(syntaxRule) {
213
- if (syntaxRule.variableNames) {
214
- for (const eachVariableName of syntaxRule.variableNames){
215
- const variable = this.css.variables[eachVariableName];
216
- if (this.css.themeLayer.rules.find(({ name })=>name === eachVariableName)) {
217
- this.css.themeLayer.usages[eachVariableName]++;
218
- } else {
219
- const newRule = new rule.Rule(eachVariableName);
220
- const addNative = (mode, _variable)=>{
221
- let isDefaultMode = false;
222
- let preifxCssRuleText;
223
- let selectorText;
224
- let endCurlyBracketCount = 1;
225
- if (mode) {
226
- switch(this.css.config.modes?.[mode]){
227
- case 'media':
228
- selectorText = ':root';
229
- preifxCssRuleText = `@media(prefers-color-scheme:${mode}){${selectorText}`;
230
- endCurlyBracketCount++;
231
- break;
232
- case 'host':
233
- preifxCssRuleText = selectorText = `:host(.${mode})`;
234
- if (!variable.value && this.css.config.defaultMode === mode) {
235
- preifxCssRuleText += ',:host';
236
- isDefaultMode = true;
237
- }
238
- break;
239
- case 'class':
240
- preifxCssRuleText = selectorText = `.${mode}`;
241
- if (!variable.value && this.css.config.defaultMode === mode) {
242
- preifxCssRuleText += ',:root';
243
- isDefaultMode = true;
244
- }
245
- break;
246
- default:
247
- return;
248
- }
249
- } else {
250
- preifxCssRuleText = selectorText = ':root';
251
- }
252
- const cssRuleText = `${preifxCssRuleText}{--${eachVariableName}:${String(_variable.value)}${'}'.repeat(endCurlyBracketCount)}`;
253
- if (isDefaultMode) {
254
- newRule.nodes.unshift({
255
- selectorText,
256
- text: cssRuleText
257
- });
258
- } else {
259
- newRule.nodes.push({
260
- text: cssRuleText
261
- });
262
- }
263
- };
264
- if (variable.value) {
265
- addNative('', variable);
266
- }
267
- if (variable.modes) {
268
- for(const mode in variable.modes){
269
- addNative(mode, variable.modes[mode]);
270
- }
271
- }
272
- this.css.themeLayer.insert(newRule);
273
- this.css.themeLayer.usages[eachVariableName] = 1;
274
- }
275
- }
276
- }
277
- }
278
- insertAnimations(syntaxRule) {
279
- if (syntaxRule.animationNames) {
280
- for (const eachAnimationName of syntaxRule.animationNames){
281
- if (this.css.animationsNonLayer.rules.find(({ name })=>name === eachAnimationName)) {
282
- this.css.animationsNonLayer.usages[eachAnimationName]++;
283
- } else {
284
- this.css.animationsNonLayer.insert(new rule.Rule(eachAnimationName, [
285
- {
286
- text: `@keyframes ${eachAnimationName}{` + Object.entries(this.css.animations[eachAnimationName]).map(([key, variables])=>`${key}{${Object.entries(variables).map(([name, value])=>name + ':' + value).join(';')}}`).join('') + '}'
287
- }
288
- ]));
289
- this.css.animationsNonLayer.usages[eachAnimationName] = 1;
290
- }
291
- }
292
- }
293
- }
294
- }
6
+ const SyntaxLayer = withSyntaxLayer(layer);
295
7
 
296
8
  module.exports = SyntaxLayer;
@@ -1,17 +1,18 @@
1
1
  import Layer from './layer';
2
- import { SyntaxRule } from './syntax-rule';
3
- export default class SyntaxLayer extends Layer {
4
- rules: SyntaxRule[];
5
- /**
6
- * normal
7
- * normal selectors
8
- * media normal
9
- * media selectors
10
- * media width
11
- * media width selectors
12
- */
13
- insert(syntaxRule: SyntaxRule): number | undefined;
14
- delete(key: string): SyntaxRule | undefined;
15
- insertVariables(syntaxRule: SyntaxRule): void;
16
- insertAnimations(syntaxRule: SyntaxRule): void;
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;