@master/css 2.0.0-alpha.40 → 2.0.0-alpha.41
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/config/breakpoints.d.ts +12 -0
- package/config/breakpoints.js +13 -0
- package/config/breakpoints.js.map +1 -0
- package/config/colors.d.ts +24 -0
- package/config/colors.js +265 -0
- package/config/colors.js.map +1 -0
- package/config/index.d.ts +9 -9
- package/config/index.js +20 -0
- package/config/index.js.map +1 -0
- package/config/root-size.d.ts +1 -0
- package/config/root-size.js +2 -0
- package/config/root-size.js.map +1 -0
- package/config/rules.d.ts +1 -0
- package/config/rules.js +196 -0
- package/config/rules.js.map +1 -0
- package/config/selectors.d.ts +18 -0
- package/config/selectors.js +19 -0
- package/config/selectors.js.map +1 -0
- package/config/semantics.d.ts +81 -0
- package/config/semantics.js +82 -0
- package/config/semantics.js.map +1 -0
- package/config/themes.d.ts +1 -0
- package/config/themes.js +2 -0
- package/config/themes.js.map +1 -0
- package/config/values.d.ts +125 -0
- package/config/values.js +71 -0
- package/config/values.js.map +1 -0
- package/configure.js +11 -0
- package/configure.js.map +1 -0
- package/constants/css-property-keyword.js +211 -0
- package/constants/css-property-keyword.js.map +1 -0
- package/constants/priority-selectors.js +2 -0
- package/constants/priority-selectors.js.map +1 -0
- package/constants/start-symbol.js +6 -0
- package/constants/start-symbol.js.map +1 -0
- package/{css.d.ts → core.d.ts} +0 -0
- package/core.js +850 -0
- package/core.js.map +1 -0
- package/fill-color-scale.d.ts +1 -0
- package/fill-color-scale.js +60 -0
- package/fill-color-scale.js.map +1 -0
- package/index.d.ts +2 -2
- package/index.js +9 -1
- package/index.js.map +1 -1
- package/init.d.ts +1 -1
- package/init.js +13 -0
- package/init.js.map +1 -0
- package/interfaces/config.js +2 -0
- package/interfaces/config.js.map +1 -0
- package/interfaces/declaration.js +2 -0
- package/interfaces/declaration.js.map +1 -0
- package/interfaces/media-feature-rule.js +2 -0
- package/interfaces/media-feature-rule.js.map +1 -0
- package/interfaces/media.js +2 -0
- package/interfaces/media.js.map +1 -0
- package/main.js +2 -0
- package/main.js.map +1 -0
- package/package.json +3 -2
- package/polyfills/css-escape.d.ts +2 -0
- package/polyfills/css-escape.js +91 -0
- package/polyfills/css-escape.js.map +1 -0
- package/render-from-html.d.ts +1 -1
- package/render-from-html.js +19 -0
- package/render-from-html.js.map +1 -0
- package/render-into-html.d.ts +1 -1
- package/render-into-html.js +10 -0
- package/render-into-html.js.map +1 -0
- package/render.d.ts +1 -1
- package/render.js +13 -1
- package/render.js.map +1 -1
- package/rule.d.ts +1 -1
- package/rule.js +540 -0
- package/rule.js.map +1 -0
- package/rules/z-index.js +9 -0
- package/rules/z-index.js.map +1 -0
- package/utils/concat-arrays.js +8 -0
- package/utils/concat-arrays.js.map +1 -0
- package/utils/get-css-property-text.js +8 -0
- package/utils/get-css-property-text.js.map +1 -0
- package/utils/get-type.js +10 -0
- package/utils/get-type.js.map +1 -0
- package/utils/hex-to-rgb.d.ts +1 -0
- package/utils/hex-to-rgb.js +8 -0
- package/utils/hex-to-rgb.js.map +1 -0
- package/utils/is-plain-object.js +13 -0
- package/utils/is-plain-object.js.map +1 -0
- package/utils/is-symbol.js +11 -0
- package/utils/is-symbol.js.map +1 -0
- package/utils/merge.js +59 -0
- package/utils/merge.js.map +1 -0
- package/utils/normalize-css-calc-text.js +56 -0
- package/utils/normalize-css-calc-text.js.map +1 -0
- package/utils/parse-value.js +86 -0
- package/utils/parse-value.js.map +1 -0
- package/utils/rgb-to-hex.d.ts +1 -0
- package/utils/rgb-to-hex.js +4 -0
- package/utils/rgb-to-hex.js.map +1 -0
- package/constants/css-browser-prefix.d.ts +0 -3
- package/constants/direction.d.ts +0 -10
- package/master-css.min.js +0 -2
- package/rules/accent-color.d.ts +0 -7
- package/rules/align-content.d.ts +0 -6
- package/rules/align-items.d.ts +0 -6
- package/rules/align-self.d.ts +0 -6
- package/rules/animation-delay.d.ts +0 -7
- package/rules/animation-direction.d.ts +0 -6
- package/rules/animation-duration.d.ts +0 -7
- package/rules/animation-fill-mode.d.ts +0 -6
- package/rules/animation-iteration-count.d.ts +0 -7
- package/rules/animation-name.d.ts +0 -6
- package/rules/animation-play-state.d.ts +0 -6
- package/rules/animation-timing-function.d.ts +0 -6
- package/rules/animation.d.ts +0 -8
- package/rules/appearance.d.ts +0 -5
- package/rules/aspect-radio.d.ts +0 -7
- package/rules/backdrop-filter.d.ts +0 -10
- package/rules/background-attachment.d.ts +0 -6
- package/rules/background-blend-mode.d.ts +0 -5
- package/rules/background-clip.d.ts +0 -9
- package/rules/background-color.d.ts +0 -8
- package/rules/background-image.d.ts +0 -7
- package/rules/background-origin.d.ts +0 -6
- package/rules/background-position.d.ts +0 -7
- package/rules/background-repeat.d.ts +0 -6
- package/rules/background-size.d.ts +0 -6
- package/rules/background.d.ts +0 -8
- package/rules/border-collapse.d.ts +0 -6
- package/rules/border-color.d.ts +0 -11
- package/rules/border-image-outset.d.ts +0 -5
- package/rules/border-image-repeat.d.ts +0 -6
- package/rules/border-image-slice.d.ts +0 -6
- package/rules/border-image-source.d.ts +0 -6
- package/rules/border-image-width.d.ts +0 -6
- package/rules/border-image.d.ts +0 -6
- package/rules/border-radius.d.ts +0 -9
- package/rules/border-spacing.d.ts +0 -5
- package/rules/border-style.d.ts +0 -9
- package/rules/border-width.d.ts +0 -9
- package/rules/border.d.ts +0 -10
- package/rules/box-decoration-break.d.ts +0 -9
- package/rules/box-shadow.d.ts +0 -7
- package/rules/box-sizing.d.ts +0 -6
- package/rules/break-after.d.ts +0 -5
- package/rules/break-before.d.ts +0 -5
- package/rules/break-inside.d.ts +0 -5
- package/rules/caret-color.d.ts +0 -7
- package/rules/clear.d.ts +0 -5
- package/rules/clip-path.d.ts +0 -6
- package/rules/color.d.ts +0 -8
- package/rules/column-span.d.ts +0 -6
- package/rules/columns.d.ts +0 -8
- package/rules/contain.d.ts +0 -5
- package/rules/content.d.ts +0 -5
- package/rules/counter-increment.d.ts +0 -6
- package/rules/cursor.d.ts +0 -5
- package/rules/cx.d.ts +0 -6
- package/rules/cy.d.ts +0 -6
- package/rules/direction.d.ts +0 -5
- package/rules/display.d.ts +0 -6
- package/rules/fill.d.ts +0 -7
- package/rules/filter.d.ts +0 -7
- package/rules/flex-basis.d.ts +0 -5
- package/rules/flex-direction.d.ts +0 -6
- package/rules/flex-grow.d.ts +0 -6
- package/rules/flex-shrink.d.ts +0 -6
- package/rules/flex-wrap.d.ts +0 -6
- package/rules/flex.d.ts +0 -7
- package/rules/float.d.ts +0 -5
- package/rules/font-family.d.ts +0 -6
- package/rules/font-size.d.ts +0 -6
- package/rules/font-smoothing.d.ts +0 -9
- package/rules/font-style.d.ts +0 -7
- package/rules/font-variant-numeric.d.ts +0 -6
- package/rules/font-weight.d.ts +0 -7
- package/rules/font.d.ts +0 -8
- package/rules/gap.d.ts +0 -9
- package/rules/grid-area.d.ts +0 -7
- package/rules/grid-auto-columns.d.ts +0 -6
- package/rules/grid-auto-flow.d.ts +0 -6
- package/rules/grid-auto-rows.d.ts +0 -5
- package/rules/grid-column-end.d.ts +0 -7
- package/rules/grid-column-start.d.ts +0 -7
- package/rules/grid-column.d.ts +0 -9
- package/rules/grid-columns.d.ts +0 -10
- package/rules/grid-row-end.d.ts +0 -6
- package/rules/grid-row-start.d.ts +0 -6
- package/rules/grid-row.d.ts +0 -9
- package/rules/grid-rows.d.ts +0 -9
- package/rules/grid-template-areas.d.ts +0 -5
- package/rules/grid-template-columns.d.ts +0 -6
- package/rules/grid-template-rows.d.ts +0 -5
- package/rules/grid-template.d.ts +0 -6
- package/rules/grid.d.ts +0 -6
- package/rules/group.d.ts +0 -9
- package/rules/height.d.ts +0 -6
- package/rules/inset.d.ts +0 -9
- package/rules/isolation.d.ts +0 -5
- package/rules/justify-content.d.ts +0 -6
- package/rules/justify-items.d.ts +0 -6
- package/rules/justify-self.d.ts +0 -6
- package/rules/letter-spacing.d.ts +0 -7
- package/rules/line-height.d.ts +0 -7
- package/rules/lines.d.ts +0 -9
- package/rules/list-style-image.d.ts +0 -6
- package/rules/list-style-position.d.ts +0 -6
- package/rules/list-style-type.d.ts +0 -6
- package/rules/list-style.d.ts +0 -6
- package/rules/margin.d.ts +0 -9
- package/rules/mask-image.d.ts +0 -8
- package/rules/max-height.d.ts +0 -6
- package/rules/max-width.d.ts +0 -6
- package/rules/min-height.d.ts +0 -6
- package/rules/min-width.d.ts +0 -6
- package/rules/mix-blend-mode.d.ts +0 -6
- package/rules/object-fit.d.ts +0 -6
- package/rules/object-position.d.ts +0 -6
- package/rules/opacity.d.ts +0 -6
- package/rules/order.d.ts +0 -7
- package/rules/outline-color.d.ts +0 -7
- package/rules/outline-offset.d.ts +0 -5
- package/rules/outline-style.d.ts +0 -6
- package/rules/outline-width.d.ts +0 -6
- package/rules/outline.d.ts +0 -7
- package/rules/overflow.d.ts +0 -9
- package/rules/overscroll-behavior.d.ts +0 -8
- package/rules/padding.d.ts +0 -9
- package/rules/place-content.d.ts +0 -6
- package/rules/place-items.d.ts +0 -6
- package/rules/place-self.d.ts +0 -6
- package/rules/placement.d.ts +0 -8
- package/rules/pointer-events.d.ts +0 -5
- package/rules/position.d.ts +0 -5
- package/rules/quotes.d.ts +0 -5
- package/rules/resize.d.ts +0 -5
- package/rules/rx.d.ts +0 -6
- package/rules/ry.d.ts +0 -6
- package/rules/scroll-behavior.d.ts +0 -5
- package/rules/scroll-margin.d.ts +0 -9
- package/rules/scroll-padding.d.ts +0 -9
- package/rules/scroll-snap-align.d.ts +0 -6
- package/rules/scroll-snap-stop.d.ts +0 -6
- package/rules/scroll-snap-type.d.ts +0 -6
- package/rules/shape-image-threshold.d.ts +0 -6
- package/rules/shape-margin.d.ts +0 -6
- package/rules/shape-outside.d.ts +0 -6
- package/rules/spacing.d.ts +0 -9
- package/rules/stroke-dasharray.d.ts +0 -5
- package/rules/stroke-dashoffset.d.ts +0 -5
- package/rules/stroke-width.d.ts +0 -6
- package/rules/stroke.d.ts +0 -6
- package/rules/table-layout.d.ts +0 -5
- package/rules/text-align.d.ts +0 -6
- package/rules/text-decoration-color.d.ts +0 -7
- package/rules/text-decoration-line.d.ts +0 -6
- package/rules/text-decoration-style.d.ts +0 -6
- package/rules/text-decoration-thickness.d.ts +0 -7
- package/rules/text-decoration.d.ts +0 -8
- package/rules/text-fill-color.d.ts +0 -10
- package/rules/text-indent.d.ts +0 -5
- package/rules/text-orientation.d.ts +0 -6
- package/rules/text-overflow.d.ts +0 -6
- package/rules/text-rendering.d.ts +0 -6
- package/rules/text-shadow.d.ts +0 -6
- package/rules/text-size.d.ts +0 -8
- package/rules/text-stroke-color.d.ts +0 -10
- package/rules/text-stroke-width.d.ts +0 -8
- package/rules/text-stroke.d.ts +0 -8
- package/rules/text-transform.d.ts +0 -6
- package/rules/text-underline-offset.d.ts +0 -5
- package/rules/touch-action.d.ts +0 -5
- package/rules/transform-box.d.ts +0 -6
- package/rules/transform-origin.d.ts +0 -7
- package/rules/transform-style.d.ts +0 -6
- package/rules/transform.d.ts +0 -8
- package/rules/transition-delay.d.ts +0 -7
- package/rules/transition-duration.d.ts +0 -7
- package/rules/transition-property.d.ts +0 -6
- package/rules/transition-timing-function.d.ts +0 -6
- package/rules/transition.d.ts +0 -7
- package/rules/user-drag.d.ts +0 -8
- package/rules/user-select.d.ts +0 -8
- package/rules/variable.d.ts +0 -9
- package/rules/vertical-align.d.ts +0 -6
- package/rules/visibility.d.ts +0 -5
- package/rules/white-space.d.ts +0 -6
- package/rules/width.d.ts +0 -6
- package/rules/will-change.d.ts +0 -5
- package/rules/word-break.d.ts +0 -6
- package/rules/word-spacing.d.ts +0 -6
- package/rules/writing-mode.d.ts +0 -6
- package/rules/x.d.ts +0 -6
- package/rules/y.d.ts +0 -6
- package/utils/get-border-props.d.ts +0 -3
- package/utils/parse-value-unit.d.ts +0 -1
package/core.js
ADDED
|
@@ -0,0 +1,850 @@
|
|
|
1
|
+
import { defaultConfig } from './config';
|
|
2
|
+
import { init } from './init';
|
|
3
|
+
import { MasterCSSRule } from './rule';
|
|
4
|
+
const selectorSymbols = [',', '.', '#', '[', '!', '*', '>', '+', '~', ':', '@'];
|
|
5
|
+
const hasDocument = typeof document !== 'undefined';
|
|
6
|
+
let STYLE;
|
|
7
|
+
if (hasDocument) {
|
|
8
|
+
STYLE = document.createElement('style');
|
|
9
|
+
STYLE.title = 'master';
|
|
10
|
+
}
|
|
11
|
+
const MAX_WIDTH = 'max-width';
|
|
12
|
+
const MIN_WIDTH = 'min-width';
|
|
13
|
+
const ATTRIBUTES = 'attributes';
|
|
14
|
+
const isBrowser = typeof window !== 'undefined';
|
|
15
|
+
const MutationObserver = isBrowser
|
|
16
|
+
? window.MutationObserver
|
|
17
|
+
: Object;
|
|
18
|
+
export default class MasterCSS extends MutationObserver {
|
|
19
|
+
constructor(config = defaultConfig, container) {
|
|
20
|
+
var _a, _b;
|
|
21
|
+
super((mutationRecords) => {
|
|
22
|
+
console.time('css engine');
|
|
23
|
+
const correctionOfClassName = {};
|
|
24
|
+
const attributeMutationRecords = [];
|
|
25
|
+
const updatedElements = [];
|
|
26
|
+
const unchangedElements = [];
|
|
27
|
+
/**
|
|
28
|
+
* 取得所有深層後代的 class names
|
|
29
|
+
*/
|
|
30
|
+
const handleClassNameDeeply = (element, remove) => {
|
|
31
|
+
if (remove) {
|
|
32
|
+
element.classList.forEach(removeClassName);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
element.classList.forEach(addClassName);
|
|
36
|
+
}
|
|
37
|
+
const children = element.children;
|
|
38
|
+
for (let i = 0; i < children.length; i++) {
|
|
39
|
+
const eachChildren = children[i];
|
|
40
|
+
if (eachChildren.classList) {
|
|
41
|
+
updatedElements.push(eachChildren);
|
|
42
|
+
handleClassNameDeeply(eachChildren, remove);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const addClassName = (className) => {
|
|
47
|
+
if (className in correctionOfClassName) {
|
|
48
|
+
correctionOfClassName[className]++;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
correctionOfClassName[className] = 1;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const removeClassName = (className) => {
|
|
55
|
+
if (className in correctionOfClassName) {
|
|
56
|
+
correctionOfClassName[className]--;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
correctionOfClassName[className] = -1;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const handleNodes = (nodes, remove) => {
|
|
63
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
64
|
+
const eachNode = nodes[i];
|
|
65
|
+
if (eachNode.classList && !updatedElements.includes(eachNode) && !unchangedElements.includes(eachNode)) {
|
|
66
|
+
if (eachNode.isConnected !== remove) {
|
|
67
|
+
updatedElements.push(eachNode);
|
|
68
|
+
handleClassNameDeeply(eachNode, remove);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
unchangedElements.push(eachNode);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
for (let i = 0; i < mutationRecords.length; i++) {
|
|
77
|
+
const mutationRecord = mutationRecords[i];
|
|
78
|
+
const { addedNodes, removedNodes, type, target, oldValue } = mutationRecord;
|
|
79
|
+
if (type === ATTRIBUTES) {
|
|
80
|
+
/**
|
|
81
|
+
* 防止同樣的 MutationRecord 重複執行
|
|
82
|
+
* According to this history,
|
|
83
|
+
* MutationObserver was designed to work that way.
|
|
84
|
+
* Any call to setAttribute triggers a mutation,
|
|
85
|
+
* regardless of whether the value is being changed or set to the current value
|
|
86
|
+
*/
|
|
87
|
+
if (target['className'] === oldValue // prevent same class to execute
|
|
88
|
+
|| attributeMutationRecords
|
|
89
|
+
.find((eachAttributeMutationRecord) => eachAttributeMutationRecord.target === target)) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
/**
|
|
94
|
+
* 第一個匹配到的 oldValue 一定是該批變動前的原始狀態值
|
|
95
|
+
*/
|
|
96
|
+
attributeMutationRecords.push(mutationRecord);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
// 先判斷節點新增或移除
|
|
101
|
+
handleNodes(addedNodes, false);
|
|
102
|
+
handleNodes(removedNodes, true);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (!attributeMutationRecords.length && !Object.keys(correctionOfClassName).length) {
|
|
106
|
+
console.timeEnd('css engine');
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
for (const { oldValue, target } of attributeMutationRecords) {
|
|
110
|
+
/**
|
|
111
|
+
* 如果被操作的元素中包含了屬性變更的目標,
|
|
112
|
+
* 則將該目標從 existedAttributeMutationTargets 中移除,
|
|
113
|
+
* 以防止執行接下來的屬性變更處理
|
|
114
|
+
*
|
|
115
|
+
* 該批 mutationRecords 中,某個 target 同時有 attribute 及 childList 的變更,
|
|
116
|
+
* 則以 childList 節點插入及移除的 target.className 為主
|
|
117
|
+
*/
|
|
118
|
+
const updated = updatedElements.includes(target);
|
|
119
|
+
const classNames = target.classList;
|
|
120
|
+
const oldClassNames = oldValue ? oldValue.split(' ') : [];
|
|
121
|
+
if (updated) {
|
|
122
|
+
if (target.isConnected) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
for (const oldClassName of oldClassNames) {
|
|
127
|
+
if (!classNames.contains(oldClassName)) {
|
|
128
|
+
removeClassName(oldClassName);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
if (target.isConnected) {
|
|
135
|
+
classNames.forEach((className) => {
|
|
136
|
+
if (!oldClassNames.includes(className)) {
|
|
137
|
+
addClassName(className);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
for (const oldClassName of oldClassNames) {
|
|
141
|
+
if (!classNames.contains(oldClassName)) {
|
|
142
|
+
removeClassName(oldClassName);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
for (const oldClassName of oldClassNames) {
|
|
148
|
+
removeClassName(oldClassName);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
for (const className in correctionOfClassName) {
|
|
154
|
+
const correction = correctionOfClassName[className];
|
|
155
|
+
const count = (this.countOfName[className] || 0) + correction;
|
|
156
|
+
if (count === 0) {
|
|
157
|
+
// remove
|
|
158
|
+
delete this.countOfName[className];
|
|
159
|
+
/**
|
|
160
|
+
* class name 從 DOM tree 中被移除,
|
|
161
|
+
* 匹配並刪除對應的 rule
|
|
162
|
+
*/
|
|
163
|
+
this.delete(className);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
if (!(className in this.countOfName)) {
|
|
167
|
+
// add
|
|
168
|
+
/**
|
|
169
|
+
* 新 class name 被 connected 至 DOM tree,
|
|
170
|
+
* 匹配並創建對應的 Rule
|
|
171
|
+
*/
|
|
172
|
+
this.findAndInsert(className);
|
|
173
|
+
}
|
|
174
|
+
this.countOfName[className] = count;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
console.timeEnd('css engine');
|
|
178
|
+
});
|
|
179
|
+
this.config = config;
|
|
180
|
+
this.container = container;
|
|
181
|
+
this.rules = [];
|
|
182
|
+
this.ruleOfName = {};
|
|
183
|
+
this.countOfName = {};
|
|
184
|
+
this.cache();
|
|
185
|
+
if (!hasDocument) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (container) {
|
|
189
|
+
let rootStyle;
|
|
190
|
+
// @ts-ignore
|
|
191
|
+
for (let sheet of (((_a = container.shadowRoot) === null || _a === void 0 ? void 0 : _a.styleSheets) || document.styleSheets)) {
|
|
192
|
+
if (sheet.title === 'master') {
|
|
193
|
+
rootStyle = sheet.ownerNode;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (rootStyle) {
|
|
197
|
+
this.style = rootStyle;
|
|
198
|
+
for (let index = 0; index < this.style.sheet.cssRules.length; index++) {
|
|
199
|
+
const getRule = (cssRule) => {
|
|
200
|
+
if (cssRule.selectorText) {
|
|
201
|
+
const selectorTexts = cssRule.selectorText.split(', ');
|
|
202
|
+
const escapedClassNames = selectorTexts[0].split(' ');
|
|
203
|
+
for (let i = 0; i < escapedClassNames.length; i++) {
|
|
204
|
+
const eachSelectorText = escapedClassNames[i];
|
|
205
|
+
if (eachSelectorText[0] === '.') {
|
|
206
|
+
const escapedClassName = eachSelectorText.slice(1);
|
|
207
|
+
let className = '';
|
|
208
|
+
for (let j = 0; j < escapedClassName.length; j++) {
|
|
209
|
+
const char = escapedClassName[j];
|
|
210
|
+
const nextChar = escapedClassName[j + 1];
|
|
211
|
+
if (char === '\\') {
|
|
212
|
+
j++;
|
|
213
|
+
if (nextChar !== '\\') {
|
|
214
|
+
className += nextChar;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else if (selectorSymbols.includes(char)) {
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
className += char;
|
|
222
|
+
}
|
|
223
|
+
if (!(className in this.ruleOfName) && !(className in this.classes)) {
|
|
224
|
+
const currentRule = this.findAndNew(className);
|
|
225
|
+
if (currentRule)
|
|
226
|
+
return currentRule;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
else if (cssRule.cssRules) {
|
|
232
|
+
for (let index = 0; index < cssRule.cssRules.length; index++) {
|
|
233
|
+
const currentRule = getRule(cssRule.cssRules[index]);
|
|
234
|
+
if (currentRule)
|
|
235
|
+
return currentRule;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
const rule = getRule(this.style.sheet.cssRules[index]);
|
|
240
|
+
if (rule) {
|
|
241
|
+
this.rules.push(rule);
|
|
242
|
+
this.ruleOfName[rule.className] = rule;
|
|
243
|
+
for (let i = 0; i < rule.natives.length; i++) {
|
|
244
|
+
rule.natives[i].cssRule = this.style.sheet.cssRules[index + i];
|
|
245
|
+
}
|
|
246
|
+
index += rule.natives.length - 1;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
this.style = STYLE.cloneNode();
|
|
252
|
+
/** 使用 prepend 而非 append 去降低 rules 類的優先層級,無法強制排在所有 <style> 之後 */
|
|
253
|
+
(_b = this.container) === null || _b === void 0 ? void 0 : _b.prepend(this.style);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
MasterCSS.instances.push(this);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* 全部 sheet 根據目前蒐集到的所有 DOM class 重新 findAndNew
|
|
260
|
+
*/
|
|
261
|
+
static refresh(config) {
|
|
262
|
+
for (const eachInstance of this.instances) {
|
|
263
|
+
eachInstance.refresh(config);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
cache() {
|
|
267
|
+
this.semantics = [];
|
|
268
|
+
this.classes = {};
|
|
269
|
+
this.colorsThemesMap = {};
|
|
270
|
+
this.relationThemesMap = {};
|
|
271
|
+
this.relations = {};
|
|
272
|
+
this.colorNames = [];
|
|
273
|
+
this.themeNames = [''];
|
|
274
|
+
this.selectors = { single: [], multiple: [] };
|
|
275
|
+
const { semantics, classes, selectors, themes, colors } = this.config;
|
|
276
|
+
function escapeString(str) {
|
|
277
|
+
return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
278
|
+
}
|
|
279
|
+
if (semantics) {
|
|
280
|
+
for (const semanticName in semantics) {
|
|
281
|
+
this.semantics.push([new RegExp('^' + escapeString(semanticName) + '(?=!|\\*|>|\\+|~|:|\\[|@|_|\\.|$)', 'm'), semanticName]);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (selectors) {
|
|
285
|
+
for (const entry of Object.entries(selectors)) {
|
|
286
|
+
const isSingle = typeof entry[1] === 'string';
|
|
287
|
+
this.selectors[isSingle ? 'single' : 'multiple'].push([
|
|
288
|
+
new RegExp(escapeString(entry[0]) + '(?![a-z-])', isSingle ? 'g' : ''),
|
|
289
|
+
entry[1]
|
|
290
|
+
]);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const mergeClasses = (theme, classes) => {
|
|
294
|
+
if (!classes)
|
|
295
|
+
return;
|
|
296
|
+
for (const semanticName in classes) {
|
|
297
|
+
const className = classes[semanticName];
|
|
298
|
+
const classNames = Array.isArray(className)
|
|
299
|
+
? className
|
|
300
|
+
: className
|
|
301
|
+
.replace(/(?:\n(?:\s*))+/g, ' ')
|
|
302
|
+
.trim()
|
|
303
|
+
.split(' ');
|
|
304
|
+
if (!(semanticName in this.classes)) {
|
|
305
|
+
this.classes[semanticName] = [];
|
|
306
|
+
}
|
|
307
|
+
const currentClass = this.classes[semanticName];
|
|
308
|
+
for (const eachClassName of classNames) {
|
|
309
|
+
if (eachClassName in this.relationThemesMap) {
|
|
310
|
+
if (theme in this.relationThemesMap[eachClassName]) {
|
|
311
|
+
this.relationThemesMap[eachClassName][theme].push(semanticName);
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
this.relationThemesMap[eachClassName][theme] = [semanticName];
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
this.relationThemesMap[eachClassName] = { [theme]: [semanticName] };
|
|
319
|
+
}
|
|
320
|
+
if (!currentClass.includes(eachClassName)) {
|
|
321
|
+
currentClass.push(eachClassName);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
for (const semanticName in this.relationThemesMap) {
|
|
326
|
+
this.relations[semanticName] = [];
|
|
327
|
+
for (const classNames of Object.values(this.relationThemesMap[semanticName])) {
|
|
328
|
+
for (const eachClassName of classNames) {
|
|
329
|
+
if (!this.relations[semanticName].includes(eachClassName)) {
|
|
330
|
+
this.relations[semanticName].push(eachClassName);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
const mergeColors = (theme, colors) => {
|
|
337
|
+
if (!colors)
|
|
338
|
+
return;
|
|
339
|
+
for (const colorName in colors) {
|
|
340
|
+
let levels = colors[colorName];
|
|
341
|
+
if (typeof levels === 'string') {
|
|
342
|
+
levels = { '': levels };
|
|
343
|
+
}
|
|
344
|
+
if (colorName in this.colorsThemesMap) {
|
|
345
|
+
const levelsThemes = this.colorsThemesMap[colorName];
|
|
346
|
+
for (const level in levels) {
|
|
347
|
+
const color = levels[level];
|
|
348
|
+
if (level in levelsThemes) {
|
|
349
|
+
levelsThemes[level][theme] = color;
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
levelsThemes[level] = { [theme]: color };
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
this.colorNames.push(colorName);
|
|
358
|
+
this.colorsThemesMap[colorName] = Object
|
|
359
|
+
.entries(levels)
|
|
360
|
+
.reduce((obj, [level, color]) => {
|
|
361
|
+
obj[level] = { [theme]: color };
|
|
362
|
+
return obj;
|
|
363
|
+
}, {});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
mergeClasses('', classes);
|
|
368
|
+
mergeColors('', colors);
|
|
369
|
+
if (themes) {
|
|
370
|
+
if (Array.isArray(themes)) {
|
|
371
|
+
this.themeNames.push(...themes);
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
for (const eachTheme in themes) {
|
|
375
|
+
const themeValue = themes[eachTheme];
|
|
376
|
+
mergeClasses(eachTheme, themeValue.classes);
|
|
377
|
+
mergeColors(eachTheme, themeValue.colors);
|
|
378
|
+
this.themeNames.push(eachTheme);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
observe(target, options = { subtree: true, childList: true }) {
|
|
384
|
+
if (options.subtree) {
|
|
385
|
+
/**
|
|
386
|
+
* 待所有 DOM 結構完成解析後,開始繪製 MasterCSSRule 樣式
|
|
387
|
+
*/
|
|
388
|
+
target
|
|
389
|
+
.querySelectorAll('[class]')
|
|
390
|
+
.forEach((element) => {
|
|
391
|
+
element.classList.forEach((className) => {
|
|
392
|
+
if (className in this.countOfName) {
|
|
393
|
+
this.countOfName[className]++;
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
this.countOfName[className] = 1;
|
|
397
|
+
this.findAndInsert(className);
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
super.observe(target, Object.assign(Object.assign({}, options), { attributes: true, attributeOldValue: true, attributeFilter: ['class'] }));
|
|
403
|
+
return this;
|
|
404
|
+
}
|
|
405
|
+
disconnect() {
|
|
406
|
+
super.disconnect();
|
|
407
|
+
// @ts-ignore
|
|
408
|
+
this.ruleOfName = {};
|
|
409
|
+
// @ts-ignore
|
|
410
|
+
this.countOfName = {};
|
|
411
|
+
this.rules.length = 0;
|
|
412
|
+
const sheet = this.style.sheet;
|
|
413
|
+
if (sheet) {
|
|
414
|
+
for (let i = sheet.cssRules.length - 1; i >= 0; i--) {
|
|
415
|
+
sheet.deleteRule(i);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* 尋找匹配的 MasterCSSRule 生成實例
|
|
421
|
+
*/
|
|
422
|
+
findAndNew(name) {
|
|
423
|
+
const findAndNewRule = (className) => {
|
|
424
|
+
if (className in this.ruleOfName)
|
|
425
|
+
return this.ruleOfName[className];
|
|
426
|
+
for (const EachRule of this.config.Rules) {
|
|
427
|
+
const matching = EachRule.match(className, this.colorNames);
|
|
428
|
+
if (matching)
|
|
429
|
+
return new EachRule(className, matching, this);
|
|
430
|
+
}
|
|
431
|
+
for (const eachSemanticEntry of this.semantics) {
|
|
432
|
+
if (className.match(eachSemanticEntry[0]))
|
|
433
|
+
return new MasterCSSRule(className, { origin: 'semantics', value: eachSemanticEntry[1] }, this);
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
return name in this.classes
|
|
437
|
+
? this.classes[name]
|
|
438
|
+
.map(findAndNewRule)
|
|
439
|
+
.filter(eachRule => eachRule)
|
|
440
|
+
: findAndNewRule(name);
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* 根據目前蒐集到的所有 DOM class 重新 findAndNew
|
|
444
|
+
*/
|
|
445
|
+
refresh(config) {
|
|
446
|
+
this.config = config;
|
|
447
|
+
this.cache();
|
|
448
|
+
if (!this.style) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const element = STYLE.cloneNode();
|
|
452
|
+
this.style.replaceWith(element);
|
|
453
|
+
// @ts-ignore
|
|
454
|
+
this.style = style;
|
|
455
|
+
this.rules.length = 0;
|
|
456
|
+
// @ts-ignore
|
|
457
|
+
this.ruleOfName = {};
|
|
458
|
+
/**
|
|
459
|
+
* 拿當前所有的 classNames 按照最新的 colors, breakpoints, config.Rules 匹配並生成新的 style
|
|
460
|
+
* 所以 refresh 過後 rules 可能會變多也可能會變少
|
|
461
|
+
*/
|
|
462
|
+
for (const name in this.countOfName) {
|
|
463
|
+
this.findAndInsert(name);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
destroy() {
|
|
467
|
+
const instances = MasterCSS.instances;
|
|
468
|
+
this.disconnect();
|
|
469
|
+
instances.splice(instances.indexOf(this), 1);
|
|
470
|
+
this.style.remove();
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* 1. where
|
|
474
|
+
* 2. normal
|
|
475
|
+
* 3. where selectors
|
|
476
|
+
* 4. normal selectors
|
|
477
|
+
* 5. media where
|
|
478
|
+
* 6. media normal
|
|
479
|
+
* 7. media selectors
|
|
480
|
+
* 8. media width where
|
|
481
|
+
* 9. media width
|
|
482
|
+
* 10. media width selectors
|
|
483
|
+
*/
|
|
484
|
+
insert(rule) {
|
|
485
|
+
if (this.ruleOfName[rule.className])
|
|
486
|
+
return;
|
|
487
|
+
let index;
|
|
488
|
+
/**
|
|
489
|
+
* 必須按斷點值遞增,並透過索引插入,
|
|
490
|
+
* 以實現響應式先後套用的規則
|
|
491
|
+
* @example <1 <2 <3 ALL >=1 >=2 >=3
|
|
492
|
+
* @description
|
|
493
|
+
*/
|
|
494
|
+
const endIndex = this.rules.length - 1;
|
|
495
|
+
const media = rule.media;
|
|
496
|
+
const order = rule.order;
|
|
497
|
+
const prioritySelectorIndex = rule.prioritySelectorIndex;
|
|
498
|
+
const hasWhere = rule.hasWhere;
|
|
499
|
+
const findPrioritySelectorInsertIndex = (rules, findStartIndex, findEndIndex, ignoreRule) => {
|
|
500
|
+
let targetRules;
|
|
501
|
+
let sIndex = 0;
|
|
502
|
+
let eIndex;
|
|
503
|
+
// 1. 找尋目標陣列
|
|
504
|
+
if (findStartIndex) {
|
|
505
|
+
sIndex = rules.findIndex(findStartIndex);
|
|
506
|
+
}
|
|
507
|
+
if (findEndIndex) {
|
|
508
|
+
eIndex = rules.findIndex(findEndIndex);
|
|
509
|
+
}
|
|
510
|
+
if (sIndex === -1) {
|
|
511
|
+
sIndex = rules.length;
|
|
512
|
+
}
|
|
513
|
+
if (eIndex === undefined || eIndex === -1) {
|
|
514
|
+
eIndex = rules.length;
|
|
515
|
+
}
|
|
516
|
+
targetRules = rules.slice(sIndex, eIndex);
|
|
517
|
+
// 2. 由目標陣列找尋插入點
|
|
518
|
+
for (let i = 0; i < targetRules.length; i++) {
|
|
519
|
+
const currentRule = targetRules[i];
|
|
520
|
+
if (currentRule.prioritySelectorIndex === -1 || ignoreRule && ignoreRule(currentRule))
|
|
521
|
+
continue;
|
|
522
|
+
if (currentRule.prioritySelectorIndex < prioritySelectorIndex
|
|
523
|
+
|| currentRule.prioritySelectorIndex === prioritySelectorIndex
|
|
524
|
+
&& (hasWhere && !currentRule.hasWhere
|
|
525
|
+
|| currentRule.order >= order))
|
|
526
|
+
return sIndex + i;
|
|
527
|
+
}
|
|
528
|
+
return sIndex + targetRules.length;
|
|
529
|
+
};
|
|
530
|
+
if (media) {
|
|
531
|
+
const mediaStartIndex = this.rules.findIndex(eachRule => eachRule.media);
|
|
532
|
+
if (mediaStartIndex !== -1) {
|
|
533
|
+
const maxWidthFeature = media.features[MAX_WIDTH];
|
|
534
|
+
const minWidthFeature = media.features[MIN_WIDTH];
|
|
535
|
+
if (maxWidthFeature && minWidthFeature) {
|
|
536
|
+
/**
|
|
537
|
+
* 範圍越小 ( 越限定 越侷限 ) 越優先,
|
|
538
|
+
* 按照範圍 max-width - min-width 遞減排序
|
|
539
|
+
* find 第一個所遇到同樣 feature 且範圍值比自己大的 rule,
|
|
540
|
+
* 並插入在該 rule 之後,讓自己優先被套用
|
|
541
|
+
*/
|
|
542
|
+
const range = maxWidthFeature.value - minWidthFeature.value;
|
|
543
|
+
for (let i = endIndex; i >= mediaStartIndex; i--) {
|
|
544
|
+
index = i;
|
|
545
|
+
const eachRule = this.rules[i];
|
|
546
|
+
const eachMedia = eachRule.media;
|
|
547
|
+
const eachMaxWidthFeature = eachMedia.features[MAX_WIDTH];
|
|
548
|
+
const eachMinWidthFeature = eachMedia.features[MIN_WIDTH];
|
|
549
|
+
if (!eachMaxWidthFeature || !eachMinWidthFeature) {
|
|
550
|
+
index++;
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
const eachRange = eachMaxWidthFeature.value - eachMinWidthFeature.value;
|
|
554
|
+
if (eachRange === range) {
|
|
555
|
+
if (hasWhere !== eachRule.hasWhere)
|
|
556
|
+
continue;
|
|
557
|
+
if (prioritySelectorIndex !== -1) {
|
|
558
|
+
const sameRangeRules = [this.rules[i]];
|
|
559
|
+
for (let j = i - 1; j >= mediaStartIndex; j--) {
|
|
560
|
+
const currentMediaRule = this.rules[j];
|
|
561
|
+
if (currentMediaRule.hasWhere !== hasWhere)
|
|
562
|
+
break;
|
|
563
|
+
const currentMedia = currentMediaRule.media;
|
|
564
|
+
const currentMaxWidthFeature = currentMedia.features[MAX_WIDTH];
|
|
565
|
+
const currentMinWidthFeature = currentMedia.features[MIN_WIDTH];
|
|
566
|
+
if (!currentMaxWidthFeature
|
|
567
|
+
|| !currentMinWidthFeature
|
|
568
|
+
|| (currentMaxWidthFeature.value - currentMinWidthFeature.value !== eachRange))
|
|
569
|
+
break;
|
|
570
|
+
sameRangeRules.unshift(this.rules[j]);
|
|
571
|
+
}
|
|
572
|
+
index = findPrioritySelectorInsertIndex(this.rules, eachRule => eachRule.media && eachRule.prioritySelectorIndex !== -1 && eachRule.media.features[MIN_WIDTH] && eachRule.media.features[MAX_WIDTH]);
|
|
573
|
+
}
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
else if (eachRange > range) {
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
else if (minWidthFeature) {
|
|
582
|
+
/**
|
|
583
|
+
* find 第一個所遇到同樣 feature 且值比自己大的 rule,
|
|
584
|
+
* 並插入在該 rule 之後,讓自己優先被套用
|
|
585
|
+
*/
|
|
586
|
+
for (let i = mediaStartIndex; i <= endIndex; i++) {
|
|
587
|
+
index = i;
|
|
588
|
+
const eachRule = this.rules[i];
|
|
589
|
+
const eachMedia = eachRule.media;
|
|
590
|
+
const eachMaxWidthFeature = eachMedia.features[MAX_WIDTH];
|
|
591
|
+
const eachMinWidthFeature = eachMedia.features[MIN_WIDTH];
|
|
592
|
+
if (eachMaxWidthFeature) {
|
|
593
|
+
/**
|
|
594
|
+
* 永遠插入在 range feature 前
|
|
595
|
+
*/
|
|
596
|
+
if (eachMinWidthFeature) {
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
const value = eachMinWidthFeature === null || eachMinWidthFeature === void 0 ? void 0 : eachMinWidthFeature.value;
|
|
604
|
+
if (value === minWidthFeature.value) {
|
|
605
|
+
if (!hasWhere && eachRule.hasWhere) {
|
|
606
|
+
index++;
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
if (prioritySelectorIndex !== -1) {
|
|
610
|
+
index = findPrioritySelectorInsertIndex(this.rules, eachRule => eachRule.media, eachRule => eachRule.media && eachRule.prioritySelectorIndex !== -1 && eachRule.media.features[MIN_WIDTH] && eachRule.media.features[MAX_WIDTH], eachRule => !eachRule.media.features[MIN_WIDTH] && !eachRule.media.features[MAX_WIDTH]);
|
|
611
|
+
}
|
|
612
|
+
else {
|
|
613
|
+
for (let j = i; j <= endIndex; j++) {
|
|
614
|
+
const currentMediaRule = this.rules[j];
|
|
615
|
+
const currentMedia = currentMediaRule.media;
|
|
616
|
+
const currentMinWidthFeature = currentMedia.features[MIN_WIDTH];
|
|
617
|
+
const currentMaxWidthFeature = currentMedia.features[MAX_WIDTH];
|
|
618
|
+
if (currentMaxWidthFeature)
|
|
619
|
+
continue;
|
|
620
|
+
if (currentMediaRule.hasWhere !== hasWhere
|
|
621
|
+
|| currentMinWidthFeature.value !== value
|
|
622
|
+
|| currentMediaRule.order >= order)
|
|
623
|
+
break;
|
|
624
|
+
index = j + 1;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
break;
|
|
628
|
+
}
|
|
629
|
+
else if (value > minWidthFeature.value) {
|
|
630
|
+
break;
|
|
631
|
+
}
|
|
632
|
+
else {
|
|
633
|
+
index++;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
else if (maxWidthFeature) {
|
|
638
|
+
/**
|
|
639
|
+
* find 第一個所遇到同樣 feature 且值比自己大的 rule,
|
|
640
|
+
* 並插入在該 rule 之後,讓自己優先被套用
|
|
641
|
+
*/
|
|
642
|
+
for (let i = endIndex; i >= mediaStartIndex; i--) {
|
|
643
|
+
index = i;
|
|
644
|
+
const eachRule = this.rules[i];
|
|
645
|
+
const eachMedia = eachRule.media;
|
|
646
|
+
const eachMaxWidthFeature = eachMedia.features[MAX_WIDTH];
|
|
647
|
+
const eachMinWidthFeature = eachMedia.features[MIN_WIDTH];
|
|
648
|
+
if (eachMinWidthFeature) {
|
|
649
|
+
/**
|
|
650
|
+
* 永遠插入在 range feature 前
|
|
651
|
+
*/
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
const value = eachMaxWidthFeature === null || eachMaxWidthFeature === void 0 ? void 0 : eachMaxWidthFeature.value;
|
|
655
|
+
if (!value || value > maxWidthFeature.value) {
|
|
656
|
+
index++;
|
|
657
|
+
break;
|
|
658
|
+
}
|
|
659
|
+
else if (value === maxWidthFeature.value) {
|
|
660
|
+
if (hasWhere && !eachRule.hasWhere)
|
|
661
|
+
continue;
|
|
662
|
+
if (prioritySelectorIndex !== -1) {
|
|
663
|
+
index = findPrioritySelectorInsertIndex(this.rules, eachRule => eachRule.media, eachRule => eachRule.media && eachRule.prioritySelectorIndex !== -1 && eachRule.media.features[MIN_WIDTH] && eachRule.media.features[MAX_WIDTH], eachRule => !eachRule.media.features[MIN_WIDTH] && !eachRule.media.features[MAX_WIDTH]);
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
const sameRangeRules = [this.rules[i]];
|
|
667
|
+
for (let j = i - 1; j >= mediaStartIndex; j--) {
|
|
668
|
+
const currentMediaRule = this.rules[j];
|
|
669
|
+
const currentMedia = currentMediaRule.media;
|
|
670
|
+
const currentMinWidthFeature = currentMedia.features[MIN_WIDTH];
|
|
671
|
+
const currentMaxWidthFeature = currentMedia.features[MAX_WIDTH];
|
|
672
|
+
if (!currentMinWidthFeature
|
|
673
|
+
&& (!currentMaxWidthFeature
|
|
674
|
+
|| currentMaxWidthFeature.value !== value
|
|
675
|
+
|| currentMediaRule.hasWhere !== hasWhere))
|
|
676
|
+
break;
|
|
677
|
+
sameRangeRules.unshift(currentMediaRule);
|
|
678
|
+
}
|
|
679
|
+
for (let j = 0; j < sameRangeRules.length; j++) {
|
|
680
|
+
const currentMediaRule = sameRangeRules[j];
|
|
681
|
+
if (currentMediaRule.media.features[MIN_WIDTH])
|
|
682
|
+
continue;
|
|
683
|
+
if (currentMediaRule.order >= order)
|
|
684
|
+
break;
|
|
685
|
+
index = i - sameRangeRules.length + 2 + j;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
break;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* 如果找不到目標位置則插在最後面
|
|
695
|
+
*/
|
|
696
|
+
if (index === undefined) {
|
|
697
|
+
if (mediaStartIndex === -1) {
|
|
698
|
+
// 無任何 media 時,優先級最高
|
|
699
|
+
index = endIndex + 1;
|
|
700
|
+
}
|
|
701
|
+
else if (prioritySelectorIndex !== -1) {
|
|
702
|
+
// 含有優先 selector
|
|
703
|
+
index = mediaStartIndex +
|
|
704
|
+
findPrioritySelectorInsertIndex(this.rules.slice(mediaStartIndex), undefined, eachRule => eachRule.media.features[MAX_WIDTH] || eachRule.media.features[MIN_WIDTH]);
|
|
705
|
+
}
|
|
706
|
+
else if (hasWhere) {
|
|
707
|
+
// 不含優先 selector,且含有 where,優先級最低
|
|
708
|
+
let i = mediaStartIndex;
|
|
709
|
+
for (; i < this.rules.length; i++) {
|
|
710
|
+
const eachRule = this.rules[i];
|
|
711
|
+
if (eachRule.prioritySelectorIndex !== -1 || !eachRule.hasWhere || eachRule.order >= order) {
|
|
712
|
+
index = i;
|
|
713
|
+
break;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
if (index === undefined) {
|
|
717
|
+
index = i;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
// 不含優先 selector,且不含有 where,優先級緊接非 min-width / max-width 的 where 之後
|
|
722
|
+
for (let i = mediaStartIndex; i <= endIndex; i++) {
|
|
723
|
+
index = i;
|
|
724
|
+
const eachRule = this.rules[i];
|
|
725
|
+
const eachMedia = eachRule.media;
|
|
726
|
+
if (eachRule.prioritySelectorIndex !== -1 || eachMedia.features[MAX_WIDTH] || eachMedia.features[MIN_WIDTH])
|
|
727
|
+
break;
|
|
728
|
+
if (eachRule.hasWhere) {
|
|
729
|
+
index++;
|
|
730
|
+
}
|
|
731
|
+
else if (eachRule.order >= order) {
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
if (prioritySelectorIndex === -1) {
|
|
740
|
+
// 不含優先 selector
|
|
741
|
+
if (hasWhere) {
|
|
742
|
+
// 含有 where,優先級最低
|
|
743
|
+
index = this.rules.findIndex(eachRule => !eachRule.hasWhere
|
|
744
|
+
|| eachRule.media
|
|
745
|
+
|| eachRule.prioritySelectorIndex !== -1
|
|
746
|
+
|| eachRule.order >= order);
|
|
747
|
+
if (index === -1) {
|
|
748
|
+
index = endIndex + 1;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
else {
|
|
752
|
+
// 不含 where,優先級緊接 where 之後
|
|
753
|
+
let i = 0;
|
|
754
|
+
for (; i < this.rules.length; i++) {
|
|
755
|
+
const eachRule = this.rules[i];
|
|
756
|
+
if (eachRule.media || !eachRule.hasWhere && (eachRule.order >= order || eachRule.prioritySelectorIndex !== -1)) {
|
|
757
|
+
index = i;
|
|
758
|
+
break;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
if (index === undefined) {
|
|
762
|
+
index = i;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
else {
|
|
767
|
+
// 含有優先 selector
|
|
768
|
+
index = findPrioritySelectorInsertIndex(this.rules, undefined, eachRule => eachRule.media);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
try {
|
|
772
|
+
this.rules.splice(index, 0, rule);
|
|
773
|
+
this.ruleOfName[rule.className] = rule;
|
|
774
|
+
if (this.style) {
|
|
775
|
+
const sheet = this.style.sheet;
|
|
776
|
+
let cssRuleIndex = 0;
|
|
777
|
+
const previousRule = this.rules[index - 1];
|
|
778
|
+
if (previousRule) {
|
|
779
|
+
const lastNativeCssRule = previousRule.natives[previousRule.natives.length - 1].cssRule;
|
|
780
|
+
for (let i = 0; i < sheet.cssRules.length; i++) {
|
|
781
|
+
if (sheet.cssRules[i] === lastNativeCssRule) {
|
|
782
|
+
cssRuleIndex = i + 1;
|
|
783
|
+
break;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
for (const eachNative of rule.natives) {
|
|
788
|
+
sheet.insertRule(eachNative.text, cssRuleIndex);
|
|
789
|
+
eachNative.cssRule = sheet.cssRules[cssRuleIndex++];
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
catch (error) {
|
|
794
|
+
console.error(error);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
delete(className) {
|
|
798
|
+
/**
|
|
799
|
+
* class name 從 DOM tree 中被移除,
|
|
800
|
+
* 匹配並刪除對應的 rule
|
|
801
|
+
*/
|
|
802
|
+
const sheet = this.style.sheet;
|
|
803
|
+
const deleteRule = (name) => {
|
|
804
|
+
const rule = this.ruleOfName[name];
|
|
805
|
+
if (!rule
|
|
806
|
+
|| name in this.relations && this.relations[name].some(eachClassName => eachClassName in this.countOfName))
|
|
807
|
+
return;
|
|
808
|
+
const firstNative = rule.natives[0];
|
|
809
|
+
for (let index = 0; index < sheet.cssRules.length; index++) {
|
|
810
|
+
const eachCssRule = sheet.cssRules[index];
|
|
811
|
+
if (eachCssRule === firstNative.cssRule) {
|
|
812
|
+
for (let i = 0; i < rule.natives.length; i++) {
|
|
813
|
+
sheet.deleteRule(index);
|
|
814
|
+
}
|
|
815
|
+
this.rules.splice(this.rules.indexOf(rule), 1);
|
|
816
|
+
delete this.ruleOfName[rule.className];
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
if (className in this.classes) {
|
|
822
|
+
for (const eachClassName of this.classes[className]) {
|
|
823
|
+
if (!(eachClassName in this.countOfName)) {
|
|
824
|
+
deleteRule(eachClassName);
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
else {
|
|
829
|
+
deleteRule(className);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
findAndInsert(className) {
|
|
833
|
+
const rule = this.findAndNew(className);
|
|
834
|
+
if (Array.isArray(rule)) {
|
|
835
|
+
for (const eachRule of rule) {
|
|
836
|
+
this.insert(eachRule);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
else if (rule) {
|
|
840
|
+
this.insert(rule);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
MasterCSS.init = init;
|
|
845
|
+
MasterCSS.defaultConfig = defaultConfig;
|
|
846
|
+
MasterCSS.instances = [];
|
|
847
|
+
if (isBrowser) {
|
|
848
|
+
window.MasterCSS = MasterCSS;
|
|
849
|
+
}
|
|
850
|
+
//# sourceMappingURL=core.js.map
|