1 line
6.8 KiB
Plaintext
1 line
6.8 KiB
Plaintext
|
{"version":3,"file":"index.es.mjs","sources":["index.js"],"sourcesContent":["import postcss from 'postcss';\nimport selectorParser from 'postcss-selector-parser';\n\nexport default postcss.plugin('postcss-dir-pseudo-class', opts => {\n\tconst dir = Object(opts).dir;\n\tconst preserve = Boolean(Object(opts).preserve);\n\n\treturn root => {\n\t\t// walk rules using the :dir pseudo-class\n\t\troot.walkRules(/:dir\\([^\\)]*\\)/, rule => {\n\t\t\tlet currentRule = rule\n\n\t\t\t// conditionally preserve the original rule\n\t\t\tif (preserve) {\n\t\t\t\tcurrentRule = rule.cloneBefore();\n\t\t\t}\n\n\t\t\t// update the rule selector\n\t\t\tcurrentRule.selector = selectorParser(selectors => {\n\t\t\t\t// for each (comma separated) selector\n\t\t\t\tselectors.nodes.forEach(selector => {\n\t\t\t\t\t// walk all selector nodes that are :dir pseudo-classes\n\t\t\t\t\tselector.walk(node => {\n\t\t\t\t\t\tif ('pseudo' === node.type && ':dir' === node.value) {\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 prevIsSpaceCombinator = prev && prev.type && 'combinator' === prev.type && ' ' === prev.value;\n\t\t\t\t\t\t\tconst nextIsSpaceCombinator = next && next.type && 'combinator' === next.type && ' ' === next.value;\n\n\t\t\t\t\t\t\t// preserve the selector tree\n\t\t\t\t\t\t\tif (prevIsSpaceCombinator && (nextIsSpaceCombinator || !next)) {\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} else {\n\t\t\t\t\t\t\t\tnode.remove();\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 = selector.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\tselector.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// value of the :dir pseudo-class\n\t\t\t\t\t\t\tconst value = node.nodes.toString();\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// 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\tselector.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\tselector.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// otherwise, insert dir attribute after html tag\n\t\t\t\t\t\t\t\tselector.insertAfter(first, dirAttr);\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\tselector.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});\n\t\t\t}).processSync(currentRule.selector);\n\t\t});\n\t};\n});\n"],"names":["postcss","plugin","opts","dir
|