[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = void 0;
|
---|
| 7 |
|
---|
| 8 | var _postcss = _interopRequireDefault(require("postcss"));
|
---|
| 9 |
|
---|
| 10 | var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
|
---|
| 11 |
|
---|
| 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 13 |
|
---|
| 14 | function nodeIsInsensitiveAttribute(node) {
|
---|
| 15 | return node.type === 'attribute' && node.insensitive;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | function selectorHasInsensitiveAttribute(selector) {
|
---|
| 19 | return selector.some(nodeIsInsensitiveAttribute);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | function transformString(strings, charPos, string) {
|
---|
| 23 | var char = string.charAt(charPos);
|
---|
| 24 |
|
---|
| 25 | if (char === '') {
|
---|
| 26 | return strings;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | var newStrings = strings.map(function (x) {
|
---|
| 30 | return x + char;
|
---|
| 31 | });
|
---|
| 32 | var upperChar = char.toLocaleUpperCase();
|
---|
| 33 |
|
---|
| 34 | if (upperChar !== char) {
|
---|
| 35 | newStrings = newStrings.concat(strings.map(function (x) {
|
---|
| 36 | return x + upperChar;
|
---|
| 37 | }));
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | return transformString(newStrings, charPos + 1, string);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | function createSensitiveAtributes(attribute) {
|
---|
| 44 | var attributes = transformString([''], 0, attribute.value);
|
---|
| 45 | return attributes.map(function (x) {
|
---|
| 46 | var newAttribute = attribute.clone({
|
---|
| 47 | spaces: {
|
---|
| 48 | after: attribute.spaces.after,
|
---|
| 49 | before: attribute.spaces.before
|
---|
| 50 | },
|
---|
| 51 | insensitive: false
|
---|
| 52 | });
|
---|
| 53 | newAttribute.setValue(x);
|
---|
| 54 | return newAttribute;
|
---|
| 55 | });
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | function createNewSelectors(selector) {
|
---|
| 59 | var newSelectors = [_postcssSelectorParser.default.selector()];
|
---|
| 60 | selector.walk(function (node) {
|
---|
| 61 | if (!nodeIsInsensitiveAttribute(node)) {
|
---|
| 62 | newSelectors.forEach(function (newSelector) {
|
---|
| 63 | newSelector.append(node.clone());
|
---|
| 64 | });
|
---|
| 65 | return;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | var sensitiveAttributes = createSensitiveAtributes(node);
|
---|
| 69 | var newSelectorsWithSensitiveAttributes = [];
|
---|
| 70 | sensitiveAttributes.forEach(function (newNode) {
|
---|
| 71 | newSelectors.forEach(function (newSelector) {
|
---|
| 72 | var newSelectorWithNewNode = newSelector.clone();
|
---|
| 73 | newSelectorWithNewNode.append(newNode);
|
---|
| 74 | newSelectorsWithSensitiveAttributes.push(newSelectorWithNewNode);
|
---|
| 75 | });
|
---|
| 76 | });
|
---|
| 77 | newSelectors = newSelectorsWithSensitiveAttributes;
|
---|
| 78 | });
|
---|
| 79 | return newSelectors;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | function transform(selectors) {
|
---|
| 83 | var newSelectors = [];
|
---|
| 84 | selectors.each(function (selector) {
|
---|
| 85 | if (selectorHasInsensitiveAttribute(selector)) {
|
---|
| 86 | newSelectors = newSelectors.concat(createNewSelectors(selector));
|
---|
| 87 | selector.remove();
|
---|
| 88 | }
|
---|
| 89 | });
|
---|
| 90 |
|
---|
| 91 | if (newSelectors.length) {
|
---|
| 92 | newSelectors.forEach(function (newSelector) {
|
---|
| 93 | return selectors.append(newSelector);
|
---|
| 94 | });
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | var caseInsensitiveRegExp = /i(\s*\/\*[\W\w]*?\*\/)*\s*\]/;
|
---|
| 99 |
|
---|
| 100 | var _default = _postcss.default.plugin('postcss-attribute-case-insensitive', function () {
|
---|
| 101 | return function (css) {
|
---|
| 102 | css.walkRules(caseInsensitiveRegExp, function (rule) {
|
---|
| 103 | rule.selector = (0, _postcssSelectorParser.default)(transform).processSync(rule.selector);
|
---|
| 104 | });
|
---|
| 105 | };
|
---|
| 106 | });
|
---|
| 107 |
|
---|
| 108 | exports.default = _default; |
---|