| 17 | * Is used for e.g. by `StyleSheet` or `ConditionalRule`. |
| 18 | */ |
| 19 | export default class RuleList { |
| 20 | // Rules registry for access by .get() method. |
| 21 | // It contains the same rule registered by name and by selector. |
| 22 | map = {} |
| 23 | |
| 24 | // Original styles object. |
| 25 | raw = {} |
| 26 | |
| 27 | // Used to ensure correct rules order. |
| 28 | index = [] |
| 29 | |
| 30 | counter = 0 |
| 31 | |
| 32 | constructor(options) { |
| 33 | this.options = options |
| 34 | this.classes = options.classes |
| 35 | this.keyframes = options.keyframes |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Create and register rule. |
| 40 | * |
| 41 | * Will not render after Style Sheet was rendered the first time. |
| 42 | */ |
| 43 | add(name, decl, ruleOptions) { |
| 44 | const {parent, sheet, jss, Renderer, generateId, scoped} = this.options |
| 45 | const options = { |
| 46 | classes: this.classes, |
| 47 | parent, |
| 48 | sheet, |
| 49 | jss, |
| 50 | Renderer, |
| 51 | generateId, |
| 52 | scoped, |
| 53 | name, |
| 54 | keyframes: this.keyframes, |
| 55 | selector: undefined, |
| 56 | ...ruleOptions |
| 57 | } |
| 58 | |
| 59 | // When user uses .createStyleSheet(), duplicate names are not possible, but |
| 60 | // `sheet.addRule()` opens the door for any duplicate rule name. When this happens |
| 61 | // we need to make the key unique within this RuleList instance scope. |
| 62 | let key = name |
| 63 | if (name in this.raw) { |
| 64 | key = `${name}-d${this.counter++}` |
| 65 | } |
| 66 | |
| 67 | // We need to save the original decl before creating the rule |
| 68 | // because cache plugin needs to use it as a key to return a cached rule. |
| 69 | this.raw[key] = decl |
| 70 | |
| 71 | if (key in this.classes) { |
| 72 | // E.g. rules inside of @media container |
| 73 | options.selector = `.${escape(this.classes[key])}` |
| 74 | } |
| 75 | |
| 76 | const rule = createRule(key, decl, options) |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…