| 1 |
/** |
| 2 |
* @license |
| 3 |
* Copyright 2024 Google LLC |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* This is a limited implemenation of the CSSStyleSheet class |
| 9 |
* and associated functionality. |
| 10 |
*/ |
| 11 |
|
| 12 |
type MediaListInterface = MediaList; |
| 13 |
|
| 14 |
const MediaListShim = class MediaList |
| 15 |
extends Array<string> |
| 16 |
implements MediaListInterface |
| 17 |
{ |
| 18 |
get mediaText(): string { |
| 19 |
return this.join(', '); |
| 20 |
} |
| 21 |
toString(): string { |
| 22 |
return this.mediaText; |
| 23 |
} |
| 24 |
appendMedium(medium: string): void { |
| 25 |
if (!this.includes(medium)) { |
| 26 |
this.push(medium); |
| 27 |
} |
| 28 |
} |
| 29 |
deleteMedium(medium: string): void { |
| 30 |
const index = this.indexOf(medium); |
| 31 |
if (index !== -1) { |
| 32 |
this.splice(index, 1); |
| 33 |
} |
| 34 |
} |
| 35 |
item(index: number): string | null { |
| 36 |
return this[index] ?? null; |
| 37 |
} |
| 38 |
}; |
| 39 |
const MediaListShimWithRealType = MediaListShim as object as typeof MediaList; |
| 40 |
export {MediaListShimWithRealType as MediaList}; |
| 41 |
|
| 42 |
type StyleSheetInterface = StyleSheet; |
| 43 |
|
| 44 |
const StyleSheetShim = class StyleSheet implements StyleSheetInterface { |
| 45 |
private __media = new MediaListShim(); |
| 46 |
|
| 47 |
disabled: boolean = false; |
| 48 |
get href(): string | null { |
| 49 |
return null; |
| 50 |
} |
| 51 |
get media(): MediaList { |
| 52 |
return this.__media; |
| 53 |
} |
| 54 |
get ownerNode(): Element | ProcessingInstruction | null { |
| 55 |
return null; |
| 56 |
} |
| 57 |
get parentStyleSheet(): CSSStyleSheet | null { |
| 58 |
return null; |
| 59 |
} |
| 60 |
get title(): string | null { |
| 61 |
return null; |
| 62 |
} |
| 63 |
get type(): string { |
| 64 |
return 'text/css'; |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
const StyleSheetShimWithRealType = |
| 69 |
StyleSheetShim as object as typeof StyleSheet; |
| 70 |
export {StyleSheetShimWithRealType as StyleSheet}; |
| 71 |
|
| 72 |
type CSSRuleInterface = CSSRule; |
| 73 |
|
| 74 |
const CSSRuleShim = class CSSRule implements CSSRuleInterface { |
| 75 |
static readonly STYLE_RULE: 1 = 1 as const; |
| 76 |
static readonly CHARSET_RULE: 2 = 2 as const; |
| 77 |
static readonly IMPORT_RULE: 3 = 3 as const; |
| 78 |
static readonly MEDIA_RULE: 4 = 4 as const; |
| 79 |
static readonly FONT_FACE_RULE: 5 = 5 as const; |
| 80 |
static readonly PAGE_RULE: 6 = 6 as const; |
| 81 |
static readonly NAMESPACE_RULE: 10 = 10 as const; |
| 82 |
static readonly KEYFRAMES_RULE: 7 = 7 as const; |
| 83 |
static readonly KEYFRAME_RULE: 8 = 8 as const; |
| 84 |
static readonly SUPPORTS_RULE: 12 = 12 as const; |
| 85 |
static readonly COUNTER_STYLE_RULE: 11 = 11 as const; |
| 86 |
static readonly FONT_FEATURE_VALUES_RULE: 14 = 14 as const; |
| 87 |
static readonly MARGIN_RULE: 9 = 9 as const; |
| 88 |
readonly STYLE_RULE: 1 = 1 as const; |
| 89 |
readonly CHARSET_RULE: 2 = 2 as const; |
| 90 |
readonly IMPORT_RULE: 3 = 3 as const; |
| 91 |
readonly MEDIA_RULE: 4 = 4 as const; |
| 92 |
readonly FONT_FACE_RULE: 5 = 5 as const; |
| 93 |
readonly PAGE_RULE: 6 = 6 as const; |
| 94 |
readonly NAMESPACE_RULE: 10 = 10 as const; |
| 95 |
readonly KEYFRAMES_RULE: 7 = 7 as const; |
| 96 |
readonly KEYFRAME_RULE: 8 = 8 as const; |
| 97 |
readonly SUPPORTS_RULE: 12 = 12 as const; |
| 98 |
readonly COUNTER_STYLE_RULE: 11 = 11 as const; |
| 99 |
readonly FONT_FEATURE_VALUES_RULE: 14 = 14 as const; |
| 100 |
readonly MARGIN_RULE: 9 = 9 as const; |
| 101 |
__parentStyleSheet: CSSStyleSheet | null = null; |
| 102 |
|
| 103 |
cssText: string = ''; |
| 104 |
get parentRule(): CSSRule | null { |
| 105 |
return null; |
| 106 |
} |
| 107 |
get parentStyleSheet(): CSSStyleSheet | null { |
| 108 |
return this.__parentStyleSheet; |
| 109 |
} |
| 110 |
get type(): number { |
| 111 |
return 0; |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
const CSSRuleShimWithRealType = CSSRuleShim as object as typeof CSSRule; |
| 116 |
export {CSSRuleShimWithRealType as CSSRule}; |
| 117 |
|
| 118 |
type CSSRuleListInterface = CSSRuleList; |
| 119 |
|
| 120 |
const CSSRuleListShim = class CSSRuleList |
| 121 |
extends Array<CSSRule> |
| 122 |
implements CSSRuleListInterface |
| 123 |
{ |
| 124 |
item(index: number): CSSRule | null { |
| 125 |
return this[index] ?? null; |
| 126 |
} |
| 127 |
}; |
| 128 |
|
| 129 |
const CSSRuleListShimWithRealType = |
| 130 |
CSSRuleListShim as object as typeof CSSRuleList; |
| 131 |
export {CSSRuleListShimWithRealType as CSSRuleList}; |
| 132 |
|
| 133 |
type CSSStyleSheetInterface = CSSStyleSheet; |
| 134 |
|
| 135 |
const CSSStyleSheetShim = class CSSStyleSheet |
| 136 |
extends StyleSheetShim |
| 137 |
implements CSSStyleSheetInterface |
| 138 |
{ |
| 139 |
private __rules = new CSSRuleListShim(); |
| 140 |
get cssRules(): CSSRuleList { |
| 141 |
return this.__rules; |
| 142 |
} |
| 143 |
get ownerRule(): CSSRule | null { |
| 144 |
return null; |
| 145 |
} |
| 146 |
get rules(): CSSRuleList { |
| 147 |
return this.cssRules; |
| 148 |
} |
| 149 |
addRule(_selector?: string, _style?: string, _index?: number): number { |
| 150 |
throw new Error('Method not implemented.'); |
| 151 |
} |
| 152 |
deleteRule(_index: number): void { |
| 153 |
throw new Error('Method not implemented.'); |
| 154 |
} |
| 155 |
insertRule(_rule: string, _index?: number): number { |
| 156 |
throw new Error('Method not implemented.'); |
| 157 |
} |
| 158 |
removeRule(_index?: number): void { |
| 159 |
throw new Error('Method not implemented.'); |
| 160 |
} |
| 161 |
replace(text: string): Promise<CSSStyleSheet> { |
| 162 |
this.replaceSync(text); |
| 163 |
return Promise.resolve(this); |
| 164 |
} |
| 165 |
replaceSync(text: string): void { |
| 166 |
this.__rules.length = 0; |
| 167 |
const rule = new CSSRuleShim(); |
| 168 |
rule.cssText = text; |
| 169 |
this.__rules.push(rule); |
| 170 |
} |
| 171 |
}; |
| 172 |
|
| 173 |
const CSSStyleSheetShimWithRealType = |
| 174 |
CSSStyleSheetShim as object as typeof CSSStyleSheet; |
| 175 |
export { |
| 176 |
CSSStyleSheetShimWithRealType as CSSStyleSheet, |
| 177 |
CSSStyleSheetShimWithRealType as CSSStyleSheetShim, |
| 178 |
}; |