Skip to content

Commit

Permalink
Add prefixed pseudo selectors to unions
Browse files Browse the repository at this point in the history
  • Loading branch information
frenic committed Apr 13, 2018
1 parent b83af7c commit e127b17
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 30 deletions.
28 changes: 26 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3342,27 +3342,51 @@ export type AtRules =
| "@supports"
| "@viewport";

export type AdvancedPseudos = ":-moz-any" | ":-webkit-any" | "::cue" | ":dir" | ":lang" | ":not" | ":nth-child" | ":nth-last-child" | ":nth-last-of-type" | ":nth-of-type";
export type AdvancedPseudos =
| ":-moz-any"
| ":-moz-dir"
| ":-webkit-any"
| "::cue"
| ":dir"
| ":lang"
| ":not"
| ":nth-child"
| ":nth-last-child"
| ":nth-last-of-type"
| ":nth-of-type";

export type SimplePseudos =
| "-moz-full-screen"
| "-webkit-full-screen"
| ":-moz-any-link"
| ":-moz-placeholder"
| ":-moz-read-only"
| ":-moz-read-write"
| ":-ms-fullscreen"
| ":-webkit-any-link"
| "::-moz-placeholder"
| "::-moz-progress-bar"
| "::-moz-range-progress"
| "::-moz-range-thumb"
| "::-moz-range-track"
| "::-moz-selection"
| "::-ms-backdrop"
| "::-ms-browse"
| "::-ms-check"
| "::-ms-clear"
| "::-ms-clear"
| "::-ms-fill"
| "::-ms-fill-lower"
| "::-ms-fill-upper"
| "::-ms-placeholder"
| "::-ms-reveal"
| "::-ms-thumb"
| "::-ms-ticks-after"
| "::-ms-ticks-before"
| "::-ms-tooltip"
| "::-ms-track"
| "::-ms-value"
| "::-webkit-backdrop"
| "::-webkit-placeholder"
| "::-webkit-progress-bar"
| "::-webkit-progress-inner-value"
| "::-webkit-progress-value"
Expand Down
28 changes: 26 additions & 2 deletions index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -3106,27 +3106,51 @@ export type AtRules =
| "@supports"
| "@viewport";

export type AdvancedPseudos = ":-moz-any" | ":-webkit-any" | "::cue" | ":dir" | ":lang" | ":not" | ":nth-child" | ":nth-last-child" | ":nth-last-of-type" | ":nth-of-type";
export type AdvancedPseudos =
| ":-moz-any"
| ":-moz-dir"
| ":-webkit-any"
| "::cue"
| ":dir"
| ":lang"
| ":not"
| ":nth-child"
| ":nth-last-child"
| ":nth-last-of-type"
| ":nth-of-type";

export type SimplePseudos =
| "-moz-full-screen"
| "-webkit-full-screen"
| ":-moz-any-link"
| ":-moz-placeholder"
| ":-moz-read-only"
| ":-moz-read-write"
| ":-ms-fullscreen"
| ":-webkit-any-link"
| "::-moz-placeholder"
| "::-moz-progress-bar"
| "::-moz-range-progress"
| "::-moz-range-thumb"
| "::-moz-range-track"
| "::-moz-selection"
| "::-ms-backdrop"
| "::-ms-browse"
| "::-ms-check"
| "::-ms-clear"
| "::-ms-clear"
| "::-ms-fill"
| "::-ms-fill-lower"
| "::-ms-fill-upper"
| "::-ms-placeholder"
| "::-ms-reveal"
| "::-ms-thumb"
| "::-ms-ticks-after"
| "::-ms-ticks-before"
| "::-ms-tooltip"
| "::-ms-track"
| "::-ms-value"
| "::-webkit-backdrop"
| "::-webkit-placeholder"
| "::-webkit-progress-bar"
| "::-webkit-progress-inner-value"
| "::-webkit-progress-value"
Expand Down
60 changes: 53 additions & 7 deletions src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ export function getCompat(data: { __compat: MDN.Compat }): MDN.Compat {
return data.__compat;
}

