Class StyleSheetResolve
Implements
- Iterable<[string, { [key: string]: string }]>
Constructors
constructor
- new StyleSheetResolve(): StyleSheetResolve
Instantiate an empty
StyleSheetResolve
instance.Returns StyleSheetResolve
Accessors
frozen
- get frozen(): boolean
Returns boolean
Current frozen state; when true no more modifications are possible.
size
- get size(): number
Returns number
Returns the size / count of selector properties tracked.
Methods
[iterator]
- "[iterator]"(): MapIterator<[string, { [key: string]: string }]>
Allows usage in
for of
loops directly.Returns MapIterator<[string, { [key: string]: string }]>
Entries Map iterator.
clear
clone
- clone(): StyleSheetResolve
Clones this instance returning a new
StyleSheetResolve
instance with a copy of the data.Returns StyleSheetResolve
Cloned instance.
delete
entries
- entries(): MapIterator<[string, { [key: string]: string }]>
Entries iterator of selector / style properties objects.
Returns MapIterator<[string, { [key: string]: string }]>
Tracked CSS selector key / value iterator.
freeze
get
- get(
selector: string | Iterable<string>,
options?: StyleSheetResolve.Options.Get,
): { [key: string]: string } Gets all properties associated with the given selector(s). You may combine multiple selectors for a combined result. You may also provide additional selectors as the
resolve
option to substitute any CSS variables in the target selector(s).Parameters
- selector: string | Iterable<string>
A selector or list of selectors to retrieve.
Optional
options: StyleSheetResolve.Options.GetOptions.
Returns { [key: string]: string }
Style properties object or undefined.
- selector: string | Iterable<string>
getProperty
- getProperty(
selector: string | Iterable<string>,
property: string,
options?: StyleSheetResolve.Options.Get,
): string Gets a specific property value from the given
selector
andproperty
key.Parameters
- selector: string | Iterable<string>
A selector or list of selectors to retrieve.
- property: string
Specific property to locate.
Optional
options: StyleSheetResolve.Options.GetOptions.
Returns string
Style property value.
- selector: string | Iterable<string>
has
keys
- keys(): MapIterator<string>
Returns MapIterator<string>
Tracked CSS selector keys iterator.
merge
- merge(source: StyleSheetResolve, options?: Merge): this
Merges selectors and style properties from another StyleSheetResolve instance into this one. By default, the source of the merge overrides existing properties. You may choose to preserve existing values along with specifying exact selector matches.
Parameters
- source: StyleSheetResolve
Another instance to merge from.
Optional
options: MergeOptions.
Returns this
This instance.
- source: StyleSheetResolve
parse
- parse(
styleSheetOrMap: CSSStyleSheet | Map<string, { [key: string]: string }>,
options?: Parse,
): this Clears existing stylesheet mapping and parses the given stylesheet or Map.
Parameters
- styleSheetOrMap: CSSStyleSheet | Map<string, { [key: string]: string }>
The stylesheet element to parse or an existing parsed stylesheet Map.
Optional
options: ParseOptions for parsing stylesheet.
Returns this
This instance.
- styleSheetOrMap: CSSStyleSheet | Map<string, { [key: string]: string }>
set
Static
parse
- parse(
styleSheetOrMap: CSSStyleSheet | Map<string, { [key: string]: string }>,
options?: Parse,
): StyleSheetResolve Parse a CSSStyleSheet instance with the given options or accept a pre-filled Map generating a new
StyleSheetResolve
instance.Parameters
- styleSheetOrMap: CSSStyleSheet | Map<string, { [key: string]: string }>
The stylesheet instance to parse or an existing parsed stylesheet Map.
Optional
options: ParseOptions for parsing stylesheet.
Returns StyleSheetResolve
New instance with the given parsed data.
- styleSheetOrMap: CSSStyleSheet | Map<string, { [key: string]: string }>
Dynamically parses and indexes a
CSSStyleSheet
at runtime, exposing a selector-to-style mapping by individual selector parts. CSS variable resolution is also possible which enables the ability to flatten and resolve complex nestedvar(--...)
chains defined across multiple selectors and layers.When retrieving specific selector styles via StyleSheetResolve.get and StyleSheetResolve.getProperty it is possible to provide additional parent selectors that may define scoped CSS variables. These parent variable definitions will be substituted in the target selector data allowing specific element scoping of CSS variables to be flattened.
Current fallback support includes recursive var(--a, var(--b, ...)) chains with graceful partial substitution if some variables are undefined. This maintains correctness without introducing ambiguity or needing a complete AST based parser.
By default, when parsing CSSStyleSheet instances relative URL rewriting occurs converting
url(...)
references to absolute paths based on theCSSStyleSheet.href
or thebaseHref
parse option for inline / synthetic CSSStyleSheets. You may turn off relative URL rewriting via setting theurlRewrite
parse option tofalse
.By default, simple media queries /
@media
rules are parsed when all conditions areprefers-*
features and the media query matches at runtime viawindow.matchMedia(...)
. Mixed conditions (IE with screen, width, etc.) are ignored by design. Only direct style rules under a media query are parsed. You may turn off media query parsing via setting themediaQuery
parse option to false.The goal of this implementation is to realize a regex-based parser with small code size, minimal memory footprint, speed, and reasonable accuracy.
Core features:
@layer
blocks..get()
and.getProperty()
.url(...)
references to absolute paths.Parse Options:
href
for inline / synthetic CSSStyleSheets being processed viabaseHref
option.excludeSelectorParts
option.includeCSSLayers
option.includeSelectorPartSet
option.urlRewrite
option tofalse
.Access Options:
camelCase
option.depth
option.resolve
option.warnCycles
option.warnResolve
option.Example