JavaScript implementation of CSS.
const {
CSSPseudoElement,
CSSStyleProperties,
CSSStyleSheet,
StyleSheetList,
install,
matchElementAgainstSelectors,
matchTreesAgainstSelectors,
parseGrammar,
parseListGrammar,
states,
} = require('@cdoublev/css')
// Install CSSOM interfaces on the global object
install(myGlobalObject, { user: { fontSize: 20 } })
// Create a CSSStyleSheet or CSSStyleProperties wrapper
const stylesheet = CSSStyleSheet.create(myGlobalObject, undefined, privateData)
const style = CSSStyleProperties.create(myGlobalObject, undefined, privateData)
// Parse something according to a CSS grammar
const selector = parseGrammar('div', '<complex-selector>')
// Parse a comma-separated list according to a CSS grammar
const selectors = parseListGrammar('div, :invalid', '<complex-selector>')
// Match a tree against a complex selector list
const elements = matchTreesAgainstSelectors([document], selectors.filter(Boolean))
// Add a pointing device
states.get(myGlobalObject).system.pointers.push({ precision: 'fine', motionable: true })
// Change the user preferred font size
states.get(myGlobalObject).user.fontSize = 16install() accepts two optional arguments:
-
a window-like global object (default:
globalThis) that must have the following properties:document,Array,Object,Number,String,TypeError -
an object defining the user agent, system, and preferences (default: see below)
They will be recursively merged with the default values.
Property Default Affected values agent.colorSchemes['light', 'dark']@media/*-color-schemeagent.navigation['back']@media/nav-controlsagent.scripting'enabled'@media/scriptingagent.styleSheetCSSStyleSheet* agent.type'screen'@media(<media-type>)agent.viewport.interfaces.*{ expanded: false, value: 0 }<length>agent.viewport.overflow['scroll', 'scroll']@media/overflow-*agent.viewport.resizabletrue@media/resizableagent.viewport.state'normal'@media/display-statedocument.manifestnull@media/display-modedocument.customPropertiesnew Map<var()>document.environmentVariablesnew Map<env()>document.focusednull:focus-*document.randomCacheNames[]<random()>system.display.blending'opaque'@media/environment-blendingsystem.display.colorIndex0@media/color-indexsystem.display.graphicModetrue@media/gridsystem.display.hdrfalse@media/*-dynamic-rangesystem.display.interlacedfalse@media/scansystem.display.monochrome0@media/monochromesystem.display.segments[1, 1]@media/*-segmentssystem.display.shape'rect'@media/shapesystem.display.update'fast'@media/updatesystem.pointersSee below @media/*-hover,@media/*-pointeruser.colorScheme'light'@media/*-color-scheme,<system-color>user.forcedColorsnull@media/forced-colors,@media/prefers-color-scheme,@media/prefers-contrast,<system-color>user.highContrastfalse@media/prefers-contrastuser.fontSize16font-sizeuser.invertedColorsfalse@media/inverted-colorsuser.reducedDatafalse@media/prefers-reduced-datauser.reducedMotionfalse@media/prefers-reduced-motionuser.reducedTransparencyfalse@media/prefers-reduced-transparencyuser.styleSheetCSSStyleSheet* user.visibleFocusfalse:focus-visibleagent.colorSchemesrepresents the color schemes supported by the user agent. The first entry is the default color scheme.agent.styleSheetanduser.styleSheetare parsed intoCSSStyleSheets.agent.viewport.interfacesrepresents the user agent interfaces affecting the viewport area when expanded. It must be an object with the propertiesbottom,left,right,top, assigned{ expanded: Boolean, value: Number }.agent.viewport.overflowrepresents the overflow behavior in the inline and block direction, respectively.document.manifest(parsed JSON) must be defined only when emulating a Web application environment.document.customPropertiesrepresents custom properties registered withCSS.registerProperty().document.environmentVariablesrepresents environment variables registered with an interface that has not yet been defined in specifications.document.fontFacesrepresents font faces registered with@font-face.document.randomCacheNamesrepresentsrandom()base values associated with the data that determines when they are shared.system.display.segmentsrepresents the number of horizontal and vertical segments, respectively.system.pointersmust be a list of pointer definitions as{ motionable: Boolean, precision: 'coarse|fine' }. The first entry represents the primary pointer, which defaults to{ motionable: true, precision: 'fine' }.user.colorSchemerepresents the user's preferred color scheme, which is assumed to be included inagent.colorSchemes. Prefix the color scheme withoverriding-to indicate an overriding preference.user.forcedColorsrepresents the colors from the high contrast mode in Windows or Firefox. It must be an object mapping a<system-color>keyword in lowercase to a resolved legacy<rgb()>. When it is defined,user.highContrastmust not be defined: it will be evaluated based on the WCAG contrast ratio betweencanvasandcanvastext, which are assumed to always be defined.user.highContrastcorresponds to the high contrast mode in macOS, which does not activate the forced color mode, souser.forcedColorsis assumed to be undefined.
CSSPseudoElement, CSSStyleSheet, CSSStyleProperties, StyleSheetList, are webidl2js wrappers intended to implement:
DocumentOrShadowRoot.styleSheets- create a CSS style sheet: when processing the content of
HTMLStyleElementor the resource referenced byHTMLLinkElementor an HTTPLinkheader withrel="stylesheet" ElementCSSInlineStyle.styleElement.pseudo()andWindow.getComputedStyle()
CSSPseudoElement accepts privateProperties that correspond to its attributes.
CSSStyleSheet accepts the following privateProperties:
- CSS rules:
rules(StringorReadableStream) - alternate flag:
alternate(Boolean, optional, default:false) - disabled flag:
disabled(Boolean, optional, default:false) - location:
location(String, optional, default:null) - media:
media(StringorMediaList) - origin-clean flag:
originClean(Boolean) - owner CSS rule:
ownerRule(CSSRule, optional, default:null) - owner node:
ownerNode(HTMLElement) - parent CSS style sheet:
parentStyleSheet(CSSStyleSheet, optional, default:null) - title:
title(String, optional, default:'')
CSSStyleProperties accepts the following privateProperties:
- computed flag:
computed(Boolean, optional, default:false) - declarations:
declarations([Declaration], optional, default:[]) - owner node:
ownerNode(HTMLElement, optional, default:null) - parent CSS rule:
parentRule(CSSRule, optional, default:null)
Declaration must be a plain object with the following properties:
name:Stringvalue:Object|[Object](parsed value)important:Boolean(optional, default:false)
parseGrammar() and parseListGrammar() are implementations of parse something according to a CSS grammar and parse a comma-separated list according to a CSS grammar, which take 3 arguments:
input:Stringdefinition:String(a CSS value definition)context:Element(optional)
matchElementAgainstSelectors() and matchTreesAgainstSelectors() are intended to implement:
element.closest()withmatchElementAgainstSelectors(ancestor, selectors, { scopes: { inclusive: true, roots: [element] } })element.matches()withmatchElementAgainstSelectors(element, selectors, { scopes: { inclusive: true, roots: [element] } })element.querySelector()withmatchTreesAgainstSelectors([document], selectors, { scopes: { roots: [element] } }, { first: true })element.querySelectorAll()withmatchTreesAgainstSelectors([document], selectors, { scopes: { roots: [element] } })
selectors is expected to be a <complex-selector-list> or <complex-real-selector-list>.