index.js 2.9 KB

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