{"version":3,"sources":["css-scoping.ts"],"sourcesContent":["/**\n * Wrap a CSS document with a custom scope selector\n * @param css css document\n * @param scope scope selector\n * @returns \n */\nexport function addCSSScopeSelector(css: string, scope: string) {\n  // remove block comments to avoid mis-detection of at-rule blocks\n  css = removeBlockComments(css);\n\t// first extract all @-rule blocks except @media and move them outside of the scope\n\tconst atBlocks: string[] = [];\n\tconst normalizedCSS = replaceAtRuleBlocks(css, (block) => {\n\t\tif (block.startsWith(\"@media\")) return block; // keep @media blocks inside the scope\n\t\tatBlocks.push(block);\n\t\treturn `/* [[UIX moved ${block.split(\" \")[0]}]] */`;\n\t});\n\n\t// wrap the rest with the scope and append the extracted @-rule blocks at the end\n\treturn `${atBlocks.join('\\n\\n')}\\n\\n${scope} {\\n${\n\t\t'    ' + \n\t\tnormalizedCSS\n\t\t\t.replaceAll(':host', '&')\n\t\t\t.replaceAll(':root', '&')\n\t\t\t.replaceAll('\\n', '\\n    ')\n\t}\\n}\\n`;\n}\n\nfunction removeBlockComments(cssText: string): string {\n  return cssText.replace(/\\/\\*[\\s\\S]*?\\*\\//g, '');\n}\n\n// Helper function to replace at-rule blocks in CSS text\nfunction replaceAtRuleBlocks(cssText: string, replacer: (block: string) => string): string {\n  const resultParts = [];\n  let lastIndex = 0;\n  const atRuleRegex = /@[a-zA-Z\\-]+[^{]*\\{/g;\n  let match;\n\n  while ((match = atRuleRegex.exec(cssText)) !== null) {\n    const start = match.index;\n    let i = atRuleRegex.lastIndex - 1;\n    let braceCount = 1;\n\n    // Walk through text to find matching closing brace\n    while (++i < cssText.length && braceCount > 0) {\n      if (cssText[i] === '{') braceCount++;\n      else if (cssText[i] === '}') braceCount--;\n    }\n\n    const end = i;\n    const block = cssText.slice(start, end);\n    const replacement = replacer(block);\n\n    // Push preceding text and replaced block\n    resultParts.push(cssText.slice(lastIndex, start), replacement);\n    lastIndex = end;\n  }\n\n  // Append remaining text\n  resultParts.push(cssText.slice(lastIndex));\n  return resultParts.join('');\n}\n\n\n// legacy implementation for addCSSScopeSelector\n// /**\n//  * Add a scope selector everywhere in a CSS document\n//  * @param css css document\n//  * @param scope scope selector\n//  * @returns \n//  */\n// export function addCSSScopeSelector(css: string, scope: string) {\n// \tconst scopedCSS = css.replace(/^[^@\\n]+{[^}]*}/gm, (part) => {\n// \t\tif (part.match(/^(to|from|\\d+%|[\\s,\\n])+{[^}]*}$/)) return part; // is inside @keyframe (e.g. \"50% {}\"\"), ignore\n// \t\telse {\n// \t\t\t// for each selectors (e.g. ':host, a#b'), add the scope, e.g. 'scope, scope a#b'\n// \t\t\treturn part.replace(/^[^@\\n]+(?={)/, (s) => {\n// \t\t\t\tconst selectors = s.split(/, */g).map(selector => \n// \t\t\t\t\t(selector.includes(\":host\") || selector.includes(\":root\")) ? \n// \t\t\t\t\t\tselector\n// \t\t\t\t\t\t\t.trimEnd()\n// \t\t\t\t\t\t\t.replace(/\\:(host|root)\\s*\\:host-context/g, ':host-context')\n// \t\t\t\t\t\t\t.replace(/\\:(host|root)(?![-\\w])/g, scope) : \n// \t\t\t\t\t\tscope + ' ' + selector.trimEnd()\n// \t\t\t\t\t);\n// \t\t\t\treturn selectors.join(\", \") + ' '\n// \t\t\t})\n// \t\t}\n// \t});\n// \treturn scopedCSS;\n// }\n\nexport function addCSSScope(css: string) {\n\treturn \"@scope {\\n\" + css + \"\\n}\";\n}\n\n// export function addCSSScope(css: string) {\n// \treturn addCSSScopeSelector(css, \"@scope\")\n// }"],"names":["addCSSScopeSelector","css","scope","removeBlockComments","atBlocks","normalizedCSS","replaceAtRuleBlocks","block","startsWith","push","split","join","replaceAll","cssText","replace","replacer","match","resultParts","lastIndex","atRuleRegex","exec","start","index","i","braceCount","length","end","replacement","slice","addCSSScope"],"mappings":"AAMA,OAAO,SAASA,oBAAoBC,GAAW,EAAEC,KAAa;IAE5DD,MAAME,oBAAoBF;IAE3B,IAAMG,WAAqB,EAAE,EACvBC,gBAAgBC,oBAAoBL,KAAK,CAACM,QAC/C,AAAIA,MAAMC,U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