Skip to content

Commit ca636a6

Browse files
committed
Minor refactoring
@import never invalidates any other following rule so it can be removed after parsing all rules instead of immediately after parsing @import.
1 parent db04f00 commit ca636a6

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

lib/cssom/CSSStyleSheet-impl.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import { ACCESS_THIRD_PARTY_STYLESHEET_ERROR, UPDATE_LOCKED_STYLESHEET_ERROR, create as error } from '../error.js'
33
import { insertCSSRule, parseCSSGrammar, parseStyleSheet, removeCSSRule } from '../parse/parser.js'
4+
import CSSImportRule from './CSSImportRule.js'
45
import CSSRuleList from './CSSRuleList.js'
56
import MediaList from './MediaList.js'
67
import StyleSheetImpl from './StyleSheet-impl.js'
@@ -83,7 +84,7 @@ export default class CSSStyleSheetImpl extends StyleSheetImpl {
8384
this._document = null
8485
this._location = location
8586
this._originClean = originClean
86-
this._rules = parseStyleSheet(rules, this, true)
87+
this._rules = parseStyleSheet(rules, this)
8788
this.disabled = disabled
8889
this.media = createMediaList(media, globalObject)
8990
this.ownerNode = ownerNode
@@ -147,7 +148,7 @@ export default class CSSStyleSheetImpl extends StyleSheetImpl {
147148
this._disallowModification = true
148149
await Promise.resolve()
149150
this._rules.splice(0)
150-
const rules = parseStyleSheet(text, this)
151+
const rules = parseStyleSheet(text, this).filter(rule => !CSSImportRule.isImpl(rule))
151152
this._rules.push(...rules)
152153
this._disallowModification = false
153154
return wrapperForImpl(this)
@@ -162,7 +163,7 @@ export default class CSSStyleSheetImpl extends StyleSheetImpl {
162163
throw error(UPDATE_LOCKED_STYLESHEET_ERROR)
163164
}
164165
this._rules.splice(0)
165-
const rules = parseStyleSheet(text, this)
166+
const rules = parseStyleSheet(text, this).filter(rule => !CSSImportRule.isImpl(rule))
166167
this._rules.push(...rules)
167168
}
168169
}

lib/parse/parser.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -746,14 +746,13 @@ function consumeBlockContents(tokens, context, ignoreCloseCurlyBlock) {
746746
/**
747747
* @param {Stream} tokens
748748
* @param {object} context
749-
* @param {boolean} [allowImport]
750749
* @returns {CSSRuleImpl[]}
751750
* @see {@link https://drafts.csswg.org/css-syntax-3/#consume-a-stylesheets-contents}
752751
*
753752
* It deviates from the specification by validating and/or discarding rules here
754-
* instead of in consumeAtRule() or CSSStyleSheet.replace*().
753+
* instead of in consumeAtRule().
755754
*/
756-
function consumeStyleSheet(tokens, context, allowImport) {
755+
function consumeStyleSheet(tokens, context) {
757756
const rules = []
758757
while (!tokens.atEnd()) {
759758
if (tokens.consume(token => isDelimiter([' ', '<!--', '-->'], token))) {
@@ -763,7 +762,6 @@ function consumeStyleSheet(tokens, context, allowImport) {
763762
const rule = consumeAtRule(tokens, context)
764763
if (
765764
isError(rule)
766-
|| (!allowImport && CSSImportRule.isImpl(rule))
767765
|| isInvalidIndexForRule(rules, rule, rules.length)
768766
|| isInvalidNamespaceRule(rules, rule)
769767
) {
@@ -919,12 +917,11 @@ function parseBlockContents(input, context) {
919917
/**
920918
* @param {ReadableStream|Stream|string|object[]} input
921919
* @param {CSSStyleSheetImpl} context
922-
* @param {boolean} [allowImport]
923920
* @returns {CSSRuleImpl[]}
924921
* @see {@link https://drafts.csswg.org/css-syntax-3/#parse-a-stylesheet}
925922
*/
926-
function parseStyleSheet(input, context, allowImport = false) {
927-
return consumeStyleSheet(normalizeIntoTokens(input), createContext(context), allowImport)
923+
function parseStyleSheet(input, context) {
924+
return consumeStyleSheet(normalizeIntoTokens(input), createContext(context))
928925
}
929926

930927
/**

0 commit comments

Comments
 (0)