11import type Location from './Location' ;
2- import { fixBrokenRules , hasBrokenRules } from './safari' ;
32import { _DOMException , bootstrapper , defineProperty } from './shared' ;
4- import { clearRules , insertAllRules , rejectImports } from './utils' ;
3+ import { rejectImports } from './utils' ;
54
65const cssStyleSheetMethods = [
76 'addRule' ,
@@ -45,12 +44,15 @@ export function isNonConstructedStyleSheetInstance(instance: object): boolean {
4544 */
4645
4746/**
48- * Basic stylesheet is an sample stylesheet that contains all the CSS rules of
49- * the current constructable stylesheet. The document or custom elements can
50- * adopt constructable stylesheet; in this case, basic stylesheet's CSS rules
51- * are reflected in document/custom element internal <style> elements.
47+ * Basic style element is a sample element that contains all the CSS of the
48+ * current constructable stylesheet. The document or custom elements can
49+ * adopt constructable stylesheet; in this case, basic stylesheet's CSS is
50+ * reflected in document/custom element internal <style> elements.
5251 */
53- const $basicStyleSheet = new WeakMap < ConstructedStyleSheet , CSSStyleSheet > ( ) ;
52+ const $basicStyleElement = new WeakMap <
53+ ConstructedStyleSheet ,
54+ HTMLStyleElement
55+ > ( ) ;
5456
5557/**
5658 * Contains all locations associated with the current ConstructedStyleSheet.
@@ -68,6 +70,20 @@ const $adoptersByLocation = new WeakMap<
6870 WeakMap < Location , HTMLStyleElement >
6971> ( ) ;
7072
73+ type CSSStyleSheetMethodMeta = Readonly < {
74+ args : IArguments ;
75+ method : string ;
76+ } > ;
77+
78+ /**
79+ * Contains all the methods called for the constructable style sheet. Used to
80+ * reflect these methods in created or restyled `<style>` adopters.
81+ */
82+ const $appliedMethods = new WeakMap <
83+ ConstructedStyleSheet ,
84+ Array < CSSStyleSheetMethodMeta >
85+ > ( ) ;
86+
7187/*
7288 * Package-level control functions
7389 */
@@ -111,8 +127,12 @@ export function restyleAdopter(
111127 adopter : HTMLStyleElement ,
112128) : void {
113129 requestAnimationFrame ( ( ) => {
114- clearRules ( adopter . sheet ! ) ;
115- insertAllRules ( $basicStyleSheet . get ( sheet ) ! , adopter . sheet ! ) ;
130+ adopter . textContent = $basicStyleElement . get ( sheet ) ! . textContent ;
131+ $appliedMethods
132+ . get ( sheet ) !
133+ . forEach ( ( command ) =>
134+ adopter . sheet ! [ command . method ] . apply ( adopter . sheet ! , command . args ) ,
135+ ) ;
116136 } ) ;
117137}
118138
@@ -126,7 +146,7 @@ export function restyleAdopter(
126146 * properties are initialized.
127147 */
128148function checkInvocationCorrectness ( self : ConstructedStyleSheet ) {
129- if ( ! $basicStyleSheet . has ( self ) ) {
149+ if ( ! $basicStyleElement . has ( self ) ) {
130150 throw new TypeError ( 'Illegal invocation' ) ;
131151 }
132152}
@@ -149,9 +169,10 @@ function ConstructedStyleSheet(this: ConstructedStyleSheet) {
149169 bootstrapper . body . appendChild ( style ) ;
150170
151171 // Init private properties
152- $basicStyleSheet . set ( self , style . sheet ! ) ;
172+ $basicStyleElement . set ( self , style ) ;
153173 $locations . set ( self , [ ] ) ;
154174 $adoptersByLocation . set ( self , new WeakMap ( ) ) ;
175+ $appliedMethods . set ( self , [ ] ) ;
155176}
156177
157178const proto = ConstructedStyleSheet . prototype ;
@@ -175,11 +196,8 @@ proto.replaceSync = function replaceSync(contents) {
175196 if ( typeof contents === 'string' ) {
176197 const self = this ;
177198
178- const style = $basicStyleSheet . get ( self ) ! . ownerNode as HTMLStyleElement ;
179- style . textContent = hasBrokenRules
180- ? fixBrokenRules ( rejectImports ( contents ) )
181- : rejectImports ( contents ) ;
182- $basicStyleSheet . set ( self , style . sheet ! ) ;
199+ $basicStyleElement . get ( self ) ! . textContent = rejectImports ( contents ) ;
200+ $appliedMethods . set ( self , [ ] ) ;
183201
184202 $locations . get ( self ) ! . forEach ( ( location ) => {
185203 if ( location . isConnected ( ) ) {
@@ -197,7 +215,7 @@ defineProperty(proto, 'cssRules', {
197215 // CSSStyleSheet.prototype.cssRules;
198216 checkInvocationCorrectness ( this ) ;
199217
200- return $basicStyleSheet . get ( this ) ! . cssRules ;
218+ return $basicStyleElement . get ( this ) ! . sheet ! . cssRules ;
201219 } ,
202220} ) ;
203221
@@ -208,6 +226,8 @@ cssStyleSheetMethods.forEach((method) => {
208226
209227 const args = arguments ;
210228
229+ $appliedMethods . get ( self ) ! . push ( { method, args} ) ;
230+
211231 $locations . get ( self ) ! . forEach ( ( location ) => {
212232 if ( location . isConnected ( ) ) {
213233 // Type Note: If location is connected, adopter is already created; and
@@ -217,19 +237,9 @@ cssStyleSheetMethods.forEach((method) => {
217237 }
218238 } ) ;
219239
220- if ( hasBrokenRules ) {
221- if ( method === 'insertRule' ) {
222- args [ 0 ] = fixBrokenRules ( args [ 0 ] ) ;
223- }
224-
225- if ( method === 'addRule' ) {
226- args [ 1 ] = fixBrokenRules ( args [ 1 ] ) ;
227- }
228- }
229-
230- const basic = $basicStyleSheet . get ( self ) ! ;
240+ const basicSheet = $basicStyleElement . get ( self ) ! . sheet ! ;
231241
232- return basic [ method ] . apply ( basic , args ) ;
242+ return basicSheet [ method ] . apply ( basicSheet , args ) ;
233243 } ;
234244} ) ;
235245
0 commit comments