function getSupport(support: MDN.Support | MDN.Support[]): MDN.Support[] {
export function getSupport(support: MDN.Support | MDN.Support[]): MDN.Support[] {
return Array.isArray(support) ? support : [support];
}

export function getAtRuleData(name: string): MDN.CompatData | null {
const data = getData('at-rules/' + name);

if (data) {
return data.css['at-rules'][name];
}

return null;
}

export function getPropertyData(name: string): MDN.CompatData | null {
const data = getData('properties/' + name);

Expand All @@ -20,21 +30,21 @@ export function getPropertyData(name: string): MDN.CompatData | null {
return null;
}

export function getTypesData(name: string): MDN.CompatData | null {
const data = getData('types/' + name);
export function getSelectorsData(name: string): MDN.CompatData | null {
const data = getData('selectors/' + name);

if (data) {
return data.css.types[name];
return data.css.selectors[name];
}

return null;
}

export function getAtRuleData(name: string): MDN.CompatData | null {
const data = getData('at-rules/' + name);
export function getTypesData(name: string): MDN.CompatData | null {
const data = getData('types/' + name);

if (data) {
return data.css['at-rules'][name];
return data.css.types[name];
}

return null;
Expand Down Expand Up @@ -169,3 +179,39 @@ export function isAddedBySome(compat: MDN.Compat): boolean {

return false;
}

export function alternativeSelectors(selector: string): string[] {
const alternatives: string[] = [];

// Pseudo without ':'
const colons = ':'.repeat(selector.lastIndexOf(':') + 1);
const name = selector.slice(colons.length);
const compatibilityData = getSelectorsData(name);

if (compatibilityData) {
const compat = getCompat(compatibilityData);

let browser: MDN.Browsers;
for (browser in compat.support) {
const support = compat.support[browser];

for (const version of getSupport(support)) {
// Assume that the version has the property implemented if `null`
const isAdded = !!version.version_added || version.version_added === null;

if (isAdded) {
if (version.prefix) {
alternatives.push(colons + version.prefix + name);
}
if (version.alternative_name) {
alternatives.push(version.alternative_name);
}
}
}
}

return alternatives;
}

return alternatives;
}
2 changes: 1 addition & 1 deletion src/declarator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
vendorPrefixedLonghandProperties,
vendorPrefixedShorthandProperties,
} from './properties';
import { advancedPseudos, simplePseudos } from './pseudos';
import { advancedPseudos, simplePseudos } from './selectors';
import { IDataType, Type, TypeType } from './typer';

export interface IAlias {
Expand Down
18 changes: 0 additions & 18 deletions src/pseudos.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as selectors from 'mdn-data/css/selectors.json';
import { alternativeSelectors } from './compat';
import { addType, ResolvedType, Type } from './typer';

const REGEX_SIMPLE_PSEUDO_SELECTOR = /(?!:?:[\w-]+\()(:?:[\w-]+)/g;
const REGEX_ADVANCED_PSEUDO_SELECTOR = /(:?:[\w-]+)\(/g;

export let simplePseudos: ResolvedType[] = [];
export let advancedPseudos: ResolvedType[] = [];

for (const selector in selectors) {
let match: RegExpMatchArray | null = null;
while ((match = REGEX_SIMPLE_PSEUDO_SELECTOR.exec(selectors[selector].syntax))) {
simplePseudos = addType(simplePseudos, { type: Type.StringLiteral, literal: match[1] });

for (const alternative of alternativeSelectors(match[1])) {
simplePseudos = addType(simplePseudos, { type: Type.StringLiteral, literal: alternative });
}
}
while ((match = REGEX_ADVANCED_PSEUDO_SELECTOR.exec(selectors[selector].syntax))) {
advancedPseudos = addType(advancedPseudos, { type: Type.StringLiteral, literal: match[1] });

for (const alternative of alternativeSelectors(match[1])) {
advancedPseudos = addType(advancedPseudos, { type: Type.StringLiteral, literal: alternative });
}
}
}

0 comments on commit e127b17

Please sign in to comment.