{"version":3,"file":"index.cjs","sources":["../src/index.js"],"sourcesContent":["const selectorParser = require('postcss-selector-parser');\n\nconst dirRegex = /:dir\\([^)]*\\)/;\n\nmodule.exports = function creator(opts) {\n\tconst dir = Object(opts).dir;\n\tconst preserve = Boolean(Object(opts).preserve);\n\tconst shadow = Boolean(Object(opts).shadow);\n\n\treturn {\n\t\tpostcssPlugin: 'postcss-dir-pseudo-class',\n\t\tRule(rule, { result }) {\n\t\t\tlet emittedWarningForHierarchicalDir = false;\n\n\t\t\t// walk rules using the :dir pseudo-class\n\t\t\tif (!dirRegex.test(rule.selector)) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet modifiedSelector;\n\n\t\t\ttry {\n\t\t\t\tmodifiedSelector = selectorParser(selectors => {\n\t\t\t\t\t// for each (comma separated) selector\n\t\t\t\t\tselectors.nodes.forEach(selector => {\n\t\t\t\t\t\t// walk all selector nodes that are :dir pseudo-classes\n\t\t\t\t\t\tselector.walk(node => {\n\t\t\t\t\t\t\tif ('pseudo' !== node.type) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (':dir' !== node.value) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// value of the :dir pseudo-class\n\t\t\t\t\t\t\tconst value = node.nodes.toString();\n\t\t\t\t\t\t\tif (value !== 'rtl' && value !== 'ltr') {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst parent = node.parent;\n\t\t\t\t\t\t\tconst otherDirPseudos = parent.nodes.filter((other) => {\n\t\t\t\t\t\t\t\treturn 'pseudo' === other.type && ':dir' === other.value;\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tif (otherDirPseudos.length > 1 && !emittedWarningForHierarchicalDir) {\n\t\t\t\t\t\t\t\temittedWarningForHierarchicalDir = true;\n\t\t\t\t\t\t\t\trule.warn(result, `Hierarchical :dir pseudo class usage can't be transformed correctly to [dir] attributes. This will lead to incorrect selectors for \"${rule.selector}\"`);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// previous and next selector nodes\n\t\t\t\t\t\t\tconst prev = node.prev();\n\t\t\t\t\t\t\tconst next = node.next();\n\n\t\t\t\t\t\t\tconst prevIsNonCombinator = prev && prev.type && 'combinator' !== prev.type;\n\t\t\t\t\t\t\tconst nextIsNonCombinator = next && next.type && 'combinator' !== next.type;\n\t\t\t\t\t\t\tconst nextIsNonCombinatorOrSpace = next && next.type && ('combinator' !== next.type || ('combinator' === next.type && ' ' === next.value));\n\n\t\t\t\t\t\t\tif (prevIsNonCombinator) {\n\t\t\t\t\t\t\t\tnode.remove();\n\t\t\t\t\t\t\t} else if (nextIsNonCombinator) {\n\t\t\t\t\t\t\t\tnode.remove();\n\t\t\t\t\t\t\t} else if (parent.nodes.indexOf(node) === 0 && nextIsNonCombinatorOrSpace) {\n\t\t\t\t\t\t\t\tnode.remove();\n\t\t\t\t\t\t\t} else if (parent.nodes.length === 1) {\n\t\t\t\t\t\t\t\tnode.remove();\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tnode.replaceWith(\n\t\t\t\t\t\t\t\t\tselectorParser.universal(),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// conditionally prepend a combinator before inserting the [dir] attribute\n\t\t\t\t\t\t\tconst first = parent.nodes[0];\n\t\t\t\t\t\t\tconst firstIsSpaceCombinator = first && 'combinator' === first.type && ' ' === first.value;\n\t\t\t\t\t\t\tconst firstIsHtml = first && 'tag' === first.type && 'html' === first.value;\n\t\t\t\t\t\t\tconst firstIsRoot = first && 'pseudo' === first.type && ':root' === first.value;\n\n\t\t\t\t\t\t\tif (first && !firstIsHtml && !firstIsRoot && !firstIsSpaceCombinator) {\n\t\t\t\t\t\t\t\tparent.prepend(\n\t\t\t\t\t\t\t\t\tselectorParser.combinator({\n\t\t\t\t\t\t\t\t\t\tvalue: ' ',\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// whether :dir matches the presumed direction\n\t\t\t\t\t\t\tconst isdir = dir === value;\n\n\t\t\t\t\t\t\t// [dir] attribute\n\t\t\t\t\t\t\tconst dirAttr = selectorParser.attribute({\n\t\t\t\t\t\t\t\tattribute: 'dir',\n\t\t\t\t\t\t\t\toperator: '=',\n\t\t\t\t\t\t\t\tquoteMark: '\"',\n\t\t\t\t\t\t\t\tvalue: `\"${value}\"`,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t// :host-context([dir]) for Shadow DOM CSS\n\t\t\t\t\t\t\tconst hostContextPseudo = selectorParser.pseudo({\n\t\t\t\t\t\t\t\tvalue: ':host-context',\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\thostContextPseudo.append(dirAttr);\n\n\t\t\t\t\t\t\t// not[dir] attribute\n\t\t\t\t\t\t\tconst notDirAttr = selectorParser.pseudo({\n\t\t\t\t\t\t\t\tvalue: `${firstIsHtml || firstIsRoot ? '' : 'html'}:not`,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tnotDirAttr.append(\n\t\t\t\t\t\t\t\tselectorParser.attribute({\n\t\t\t\t\t\t\t\t\tattribute: 'dir',\n\t\t\t\t\t\t\t\t\toperator: '=',\n\t\t\t\t\t\t\t\t\tquoteMark: '\"',\n\t\t\t\t\t\t\t\t\tvalue: `\"${'ltr' === value ? 'rtl' : 'ltr'}\"`,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\tif (isdir) {\n\t\t\t\t\t\t\t\t// if the direction is presumed\n\t\t\t\t\t\t\t\tif (firstIsHtml) {\n\t\t\t\t\t\t\t\t\t// insert :root after html tag\n\t\t\t\t\t\t\t\t\tparent.insertAfter(first, notDirAttr);\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t// prepend :root\n\t\t\t\t\t\t\t\t\tparent.prepend(notDirAttr);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else if (firstIsHtml) {\n\t\t\t\t\t\t\t\t// insert dir attribute after html tag\n\t\t\t\t\t\t\t\tparent.insertAfter(first, dirAttr);\n\t\t\t\t\t\t\t} else if (shadow && !firstIsRoot) {\n\t\t\t\t\t\t\t\t// prepend :host-context([dir])\n\t\t\t\t\t\t\t\tparent.prepend(hostContextPseudo);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// otherwise, prepend the dir attribute\n\t\t\t\t\t\t\t\tparent.prepend(dirAttr);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t});\n\t\t\t\t}).processSync(rule.selector);\n\t\t\t} catch (_) {\n\t\t\t\trule.warn(result, `Failed to parse selector : ${rule.selector}`);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (typeof modifiedSelector === 'undefined') {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (modifiedSelector === rule.selector) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (preserve) {\n\t\t\t\trule.cloneBefore({ selector: modifiedSelector });\n\t\t\t} else {\n\t\t\t\trule.assign({ selector: modifiedSelector });\n\t\t\t}\n\t\t},\n\t};\n};\n\nmodule.exports.postcss = true;\n"],"names":["selectorParser","require","dirRegex","module","exports","opts","dir","Object","preserve","Boolean","shadow","postcssPlugin","Rule","rule","result","modifiedSelector","emittedWarningForHierarchicalDir","test","selector","selectors","nodes","forEach","walk","node","type","value","toString","parent","filter","other","length","warn","prev","next","prevIsNonCombinator","nextIsNonCombinator","nextIsNonCombinatorOrSpace","indexOf","remove","replaceWith","universal","first","firstIsSpaceCombinator","firstIsHtml","firstIsRoot","prepend","combinator","isdir","dirAttr","attribute","operator","quoteMark","hostContextPseudo","pseudo","append","notDirAttr","insertAfter","processSync","_","cloneBefore","assign","postcss"],"mappings":"aA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