Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store style rule type in abstract class #1539

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 35 additions & 53 deletions docs/review/api/alfa-dom.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,22 @@ export namespace Comment {
}

// @public (undocumented)
export abstract class ConditionRule extends GroupingRule {
protected constructor(condition: string, rules: Array<Rule>);
export abstract class ConditionRule<T extends string = string> extends GroupingRule<T> {
protected constructor(type: T, condition: string, rules: Array<Rule>);
// (undocumented)
get condition(): string;
// (undocumented)
protected readonly _condition: string;
// (undocumented)
abstract toJSON(): ConditionRule.JSON;
toJSON(): ConditionRule.JSON<T>;
}

// @public (undocumented)
export namespace ConditionRule {
// (undocumented)
export function isConditionRule(value: unknown): value is ConditionRule;
// (undocumented)
export interface JSON extends GroupingRule.JSON {
export interface JSON<T extends string = string> extends GroupingRule.JSON<T> {
// (undocumented)
condition: string;
}
Expand Down Expand Up @@ -362,7 +362,7 @@ export namespace Element {
}

// @public (undocumented)
export class FontFaceRule extends Rule {
export class FontFaceRule extends Rule<"font-face"> {
// (undocumented)
static of(declarations: Iterable<Declaration>): FontFaceRule;
// (undocumented)
Expand All @@ -380,13 +380,9 @@ export namespace FontFaceRule {
// (undocumented)
export function isFontFaceRule(value: unknown): value is FontFaceRule;
// (undocumented)
export interface JSON {
// (undocumented)
[key: string]: json.JSON;
export interface JSON extends Rule.JSON<"font-face"> {
// (undocumented)
style: Block.JSON;
// (undocumented)
type: "font-face";
}
}

Expand Down Expand Up @@ -418,26 +414,26 @@ export namespace Fragment {
}

// @public (undocumented)
export abstract class GroupingRule extends Rule {
protected constructor(rules: Array<Rule>);
export abstract class GroupingRule<T extends string = string> extends Rule<T> {
protected constructor(type: T, rules: Array_2<Rule>);
// (undocumented)
children(): Iterable<Rule>;
// (undocumented)
get rules(): Iterable<Rule>;
// (undocumented)
protected readonly _rules: Array<Rule>;
protected readonly _rules: Array_2<Rule>;
// (undocumented)
abstract toJSON(): GroupingRule.JSON;
toJSON(): GroupingRule.JSON<T>;
}

// @public (undocumented)
export namespace GroupingRule {
// (undocumented)
export function isGroupingRule(value: unknown): value is GroupingRule;
// (undocumented)
export interface JSON extends Rule.JSON {
export interface JSON<T extends string = string> extends Rule.JSON<T> {
// (undocumented)
rules: Array<Rule.JSON>;
rules: Array_2<Rule.JSON>;
}
}

Expand Down Expand Up @@ -488,7 +484,7 @@ export namespace h {
}

// @public (undocumented)
export class ImportRule extends ConditionRule {
export class ImportRule extends ConditionRule<"import"> {
// (undocumented)
get href(): string;
// (undocumented)
Expand All @@ -512,11 +508,9 @@ export namespace ImportRule {
// (undocumented)
export function isImportRule(value: unknown): value is ImportRule;
// (undocumented)
export interface JSON extends ConditionRule.JSON {
export interface JSON extends ConditionRule.JSON<"import"> {
// (undocumented)
href: string;
// (undocumented)
type: "import";
}
}

Expand Down Expand Up @@ -550,7 +544,7 @@ export namespace jsx {
}

// @public (undocumented)
export class KeyframeRule extends Rule {
export class KeyframeRule extends Rule<"keyframe"> {
// (undocumented)
get key(): string;
// (undocumented)
Expand All @@ -570,18 +564,16 @@ export namespace KeyframeRule {
// (undocumented)
export function isKeyframeRule(value: unknown): value is KeyframeRule;
// (undocumented)
export interface JSON extends Rule.JSON {
export interface JSON extends Rule.JSON<"keyframe"> {
// (undocumented)
key: string;
// (undocumented)
style: Block.JSON;
// (undocumented)
type: "keyframe";
}
}

// @public (undocumented)
export class KeyframesRule extends GroupingRule {
export class KeyframesRule extends GroupingRule<"keyframes"> {
// (undocumented)
get name(): string;
// (undocumented)
Expand All @@ -599,16 +591,14 @@ export namespace KeyframesRule {
// (undocumented)
export function isKeyframesRule(value: unknown): value is KeyframesRule;
// (undocumented)
export interface JSON extends GroupingRule.JSON {
export interface JSON extends GroupingRule.JSON<"keyframes"> {
// (undocumented)
name: string;
// (undocumented)
type: "keyframes";
}
}

// @public (undocumented)
export class MediaRule extends ConditionRule {
export class MediaRule extends ConditionRule<"media"> {
// (undocumented)
static of(condition: string, rules: Iterable_2<Rule>): MediaRule;
// (undocumented)
Expand All @@ -626,9 +616,7 @@ export namespace MediaRule {
// (undocumented)
export function isMediaRule(value: unknown): value is MediaRule;
// (undocumented)
export interface JSON extends ConditionRule.JSON {
// (undocumented)
type: "media";
export interface JSON extends ConditionRule.JSON<"media"> {
}
}

Expand All @@ -655,7 +643,7 @@ export namespace Namespace {
}

// @public (undocumented)
export class NamespaceRule extends Rule {
export class NamespaceRule extends Rule<"namespace"> {
// (undocumented)
get namespace(): string;
// (undocumented)
Expand All @@ -675,13 +663,11 @@ export namespace NamespaceRule {
// (undocumented)
export function isNamespaceRule(value: unknown): value is NamespaceRule;
// (undocumented)
export interface JSON extends Rule.JSON {
export interface JSON extends Rule.JSON<"namespace"> {
// (undocumented)
namespace: string;
// (undocumented)
prefix: string | null;
// (undocumented)
type: "namespace";
}
}

Expand Down Expand Up @@ -865,7 +851,7 @@ export namespace Node {
}

// @public (undocumented)
export class PageRule extends Rule {
export class PageRule extends Rule<"page"> {
// (undocumented)
static of(selector: string, declarations: Iterable<Declaration>): PageRule;
// (undocumented)
Expand All @@ -885,13 +871,11 @@ export namespace PageRule {
// (undocumented)
export function isPageRule(value: unknown): value is PageRule;
// (undocumented)
export interface JSON extends Rule.JSON {
export interface JSON extends Rule.JSON<"page"> {
// (undocumented)
selector: string;
// (undocumented)
style: Block.JSON;
// (undocumented)
type: "page";
}
}

Expand All @@ -908,8 +892,8 @@ export namespace Query {
}

// @public (undocumented)
export abstract class Rule implements Equatable, Serializable {
protected constructor();
export abstract class Rule<T extends string = string> implements Equatable, Serializable {
protected constructor(type: T);
// (undocumented)
ancestors(): Iterable<Rule>;
// @internal (undocumented)
Expand All @@ -933,7 +917,9 @@ export abstract class Rule implements Equatable, Serializable {
// (undocumented)
protected _parent: Option<Rule>;
// (undocumented)
abstract toJSON(): Rule.JSON;
toJSON(): Rule.JSON<T>;
// (undocumented)
get type(): T;
}

// @public (undocumented)
Expand Down Expand Up @@ -961,11 +947,11 @@ export namespace Rule {
// @internal (undocumented)
export function fromRule(json: JSON): Trampoline<Rule>;
// (undocumented)
export interface JSON {
export interface JSON<T extends string = string> {
// (undocumented)
[key: string]: json.JSON;
// (undocumented)
type: string;
type: T;
}
}

Expand Down Expand Up @@ -1095,7 +1081,7 @@ export namespace Slotable {
}

// @public (undocumented)
export class StyleRule extends Rule {
export class StyleRule extends Rule<"style"> {
// (undocumented)
get hint(): boolean;
// (undocumented)
Expand All @@ -1117,18 +1103,16 @@ export namespace StyleRule {
// (undocumented)
export function isStyleRule(value: unknown): value is StyleRule;
// (undocumented)
export interface JSON extends Rule.JSON {
export interface JSON extends Rule.JSON<"style"> {
// (undocumented)
selector: string;
// (undocumented)
style: Block.JSON;
// (undocumented)
type: "style";
}
}

// @public (undocumented)
export class SupportsRule extends ConditionRule {
export class SupportsRule extends ConditionRule<"supports"> {
// (undocumented)
static of(condition: string, rules: Iterable<Rule>): SupportsRule;
// (undocumented)
Expand All @@ -1144,9 +1128,7 @@ export namespace SupportsRule {
// (undocumented)
export function isSupportsRue(value: unknown): value is SupportsRule;
// (undocumented)
export interface JSON extends ConditionRule.JSON {
// (undocumented)
type: "supports";
export interface JSON extends ConditionRule.JSON<"supports"> {
}
}

Expand Down
21 changes: 16 additions & 5 deletions packages/alfa-dom/src/style/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ import {
/**
* @public
*/
export abstract class Rule implements Equatable, Serializable {
export abstract class Rule<T extends string = string>
implements Equatable, Serializable
{
protected _owner: Option<Sheet> = None;
protected _parent: Option<Rule> = None;
private readonly _type: T;

protected constructor() {}
protected constructor(type: T) {
this._type = type;
}

public get type(): T {
return this._type;
}

public get owner(): Option<Sheet> {
return this._owner;
Expand Down Expand Up @@ -61,7 +70,9 @@ export abstract class Rule implements Equatable, Serializable {
return value === this;
}

public abstract toJSON(): Rule.JSON;
public toJSON(): Rule.JSON<T> {
return { type: this._type };
}

/**
* @internal
Expand Down Expand Up @@ -94,9 +105,9 @@ export abstract class Rule implements Equatable, Serializable {
* @public
*/
export namespace Rule {
export interface JSON {
export interface JSON<T extends string = string> {
[key: string]: json.JSON;
type: string;
type: T;
}

export function from(json: StyleRule.JSON): StyleRule;
Expand Down
18 changes: 13 additions & 5 deletions packages/alfa-dom/src/style/rule/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { GroupingRule } from "./grouping";
/**
* @public
*/
export abstract class ConditionRule extends GroupingRule {
export abstract class ConditionRule<
T extends string = string,
> extends GroupingRule<T> {
protected readonly _condition: string;

protected constructor(condition: string, rules: Array<Rule>) {
super(rules);
protected constructor(type: T, condition: string, rules: Array<Rule>) {
super(type, rules);

this._condition = condition;
}
Expand All @@ -17,14 +19,20 @@ export abstract class ConditionRule extends GroupingRule {
return this._condition;
}

public abstract toJSON(): ConditionRule.JSON;
public toJSON(): ConditionRule.JSON<T> {
return {
...super.toJSON(),
condition: this._condition,
};
}
}

/**
* @public
*/
export namespace ConditionRule {
export interface JSON extends GroupingRule.JSON {
export interface JSON<T extends string = string>
extends GroupingRule.JSON<T> {
condition: string;
}

Expand Down
Loading