Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function insertRule(
{ selector, declaration, support, media, pseudo }: Object,
renderer: DOMRenderer,
node: Object
) {
const nodeReference = media + support
// only use insertRule in production as browser devtools might have
// weird behavior if used together with insertRule at runtime
if (renderer.devMode) {
insertRuleInDevMode(renderer, node)
return
}
try {
const score = getRuleScore(renderer.ruleOrder, pseudo)
const { cssRules } = node.sheet
let index = cssRules.length
// This heavily optimises the amount of rule iterations we need
// due to most rules having a score=0 anyways
if (score === 0) {
if (renderer.scoreIndex[nodeReference] === undefined) {
index = 0
} else {
index = renderer.scoreIndex[nodeReference] + 1
}
} else {
// we start iterating from the last score=0 entry
// to correctly inject pseudo classes etc.
const startIndex = renderer.scoreIndex[nodeReference] || 0
arrayEach(node.sheet.cssRules, (rule, index) => {
const selectorText = rule.conditionText
? rule.cssRules[0].selectorText
: rule.selectorText
const score = getRuleScore(
renderer.ruleOrder,
selectorText.split(CLASSNAME_REGEX)[1]
)
if (score === 0) {
renderer.scoreIndex[nodeReference] = index
}
rule.score = score
})
}