| 2 | import { StyleSheet } from "./StyleSheet"; |
| 3 | |
| 4 | export class ClientStyleSheet implements StyleSheet { |
| 5 | private name: string; |
| 6 | private deletedRulePlaceholder: string; |
| 7 | private speedy: boolean; |
| 8 | private tags: (HTMLStyleElement | undefined)[]; |
| 9 | private injected: boolean; |
| 10 | private rulesCount: number; |
| 11 | |
| 12 | constructor(name: string, speedy = false) { |
| 13 | this.name = name; |
| 14 | this.deletedRulePlaceholder = `#${name}-deleted-rule{}`; |
| 15 | this.speedy = speedy; |
| 16 | this.tags = []; |
| 17 | this.injected = false; |
| 18 | this.rulesCount = 0; |
| 19 | } |
| 20 | |
| 21 | public inject(): void { |
| 22 | if (this.injected) { |
| 23 | throw new Error("ClientStyleSheet: sheet already injected"); |
| 24 | } |
| 25 | |
| 26 | if (this.speedy) { |
| 27 | this.tags[0] = this.makeStyleTag(); |
| 28 | this.speedy = "insertRule" in this.getLatestSheet(); |
| 29 | if (!this.speedy) { |
| 30 | if (!isProduction) { |
| 31 | console.warn( |
| 32 | "ClientStyleSheet: speedy mode not supported falling back to standard mode.", |
| 33 | ); |
| 34 | } |
| 35 | this.flush(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | this.injected = true; |
| 40 | } |
| 41 | |
| 42 | public isSpeedy(): boolean { |
| 43 | return this.speedy; |
| 44 | } |
| 45 | |
| 46 | public setSpeedy(bool: boolean): void { |
| 47 | if (this.rulesCount === 0) { |
| 48 | throw new Error( |
| 49 | "ClientStyleSheet: speedy cannot be when rules have already been inserted", |
| 50 | ); |
| 51 | } |
| 52 | this.flush(); |
| 53 | this.speedy = bool; |
| 54 | this.inject(); |
| 55 | } |
| 56 | |
| 57 | public insertRule(rule: string, index?: number): number { |
| 58 | if (this.speedy) { |
| 59 | const sheet = this.getLatestSheet(); |
| 60 | if (typeof index !== "number") { |
| 61 | index = sheet.cssRules.length; |
nothing calls this directly
no outgoing calls
no test coverage detected