pull/9549/head
Rich Harris 3 years ago
parent 7ea05ce380
commit 61497b2616

@ -104,8 +104,8 @@ export default class Selector {
// We don't make any changes to the invisible selectors // We don't make any changes to the invisible selectors
// because they don't exist in reality in css nesting // because they don't exist in reality in css nesting
// and changing them would affect the nested rules parent rule selectors // and changing them would affect the nested rules parent rule selectors
if(selector.invisible) { if (selector.invisible) {
continue continue;
} }
if (selector.type === 'PseudoElementSelector' || selector.type === 'PseudoClassSelector') { if (selector.type === 'PseudoElementSelector' || selector.type === 'PseudoClassSelector') {
@ -129,7 +129,7 @@ export default class Selector {
if (block.should_encapsulate) { if (block.should_encapsulate) {
encapsulate_block( encapsulate_block(
block, block,
index === this.blocks.filter(block => !block.invisible).length - 1 index === this.blocks.filter((block) => !block.invisible).length - 1
? attr.repeat(amount_class_specificity_to_increase + 1) ? attr.repeat(amount_class_specificity_to_increase + 1)
: attr : attr
); );
@ -808,7 +808,7 @@ class Block {
should_encapsulate; should_encapsulate;
/** @type {boolean} */ /** @type {boolean} */
invisible invisible;
/** @param {import('#compiler').Css.Combinator | null} combinator */ /** @param {import('#compiler').Css.Combinator | null} combinator */
constructor(combinator) { constructor(combinator) {
@ -819,7 +819,7 @@ class Block {
this.start = -1; this.start = -1;
this.end = -1; this.end = -1;
this.should_encapsulate = false; this.should_encapsulate = false;
this.invisible = false this.invisible = false;
} }
/** @param {import('#compiler').Css.SimpleSelector} selector */ /** @param {import('#compiler').Css.SimpleSelector} selector */
@ -845,7 +845,12 @@ class Block {
} }
} }
const InvisibleCombinator = { type: /** @type {"Combinator"} **/ ("Combinator"), name: ' ', start: -1, end: -1} const InvisibleCombinator = {
type: /** @type {"Combinator"} **/ ('Combinator'),
name: ' ',
start: -1,
end: -1
};
/** /**
* Groups selectors by combinator into blocks * Groups selectors by combinator into blocks
@ -867,11 +872,11 @@ function group_selectors(selector, parent_selector) {
if (parent_selector) { if (parent_selector) {
const nested_rule_indices = selector.children const nested_rule_indices = selector.children
.map((child, index) => (child.type === 'NestedSelector' ? index : -1)) .map((child, index) => (child.type === 'NestedSelector' ? index : -1))
.filter(index => index !== -1); .filter((index) => index !== -1);
const parent_children = parent_selector.node.children.map(child => ({ const parent_children = parent_selector.node.children.map((child) => ({
...child, ...child,
invisible: true, invisible: true
})); }));
if (nested_rule_indices.length === 0) { if (nested_rule_indices.length === 0) {
@ -884,7 +889,9 @@ function group_selectors(selector, parent_selector) {
} else { } else {
// There's an & nesting selectors somewhere // There's an & nesting selectors somewhere
// so we delete it and insert invisible parent's children there // so we delete it and insert invisible parent's children there
nested_rule_indices.forEach(nested_rule_index => selector.children.splice(nested_rule_index, 1, ...parent_children)); nested_rule_indices.forEach((nested_rule_index) =>
selector.children.splice(nested_rule_index, 1, ...parent_children)
);
} }
} }

@ -88,17 +88,18 @@ export class Rule {
* - .b .c * - .b .c
*/ */
if (parent && parent.node.type === 'Rule') { if (parent && parent.node.type === 'Rule') {
this.selectors = /** @type {Rule} **/ (parent) this.selectors = /** @type {Rule} **/ (parent).selectors
.selectors .map((parent_selector) =>
.map(parent_selector => node.prelude.children.map((node) => new Selector(node, stylesheet, parent_selector))) node.prelude.children.map((node) => new Selector(node, stylesheet, parent_selector))
.flat() )
.flat();
} else { } else {
this.selectors = node.prelude.children.map((node) => new Selector(node, stylesheet, null)); this.selectors = node.prelude.children.map((node) => new Selector(node, stylesheet, null));
} }
this.nested_rules = node.block.children this.nested_rules = node.block.children
.filter((node) => node.type === 'Rule') .filter((node) => node.type === 'Rule')
.map(node => new Rule(/** @type {import('#compiler').Css.Rule}*/ (node), stylesheet, this)); .map((node) => new Rule(/** @type {import('#compiler').Css.Rule}*/ (node), stylesheet, this));
this.declarations = node.block.children this.declarations = node.block.children
.filter((node) => node.type === 'Declaration') .filter((node) => node.type === 'Declaration')
.map((node) => new Declaration(/** @type {import('#compiler').Css.Declaration} */ (node))); .map((node) => new Declaration(/** @type {import('#compiler').Css.Declaration} */ (node)));
@ -122,7 +123,9 @@ export class Rule {
// see them in devtools // see them in devtools
if (this.declarations.length === 0) return dev; if (this.declarations.length === 0) return dev;
return [this.selectors.some((s) => s.used), this.nested_rules.some(r => r.is_used(dev))].some(Boolean); return [this.selectors.some((s) => s.used), this.nested_rules.some((r) => r.is_used(dev))].some(
Boolean
);
} }
/** /**
@ -141,7 +144,9 @@ export class Rule {
selector.transform(code, attr, max_amount_class_specificity_increased) selector.transform(code, attr, max_amount_class_specificity_increased)
); );
this.declarations.forEach((declaration) => declaration.transform(code, keyframes)); this.declarations.forEach((declaration) => declaration.transform(code, keyframes));
this.nested_rules.forEach((rule) => rule.transform(code, id, keyframes, max_amount_class_specificity_increased)); this.nested_rules.forEach((rule) =>
rule.transform(code, id, keyframes, max_amount_class_specificity_increased)
);
} }
/** @param {import('../../types.js').ComponentAnalysis} analysis */ /** @param {import('../../types.js').ComponentAnalysis} analysis */
@ -430,7 +435,7 @@ export default class Stylesheet {
const state = { const state = {
/** @type {Atrule | undefined} */ /** @type {Atrule | undefined} */
atrule: undefined, atrule: undefined
}; };
walk(/** @type {import('#compiler').Css.Node} */ (ast), state, { walk(/** @type {import('#compiler').Css.Node} */ (ast), state, {
@ -478,7 +483,7 @@ export default class Stylesheet {
if (rule.nested_rules.length > 0) { if (rule.nested_rules.length > 0) {
// Skip nested rules as they are instantiated in the Rule constructor // Skip nested rules as they are instantiated in the Rule constructor
return node return node;
} }
context.next(); context.next();
} }

@ -89,7 +89,7 @@ export interface Combinator extends BaseNode {
export interface NestingSelector extends BaseNode { export interface NestingSelector extends BaseNode {
type: 'NestedSelector'; type: 'NestedSelector';
name: "&"; name: '&';
} }
export interface Block extends BaseNode { export interface Block extends BaseNode {

Loading…
Cancel
Save