|
| 1 | +import gentleRegisterProperty from "../gentle-register-property.js"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Detect if the browser is affected by the Safari adopted style sheet bug: |
| 5 | + * removing a CSS custom property from an adopted stylesheet in a ShadowRoot |
| 6 | + * does not update the computed value as expected and doesn't trigger a transition. |
| 7 | + * @returns {{value: boolean, valuePending: boolean}} |
| 8 | + */ |
| 9 | +export default { |
| 10 | + get value () { |
| 11 | + let dummy = document.createElement("div"); |
| 12 | + document.body.append(dummy); |
| 13 | + dummy.attachShadow({ mode: "open" }); |
| 14 | + |
| 15 | + if (Object.isFrozen(dummy.shadowRoot.adoptedStyleSheets)) { |
| 16 | + // The browser doesn't support the modern adoptedStyleSheets API, i.e., it is not affected by the bug |
| 17 | + dummy.remove(); |
| 18 | + delete this.value; |
| 19 | + |
| 20 | + return (this.value = false); |
| 21 | + } |
| 22 | + |
| 23 | + gentleRegisterProperty("--style-observer-adopted-style-sheet-bug", { |
| 24 | + syntax: "<number>", |
| 25 | + inherits: true, |
| 26 | + initialValue: 0, |
| 27 | + }); |
| 28 | + |
| 29 | + let sheet = new CSSStyleSheet(); |
| 30 | + sheet.insertRule(` |
| 31 | + :host { |
| 32 | + /* This declaration shouldn't be empty for the bug to trigger */ |
| 33 | + color: transparent; |
| 34 | + } |
| 35 | + `); |
| 36 | + |
| 37 | + dummy.shadowRoot.adoptedStyleSheets.push(sheet); |
| 38 | + let style = sheet.cssRules[0].style; |
| 39 | + |
| 40 | + style.setProperty("--style-observer-adopted-style-sheet-bug", "1"); |
| 41 | + let cs = getComputedStyle(dummy); |
| 42 | + let oldValue = cs.getPropertyValue("--style-observer-adopted-style-sheet-bug"); |
| 43 | + |
| 44 | + style.removeProperty("--style-observer-adopted-style-sheet-bug"); |
| 45 | + cs = getComputedStyle(dummy); |
| 46 | + let newValue = cs.getPropertyValue("--style-observer-adopted-style-sheet-bug"); |
| 47 | + |
| 48 | + dummy.remove(); |
| 49 | + delete this.value; |
| 50 | + |
| 51 | + return (this.value = oldValue === newValue); |
| 52 | + }, |
| 53 | + get valuePending () { |
| 54 | + delete this.valuePending; |
| 55 | + return this.value; |
| 56 | + }, |
| 57 | +}; |
0 commit comments