Skip to content

Commit 2fe0612

Browse files
committed
Do not expect the current realm global object to be window
- do not access `globalThis` (current realm global object) - do not access `CSS` implicitly resolved from `globalThis` - do not create (user exposed) `DOMException` implicitly resolved from `globalThis`
1 parent fa294f0 commit 2fe0612

12 files changed

+61
-52
lines changed

lib/cssom/CSS-impl.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ function isValidUniversalInitialValue(initial) {
4848

4949
export default class CSSImpl {
5050

51+
/**
52+
* @param {object} globalObject
53+
*/
54+
constructor(globalObject) {
55+
this._globalObject = globalObject
56+
}
57+
5158
/**
5259
* @param {string} ident
5360
* @returns {string}
@@ -63,15 +70,15 @@ export default class CSSImpl {
6370
*/
6471
registerProperty({ name, inherits, initialValue, syntax }) {
6572
if (isFailure(parseGrammar(name, '<custom-property-name>', '@property'))) {
66-
throw error(INVALID_CUSTOM_PROPERTY_NAME)
73+
throw error(INVALID_CUSTOM_PROPERTY_NAME, this._globalObject)
6774
}
68-
const register = globalThis.document._registeredProperties
75+
const register = this._globalObject.document._registeredProperties
6976
if (register.has(name)) {
70-
throw error(INVALID_CUSTOM_PROPERTY_OVERRIDE)
77+
throw error(INVALID_CUSTOM_PROPERTY_OVERRIDE, this._globalObject)
7178
}
7279
syntax = parseDeclarationValue(`"${syntax}"`, 'syntax', '@property')
7380
if (isFailure(syntax)) {
74-
throw error(INVALID_CUSTOM_PROPERTY_SYNTAX)
81+
throw error(INVALID_CUSTOM_PROPERTY_SYNTAX, this._globalObject)
7582
}
7683
syntax = serializeComponentValue(syntax)
7784
if (syntax === '*') {
@@ -84,17 +91,17 @@ export default class CSSImpl {
8491
register.set(name, { inherits, initialValue, syntax })
8592
return
8693
}
87-
throw error(INVALID_INITIAL_CUSTOM_PROPERTY_VALUE_UNIVERSAL)
94+
throw error(INVALID_INITIAL_CUSTOM_PROPERTY_VALUE_UNIVERSAL, this._globalObject)
8895
}
8996
if (initialValue) {
9097
initialValue = parseGrammar(initialValue, syntax, '@property')
9198
if (!isFailure(initialValue) && isComputationallyIndependent(initialValue)) {
9299
register.set(name, { inherits, initialValue, syntax })
93100
return
94101
}
95-
throw error(INVALID_INITIAL_CUSTOM_PROPERTY_VALUE)
102+
throw error(INVALID_INITIAL_CUSTOM_PROPERTY_VALUE, this._globalObject)
96103
}
97-
throw error(MISSING_INITIAL_CUSTOM_PROPERTY_VALUE)
104+
throw error(MISSING_INITIAL_CUSTOM_PROPERTY_VALUE, this._globalObject)
98105
}
99106

100107
/**

lib/cssom/CSSCounterStyleRule-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default class CSSCounterStyleRuleImpl extends CSSRuleImpl {
8585
return serializeComponentValue(this._name).trim()
8686
}
8787
set name(name) {
88-
name = parseGrammar(CSS.escape(name), '<counter-style-name>', this)
88+
name = parseGrammar(this._globalObject.CSS.escape(name), '<counter-style-name>', this)
8989
if (!isFailure(name)) {
9090
const lowercase = toLowerCase(name.value)
9191
if (counterStyles.predefined.includes(lowercase)) {

lib/cssom/CSSFontFeatureValuesMap-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default class CSSFontFeatureValuesMapImpl {
8787
values = [values]
8888
}
8989
if (isFailure(parseDeclarationValue(values.join(' '), key, this))) {
90-
throw error(INVALID_FONT_FEATURE_VALUE_ERROR)
90+
throw error(INVALID_FONT_FEATURE_VALUE_ERROR, this._globalObject)
9191
}
9292
this._map.set(key, values)
9393
}

lib/cssom/CSSGroupingRule-impl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export default class CSSGroupingRuleImpl extends CSSRuleImpl {
1414
* @see {@link https://drafts.csswg.org/cssom-1/#dom-cssgroupingrule-insertrule}
1515
*/
1616
insertRule(rule, index) {
17-
return insertRule(this.cssRules._rules, rule, index, this)
17+
return insertRule(this.cssRules, rule, index, this)
1818
}
1919

2020
/**
2121
* @param {number} index
2222
* @see {@link https://drafts.csswg.org/cssom-1/#dom-cssgroupingrule-deleterule}
2323
*/
2424
deleteRule(index) {
25-
removeRule(this.cssRules._rules, index)
25+
removeRule(this.cssRules, index)
2626
}
2727
}

lib/cssom/CSSKeyframeRule-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class CSSKeyframeRuleImpl extends CSSRuleImpl {
5252
set keyText(selector) {
5353
selector = parseGrammar(selector, '<keyframe-selector>#', this)
5454
if (isFailure(selector)) {
55-
throw error(SET_INVALID_KEYFRAME_SELECTOR_ERROR)
55+
throw error(SET_INVALID_KEYFRAME_SELECTOR_ERROR, this._globalObject)
5656
}
5757
this._keyText = selector
5858
}

lib/cssom/CSSKeyframesRule-impl.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export default class CSSKeyframesRuleImpl extends CSSRuleImpl {
5959
* @see {@link https://drafts.csswg.org/css-animations-1/#dom-csskeyframesrule-name}
6060
*/
6161
set name(name) {
62-
name = parseGrammar(CSS.escape(name), '<keyframes-name>', this)
62+
name = parseGrammar(this._globalObject.CSS.escape(name), '<keyframes-name>', this)
6363
if (isFailure(name)) {
64-
throw error(SET_INVALID_KEYFRAMES_NAME_ERROR)
64+
throw error(SET_INVALID_KEYFRAMES_NAME_ERROR, this._globalObject)
6565
}
6666
this._name = name
6767
}
@@ -94,8 +94,7 @@ export default class CSSKeyframesRuleImpl extends CSSRuleImpl {
9494
* @see {@link https://drafts.csswg.org/css-animations-1/#dom-csskeyframesrule-appendrule}
9595
*/
9696
appendRule(rule) {
97-
const rules = this.cssRules._rules
98-
insertRule(rules, rule, rules.length, this)
97+
insertRule(this.cssRules, rule, this.cssRules.length, this)
9998
}
10099

101100
/**
@@ -109,7 +108,7 @@ export default class CSSKeyframesRuleImpl extends CSSRuleImpl {
109108
const rules = this.cssRules._rules
110109
const index = rules.findLastIndex(rule => rule.keyText === selector)
111110
if (-1 < index) {
112-
removeRule(rules, index)
111+
removeRule(this.cssRules, index)
113112
}
114113
}
115114
}

lib/cssom/CSSStyleDeclaration-impl.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export default class CSSStyleDeclarationImpl {
110110
*/
111111
set cssText(value) {
112112
if (this._readOnly) {
113-
throw error(UPDATE_COMPUTED_STYLE_DECLARATION_ERROR)
113+
throw error(UPDATE_COMPUTED_STYLE_DECLARATION_ERROR, this._globalObject)
114114
}
115115
this._declarations = parseDeclarationList(value, this.parentRule ?? this._ownerNode)
116116
this._update()
@@ -178,7 +178,7 @@ export default class CSSStyleDeclarationImpl {
178178
*/
179179
setProperty(name, value, priority = '') {
180180
if (this._readOnly) {
181-
throw error(UPDATE_COMPUTED_STYLE_DECLARATION_ERROR)
181+
throw error(UPDATE_COMPUTED_STYLE_DECLARATION_ERROR, this._globalObject)
182182
}
183183
if (value === '') {
184184
this.removeProperty(name)
@@ -211,7 +211,7 @@ export default class CSSStyleDeclarationImpl {
211211
*/
212212
removeProperty(name) {
213213
if (this._readOnly) {
214-
throw error(UPDATE_COMPUTED_STYLE_DECLARATION_ERROR)
214+
throw error(UPDATE_COMPUTED_STYLE_DECLARATION_ERROR, this._globalObject)
215215
}
216216
name = getDeclarationName(this.parentRule ?? this._ownerNode, name)
217217
if (!name) {

lib/cssom/CSSStyleSheet-impl.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export default class CSSStyleSheetImpl extends StyleSheetImpl {
101101
*/
102102
get cssRules() {
103103
if (!this._originClean) {
104-
throw error(ACCESS_THIRD_PARTY_STYLESHEET_ERROR)
104+
throw error(ACCESS_THIRD_PARTY_STYLESHEET_ERROR, this._globalObject)
105105
}
106106
return this._cssRules
107107
}
@@ -122,12 +122,12 @@ export default class CSSStyleSheetImpl extends StyleSheetImpl {
122122
*/
123123
insertRule(rule, index) {
124124
if (!this._originClean) {
125-
throw error(ACCESS_THIRD_PARTY_STYLESHEET_ERROR)
125+
throw error(ACCESS_THIRD_PARTY_STYLESHEET_ERROR, this._globalObject)
126126
}
127127
if (this._disallowModification) {
128-
throw error(UPDATE_LOCKED_STYLESHEET_ERROR)
128+
throw error(UPDATE_LOCKED_STYLESHEET_ERROR, this._globalObject)
129129
}
130-
return insertRule(this._rules, rule, index, this, !this._constructed)
130+
return insertRule(this.cssRules, rule, index, this, !this._constructed)
131131
}
132132

133133
/**
@@ -137,8 +137,8 @@ export default class CSSStyleSheetImpl extends StyleSheetImpl {
137137
* @see {@link https://drafts.csswg.org/cssom-1/#dom-cssstylesheet-addrule}
138138
*/
139139
addRule(selector, contents, index) {
140-
const { _constructed, _rules } = this
141-
insertRule(_rules, `${selector} { ${contents} }`, index ?? _rules.length, this, !_constructed)
140+
const { _constructed, cssRules } = this
141+
insertRule(cssRules, `${selector} { ${contents} }`, index ?? cssRules.length, this, !_constructed)
142142
return -1
143143
}
144144

@@ -148,12 +148,12 @@ export default class CSSStyleSheetImpl extends StyleSheetImpl {
148148
*/
149149
deleteRule(index) {
150150
if (!this._originClean) {
151-
throw error(ACCESS_THIRD_PARTY_STYLESHEET_ERROR)
151+
throw error(ACCESS_THIRD_PARTY_STYLESHEET_ERROR, this._globalObject)
152152
}
153153
if (this._disallowModification) {
154-
throw error(UPDATE_LOCKED_STYLESHEET_ERROR)
154+
throw error(UPDATE_LOCKED_STYLESHEET_ERROR, this._globalObject)
155155
}
156-
removeRule(this._rules, index)
156+
removeRule(this.cssRules, index)
157157
}
158158

159159
/**
@@ -171,7 +171,7 @@ export default class CSSStyleSheetImpl extends StyleSheetImpl {
171171
*/
172172
async replace(text) {
173173
if (!this._constructed || this._disallowModification) {
174-
return Promise.reject(error(UPDATE_LOCKED_STYLESHEET_ERROR))
174+
return Promise.reject(error(UPDATE_LOCKED_STYLESHEET_ERROR, this._globalObject))
175175
}
176176
this._disallowModification = true
177177
await Promise.resolve()
@@ -194,7 +194,7 @@ export default class CSSStyleSheetImpl extends StyleSheetImpl {
194194
*/
195195
replaceSync(text) {
196196
if (!this._constructed || this._disallowModification) {
197-
throw error(UPDATE_LOCKED_STYLESHEET_ERROR)
197+
throw error(UPDATE_LOCKED_STYLESHEET_ERROR, this._globalObject)
198198
}
199199
this._rules.splice(0)
200200
const rules = parseGrammar(text, root.value, this).filter(rule => {

lib/cssom/MediaList-impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export default class MediaListImpl {
112112
}
113113
}
114114
if (removed === 0) {
115-
throw error(DELETE_UNEXISTENT_MEDIUM_ERROR)
115+
throw error(DELETE_UNEXISTENT_MEDIUM_ERROR, this._globalObject)
116116
}
117117
}
118118
}

lib/error.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,15 @@ export const UPDATE_LOCKED_STYLESHEET_ERROR = {
8787

8888
/**
8989
* @param {object} description
90+
* @param {object} [globalObject]
9091
* @returns {DOMException|Error}
9192
*/
92-
export function create({ message, name, type: Type = 'SyntaxError' }) {
93-
if (name) {
94-
return new DOMException(message, name)
93+
export function create({ message, name = 'SyntaxError'}, globalObject) {
94+
if (globalObject) {
95+
return new globalObject.DOMException(message, name)
9596
}
9697
if (environment.test) {
97-
console.error(`${Type}: ${message}`)
98+
console.error(`${name}: ${message}`)
9899
}
99-
return new Type(message)
100+
return new globalThis[name](message)
100101
}

0 commit comments

Comments
 (0)