-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prefixed pseudo selectors to unions
- Loading branch information
Showing
6 changed files
with
133 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as selectors from 'mdn-data/css/selectors.json'; | ||
import { compatNames, getCompat, getSelectorsData, getSupport } 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 }); | ||
} | ||
} | ||
} | ||
|
||
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; | ||
} |