storybook-addon-pseudo-states 1.15.0 → 1.15.2--canary.45.07abc1b.0
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/CHANGELOG.md +12 -0
- package/dist/cjs/PseudoStateTool.js +38 -74
- package/dist/cjs/constants.js +11 -5
- package/dist/cjs/preset/manager.js +5 -6
- package/dist/cjs/preset/preview.js +1 -3
- package/dist/cjs/rewriteStyleSheet.js +41 -71
- package/dist/cjs/rewriteStyleSheet.test.js +34 -50
- package/dist/cjs/splitSelectors.js +8 -17
- package/dist/cjs/splitSelectors.test.js +4 -5
- package/dist/cjs/withPseudoState.js +67 -125
- package/dist/esm/PseudoStateTool.js +36 -59
- package/dist/esm/constants.js +9 -4
- package/dist/esm/preset/manager.js +5 -3
- package/dist/esm/preset/preview.js +1 -1
- package/dist/esm/rewriteStyleSheet.js +42 -68
- package/dist/esm/rewriteStyleSheet.test.js +34 -50
- package/dist/esm/splitSelectors.js +8 -15
- package/dist/esm/splitSelectors.test.js +4 -4
- package/dist/esm/withPseudoState.js +67 -119
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
# v1.15.1 (Wed Jun 22 2022)
|
|
2
|
+
|
|
3
|
+
#### 🐛 Bug Fix
|
|
4
|
+
|
|
5
|
+
- Update react and react-dom peer dependencies level [#35](https://github.com/chromaui/storybook-addon-pseudo-states/pull/35) ([@dsueltenfuss](https://github.com/dsueltenfuss))
|
|
6
|
+
|
|
7
|
+
#### Authors: 1
|
|
8
|
+
|
|
9
|
+
- Dave Sueltenfuss ([@dsueltenfuss](https://github.com/dsueltenfuss))
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
1
13
|
# v1.15.0 (Thu Jun 16 2022)
|
|
2
14
|
|
|
3
15
|
#### 🚀 Enhancement
|
|
@@ -1,53 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
-
|
|
5
3
|
Object.defineProperty(exports, "__esModule", {
|
|
6
4
|
value: true
|
|
7
5
|
});
|
|
8
6
|
exports.PseudoStateTool = void 0;
|
|
9
|
-
|
|
10
7
|
var _react = _interopRequireWildcard(require("react"));
|
|
11
|
-
|
|
12
8
|
var _api = require("@storybook/api");
|
|
13
|
-
|
|
14
9
|
var _components = require("@storybook/components");
|
|
15
|
-
|
|
16
10
|
var _theming = require("@storybook/theming");
|
|
17
|
-
|
|
18
11
|
var _constants = require("./constants");
|
|
19
|
-
|
|
20
|
-
function
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
27
|
-
|
|
28
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
29
|
-
|
|
30
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
31
|
-
|
|
32
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
33
|
-
|
|
34
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
35
|
-
|
|
36
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
37
|
-
|
|
38
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
39
|
-
|
|
40
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
41
|
-
|
|
42
|
-
var LinkTitle = _theming.styled.span(function (_ref) {
|
|
43
|
-
var active = _ref.active;
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
14
|
+
const LinkTitle = _theming.styled.span(_ref => {
|
|
15
|
+
let {
|
|
16
|
+
active
|
|
17
|
+
} = _ref;
|
|
44
18
|
return {
|
|
45
19
|
color: active ? _theming.color.secondary : "inherit"
|
|
46
20
|
};
|
|
47
21
|
});
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
22
|
+
const LinkIcon = (0, _theming.styled)(_components.Icons)(_ref2 => {
|
|
23
|
+
let {
|
|
24
|
+
active
|
|
25
|
+
} = _ref2;
|
|
51
26
|
return {
|
|
52
27
|
opacity: active ? 1 : 0,
|
|
53
28
|
path: {
|
|
@@ -55,47 +30,37 @@ var LinkIcon = (0, _theming.styled)(_components.Icons)(function (_ref2) {
|
|
|
55
30
|
}
|
|
56
31
|
};
|
|
57
32
|
});
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return function () {
|
|
71
|
-
return updateGlobals({
|
|
72
|
-
pseudo: _objectSpread(_objectSpread({}, pseudo), {}, _defineProperty({}, option, !isActive(option)))
|
|
73
|
-
});
|
|
74
|
-
};
|
|
75
|
-
}, [pseudo]);
|
|
33
|
+
const options = Object.keys(_constants.PSEUDO_STATES).sort();
|
|
34
|
+
const PseudoStateTool = () => {
|
|
35
|
+
const [{
|
|
36
|
+
pseudo
|
|
37
|
+
}, updateGlobals] = (0, _api.useGlobals)();
|
|
38
|
+
const isActive = (0, _react.useCallback)(option => pseudo?.[option] === true, [pseudo]);
|
|
39
|
+
const toggleOption = (0, _react.useCallback)(option => () => updateGlobals({
|
|
40
|
+
pseudo: {
|
|
41
|
+
...pseudo,
|
|
42
|
+
[option]: !isActive(option)
|
|
43
|
+
}
|
|
44
|
+
}), [pseudo]);
|
|
76
45
|
return /*#__PURE__*/_react.default.createElement(_components.WithTooltip, {
|
|
77
46
|
placement: "top",
|
|
78
47
|
trigger: "click",
|
|
79
|
-
tooltip:
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
};
|
|
96
|
-
})
|
|
97
|
-
});
|
|
98
|
-
}
|
|
48
|
+
tooltip: () => /*#__PURE__*/_react.default.createElement(_components.TooltipLinkList, {
|
|
49
|
+
links: options.map(option => ({
|
|
50
|
+
id: option,
|
|
51
|
+
title: /*#__PURE__*/_react.default.createElement(LinkTitle, {
|
|
52
|
+
active: isActive(option)
|
|
53
|
+
}, ":", _constants.PSEUDO_STATES[option]),
|
|
54
|
+
right: /*#__PURE__*/_react.default.createElement(LinkIcon, {
|
|
55
|
+
icon: "check",
|
|
56
|
+
width: 12,
|
|
57
|
+
height: 12,
|
|
58
|
+
active: isActive(option)
|
|
59
|
+
}),
|
|
60
|
+
onClick: toggleOption(option),
|
|
61
|
+
active: isActive(option)
|
|
62
|
+
}))
|
|
63
|
+
})
|
|
99
64
|
}, /*#__PURE__*/_react.default.createElement(_components.IconButton, {
|
|
100
65
|
key: "pseudo-state",
|
|
101
66
|
title: "Select CSS pseudo states",
|
|
@@ -104,5 +69,4 @@ var PseudoStateTool = function PseudoStateTool() {
|
|
|
104
69
|
icon: "button"
|
|
105
70
|
})));
|
|
106
71
|
};
|
|
107
|
-
|
|
108
72
|
exports.PseudoStateTool = PseudoStateTool;
|
package/dist/cjs/constants.js
CHANGED
|
@@ -3,14 +3,20 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.TOOL_ID = exports.PSEUDO_STATES = exports.ADDON_ID = void 0;
|
|
7
|
-
|
|
6
|
+
exports.TOOL_ID = exports.PSEUDO_STATES = exports.EXCLUDED_PSEUDO_ELEMENTS = exports.ADDON_ID = void 0;
|
|
7
|
+
const ADDON_ID = "storybook/pseudo-states";
|
|
8
8
|
exports.ADDON_ID = ADDON_ID;
|
|
9
|
-
|
|
10
|
-
// @see https://www.w3.org/TR/2018/REC-selectors-3-20181106/#dynamic-pseudos
|
|
9
|
+
const TOOL_ID = `${ADDON_ID}/tool`;
|
|
11
10
|
|
|
11
|
+
// Pseudo-classes, which are not allowed to have classes applied on
|
|
12
|
+
// e.g. ::-webkit-scrollbar-thumb.pseudo-hover is not a valid selector
|
|
12
13
|
exports.TOOL_ID = TOOL_ID;
|
|
13
|
-
|
|
14
|
+
const EXCLUDED_PSEUDO_ELEMENTS = ["::-webkit-scrollbar-thumb"];
|
|
15
|
+
|
|
16
|
+
// Dynamic pseudo-classes
|
|
17
|
+
// @see https://www.w3.org/TR/2018/REC-selectors-3-20181106/#dynamic-pseudos
|
|
18
|
+
exports.EXCLUDED_PSEUDO_ELEMENTS = EXCLUDED_PSEUDO_ELEMENTS;
|
|
19
|
+
const PSEUDO_STATES = {
|
|
14
20
|
hover: "hover",
|
|
15
21
|
active: "active",
|
|
16
22
|
focusVisible: "focus-visible",
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _addons = require("@storybook/addons");
|
|
4
|
-
|
|
5
4
|
var _constants = require("../constants");
|
|
6
|
-
|
|
7
5
|
var _PseudoStateTool = require("../PseudoStateTool");
|
|
8
|
-
|
|
9
|
-
_addons.addons.register(_constants.ADDON_ID, function () {
|
|
6
|
+
_addons.addons.register(_constants.ADDON_ID, () => {
|
|
10
7
|
_addons.addons.add(_constants.TOOL_ID, {
|
|
11
8
|
type: _addons.types.TOOL,
|
|
12
9
|
title: "CSS pseudo states",
|
|
13
|
-
match:
|
|
14
|
-
|
|
10
|
+
match: _ref => {
|
|
11
|
+
let {
|
|
12
|
+
viewMode
|
|
13
|
+
} = _ref;
|
|
15
14
|
return viewMode === "story";
|
|
16
15
|
},
|
|
17
16
|
render: _PseudoStateTool.PseudoStateTool
|
|
@@ -4,8 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.decorators = void 0;
|
|
7
|
-
|
|
8
7
|
var _withPseudoState = require("../withPseudoState");
|
|
9
|
-
|
|
10
|
-
var decorators = [_withPseudoState.withPseudoState];
|
|
8
|
+
const decorators = [_withPseudoState.withPseudoState];
|
|
11
9
|
exports.decorators = decorators;
|
|
@@ -4,108 +4,78 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.rewriteStyleSheet = void 0;
|
|
7
|
-
|
|
8
7
|
var _constants = require("./constants");
|
|
9
|
-
|
|
10
8
|
var _splitSelectors = require("./splitSelectors");
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var pseudoStates = Object.values(_constants.PSEUDO_STATES);
|
|
19
|
-
var matchOne = new RegExp(":(".concat(pseudoStates.join("|"), ")"));
|
|
20
|
-
var matchAll = new RegExp(":(".concat(pseudoStates.join("|"), ")"), "g");
|
|
21
|
-
var warnings = new Set();
|
|
22
|
-
|
|
23
|
-
var warnOnce = function warnOnce(message) {
|
|
24
|
-
if (warnings.has(message)) return; // eslint-disable-next-line no-console
|
|
25
|
-
|
|
9
|
+
const pseudoStates = Object.values(_constants.PSEUDO_STATES);
|
|
10
|
+
const matchOne = new RegExp(`:(${pseudoStates.join("|")})`);
|
|
11
|
+
const matchAll = new RegExp(`:(${pseudoStates.join("|")})`, "g");
|
|
12
|
+
const warnings = new Set();
|
|
13
|
+
const warnOnce = message => {
|
|
14
|
+
if (warnings.has(message)) return;
|
|
15
|
+
// eslint-disable-next-line no-console
|
|
26
16
|
console.warn(message);
|
|
27
17
|
warnings.add(message);
|
|
28
18
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return cssText.replace(selectorText, (0, _splitSelectors.splitSelectors)(selectorText).flatMap(function (selector) {
|
|
19
|
+
const rewriteRule = (cssText, selectorText, shadowRoot) => {
|
|
20
|
+
return cssText.replace(selectorText, (0, _splitSelectors.splitSelectors)(selectorText).flatMap(selector => {
|
|
32
21
|
if (selector.includes(".pseudo-")) {
|
|
33
22
|
return [];
|
|
34
23
|
}
|
|
35
|
-
|
|
36
24
|
if (!matchOne.test(selector)) {
|
|
37
25
|
return [selector];
|
|
38
26
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var plainSelector = selector.replace(matchAll, function (_, state) {
|
|
27
|
+
const states = [];
|
|
28
|
+
const plainSelector = selector.replace(matchAll, (_, state) => {
|
|
42
29
|
states.push(state);
|
|
43
30
|
return "";
|
|
44
31
|
});
|
|
45
|
-
|
|
46
|
-
return
|
|
32
|
+
const getDoesSelectorEndsWithExcludedPseudoElement = pseudo => _constants.EXCLUDED_PSEUDO_ELEMENTS.some(element => {
|
|
33
|
+
return selector.endsWith(`${element}:${pseudo}`);
|
|
34
|
+
});
|
|
35
|
+
const classSelector = states.reduce((acc, state) => {
|
|
36
|
+
if (getDoesSelectorEndsWithExcludedPseudoElement(state)) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
return acc.replace(new RegExp(`(?<!Y):${state}`, "g"), `.pseudo-${state}`);
|
|
47
40
|
}, selector);
|
|
48
|
-
|
|
49
41
|
if (selector.startsWith(":host(") || selector.startsWith("::slotted(")) {
|
|
50
42
|
return [selector, classSelector];
|
|
51
43
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return ".pseudo-".concat(s);
|
|
55
|
-
}).join(""), ") ").concat(plainSelector) : "".concat(states.map(function (s) {
|
|
56
|
-
return ".pseudo-".concat(s);
|
|
57
|
-
}).join(""), " ").concat(plainSelector);
|
|
58
|
-
return [selector, classSelector, ancestorSelector].filter(function (selector) {
|
|
59
|
-
return !selector.includes(":not()");
|
|
60
|
-
});
|
|
44
|
+
const ancestorSelector = shadowRoot ? `:host(${states.map(s => `.pseudo-${s}`).join("")}) ${plainSelector}` : `${states.map(s => `.pseudo-${s}`).join("")} ${plainSelector}`;
|
|
45
|
+
return [selector, classSelector, ancestorSelector].filter(selector => !selector?.includes(":not()")).filter(Boolean);
|
|
61
46
|
}).join(", "));
|
|
62
|
-
};
|
|
63
|
-
// A sheet can only be rewritten once, and may carry over between stories.
|
|
64
|
-
|
|
47
|
+
};
|
|
65
48
|
|
|
66
|
-
|
|
49
|
+
// Rewrites the style sheet to add alternative selectors for any rule that targets a pseudo state.
|
|
50
|
+
// A sheet can only be rewritten once, and may carry over between stories.
|
|
51
|
+
const rewriteStyleSheet = (sheet, shadowRoot, shadowHosts) => {
|
|
67
52
|
if (sheet.__pseudoStatesRewritten) return;
|
|
68
53
|
sheet.__pseudoStatesRewritten = true;
|
|
69
|
-
|
|
70
54
|
try {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (shadowRoot) shadowHosts.add(shadowRoot.host);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
index++;
|
|
90
|
-
|
|
91
|
-
if (index > 1000) {
|
|
92
|
-
warnOnce("Reached maximum of 1000 pseudo selectors per sheet, skipping the rest.");
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
55
|
+
let index = 0;
|
|
56
|
+
for (const {
|
|
57
|
+
cssText,
|
|
58
|
+
selectorText
|
|
59
|
+
} of sheet.cssRules) {
|
|
60
|
+
if (matchOne.test(selectorText)) {
|
|
61
|
+
const newRule = rewriteRule(cssText, selectorText, shadowRoot);
|
|
62
|
+
sheet.deleteRule(index);
|
|
63
|
+
sheet.insertRule(newRule, index);
|
|
64
|
+
if (shadowRoot) shadowHosts.add(shadowRoot.host);
|
|
65
|
+
}
|
|
66
|
+
index++;
|
|
67
|
+
if (index > 1000) {
|
|
68
|
+
warnOnce("Reached maximum of 1000 pseudo selectors per sheet, skipping the rest.");
|
|
69
|
+
break;
|
|
95
70
|
}
|
|
96
|
-
} catch (err) {
|
|
97
|
-
_iterator.e(err);
|
|
98
|
-
} finally {
|
|
99
|
-
_iterator.f();
|
|
100
71
|
}
|
|
101
72
|
} catch (e) {
|
|
102
73
|
if (e.toString().includes("cssRules")) {
|
|
103
|
-
warnOnce(
|
|
74
|
+
warnOnce(`Can't access cssRules, likely due to CORS restrictions: ${sheet.href}`);
|
|
104
75
|
} else {
|
|
105
76
|
// eslint-disable-next-line no-console
|
|
106
77
|
console.error(e, sheet.href);
|
|
107
78
|
}
|
|
108
79
|
}
|
|
109
80
|
};
|
|
110
|
-
|
|
111
81
|
exports.rewriteStyleSheet = rewriteStyleSheet;
|
|
@@ -1,84 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _rewriteStyleSheet = require("./rewriteStyleSheet");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
8
|
-
|
|
9
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
10
|
-
|
|
11
|
-
var Sheet = /*#__PURE__*/function () {
|
|
12
|
-
function Sheet() {
|
|
13
|
-
_classCallCheck(this, Sheet);
|
|
14
|
-
|
|
4
|
+
class Sheet {
|
|
5
|
+
constructor() {
|
|
15
6
|
this.__pseudoStatesRewritten = false;
|
|
16
|
-
|
|
17
7
|
for (var _len = arguments.length, rules = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
18
8
|
rules[_key] = arguments[_key];
|
|
19
9
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
selectorText: cssText.slice(0, cssText.indexOf(" {"))
|
|
25
|
-
};
|
|
26
|
-
});
|
|
10
|
+
this.cssRules = rules.map(cssText => ({
|
|
11
|
+
cssText,
|
|
12
|
+
selectorText: cssText.slice(0, cssText.indexOf(" {"))
|
|
13
|
+
}));
|
|
27
14
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
}]);
|
|
40
|
-
|
|
41
|
-
return Sheet;
|
|
42
|
-
}();
|
|
43
|
-
|
|
44
|
-
describe("rewriteStyleSheet", function () {
|
|
45
|
-
it("adds alternative selector targeting the element directly", function () {
|
|
46
|
-
var sheet = new Sheet("a:hover { color: red }");
|
|
15
|
+
deleteRule(index) {
|
|
16
|
+
this.cssRules.splice(index, 1);
|
|
17
|
+
}
|
|
18
|
+
insertRule(cssText, index) {
|
|
19
|
+
this.cssRules.splice(index, 0, cssText);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
describe("rewriteStyleSheet", () => {
|
|
23
|
+
it("adds alternative selector targeting the element directly", () => {
|
|
24
|
+
const sheet = new Sheet("a:hover { color: red }");
|
|
47
25
|
(0, _rewriteStyleSheet.rewriteStyleSheet)(sheet);
|
|
48
26
|
expect(sheet.cssRules[0]).toContain("a.pseudo-hover");
|
|
49
27
|
});
|
|
50
|
-
it("adds alternative selector targeting an ancestor",
|
|
51
|
-
|
|
28
|
+
it("adds alternative selector targeting an ancestor", () => {
|
|
29
|
+
const sheet = new Sheet("a:hover { color: red }");
|
|
52
30
|
(0, _rewriteStyleSheet.rewriteStyleSheet)(sheet);
|
|
53
31
|
expect(sheet.cssRules[0]).toContain(".pseudo-hover a");
|
|
54
32
|
});
|
|
55
|
-
it("
|
|
56
|
-
|
|
33
|
+
it("does not add .pseudo-<class> to pseudo-class, which does not support classes", () => {
|
|
34
|
+
const sheet = new Sheet("::-webkit-scrollbar-thumb:hover { border-color: transparent; }");
|
|
35
|
+
(0, _rewriteStyleSheet.rewriteStyleSheet)(sheet);
|
|
36
|
+
console.log(sheet.cssRules[0]);
|
|
37
|
+
expect(sheet.cssRules[0]).not.toContain("::-webkit-scrollbar-thumb.pseudo-hover");
|
|
38
|
+
});
|
|
39
|
+
it("adds alternative selector for each pseudo selector", () => {
|
|
40
|
+
const sheet = new Sheet("a:hover, a:focus { color: red }");
|
|
57
41
|
(0, _rewriteStyleSheet.rewriteStyleSheet)(sheet);
|
|
58
42
|
expect(sheet.cssRules[0]).toContain("a.pseudo-hover");
|
|
59
43
|
expect(sheet.cssRules[0]).toContain("a.pseudo-focus");
|
|
60
44
|
expect(sheet.cssRules[0]).toContain(".pseudo-hover a");
|
|
61
45
|
expect(sheet.cssRules[0]).toContain(".pseudo-focus a");
|
|
62
46
|
});
|
|
63
|
-
it("keeps non-pseudo selectors as-is",
|
|
64
|
-
|
|
47
|
+
it("keeps non-pseudo selectors as-is", () => {
|
|
48
|
+
const sheet = new Sheet("a.class, a:hover, a:focus, a#id { color: red }");
|
|
65
49
|
(0, _rewriteStyleSheet.rewriteStyleSheet)(sheet);
|
|
66
50
|
expect(sheet.cssRules[0]).toContain("a.class");
|
|
67
51
|
expect(sheet.cssRules[0]).toContain("a#id");
|
|
68
52
|
});
|
|
69
|
-
it("supports combined pseudo selectors",
|
|
70
|
-
|
|
53
|
+
it("supports combined pseudo selectors", () => {
|
|
54
|
+
const sheet = new Sheet("a:hover:focus { color: red }");
|
|
71
55
|
(0, _rewriteStyleSheet.rewriteStyleSheet)(sheet);
|
|
72
56
|
expect(sheet.cssRules[0]).toContain("a.pseudo-hover.pseudo-focus");
|
|
73
57
|
expect(sheet.cssRules[0]).toContain(".pseudo-hover.pseudo-focus a");
|
|
74
58
|
});
|
|
75
|
-
it('supports ":host"',
|
|
76
|
-
|
|
59
|
+
it('supports ":host"', () => {
|
|
60
|
+
const sheet = new Sheet(":host(:hover) { color: red }");
|
|
77
61
|
(0, _rewriteStyleSheet.rewriteStyleSheet)(sheet);
|
|
78
62
|
expect(sheet.cssRules[0]).toEqual(":host(:hover), :host(.pseudo-hover) { color: red }");
|
|
79
63
|
});
|
|
80
|
-
it('supports ":not"',
|
|
81
|
-
|
|
64
|
+
it('supports ":not"', () => {
|
|
65
|
+
const sheet = new Sheet(":not(:hover) { color: red }");
|
|
82
66
|
(0, _rewriteStyleSheet.rewriteStyleSheet)(sheet);
|
|
83
67
|
expect(sheet.cssRules[0]).toEqual(":not(:hover), :not(.pseudo-hover) { color: red }");
|
|
84
68
|
});
|
|
@@ -4,21 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.splitSelectors = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return selector.indexOf("@") === 0;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
var splitSelectors = function splitSelectors(selectors) {
|
|
7
|
+
const isAtRule = selector => selector.indexOf("@") === 0;
|
|
8
|
+
const splitSelectors = selectors => {
|
|
13
9
|
if (isAtRule(selectors)) return [selectors];
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var char = selectors[i];
|
|
21
|
-
|
|
10
|
+
let result = [];
|
|
11
|
+
let parentheses = 0;
|
|
12
|
+
let brackets = 0;
|
|
13
|
+
let selector = "";
|
|
14
|
+
for (let i = 0, len = selectors.length; i < len; i++) {
|
|
15
|
+
const char = selectors[i];
|
|
22
16
|
if (char === "(") {
|
|
23
17
|
parentheses += 1;
|
|
24
18
|
} else if (char === ")") {
|
|
@@ -34,12 +28,9 @@ var splitSelectors = function splitSelectors(selectors) {
|
|
|
34
28
|
continue;
|
|
35
29
|
}
|
|
36
30
|
}
|
|
37
|
-
|
|
38
31
|
selector += char;
|
|
39
32
|
}
|
|
40
|
-
|
|
41
33
|
result.push(selector.trim());
|
|
42
34
|
return result;
|
|
43
35
|
};
|
|
44
|
-
|
|
45
36
|
exports.splitSelectors = splitSelectors;
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _splitSelectors = require("./splitSelectors");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
test("handles basic selectors", function () {
|
|
4
|
+
describe("splitSelectors", () => {
|
|
5
|
+
test("handles basic selectors", () => {
|
|
7
6
|
expect((0, _splitSelectors.splitSelectors)(".a")).toEqual([".a"]);
|
|
8
7
|
expect((0, _splitSelectors.splitSelectors)(".a, .b")).toEqual([".a", ".b"]);
|
|
9
8
|
});
|
|
10
|
-
test("supports ::slotted and :is",
|
|
9
|
+
test("supports ::slotted and :is", () => {
|
|
11
10
|
expect((0, _splitSelectors.splitSelectors)("::slotted(:is(button, a):active)")).toEqual(["::slotted(:is(button, a):active)"]);
|
|
12
11
|
expect((0, _splitSelectors.splitSelectors)("::slotted(:is(button, a):active), ::slotted(:is(button, a):hover)")).toEqual(["::slotted(:is(button, a):active)", "::slotted(:is(button, a):hover)"]);
|
|
13
12
|
});
|
|
14
|
-
test("supports :host",
|
|
13
|
+
test("supports :host", () => {
|
|
15
14
|
expect((0, _splitSelectors.splitSelectors)(":host([type='secondary']) ::slotted(:is(button, a)), :host([type='primary']) ::slotted(:is(button, a):active)")).toEqual([":host([type='secondary']) ::slotted(:is(button, a))", ":host([type='primary']) ::slotted(:is(button, a):active)"]);
|
|
16
15
|
expect((0, _splitSelectors.splitSelectors)(":host([outline]) ::slotted(:is(button, a):focus-within:focus-visible:not(:active))")).toEqual([":host([outline]) ::slotted(:is(button, a):focus-within:focus-visible:not(:active))"]);
|
|
17
16
|
});
|