2017-12-26 20:39:52 +00:00
|
|
|
/*
|
|
|
|
Modded by tophf <github.com/tophf>
|
|
|
|
========== Original disclaimer:
|
|
|
|
|
|
|
|
Parser-Lib
|
|
|
|
Copyright (c) 2009-2016 Nicholas C. Zakas. All rights reserved.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
/* eslint-disable class-methods-use-this */
|
|
|
|
|
|
|
|
self.parserlib = (() => {
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#region Properties
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2021-08-20 10:01:56 +00:00
|
|
|
// Global keywords that can be set for any property are conveniently listed in `all` prop:
|
|
|
|
// https://drafts.csswg.org/css-cascade/#all-shorthand
|
|
|
|
const GlobalKeywords = ['initial', 'inherit', 'revert', 'unset'];
|
2022-07-01 07:26:46 +00:00
|
|
|
const rxGlobalKeyword = new RegExp(`^(${GlobalKeywords.join('|')})$`, 'i');
|
|
|
|
/** @param {string} [ex] |-separated exceptions
|
|
|
|
* @param {RegExp} [re] custom regexp check
|
|
|
|
* @param {function} [alt] alternative check */
|
|
|
|
const customIdentChecker = (ex, re, alt) =>
|
|
|
|
(re = new RegExp(`^(?!(default|${ex ? ex + '|' : ''}${GlobalKeywords.join('|')})$)${re ? re.source : ''}`, 'i')) &&
|
|
|
|
(p => p.tokenType === Tokens.IDENT && re.test(p.value) || (!alt || alt(p))); // eslint-disable-line no-use-before-define
|
2021-08-20 10:01:56 +00:00
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
const Properties = {
|
2021-08-03 16:45:44 +00:00
|
|
|
'accent-color': 'auto | <color>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'align-items': 'normal | stretch | <baseline-position> | [ <overflow-position>? <self-position> ]',
|
|
|
|
'align-content': '<align-content>',
|
|
|
|
'align-self': '<align-self>',
|
2021-08-20 10:01:56 +00:00
|
|
|
'all': GlobalKeywords.join(' | '),
|
2020-11-25 18:01:36 +00:00
|
|
|
'alignment-adjust': 'auto | baseline | before-edge | text-before-edge | middle | central | ' +
|
|
|
|
'after-edge | text-after-edge | ideographic | alphabetic | hanging | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'mathematical | <len-pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'alignment-baseline': 'auto | baseline | use-script | before-edge | text-before-edge | ' +
|
|
|
|
'after-edge | text-after-edge | central | middle | ideographic | alphabetic | ' +
|
|
|
|
'hanging | mathematical',
|
|
|
|
'animation': '[ <time> || <single-timing-function> || <time> || [ infinite | <number> ] || ' +
|
|
|
|
'<single-animation-direction> || <single-animation-fill-mode> || ' +
|
|
|
|
'[ running | paused ] || [ none | <ident> | <string> ] ]#',
|
|
|
|
'animation-delay': '<time>#',
|
|
|
|
'animation-direction': '<single-animation-direction>#',
|
|
|
|
'animation-duration': '<time>#',
|
|
|
|
'animation-fill-mode': '<single-animation-fill-mode>#',
|
2017-12-26 20:39:52 +00:00
|
|
|
'animation-iteration-count': '[ <number> | infinite ]#',
|
2022-07-01 07:26:46 +00:00
|
|
|
'animation-name': '[ none | <keyframes-name> ]#',
|
2020-11-25 18:01:36 +00:00
|
|
|
'animation-play-state': '[ running | paused ]#',
|
2017-12-26 20:39:52 +00:00
|
|
|
'animation-timing-function': '<single-timing-function>#',
|
2020-10-24 10:18:17 +00:00
|
|
|
'appearance': 'none | auto',
|
|
|
|
'-moz-appearance':
|
|
|
|
'none | button | button-arrow-down | button-arrow-next | button-arrow-previous | ' +
|
|
|
|
'button-arrow-up | button-bevel | button-focus | caret | checkbox | checkbox-container | ' +
|
|
|
|
'checkbox-label | checkmenuitem | dualbutton | groupbox | listbox | listitem | ' +
|
|
|
|
'menuarrow | menubar | menucheckbox | menuimage | menuitem | menuitemtext | menulist | ' +
|
|
|
|
'menulist-button | menulist-text | menulist-textfield | menupopup | menuradio | ' +
|
|
|
|
'menuseparator | meterbar | meterchunk | progressbar | progressbar-vertical | ' +
|
|
|
|
'progresschunk | progresschunk-vertical | radio | radio-container | radio-label | ' +
|
|
|
|
'radiomenuitem | range | range-thumb | resizer | resizerpanel | scale-horizontal | ' +
|
|
|
|
'scalethumbend | scalethumb-horizontal | scalethumbstart | scalethumbtick | ' +
|
|
|
|
'scalethumb-vertical | scale-vertical | scrollbarbutton-down | scrollbarbutton-left | ' +
|
|
|
|
'scrollbarbutton-right | scrollbarbutton-up | scrollbarthumb-horizontal | ' +
|
|
|
|
'scrollbarthumb-vertical | scrollbartrack-horizontal | scrollbartrack-vertical | ' +
|
|
|
|
'searchfield | separator | sheet | spinner | spinner-downbutton | spinner-textfield | ' +
|
|
|
|
'spinner-upbutton | splitter | statusbar | statusbarpanel | tab | tabpanel | tabpanels | ' +
|
|
|
|
'tab-scroll-arrow-back | tab-scroll-arrow-forward | textfield | textfield-multiline | ' +
|
|
|
|
'toolbar | toolbarbutton | toolbarbutton-dropdown | toolbargripper | toolbox | tooltip | ' +
|
|
|
|
'treeheader | treeheadercell | treeheadersortarrow | treeitem | treeline | treetwisty | ' +
|
|
|
|
'treetwistyopen | treeview | -moz-mac-unified-toolbar | -moz-win-borderless-glass | ' +
|
|
|
|
'-moz-win-browsertabbar-toolbox | -moz-win-communicationstext | ' +
|
|
|
|
'-moz-win-communications-toolbox | -moz-win-exclude-glass | -moz-win-glass | ' +
|
|
|
|
'-moz-win-mediatext | -moz-win-media-toolbox | -moz-window-button-box | ' +
|
|
|
|
'-moz-window-button-box-maximized | -moz-window-button-close | ' +
|
|
|
|
'-moz-window-button-maximize | -moz-window-button-minimize | -moz-window-button-restore | ' +
|
|
|
|
'-moz-window-frame-bottom | -moz-window-frame-left | -moz-window-frame-right | ' +
|
|
|
|
'-moz-window-titlebar | -moz-window-titlebar-maximized',
|
|
|
|
'-ms-appearance':
|
|
|
|
'none | icon | window | desktop | workspace | document | tooltip | dialog | button | ' +
|
|
|
|
'push-button | hyperlink | radio | radio-button | checkbox | menu-item | tab | menu | ' +
|
|
|
|
'menubar | pull-down-menu | pop-up-menu | list-menu | radio-group | checkbox-group | ' +
|
|
|
|
'outline-tree | range | field | combo-box | signature | password | normal',
|
|
|
|
'-webkit-appearance':
|
|
|
|
'auto | none | button | button-bevel | caps-lock-indicator | caret | checkbox | ' +
|
|
|
|
'default-button | listbox | listitem | media-fullscreen-button | media-mute-button | ' +
|
|
|
|
'media-play-button | media-seek-back-button | media-seek-forward-button | media-slider | ' +
|
|
|
|
'media-sliderthumb | menulist | menulist-button | menulist-text | menulist-textfield | ' +
|
|
|
|
'push-button | radio | searchfield | searchfield-cancel-button | searchfield-decoration | ' +
|
|
|
|
'searchfield-results-button | searchfield-results-decoration | slider-horizontal | ' +
|
|
|
|
'slider-vertical | sliderthumb-horizontal | sliderthumb-vertical | square-button | ' +
|
|
|
|
'textarea | textfield | scrollbarbutton-down | scrollbarbutton-left | ' +
|
|
|
|
'scrollbarbutton-right | scrollbarbutton-up | scrollbargripper-horizontal | ' +
|
|
|
|
'scrollbargripper-vertical | scrollbarthumb-horizontal | scrollbarthumb-vertical | ' +
|
|
|
|
'scrollbartrack-horizontal | scrollbartrack-vertical',
|
|
|
|
'-o-appearance':
|
|
|
|
'none | window | desktop | workspace | document | tooltip | dialog | button | ' +
|
|
|
|
'push-button | hyperlink | radio | radio-button | checkbox | menu-item | tab | menu | ' +
|
|
|
|
'menubar | pull-down-menu | pop-up-menu | list-menu | radio-group | checkbox-group | ' +
|
|
|
|
'outline-tree | range | field | combo-box | signature | password | normal',
|
2021-09-07 09:36:36 +00:00
|
|
|
'aspect-ratio': 'auto || [ <num0+> / <num0+> ]',
|
2017-12-26 20:39:52 +00:00
|
|
|
'azimuth': '<azimuth>',
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
'backdrop-filter': '<filter-function-list> | none',
|
|
|
|
'backface-visibility': 'visible | hidden',
|
|
|
|
'background': '[ <bg-layer> , ]* <final-bg-layer>',
|
|
|
|
'background-attachment': '<attachment>#',
|
|
|
|
'background-blend-mode': '<blend-mode>',
|
|
|
|
'background-clip': '[ <box> | text ]#',
|
|
|
|
'background-color': '<color>',
|
|
|
|
'background-image': '<bg-image>#',
|
|
|
|
'background-origin': '<box>#',
|
|
|
|
'background-position': '<bg-position>#',
|
2021-09-07 09:36:36 +00:00
|
|
|
'background-position-x': '[ center | [ left | right ]? <len-pct>? ]#',
|
|
|
|
'background-position-y': '[ center | [ top | bottom ]? <len-pct>? ]#',
|
2020-11-25 18:01:36 +00:00
|
|
|
'background-repeat': '<repeat-style>#',
|
|
|
|
'background-size': '<bg-size>#',
|
2021-09-07 09:36:36 +00:00
|
|
|
'baseline-shift': 'baseline | sub | super | <len-pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'behavior': 1,
|
|
|
|
'binding': 1,
|
|
|
|
'bleed': '<length>',
|
|
|
|
'block-size': '<width>',
|
|
|
|
'bookmark-label': '<content-list>',
|
|
|
|
'bookmark-level': 'none | <integer>',
|
|
|
|
'bookmark-state': 'open | closed',
|
|
|
|
'bookmark-target': 'none | <uri>',
|
|
|
|
'border-boundary': 'none | parent | display',
|
|
|
|
'border-collapse': 'collapse | separate',
|
|
|
|
'border-image': '[ none | <image> ] || <border-image-slice> ' +
|
|
|
|
'[ / <border-image-width> | / <border-image-width>? / <border-image-outset> ]? || ' +
|
|
|
|
'<border-image-repeat>',
|
|
|
|
'border-image-outset': '<border-image-outset>',
|
|
|
|
'border-image-repeat': '<border-image-repeat>',
|
|
|
|
'border-image-slice': '<border-image-slice>',
|
|
|
|
'border-image-source': '<image> | none',
|
|
|
|
'border-image-width': '<border-image-width>',
|
|
|
|
'border-spacing': '<length>{1,2}',
|
2021-01-01 16:43:28 +00:00
|
|
|
|
2021-09-07 09:36:36 +00:00
|
|
|
'border-bottom-left-radius': '<len-pct>{1,2}',
|
|
|
|
'border-bottom-right-radius': '<len-pct>{1,2}',
|
|
|
|
'border-end-end-radius': '<len-pct>{1,2}',
|
|
|
|
'border-end-start-radius': '<len-pct>{1,2}',
|
2021-01-01 16:43:28 +00:00
|
|
|
'border-radius': '<border-radius>',
|
2021-09-07 09:36:36 +00:00
|
|
|
'border-start-end-radius': '<len-pct>{1,2}',
|
|
|
|
'border-start-start-radius': '<len-pct>{1,2}',
|
|
|
|
'border-top-left-radius': '<len-pct>{1,2}',
|
|
|
|
'border-top-right-radius': '<len-pct>{1,2}',
|
2021-01-01 16:43:28 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
'bottom': '<width>',
|
|
|
|
'box-decoration-break': 'slice | clone',
|
|
|
|
'box-shadow': '<box-shadow>',
|
|
|
|
'box-sizing': 'content-box | border-box',
|
|
|
|
'break-after': 'auto | always | avoid | left | right | page | column | avoid-page | avoid-column',
|
|
|
|
'break-before': 'auto | always | avoid | left | right | page | column | avoid-page | avoid-column',
|
|
|
|
'break-inside': 'auto | avoid | avoid-page | avoid-column',
|
|
|
|
'-moz-box-align': 1,
|
|
|
|
'-moz-box-decoration-break': 1,
|
|
|
|
'-moz-box-direction': 1,
|
|
|
|
'-moz-box-flex': 1,
|
|
|
|
'-moz-box-flex-group': 1,
|
|
|
|
'-moz-box-lines': 1,
|
|
|
|
'-moz-box-ordinal-group': 1,
|
|
|
|
'-moz-box-orient': 1,
|
|
|
|
'-moz-box-pack': 1,
|
|
|
|
'-o-box-decoration-break': 1,
|
|
|
|
'-webkit-box-align': 1,
|
|
|
|
'-webkit-box-decoration-break': 1,
|
|
|
|
'-webkit-box-direction': 1,
|
|
|
|
'-webkit-box-flex': 1,
|
|
|
|
'-webkit-box-flex-group': 1,
|
|
|
|
'-webkit-box-lines': 1,
|
|
|
|
'-webkit-box-ordinal-group': 1,
|
|
|
|
'-webkit-box-orient': 1,
|
|
|
|
'-webkit-box-pack': 1,
|
|
|
|
|
|
|
|
'caret-color': 'auto | <color>',
|
|
|
|
'caption-side': 'top | bottom | inline-start | inline-end',
|
|
|
|
'clear': 'none | right | left | both | inline-start | inline-end',
|
2022-06-24 05:25:33 +00:00
|
|
|
'clip': '<rect> | auto',
|
2020-11-25 18:01:36 +00:00
|
|
|
'clip-path': '<clip-source> | <clip-path> | none',
|
|
|
|
'clip-rule': 'nonzero | evenodd',
|
|
|
|
'color': '<color>',
|
2021-02-01 15:51:24 +00:00
|
|
|
'color-adjust': 'economy | exact',
|
2020-11-25 18:01:36 +00:00
|
|
|
'color-interpolation': 'auto | sRGB | linearRGB',
|
2017-12-26 20:39:52 +00:00
|
|
|
'color-interpolation-filters': 'auto | sRGB | linearRGB',
|
2020-11-25 18:01:36 +00:00
|
|
|
'color-profile': 1,
|
|
|
|
'color-rendering': 'auto | optimizeSpeed | optimizeQuality',
|
2022-01-10 04:30:22 +00:00
|
|
|
'color-scheme': 'normal | [ light | dark | <ident> ]+ && only?',
|
2020-11-25 18:01:36 +00:00
|
|
|
'column-count': '<integer> | auto',
|
|
|
|
'column-fill': 'auto | balance',
|
|
|
|
'column-gap': '<column-gap>',
|
|
|
|
'column-rule': '<border-shorthand>',
|
2017-12-26 20:39:52 +00:00
|
|
|
'column-rule-color': '<color>',
|
|
|
|
'column-rule-style': '<border-style>',
|
|
|
|
'column-rule-width': '<border-width>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'column-span': 'none | all',
|
|
|
|
'column-width': '<length> | auto',
|
|
|
|
'columns': 1,
|
|
|
|
'contain': 'none | strict | content | [ size || layout || style || paint ]',
|
2020-10-24 10:18:17 +00:00
|
|
|
'contain-intrinsic-size': 'none | <length>{1,2}',
|
2020-11-25 18:01:36 +00:00
|
|
|
'content': 'normal | none | <content-list> [ / <string> ]?',
|
2020-09-25 10:51:03 +00:00
|
|
|
'content-visibility': 'visible | auto | hidden',
|
2020-10-24 10:18:17 +00:00
|
|
|
'counter-increment': '<counter>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'counter-reset': '<counter>',
|
|
|
|
'counter-set': '<counter>',
|
|
|
|
'cue': 'cue-after | cue-before',
|
|
|
|
'cue-after': 1,
|
|
|
|
'cue-before': 1,
|
|
|
|
'cursor': '[ <uri> [ <number> <number> ]? , ]* ' +
|
|
|
|
'[ auto | default | none | context-menu | help | pointer | progress | wait | ' +
|
|
|
|
'cell | crosshair | text | vertical-text | alias | copy | move | no-drop | ' +
|
|
|
|
'not-allowed | grab | grabbing | e-resize | n-resize | ne-resize | nw-resize | ' +
|
|
|
|
's-resize | se-resize | sw-resize | w-resize | ew-resize | ns-resize | ' +
|
|
|
|
'nesw-resize | nwse-resize | col-resize | row-resize | all-scroll | ' +
|
|
|
|
'zoom-in | zoom-out ]',
|
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
'direction': 'ltr | rtl',
|
2018-01-06 10:17:26 +00:00
|
|
|
'display': '[ <display-outside> || <display-inside> ] | ' +
|
2020-11-25 18:01:36 +00:00
|
|
|
'<display-listitem> | <display-internal> | <display-box> | <display-legacy> | ' +
|
|
|
|
// deprecated and nonstandard
|
|
|
|
'-webkit-box | -webkit-inline-box | -ms-flexbox',
|
|
|
|
'dominant-baseline': 'auto | use-script | no-change | reset-size | ideographic | alphabetic | ' +
|
|
|
|
'hanging | mathematical | central | middle | text-after-edge | text-before-edge',
|
|
|
|
'drop-initial-after-adjust': 'central | middle | after-edge | text-after-edge | ideographic | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'alphabetic | mathematical | <len-pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'drop-initial-after-align': 'baseline | use-script | before-edge | text-before-edge | ' +
|
|
|
|
'after-edge | text-after-edge | central | middle | ideographic | alphabetic | hanging | ' +
|
|
|
|
'mathematical',
|
|
|
|
'drop-initial-before-adjust': 'before-edge | text-before-edge | central | middle | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'hanging | mathematical | <len-pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'drop-initial-before-align': 'caps-height | baseline | use-script | before-edge | ' +
|
|
|
|
'text-before-edge | after-edge | text-after-edge | central | middle | ideographic | ' +
|
|
|
|
'alphabetic | hanging | mathematical',
|
2021-09-07 09:36:36 +00:00
|
|
|
'drop-initial-size': 'auto | line | <len-pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'drop-initial-value': '<integer>',
|
|
|
|
|
|
|
|
'elevation': '<angle> | below | level | above | higher | lower',
|
|
|
|
'empty-cells': 'show | hide',
|
2017-12-26 20:39:52 +00:00
|
|
|
'enable-background': 1,
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
'fill': '<paint>',
|
|
|
|
'fill-opacity': '<opacity-value>',
|
|
|
|
'fill-rule': 'nonzero | evenodd',
|
2021-08-26 19:03:05 +00:00
|
|
|
'filter': '<filter-function-list> | <ie-function> | none',
|
2020-11-25 18:01:36 +00:00
|
|
|
'fit': 'fill | hidden | meet | slice',
|
|
|
|
'fit-position': 1,
|
|
|
|
'flex': '<flex-shorthand>',
|
|
|
|
'flex-basis': '<width>',
|
2017-12-26 20:39:52 +00:00
|
|
|
'flex-direction': 'row | row-reverse | column | column-reverse',
|
2020-11-25 18:01:36 +00:00
|
|
|
'flex-flow': '<flex-direction> || <flex-wrap>',
|
|
|
|
'flex-grow': '<number>',
|
|
|
|
'flex-shrink': '<number>',
|
|
|
|
'flex-wrap': 'nowrap | wrap | wrap-reverse',
|
|
|
|
'float': 'left | right | none | inline-start | inline-end',
|
|
|
|
'float-offset': 1,
|
|
|
|
'flood-color': 1,
|
|
|
|
'flood-opacity': '<opacity-value>',
|
|
|
|
// matching no-pct first because Matcher doesn't retry for a longer match in nested definitions
|
|
|
|
'font': '<font-short-tweak-no-pct>? <font-short-core> | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'[ <font-short-tweak-no-pct> || <pct> ]? <font-short-core> | ' +
|
2020-11-25 18:01:36 +00:00
|
|
|
'caption | icon | menu | message-box | small-caption | status-bar',
|
|
|
|
'font-family': '<font-family>',
|
|
|
|
'font-feature-settings': '<feature-tag-value># | normal',
|
|
|
|
'font-kerning': 'auto | normal | none',
|
|
|
|
'font-language-override': 'normal | <string>',
|
|
|
|
'font-optical-sizing': 'auto | none',
|
|
|
|
'font-palette': 'none | normal | light | dark | <ident>',
|
|
|
|
'font-size': '<font-size>',
|
|
|
|
'font-size-adjust': '<number> | none',
|
|
|
|
'font-stretch': '<font-stretch>',
|
|
|
|
'font-style': '<font-style>',
|
|
|
|
'font-synthesis': 'none | [ weight || style ]',
|
|
|
|
'font-synthesis-style': 'auto | none',
|
|
|
|
'font-synthesis-weight': 'auto | none',
|
2020-06-17 13:13:28 +00:00
|
|
|
'font-synthesis-small-caps': 'auto | none',
|
2021-09-07 09:36:36 +00:00
|
|
|
'font-variant': '<font-variant>',
|
2017-12-26 20:39:52 +00:00
|
|
|
'font-variant-alternates': '<font-variant-alternates> | normal',
|
2020-11-25 18:01:36 +00:00
|
|
|
'font-variant-caps': '<font-variant-caps> | normal',
|
2017-12-26 20:39:52 +00:00
|
|
|
'font-variant-east-asian': '<font-variant-east-asian> | normal',
|
2020-11-25 18:01:36 +00:00
|
|
|
'font-variant-emoji': 'auto | text | emoji | unicode',
|
|
|
|
'font-variant-ligatures': '<font-variant-ligatures> | normal | none',
|
|
|
|
'font-variant-numeric': '<font-variant-numeric> | normal',
|
|
|
|
'font-variant-position': 'normal | sub | super',
|
2020-10-11 06:44:17 +00:00
|
|
|
'font-variation-settings': 'normal | [ <string> <number> ]#',
|
2020-11-25 18:01:36 +00:00
|
|
|
'font-weight': '<font-weight>',
|
2021-02-01 15:51:24 +00:00
|
|
|
'forced-color-adjust': 'auto | none',
|
2020-11-25 18:01:36 +00:00
|
|
|
'-ms-flex-align': 1,
|
|
|
|
'-ms-flex-order': 1,
|
|
|
|
'-ms-flex-pack': 1,
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
'gap': '<row-gap> <column-gap>?',
|
2017-12-26 20:39:52 +00:00
|
|
|
'glyph-orientation-horizontal': '<glyph-angle>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'glyph-orientation-vertical': 'auto | <glyph-angle>',
|
2017-12-26 20:39:52 +00:00
|
|
|
|
|
|
|
'grid': '<grid-template> | <grid-template-rows> / [ auto-flow && dense? ] <grid-auto-columns>? | ' +
|
2020-11-25 18:01:36 +00:00
|
|
|
'[ auto-flow && dense? ] <grid-auto-rows>? / <grid-template-columns>',
|
|
|
|
'grid-area': '<grid-line> [ / <grid-line> ]{0,3}',
|
2017-12-26 20:39:52 +00:00
|
|
|
'grid-auto-columns': '<grid-auto-columns>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'grid-auto-flow': '[ row | column ] || dense',
|
|
|
|
'grid-auto-rows': '<grid-auto-rows>',
|
|
|
|
'grid-column': '<grid-line> [ / <grid-line> ]?',
|
2017-12-26 20:39:52 +00:00
|
|
|
'grid-column-start': '<grid-line>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'grid-column-end': '<grid-line>',
|
|
|
|
'grid-row': '<grid-line> [ / <grid-line> ]?',
|
|
|
|
'grid-row-start': '<grid-line>',
|
|
|
|
'grid-row-end': '<grid-line>',
|
|
|
|
'grid-template': 'none | [ <grid-template-rows> / <grid-template-columns> ] | ' +
|
|
|
|
'[ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]?',
|
|
|
|
'grid-template-areas': 'none | <string>+',
|
2017-12-26 20:39:52 +00:00
|
|
|
'grid-template-columns': '<grid-template-columns>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'grid-template-rows': '<grid-template-rows>',
|
|
|
|
'grid-row-gap': '<row-gap>',
|
|
|
|
'grid-column-gap': '<column-gap>',
|
|
|
|
'grid-gap': '<row-gap> <column-gap>?',
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-06-17 13:13:28 +00:00
|
|
|
'hanging-punctuation': 'none | [ first || [ force-end | allow-end ] || last ]',
|
2020-11-25 18:01:36 +00:00
|
|
|
'height': 'auto | <width-height>',
|
|
|
|
'hyphenate-after': '<integer> | auto',
|
|
|
|
'hyphenate-before': '<integer> | auto',
|
2017-12-26 20:39:52 +00:00
|
|
|
'hyphenate-character': '<string> | auto',
|
2020-11-25 18:01:36 +00:00
|
|
|
'hyphenate-lines': 'no-limit | <integer>',
|
|
|
|
'hyphenate-resource': 1,
|
|
|
|
'hyphens': 'none | manual | auto',
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
'icon': 1,
|
2020-10-24 10:18:17 +00:00
|
|
|
'image-orientation': 'from-image | none | [ <angle> || flip ]',
|
2020-11-25 18:01:36 +00:00
|
|
|
'image-rendering': 'auto | smooth | high-quality | crisp-edges | pixelated | ' +
|
2022-04-07 14:18:07 +00:00
|
|
|
'optimizeSpeed | optimizeQuality | -webkit-optimize-contrast',
|
2020-11-25 18:01:36 +00:00
|
|
|
'image-resolution': 1,
|
|
|
|
'ime-mode': 'auto | normal | active | inactive | disabled',
|
|
|
|
'inline-box-align': 'last | <integer>',
|
|
|
|
'inline-size': '<width>',
|
|
|
|
'inset': '<width>{1,4}',
|
|
|
|
'inset-block': '<width>{1,2}',
|
|
|
|
'inset-block-end': '<width>',
|
2018-06-06 08:41:17 +00:00
|
|
|
'inset-block-start': '<width>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'inset-inline': '<width>{1,2}',
|
|
|
|
'inset-inline-end': '<width>',
|
2018-06-06 08:41:17 +00:00
|
|
|
'inset-inline-start': '<width>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'isolation': 'auto | isolate',
|
2017-12-26 20:39:52 +00:00
|
|
|
|
|
|
|
'justify-content': '<justify-content>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'justify-items': 'normal | stretch | <baseline-position> | ' +
|
|
|
|
'[ <overflow-position>? <self-position> ] | ' +
|
|
|
|
'[ legacy || [ left | right | center ] ]',
|
|
|
|
'justify-self': '<justify-self>',
|
2017-12-26 20:39:52 +00:00
|
|
|
|
|
|
|
'kerning': 'auto | <length>',
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
'left': '<width>',
|
|
|
|
'letter-spacing': '<length> | normal',
|
|
|
|
'line-height': '<line-height>',
|
|
|
|
'line-break': 'auto | loose | normal | strict | anywhere',
|
|
|
|
'line-stacking': 1,
|
|
|
|
'line-stacking-ruby': 'exclude-ruby | include-ruby',
|
|
|
|
'line-stacking-shift': 'consider-shifts | disregard-shifts',
|
2017-12-26 20:39:52 +00:00
|
|
|
'line-stacking-strategy': 'inline-line-height | block-line-height | max-height | grid-height',
|
2020-11-25 18:01:36 +00:00
|
|
|
'list-style': 1,
|
|
|
|
'list-style-image': '<uri> | none',
|
|
|
|
'list-style-position': 'inside | outside',
|
|
|
|
'list-style-type': '<string> | disc | circle | square | decimal | decimal-leading-zero | ' +
|
|
|
|
'lower-roman | upper-roman | lower-greek | lower-latin | upper-latin | armenian | ' +
|
2020-10-24 10:18:17 +00:00
|
|
|
'georgian | lower-alpha | upper-alpha | none',
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
'margin': '<width>{1,4}',
|
|
|
|
'margin-bottom': '<width>',
|
|
|
|
'margin-left': '<width>',
|
|
|
|
'margin-right': '<width>',
|
|
|
|
'margin-top': '<width>',
|
|
|
|
'margin-block': '<width>{1,2}',
|
|
|
|
'margin-block-end': '<width>',
|
2018-06-06 08:41:17 +00:00
|
|
|
'margin-block-start': '<width>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'margin-inline': '<width>{1,2}',
|
|
|
|
'margin-inline-end': '<width>',
|
2018-06-06 08:41:17 +00:00
|
|
|
'margin-inline-start': '<width>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'mark': 1,
|
|
|
|
'mark-after': 1,
|
|
|
|
'mark-before': 1,
|
|
|
|
'marker': 1,
|
|
|
|
'marker-end': 1,
|
|
|
|
'marker-mid': 1,
|
|
|
|
'marker-start': 1,
|
|
|
|
'marks': 1,
|
|
|
|
'marquee-direction': 1,
|
2017-12-26 20:39:52 +00:00
|
|
|
'marquee-play-count': 1,
|
2020-11-25 18:01:36 +00:00
|
|
|
'marquee-speed': 1,
|
|
|
|
'marquee-style': 1,
|
|
|
|
'mask': 1,
|
|
|
|
'mask-image': '[ none | <image> | <uri> ]#',
|
|
|
|
'max-height': 'none | <width-height>',
|
|
|
|
'max-width': 'none | <width-height>',
|
|
|
|
'min-height': 'auto | <width-height>',
|
|
|
|
'min-width': 'auto | <width-height>',
|
2021-09-07 09:36:36 +00:00
|
|
|
'max-block-size': '<len-pct> | none',
|
|
|
|
'max-inline-size': '<len-pct> | none',
|
|
|
|
'min-block-size': '<len-pct>',
|
|
|
|
'min-inline-size': '<len-pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'mix-blend-mode': '<blend-mode>',
|
|
|
|
'move-to': 1,
|
|
|
|
|
|
|
|
'nav-down': 1,
|
2017-12-26 20:39:52 +00:00
|
|
|
'nav-index': 1,
|
2020-11-25 18:01:36 +00:00
|
|
|
'nav-left': 1,
|
2017-12-26 20:39:52 +00:00
|
|
|
'nav-right': 1,
|
2020-11-25 18:01:36 +00:00
|
|
|
'nav-up': 1,
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
'object-fit': 'fill | contain | cover | none | scale-down',
|
2022-06-24 05:25:33 +00:00
|
|
|
'object-overflow': 'clip | visible',
|
2017-12-26 20:39:52 +00:00
|
|
|
'object-position': '<position>',
|
2022-06-24 05:25:33 +00:00
|
|
|
'object-view-box': 'none | <inset> | <rect> | <xywh>',
|
2021-09-07 09:36:36 +00:00
|
|
|
'opacity': '<opacity-value> | <pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'order': '<integer>',
|
|
|
|
'orphans': '<integer>',
|
|
|
|
'outline': '[ <color> | invert ] || [ auto | <border-style> ] || <border-width>',
|
|
|
|
'outline-color': '<color> | invert',
|
|
|
|
'outline-offset': '<length>',
|
|
|
|
'outline-style': '<border-style> | auto',
|
|
|
|
'outline-width': '<border-width>',
|
|
|
|
'overflow': '<overflow>{1,2}',
|
2020-09-22 10:56:53 +00:00
|
|
|
'overflow-anchor': 'auto | none',
|
2020-11-25 18:01:36 +00:00
|
|
|
'overflow-block': '<overflow>',
|
2022-06-24 05:25:33 +00:00
|
|
|
'overflow-clip-margin': 'visual-box | <len0+>',
|
2019-03-03 22:55:15 +00:00
|
|
|
'overflow-inline': '<overflow>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'overflow-style': 1,
|
|
|
|
'overflow-wrap': 'normal | break-word | anywhere',
|
|
|
|
'overflow-x': '<overflow>',
|
|
|
|
'overflow-y': '<overflow>',
|
2020-08-13 17:58:09 +00:00
|
|
|
'overscroll-behavior': '<overscroll>{1,2}',
|
|
|
|
'overscroll-behavior-block': '<overscroll>',
|
|
|
|
'overscroll-behavior-inline': '<overscroll>',
|
|
|
|
'overscroll-behavior-x': '<overscroll>',
|
|
|
|
'overscroll-behavior-y': '<overscroll>',
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2021-09-07 09:36:36 +00:00
|
|
|
'padding': '<len-pct0+>{1,4}',
|
|
|
|
'padding-block': '<len-pct0+>{1,2}',
|
|
|
|
'padding-block-end': '<len-pct0+>',
|
|
|
|
'padding-block-start': '<len-pct0+>',
|
|
|
|
'padding-bottom': '<len-pct0+>',
|
|
|
|
'padding-inline': '<len-pct0+>{1,2}',
|
|
|
|
'padding-inline-end': '<len-pct0+>',
|
|
|
|
'padding-inline-start': '<len-pct0+>',
|
|
|
|
'padding-left': '<len-pct0+>',
|
|
|
|
'padding-right': '<len-pct0+>',
|
|
|
|
'padding-top': '<len-pct0+>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'page': 1,
|
|
|
|
'page-break-after': 'auto | always | avoid | left | right | recto | verso',
|
|
|
|
'page-break-before': 'auto | always | avoid | left | right | recto | verso',
|
|
|
|
'page-break-inside': 'auto | avoid',
|
|
|
|
'page-policy': 1,
|
|
|
|
'pause': 1,
|
|
|
|
'pause-after': 1,
|
|
|
|
'pause-before': 1,
|
2021-10-21 20:25:32 +00:00
|
|
|
'perspective': 'none | <len0+>',
|
2017-12-26 20:39:52 +00:00
|
|
|
'perspective-origin': '<position>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'phonemes': 1,
|
|
|
|
'pitch': 1,
|
|
|
|
'pitch-range': 1,
|
|
|
|
'place-content': '<align-content> <justify-content>?',
|
|
|
|
'place-items': '[ normal | stretch | <baseline-position> | <self-position> ] ' +
|
|
|
|
'[ normal | stretch | <baseline-position> | <self-position> ]?',
|
|
|
|
'place-self': '<align-self> <justify-self>?',
|
|
|
|
'play-during': 1,
|
|
|
|
'pointer-events': 'auto | none | visiblePainted | visibleFill | visibleStroke | visible | ' +
|
|
|
|
'painted | fill | stroke | all',
|
|
|
|
'position': 'static | relative | absolute | fixed | sticky | -webkit-sticky',
|
2017-12-26 20:39:52 +00:00
|
|
|
'presentation-level': 1,
|
2020-11-25 18:01:36 +00:00
|
|
|
'punctuation-trim': 1,
|
2017-12-26 20:39:52 +00:00
|
|
|
|
|
|
|
'quotes': 1,
|
|
|
|
|
|
|
|
'rendering-intent': 1,
|
2020-11-25 18:01:36 +00:00
|
|
|
'resize': 'none | both | horizontal | vertical | block | inline',
|
|
|
|
'rest': 1,
|
|
|
|
'rest-after': 1,
|
|
|
|
'rest-before': 1,
|
|
|
|
'richness': 1,
|
|
|
|
'right': '<width>',
|
|
|
|
'rotate': 'none | [ x | y | z | <number>{3} ]? && <angle>',
|
|
|
|
'rotation': 1,
|
|
|
|
'rotation-point': 1,
|
|
|
|
'row-gap': '<row-gap>',
|
|
|
|
'ruby-align': 1,
|
|
|
|
'ruby-overhang': 1,
|
|
|
|
'ruby-position': 1,
|
|
|
|
'ruby-span': 1,
|
|
|
|
|
2021-10-21 20:25:32 +00:00
|
|
|
'scale': 'none | <num-pct>{1,3}',
|
2020-11-25 18:01:36 +00:00
|
|
|
|
|
|
|
'scroll-behavior': 'auto | smooth',
|
|
|
|
'scroll-margin': '<length>{1,4}',
|
|
|
|
'scroll-margin-bottom': '<length>',
|
|
|
|
'scroll-margin-left': '<length>',
|
|
|
|
'scroll-margin-right': '<length>',
|
|
|
|
'scroll-margin-top': '<length>',
|
|
|
|
'scroll-margin-block': '<length>{1,2}',
|
|
|
|
'scroll-margin-block-end': '<length>',
|
|
|
|
'scroll-margin-block-start': '<length>',
|
|
|
|
'scroll-margin-inline': '<length>{1,2}',
|
|
|
|
'scroll-margin-inline-end': '<length>',
|
|
|
|
'scroll-margin-inline-start': '<length>',
|
|
|
|
'scroll-padding': '<width>{1,4}',
|
|
|
|
'scroll-padding-left': '<width>',
|
|
|
|
'scroll-padding-right': '<width>',
|
|
|
|
'scroll-padding-top': '<width>',
|
|
|
|
'scroll-padding-bottom': '<width>',
|
|
|
|
'scroll-padding-block': '<width>{1,2}',
|
|
|
|
'scroll-padding-block-end': '<width>',
|
|
|
|
'scroll-padding-block-start': '<width>',
|
|
|
|
'scroll-padding-inline': '<width>{1,2}',
|
|
|
|
'scroll-padding-inline-end': '<width>',
|
|
|
|
'scroll-padding-inline-start': '<width>',
|
|
|
|
'scroll-snap-align': '[ none | start | end | center ]{1,2}',
|
|
|
|
'scroll-snap-stop': 'normal | always',
|
|
|
|
'scroll-snap-type': 'none | [ x | y | block | inline | both ] [ mandatory | proximity ]?',
|
|
|
|
|
|
|
|
'scrollbar-color': 'auto | dark | light | <color>{2}',
|
2022-02-08 11:05:46 +00:00
|
|
|
'scrollbar-gutter': 'auto | stable && both-edges?',
|
2020-11-25 18:01:36 +00:00
|
|
|
'scrollbar-width': 'auto | thin | none',
|
|
|
|
'shape-inside': 'auto | outside-shape | [ <basic-shape> || shape-box ] | <image> | display',
|
|
|
|
'shape-rendering': 'auto | optimizeSpeed | crispEdges | geometricPrecision',
|
|
|
|
'size': 1,
|
|
|
|
'speak': 'normal | none | spell-out',
|
|
|
|
'speak-header': 'once | always',
|
|
|
|
'speak-numeral': 'digits | continuous',
|
2017-12-26 20:39:52 +00:00
|
|
|
'speak-punctuation': 'code | none',
|
2020-11-25 18:01:36 +00:00
|
|
|
'speech-rate': 1,
|
|
|
|
'stop-color': 1,
|
|
|
|
'stop-opacity': '<opacity-value>',
|
|
|
|
'stress': 1,
|
|
|
|
'string-set': 1,
|
|
|
|
'stroke': '<paint>',
|
|
|
|
'stroke-dasharray': 'none | <dasharray>',
|
2021-09-07 09:36:36 +00:00
|
|
|
'stroke-dashoffset': '<len-pct> | <number>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'stroke-linecap': 'butt | round | square',
|
2021-01-01 16:43:28 +00:00
|
|
|
'stroke-linejoin': 'miter | miter-clip | round | bevel | arcs',
|
2021-09-07 09:36:36 +00:00
|
|
|
'stroke-miterlimit': '<num0+>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'stroke-opacity': '<opacity-value>',
|
2021-09-07 09:36:36 +00:00
|
|
|
'stroke-width': '<len-pct> | <number>',
|
2020-11-25 18:01:36 +00:00
|
|
|
|
|
|
|
'table-layout': 'auto | fixed',
|
|
|
|
'tab-size': '<number> | <length>',
|
|
|
|
'target': 1,
|
|
|
|
'target-name': 1,
|
|
|
|
'target-new': 1,
|
2017-12-26 20:39:52 +00:00
|
|
|
'target-position': 1,
|
2020-11-25 18:01:36 +00:00
|
|
|
'text-align': '<text-align> | justify-all',
|
|
|
|
'text-align-all': '<text-align>',
|
2020-06-17 13:13:28 +00:00
|
|
|
'text-align-last': '<text-align> | auto',
|
2020-11-25 18:01:36 +00:00
|
|
|
'text-anchor': 'start | middle | end',
|
|
|
|
'text-decoration': '<text-decoration-line> || <text-decoration-style> || <color>',
|
2018-08-15 16:12:01 +00:00
|
|
|
'text-decoration-color': '<color>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'text-decoration-line': '<text-decoration-line>',
|
|
|
|
'text-decoration-skip': 'none | ' +
|
|
|
|
'[ objects || [ spaces | [ leading-spaces || trailing-spaces ] ] || edges || box-decoration ]',
|
2017-12-26 20:39:52 +00:00
|
|
|
'text-decoration-style': '<text-decoration-style>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'text-emphasis': '<text-emphasis-style> || <color>',
|
2022-02-04 10:08:03 +00:00
|
|
|
'text-emphasis-color': '<color>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'text-emphasis-style': '<text-emphasis-style>',
|
2019-03-03 22:55:15 +00:00
|
|
|
'text-emphasis-position': '[ over | under ] && [ right | left ]?',
|
2020-11-25 18:01:36 +00:00
|
|
|
'text-height': 1,
|
2021-09-07 09:36:36 +00:00
|
|
|
'text-indent': '<len-pct> && hanging? && each-line?',
|
2020-11-25 18:01:36 +00:00
|
|
|
'text-justify': 'auto | none | inter-word | inter-character',
|
|
|
|
'text-outline': 1,
|
|
|
|
'text-overflow': 'clip | ellipsis',
|
|
|
|
'text-rendering': 'auto | optimizeSpeed | optimizeLegibility | geometricPrecision',
|
|
|
|
'text-shadow': 'none | [ <color>? && <length>{2,3} ]#',
|
|
|
|
'text-transform': 'none | [ capitalize | uppercase | lowercase ] || full-width || full-size-kana',
|
2019-03-03 22:55:15 +00:00
|
|
|
'text-underline-position': 'auto | [ under || [ left | right ] ]',
|
2020-11-25 18:01:36 +00:00
|
|
|
'text-wrap': 'normal | none | avoid',
|
|
|
|
'top': '<width>',
|
|
|
|
'touch-action': 'auto | none | ' +
|
|
|
|
'pan-x | pan-y | pan-left | pan-right | pan-up | pan-down | manipulation',
|
|
|
|
'transform': 'none | <transform-function>+',
|
|
|
|
'transform-box': 'border-box | fill-box | view-box',
|
2017-12-26 20:39:52 +00:00
|
|
|
'transform-origin': '<transform-origin>',
|
2021-10-21 20:25:32 +00:00
|
|
|
'transform-style': 'flat | preserve-3d',
|
2020-11-25 18:01:36 +00:00
|
|
|
'transition': '<transition>#',
|
2017-12-26 20:39:52 +00:00
|
|
|
'transition-delay': '<time>#',
|
|
|
|
'transition-duration': '<time>#',
|
|
|
|
'transition-property': 'none | [ all | <ident> ]#',
|
|
|
|
'transition-timing-function': '<single-timing-function>#',
|
2021-09-07 09:36:36 +00:00
|
|
|
'translate': 'none | <len-pct> [ <len-pct> <length>? ]?',
|
2017-12-26 20:39:52 +00:00
|
|
|
|
|
|
|
'unicode-range': '<unicode-range>#',
|
2020-11-25 18:01:36 +00:00
|
|
|
'unicode-bidi': 'normal | embed | isolate | bidi-override | isolate-override | plaintext',
|
|
|
|
'user-modify': 'read-only | read-write | write-only',
|
|
|
|
'user-select': 'auto | text | none | contain | all',
|
|
|
|
|
|
|
|
'vertical-align': 'auto | use-script | baseline | sub | super | top | text-top | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'central | middle | bottom | text-bottom | <len-pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'visibility': 'visible | hidden | collapse',
|
|
|
|
'voice-balance': 1,
|
|
|
|
'voice-duration': 1,
|
|
|
|
'voice-family': 1,
|
|
|
|
'voice-pitch': 1,
|
2017-12-26 20:39:52 +00:00
|
|
|
'voice-pitch-range': 1,
|
2020-11-25 18:01:36 +00:00
|
|
|
'voice-rate': 1,
|
|
|
|
'voice-stress': 1,
|
|
|
|
'voice-volume': 1,
|
|
|
|
'volume': 1,
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
'white-space': 'normal | pre | nowrap | pre-wrap | break-spaces | pre-line',
|
2017-12-26 20:39:52 +00:00
|
|
|
'white-space-collapse': 1,
|
2020-11-25 18:01:36 +00:00
|
|
|
'widows': '<integer>',
|
|
|
|
'width': 'auto | <width-height>',
|
|
|
|
'will-change': '<will-change>',
|
|
|
|
'word-break': 'normal | keep-all | break-all | break-word',
|
|
|
|
'word-spacing': '<length> | normal',
|
|
|
|
'word-wrap': 'normal | break-word | anywhere',
|
|
|
|
'writing-mode': 'horizontal-tb | vertical-rl | vertical-lr | ' +
|
|
|
|
'lr-tb | rl-tb | tb-rl | bt-rl | tb-lr | bt-lr | lr-bt | rl-bt | lr | rl | tb',
|
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
'z-index': '<integer> | auto',
|
2021-09-07 09:36:36 +00:00
|
|
|
'zoom': '<number> | <pct> | normal',
|
2018-03-08 23:34:29 +00:00
|
|
|
|
|
|
|
// nonstandard https://compat.spec.whatwg.org/
|
2020-11-25 18:01:36 +00:00
|
|
|
'-webkit-box-reflect': '[ above | below | right | left ]? <length>? <image>?',
|
|
|
|
'-webkit-text-fill-color': '<color>',
|
|
|
|
'-webkit-text-stroke': '<border-width> || <color>',
|
2018-03-08 23:34:29 +00:00
|
|
|
'-webkit-text-stroke-color': '<color>',
|
|
|
|
'-webkit-text-stroke-width': '<border-width>',
|
2017-12-26 20:39:52 +00:00
|
|
|
};
|
|
|
|
|
2021-09-07 09:36:36 +00:00
|
|
|
const ScopedProperties = {
|
|
|
|
'@font-face': Object.assign({
|
|
|
|
'ascent-override': '[ normal | <pct0+> ]{1,2}',
|
|
|
|
'descent-override': '[ normal | <pct0+> ]{1,2}',
|
|
|
|
'font-display': 'auto | block | swap | fallback | optional',
|
|
|
|
'font-stretch': 'auto | <font-stretch>{1,2}',
|
|
|
|
'font-style': 'auto | normal | italic | oblique <angle>{0,2}',
|
|
|
|
'font-weight': 'auto | [ normal | bold | <num1-1000> ]{1,2}',
|
|
|
|
'line-gap-override': '[ normal | <pct0+> ]{1,2}',
|
|
|
|
'size-adjust': '<pct0+>',
|
|
|
|
'src': '[ url() [ format( <string># ) ]? | local( <family-name> ) ]#',
|
|
|
|
}, ...[
|
|
|
|
'font-family',
|
|
|
|
'font-size',
|
|
|
|
'font-variant',
|
|
|
|
'font-variation-settings',
|
|
|
|
'unicode-range',
|
|
|
|
].map(p => ({[p]: Properties[p]}))),
|
2022-04-01 13:05:45 +00:00
|
|
|
|
|
|
|
'@font-palette-values': Object.assign({
|
|
|
|
'base-palette': 'light | dark | <int0+>',
|
|
|
|
'override-colors': '[ <int0+> <color> ]#',
|
|
|
|
}, ...[
|
|
|
|
'font-family',
|
|
|
|
].map(p => ({[p]: Properties[p]}))),
|
2021-09-07 09:36:36 +00:00
|
|
|
};
|
|
|
|
|
2020-10-11 06:44:17 +00:00
|
|
|
for (const [k, reps] of Object.entries({
|
|
|
|
'border': '{1,4}',
|
|
|
|
'border-bottom': '',
|
|
|
|
'border-left': '',
|
|
|
|
'border-right': '',
|
|
|
|
'border-top': '',
|
|
|
|
'border-block': '{1,2}',
|
|
|
|
'border-block-end': '',
|
|
|
|
'border-block-start': '',
|
|
|
|
'border-inline': '{1,2}',
|
|
|
|
'border-inline-end': '',
|
|
|
|
'border-inline-start': '',
|
|
|
|
})) {
|
|
|
|
Properties[k] = '<border-shorthand>';
|
|
|
|
Properties[`${k}-color`] = '<color>' + reps;
|
|
|
|
Properties[`${k}-style`] = '<border-style>' + reps;
|
|
|
|
Properties[`${k}-width`] = '<border-width>' + reps;
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region Types
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
const TYPES = /** @namespace Parser */ {
|
|
|
|
DEFAULT_TYPE: 0,
|
|
|
|
COMBINATOR_TYPE: 1,
|
|
|
|
MEDIA_FEATURE_TYPE: 2,
|
|
|
|
MEDIA_QUERY_TYPE: 3,
|
|
|
|
PROPERTY_NAME_TYPE: 4,
|
|
|
|
PROPERTY_VALUE_TYPE: 5,
|
|
|
|
PROPERTY_VALUE_PART_TYPE: 6,
|
|
|
|
SELECTOR_TYPE: 7,
|
|
|
|
SELECTOR_PART_TYPE: 8,
|
|
|
|
SELECTOR_SUB_PART_TYPE: 9,
|
|
|
|
};
|
2022-02-08 11:05:46 +00:00
|
|
|
const UNITS = JSON.parse(`{${Object.entries({
|
|
|
|
angle: 'deg,grad,rad,turn',
|
|
|
|
frequency: 'hz,khz',
|
|
|
|
length: 'cap,ch,em,ex,ic,lh,rlh,rem,' +
|
|
|
|
'cm,mm,in,pc,pt,px,q,' +
|
|
|
|
'fr,' + // grids
|
|
|
|
'vb,vi,vh,vw,vmin,vmax'.replace(/\w+/g, '$&,d$&,l$&,s$&'),
|
|
|
|
resolution: 'dpcm,dpi,dppx,x',
|
|
|
|
time: 'ms,s',
|
|
|
|
}).map(([type, units]) => units.replace(/\w+/g, `"$&":"${type}"`)).join(',')}}`);
|
2021-01-01 14:27:58 +00:00
|
|
|
// Sticky `y` flag must be used in expressions used with peekTest and readMatch
|
|
|
|
const rxIdentStart = /[-\\_a-zA-Z\u00A0-\uFFFF]/u;
|
|
|
|
const rxNameChar = /[-\\_\da-zA-Z\u00A0-\uFFFF]/u;
|
2020-12-14 18:18:15 +00:00
|
|
|
const rxNameCharNoEsc = /[-_\da-zA-Z\u00A0-\uFFFF]+/yu; // must not match \\
|
|
|
|
const rxUnquotedUrlCharNoEsc = /[-!#$%&*-[\]-~\u00A0-\uFFFF]+/yu; // must not match \\
|
2020-11-25 18:01:36 +00:00
|
|
|
const rxVendorPrefix = /^-(webkit|moz|ms|o)-(.+)/i;
|
|
|
|
const rxCalc = /^(?:-(webkit|moz|ms|o)-)?(calc|min|max|clamp)\(/i;
|
|
|
|
const lowercaseCache = new Map();
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region ValidationTypes - definitions
|
|
|
|
|
|
|
|
/** Allowed syntax: text, |, <syntax>, func() */
|
|
|
|
const VTSimple = {
|
|
|
|
'<absolute-size>': 'xx-small | x-small | small | medium | large | x-large | xx-large',
|
|
|
|
'<animateable-feature>': 'scroll-position | contents | <animateable-feature-name>',
|
2022-07-01 07:26:46 +00:00
|
|
|
'<animateable-feature-name>': customIdentChecker('will-change|auto|scroll-position|contents'),
|
2020-11-25 18:01:36 +00:00
|
|
|
'<angle>': p => p.type === 'angle' || p.isCalc,
|
|
|
|
'<angle-or-0>': p => p.text === '0' || p.type === 'angle' || p.isCalc,
|
2020-12-09 07:38:28 +00:00
|
|
|
'<attr>': vtIsAttr,
|
2020-11-25 18:01:36 +00:00
|
|
|
'<attachment>': 'scroll | fixed | local',
|
|
|
|
'<bg-image>': '<image> | none',
|
|
|
|
'<blend-mode>': 'normal | multiply | screen | overlay | darken | lighten | color-dodge | ' +
|
|
|
|
'color-burn | hard-light | soft-light | difference | exclusion | hue | ' +
|
2022-03-04 16:07:52 +00:00
|
|
|
'saturation | color | luminosity | plus-darker | plus-lighter',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<border-style>': 'none | ' +
|
|
|
|
'hidden | dotted | dashed | solid | double | groove | ridge | inset | outset',
|
|
|
|
'<border-width>': '<length> | thin | medium | thick',
|
|
|
|
'<box>': 'padding-box | border-box | content-box',
|
|
|
|
'<clip-source>': '<uri>',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<column-gap>': 'normal | <len-pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<content-distribution>': 'space-between | space-around | space-evenly | stretch',
|
|
|
|
'<content-position>': 'center | start | end | flex-start | flex-end',
|
|
|
|
'<display-box>': 'contents | none',
|
|
|
|
'<display-inside>': 'flow | flow-root | table | flex | grid | ruby',
|
|
|
|
'<display-internal>': 'table-row-group | table-header-group | table-footer-group | ' +
|
|
|
|
'table-row | table-cell | table-column-group | table-column | table-caption | ' +
|
|
|
|
'ruby-base | ruby-text | ruby-base-container | ruby-text-container',
|
|
|
|
'<display-legacy>': 'inline-block | inline-table | inline-flex | inline-grid',
|
|
|
|
'<display-outside>': 'block | inline | run-in',
|
|
|
|
'<feature-tag-value>': p => p.type === 'function' && /^[A-Z0-9]{4}$/i.test(p),
|
|
|
|
'<flex>': p => p.type === 'grid' && p.value >= 0 || p.isCalc,
|
|
|
|
'<flex-basis>': '<width>',
|
|
|
|
'<flex-direction>': 'row | row-reverse | column | column-reverse',
|
|
|
|
'<flex-grow>': '<number>',
|
|
|
|
'<flex-shrink>': '<number>',
|
|
|
|
'<flex-wrap>': 'nowrap | wrap | wrap-reverse',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<font-size>': '<absolute-size> | <relative-size> | <len-pct0+>',
|
|
|
|
'<font-stretch>': '<font-stretch-named> | <pct>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<font-stretch-named>': 'normal | ultra-condensed | extra-condensed | condensed | ' +
|
|
|
|
'semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded',
|
|
|
|
'<font-variant-caps>':
|
|
|
|
'small-caps | all-small-caps | petite-caps | all-petite-caps | unicase | titling-caps',
|
|
|
|
'<font-variant-css21>': 'normal | small-caps',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<font-weight>': 'normal | bold | bolder | lighter | <num1-1000>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<generic-family>': 'serif | sans-serif | cursive | fantasy | monospace | system-ui | ' +
|
|
|
|
'emoji | math | fangsong | ui-serif | ui-sans-serif | ui-monospace | ui-rounded',
|
|
|
|
'<geometry-box>': '<shape-box> | fill-box | stroke-box | view-box',
|
|
|
|
'<glyph-angle>': p => p.type === 'angle' && p.units === 'deg',
|
2021-03-14 05:33:26 +00:00
|
|
|
'<gradient>': 'radial-gradient() | linear-gradient() | conic-gradient() | gradient() | ' +
|
|
|
|
'repeating-radial-gradient() | repeating-linear-gradient() | repeating-conic-gradient() | ' +
|
|
|
|
'repeating-gradient()',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<hex-color>': p => p.tokenType === Tokens.HASH, //eslint-disable-line no-use-before-define
|
|
|
|
'<icccolor>': 'cielab() | cielch() | cielchab() | icc-color() | icc-named-color()',
|
|
|
|
'<ident>': vtIsIdent,
|
2022-07-01 07:26:46 +00:00
|
|
|
'<ident-for-grid>': customIdentChecker('span|auto'),
|
2020-11-25 18:01:36 +00:00
|
|
|
'<ident-not-generic-family>': p => vtIsIdent(p) && !VTSimple['<generic-family>'](p),
|
|
|
|
'<ident-not-none>': p => vtIsIdent(p) && !lowerCmp(p.value, 'none'),
|
2021-08-26 19:03:05 +00:00
|
|
|
'<ie-function>': p => p.tokenType === Tokens.IE_FUNCTION, //eslint-disable-line no-use-before-define
|
2020-11-25 18:01:36 +00:00
|
|
|
'<image>': '<uri> | <gradient> | cross-fade()',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<inflexible-breadth>': '<len-pct> | min-content | max-content | auto',
|
2022-06-24 05:25:33 +00:00
|
|
|
'<inset>': 'inset( <len-pct>{1,4} <border-radius-round>? )',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<integer>': p => p.isInt || p.isCalc,
|
2022-04-01 13:05:45 +00:00
|
|
|
'<int0+>': p => p.isInt && p.value >= 0 || p.isCalc,
|
|
|
|
'<int1+>': p => p.isInt && p.value > 0 || p.isCalc,
|
2020-11-25 18:01:36 +00:00
|
|
|
'<length>': vtIsLength,
|
2021-09-07 09:36:36 +00:00
|
|
|
'<len0+>': p =>
|
|
|
|
p.value >= 0 && vtIsLength(p) || p.isCalc,
|
|
|
|
'<len-pct>': p => vtIsLength(p) || vtIsPct(p),
|
|
|
|
'<len-pct0+>': p =>
|
|
|
|
p.value >= 0 && (p.type === 'percentage' || vtIsLength(p)) || p.isCalc,
|
2022-06-26 18:37:43 +00:00
|
|
|
'<len-pct-side>': '<len-pct> | closest-side | farthest-side',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<line>': p => p.isInt,
|
2021-09-07 09:36:36 +00:00
|
|
|
'<line-height>': '<number> | <len-pct> | normal',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<line-names>': p =>
|
|
|
|
p.tokenType === Tokens.LBRACKET && // eslint-disable-line no-use-before-define
|
|
|
|
p.text.endsWith(']') && (
|
|
|
|
!p.expr ||
|
|
|
|
!p.expr.parts.length ||
|
|
|
|
p.expr.parts.every(VTSimple['<ident-for-grid>'], VTSimple)
|
|
|
|
),
|
|
|
|
//eslint-disable-next-line no-use-before-define
|
2021-03-14 05:33:26 +00:00
|
|
|
'<named-color>': p => p.text in Colors || ColorsLC.has(lower(p.text)),
|
2020-11-25 18:01:36 +00:00
|
|
|
'<number>': p => p.type === 'number' || p.isCalc,
|
2021-09-07 09:36:36 +00:00
|
|
|
'<num0+>': p =>
|
|
|
|
p.value >= 0 && p.type === 'number' || p.isCalc,
|
|
|
|
'<num1-1000>': p => (p.type === 'number' && p.value >= 1 && p.value <= 1000) || p.isCalc,
|
|
|
|
'<num-pct>': p => p.type === 'number' || p.type === 'percentage' || p.isCalc,
|
|
|
|
'<num-pct0+>': p =>
|
|
|
|
p.value >= 0 && (p.type === 'number' || p.type === 'percentage') || p.isCalc,
|
2020-11-25 18:01:36 +00:00
|
|
|
'<opacity-value>': p => p.type === 'number' && p.value >= 0 && p.value <= 1 || p.isCalc,
|
|
|
|
'<overflow>': 'visible | hidden | clip | scroll | auto',
|
|
|
|
'<overflow-position>': 'unsafe | safe',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<pct>': vtIsPct,
|
|
|
|
'<pct0+>': p =>
|
|
|
|
p.value >= 0 && p.type === 'percentage' || p.isCalc,
|
2022-06-24 05:25:33 +00:00
|
|
|
'<rect>': 'rect( [ <length> | auto ]#{4} <border-radius-round>? )',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<relative-size>': 'smaller | larger',
|
|
|
|
'<row-gap>': '<column-gap>',
|
|
|
|
'<self-position>': 'center | start | end | self-start | self-end | flex-start | flex-end',
|
|
|
|
'<shape-box>': '<box> | margin-box',
|
|
|
|
'<single-animation-direction>': 'normal | reverse | alternate | alternate-reverse',
|
|
|
|
'<single-animation-fill-mode>': 'none | forwards | backwards | both',
|
2022-07-01 07:26:46 +00:00
|
|
|
'<keyframes-name>': customIdentChecker('', /^-?[a-z_][-a-z0-9_]+$/, p => p.type === 'string'),
|
2020-11-25 18:01:36 +00:00
|
|
|
'<string>': p => p.type === 'string',
|
|
|
|
'<text-align>': 'start | end | left | right | center | justify | match-parent',
|
|
|
|
'<text-decoration-style>': 'solid | double | dotted | dashed | wavy',
|
|
|
|
'<time>': p => p.type === 'time',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<track-breadth>': '<len-pct> | <flex> | min-content | max-content | auto',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<unicode-range>': p => /^U\+[0-9a-f?]{1,6}(-[0-9a-f?]{1,6})?\s*$/i.test(p),
|
|
|
|
'<unit>': p => p.text === '%' || p in UNITS || lower(p) in UNITS,
|
|
|
|
'<uri>': p => p.type === 'uri',
|
|
|
|
'<width>': p => vtIsLength(p) || vtIsPct(p) || lowerCmp(p.text, 'auto'),
|
2022-06-24 05:25:33 +00:00
|
|
|
'<xywh>': 'xywh( <len-pct>{2} <len-pct0+>{2} <border-radius-round>? )',
|
2020-11-25 18:01:36 +00:00
|
|
|
};
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
const VTComplex = {
|
|
|
|
'<align-content>': 'normal | <baseline-position> | <content-distribution> | ' +
|
2021-02-01 15:51:24 +00:00
|
|
|
'<overflow-position>? <content-position>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<align-self>':
|
|
|
|
'auto | normal | stretch | <baseline-position> | <overflow-position>? <self-position>',
|
|
|
|
'<auto-repeat>':
|
|
|
|
'repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? )',
|
|
|
|
'<auto-track-list>':
|
|
|
|
'[ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>? <auto-repeat> ' +
|
|
|
|
'[ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>?',
|
|
|
|
'<azimuth>':
|
|
|
|
'<angle> | [ [ left-side | far-left | left | center-left | center | center-right | ' +
|
|
|
|
'right | far-right | right-side ] || behind ] | leftwards | rightwards',
|
|
|
|
'<baseline-position>': '[ first | last ]? baseline',
|
2020-12-09 07:38:28 +00:00
|
|
|
'<basic-shape>':
|
2022-06-24 05:25:33 +00:00
|
|
|
'<inset> | ' +
|
|
|
|
'circle( <len-pct-side>? [ at <position> ]? ) | ' +
|
|
|
|
'ellipse( [ <len-pct-side>{2} ]? [ at <position> ]? ) | ' +
|
2020-12-09 07:38:28 +00:00
|
|
|
'path( [ [ nonzero | evenodd ] , ]? <string> ) | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'polygon( [ [ nonzero | evenodd | inherit ] , ]? [ <len-pct> <len-pct> ]# )',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<bg-layer>':
|
|
|
|
'<bg-image> || <bg-position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box>{1,2}',
|
|
|
|
'<bg-position>':
|
2021-09-07 09:36:36 +00:00
|
|
|
'[ center | [ left | right ] <len-pct>? ] && [ center | [ top | bottom ] <len-pct>? ] | ' +
|
|
|
|
'[ left | center | right | <len-pct> ] [ top | center | bottom | <len-pct> ] | ' +
|
|
|
|
'[ left | center | right | top | bottom | <len-pct> ]',
|
|
|
|
'<bg-size>': '[ <len-pct> | auto ]{1,2} | cover | contain',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<border-image-outset>': '[ <length> | <number> ]{1,4}',
|
|
|
|
'<border-image-repeat>': '[ stretch | repeat | round | space ]{1,2}',
|
|
|
|
'<border-image-slice>': Matcher =>
|
2021-09-07 09:36:36 +00:00
|
|
|
// [<number> | <pct>]{1,4} && fill?
|
2020-11-25 18:01:36 +00:00
|
|
|
// but 'fill' can appear between any of the numbers
|
|
|
|
Matcher.many(
|
|
|
|
[true],
|
2021-09-07 09:36:36 +00:00
|
|
|
Matcher.parse('<num-pct0+>'),
|
|
|
|
Matcher.parse('<num-pct0+>'),
|
|
|
|
Matcher.parse('<num-pct0+>'),
|
|
|
|
Matcher.parse('<num-pct0+>'),
|
2020-11-25 18:01:36 +00:00
|
|
|
'fill'),
|
2021-09-07 09:36:36 +00:00
|
|
|
'<border-image-width>': '[ <len-pct> | <number> | auto ]{1,4}',
|
|
|
|
'<border-radius>': '<len-pct0+>{1,4} [ / <len-pct0+>{1,4} ]?',
|
2022-06-24 05:25:33 +00:00
|
|
|
'<border-radius-round>': 'round <border-radius>',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<border-shorthand>': '<border-width> || <border-style> || <color>',
|
|
|
|
'<box-shadow>': 'none | <shadow>#',
|
|
|
|
'<clip-path>': '<basic-shape> || <geometry-box>',
|
2020-12-09 07:38:28 +00:00
|
|
|
'<color>': '<hex-color> | <named-color> | rgb( <rgb-color> ) | rgba( <rgb-color> ) | ' +
|
2022-04-01 13:05:45 +00:00
|
|
|
'hsl( <hsl-color> ) | hsla( <hsl-color> ) | hwb( <hwb-color> )',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<content-list>':
|
2020-12-09 07:38:28 +00:00
|
|
|
'[ <string> | <image> | <attr> | ' +
|
|
|
|
'content( text | before | after | first-letter | marker ) | ' +
|
|
|
|
'counter() | counters() | leader() | ' +
|
2020-11-25 18:01:36 +00:00
|
|
|
'[ open-quote | close-quote | no-open-quote | no-close-quote ] | ' +
|
|
|
|
'[ target-counter() | target-counters() | target-text() ] ]+',
|
|
|
|
'<counter>': '[ <ident-not-none> <integer>? ]+ | none',
|
2020-12-09 07:38:28 +00:00
|
|
|
'<cubic-bezier-timing-function>': 'ease | ease-in | ease-out | ease-in-out | ' +
|
|
|
|
'cubic-bezier( <number>#{4} )',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<dasharray>': Matcher =>
|
2021-09-07 09:36:36 +00:00
|
|
|
Matcher.parse('<len-pct0+> | <num0+>')
|
2020-11-25 18:01:36 +00:00
|
|
|
.braces(1, Infinity, '#', Matcher.parse(',').braces(0, 1, '?')),
|
|
|
|
'<display-listitem>': '<display-outside>? && [ flow | flow-root ]? && list-item',
|
|
|
|
'<explicit-track-list>': '[ <line-names>? <track-size> ]+ <line-names>?',
|
|
|
|
'<family-name>': '<string> | <ident-not-generic-family> <ident>*',
|
2020-12-09 07:38:28 +00:00
|
|
|
// https://drafts.fxtf.org/filter-effects/#supported-filter-functions
|
|
|
|
// Value may be omitted in which case the default is used
|
|
|
|
'<filter-function>':
|
|
|
|
'blur( <length>? ) | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'brightness( <num-pct>? ) | ' +
|
|
|
|
'contrast( <num-pct>? ) | ' +
|
2020-12-09 07:38:28 +00:00
|
|
|
'drop-shadow( [ <length>{2,3} && <color>? ]? ) | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'grayscale( <num-pct>? ) | ' +
|
2020-12-09 07:38:28 +00:00
|
|
|
'hue-rotate( <angle-or-0>? ) | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'invert( <num-pct>? ) | ' +
|
|
|
|
'opacity( <num-pct>? ) | ' +
|
|
|
|
'saturate( <num-pct>? ) | ' +
|
|
|
|
'sepia( <num-pct>? )',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<filter-function-list>': '[ <filter-function> | <uri> ]+',
|
|
|
|
'<final-bg-layer>': '<color> || <bg-image> || <bg-position> [ / <bg-size> ]? || ' +
|
|
|
|
'<repeat-style> || <attachment> || <box>{1,2}',
|
|
|
|
'<fixed-repeat>':
|
2022-04-01 13:05:45 +00:00
|
|
|
'repeat( [ <int1+> ] , [ <line-names>? <fixed-size> ]+ <line-names>? )',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<fixed-size>': '<len-pct> | ' +
|
|
|
|
'minmax( <len-pct> , <track-breadth> ) | ' +
|
|
|
|
'minmax( <inflexible-breadth> , <len-pct> )',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<flex-shorthand>': 'none | [ <flex-grow> <flex-shrink>? || <flex-basis> ]',
|
|
|
|
'<font-family>': '[ <generic-family> | <family-name> ]#',
|
|
|
|
'<font-style>': 'normal | italic | oblique <angle>?',
|
|
|
|
'<font-short-core>': '<font-size> [ / <line-height> ]? <font-family>',
|
|
|
|
'<font-short-tweak-no-pct>':
|
|
|
|
'<font-style> || <font-variant-css21> || <font-weight> || <font-stretch-named>',
|
|
|
|
'<font-variant-alternates>': 'stylistic() || historical-forms || styleset() || ' +
|
|
|
|
'character-variant() || swash() || ornaments() || annotation()',
|
|
|
|
'<font-variant-ligatures>': '[ common-ligatures | no-common-ligatures ] || ' +
|
|
|
|
'[ discretionary-ligatures | no-discretionary-ligatures ] || ' +
|
|
|
|
'[ historical-ligatures | no-historical-ligatures ] || ' +
|
|
|
|
'[ contextual | no-contextual ]',
|
|
|
|
'<font-variant-numeric>': '[ lining-nums | oldstyle-nums ] || ' +
|
|
|
|
'[ proportional-nums | tabular-nums ] || ' +
|
|
|
|
'[ diagonal-fractions | stacked-fractions ] || ' +
|
|
|
|
'ordinal || slashed-zero',
|
|
|
|
'<font-variant-east-asian>': '[ jis78 | jis83 | jis90 | jis04 | simplified | traditional ] || ' +
|
|
|
|
'[ full-width | proportional-width ] || ruby',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<font-variant>': 'normal | none | [ ' +
|
|
|
|
'<font-variant-ligatures> || <font-variant-alternates> || ' +
|
|
|
|
'<font-variant-caps> || <font-variant-numeric> || <font-variant-east-asian> ]',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<grid-auto-columns>': '<track-size>+',
|
|
|
|
'<grid-auto-rows>': '<track-size>+',
|
|
|
|
'<grid-line>': 'auto | [ <integer> && <ident-for-grid>? ] | <ident-for-grid> | ' +
|
|
|
|
'[ span && [ <integer> || <ident-for-grid> ] ]',
|
|
|
|
'<grid-template>': 'none | [ <grid-template-rows> / <grid-template-columns> ] | ' +
|
|
|
|
'[ <line-names>? <string> <track-size>? <line-names>? ]+ ' +
|
|
|
|
'[ / <explicit-track-list> ]?',
|
|
|
|
'<grid-template-columns>': 'none | <track-list> | <auto-track-list>',
|
|
|
|
'<grid-template-rows>': '<grid-template-columns>',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<hsl-color>': '[ <number> | <angle> ] <pct>{2} [ / <num-pct0+> ]? | ' +
|
|
|
|
'[ <number> | <angle> ] , <pct>#{2} [ , <num-pct0+> ]?',
|
2022-04-01 13:05:45 +00:00
|
|
|
'<hwb-color>': '[ <number> | <angle> | none ] [ <pct> | none ] [ <pct> | none ] [ / [ <num-pct> | none ] ]?',
|
2021-02-01 15:51:24 +00:00
|
|
|
'<justify-content>': 'normal | <content-distribution> | ' +
|
2020-11-25 18:01:36 +00:00
|
|
|
'<overflow-position>? [ <content-position> | left | right ]',
|
|
|
|
'<justify-self>': 'auto | normal | stretch | <baseline-position> | <overflow-position>? ' +
|
|
|
|
'[ <self-position> | left | right ]',
|
|
|
|
'<overscroll>': 'contain | none | auto',
|
2021-01-01 16:43:28 +00:00
|
|
|
'<paint>': 'none | <color> | <uri> [ none | <color> ]? | context-fill | context-stroke',
|
2020-11-25 18:01:36 +00:00
|
|
|
// Because our `alt` combinator is ordered, we need to test these
|
|
|
|
// in order from longest possible match to shortest.
|
|
|
|
'<position>':
|
2021-09-07 09:36:36 +00:00
|
|
|
'[ [ left | right ] <len-pct> ] && [ [ top | bottom ] <len-pct> ] | ' +
|
|
|
|
'[ left | center | right | <len-pct> ] ' +
|
|
|
|
'[ top | center | bottom | <len-pct> ]? | ' +
|
2020-11-25 18:01:36 +00:00
|
|
|
'[ left | center | right ] || [ top | center | bottom ]',
|
|
|
|
'<repeat-style>': 'repeat-x | repeat-y | [ repeat | space | round | no-repeat ]{1,2}',
|
|
|
|
'<rgb-color>':
|
2021-09-07 09:36:36 +00:00
|
|
|
'[ <number>{3} | <pct>{3} ] [ / <num-pct0+> ]? | ' +
|
|
|
|
'[ <number>#{3} | <pct>#{3} ] [ , <num-pct0+> ]?',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<shadow>': 'inset? && [ <length>{2,4} && <color>? ]',
|
2020-12-09 07:38:28 +00:00
|
|
|
'<single-timing-function>':
|
|
|
|
'linear | <cubic-bezier-timing-function> | <step-timing-function> | frames( <integer> )',
|
|
|
|
'<step-timing-function>': 'step-start | step-end | ' +
|
|
|
|
'steps( <integer> [ , [ jump-start | jump-end | jump-none | jump-both | start | end ] ]? )',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<text-decoration-line>': 'none | [ underline || overline || line-through || blink ]',
|
|
|
|
'<text-emphasis-style>': 'none | ' +
|
|
|
|
'[ [ filled | open ] || [ dot | circle | double-circle | triangle | sesame ] ] | ' +
|
|
|
|
'<string>',
|
|
|
|
'<track-list>': '[ <line-names>? [ <track-size> | <track-repeat> ] ]+ <line-names>?',
|
2022-04-01 13:05:45 +00:00
|
|
|
'<track-repeat>': 'repeat( [ <int1+> ] , [ <line-names>? <track-size> ]+ <line-names>? )',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<track-size>': '<track-breadth> | minmax( <inflexible-breadth> , <track-breadth> ) | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'fit-content( <len-pct> )',
|
2020-12-09 07:38:28 +00:00
|
|
|
'<transform-function>':
|
|
|
|
'matrix( <number>#{6} ) | ' +
|
|
|
|
'matrix3d( <number>#{16} ) | ' +
|
2021-10-21 20:25:32 +00:00
|
|
|
'perspective( <len0+> | none ) | ' +
|
2022-01-10 04:30:22 +00:00
|
|
|
'rotate( <angle-or-0> | none ) | ' +
|
2020-12-09 07:38:28 +00:00
|
|
|
'rotate3d( <number>#{3} , <angle-or-0> ) | ' +
|
2021-10-21 20:25:32 +00:00
|
|
|
'rotateX( <angle-or-0> ) | ' +
|
|
|
|
'rotateY( <angle-or-0> ) | ' +
|
|
|
|
'rotateZ( <angle-or-0> ) | ' +
|
2022-01-10 04:30:22 +00:00
|
|
|
'scale( [ <num-pct> ]#{1,2} | none ) | ' +
|
2021-10-21 20:25:32 +00:00
|
|
|
'scale3d( <num-pct>#{3} ) | ' +
|
|
|
|
'scaleX( <num-pct> ) | ' +
|
|
|
|
'scaleY( <num-pct> ) | ' +
|
|
|
|
'scaleZ( <num-pct> ) | ' +
|
2020-12-09 07:38:28 +00:00
|
|
|
'skew( <angle-or-0> [ , <angle-or-0> ]? ) | ' +
|
|
|
|
'skewX( <angle-or-0> ) | ' +
|
|
|
|
'skewY( <angle-or-0> ) | ' +
|
2022-01-10 04:30:22 +00:00
|
|
|
'translate( <len-pct>#{1,2} | none ) | ' +
|
2021-09-07 09:36:36 +00:00
|
|
|
'translate3d( <len-pct>#{2} , <length> ) | ' +
|
|
|
|
'translateX( <len-pct> ) | ' +
|
|
|
|
'translateY( <len-pct> ) | ' +
|
2020-12-09 07:38:28 +00:00
|
|
|
'translateZ( <length> )',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<transform-origin>': '[ left | center | right | <len-pct> ] ' +
|
|
|
|
'[ top | center | bottom | <len-pct> ] <length>? | ' +
|
|
|
|
'[ left | center | right | top | bottom | <len-pct> ] | ' +
|
2020-11-25 18:01:36 +00:00
|
|
|
'[ [ center | left | right ] && [ center | top | bottom ] ] <length>?',
|
|
|
|
'<transition>': '[ none | [ all | <ident> ]# ] || <time> || <single-timing-function> || <time>',
|
2021-09-07 09:36:36 +00:00
|
|
|
'<width-height>': '<len-pct> | min-content | max-content | ' +
|
|
|
|
'fit-content | fit-content( <len-pct> ) | -moz-available | -webkit-fill-available',
|
2020-11-25 18:01:36 +00:00
|
|
|
'<will-change>': 'auto | <animateable-feature>#',
|
|
|
|
};
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region Colors
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2021-03-14 05:33:26 +00:00
|
|
|
const Colors = Object.assign(Object.create(null), {
|
|
|
|
// 'currentColor' color keyword
|
|
|
|
// https://www.w3.org/TR/css3-color/#currentcolor
|
|
|
|
currentColor: '',
|
|
|
|
transparent: '#0000',
|
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
aliceblue: '#f0f8ff',
|
|
|
|
antiquewhite: '#faebd7',
|
|
|
|
aqua: '#00ffff',
|
|
|
|
aquamarine: '#7fffd4',
|
|
|
|
azure: '#f0ffff',
|
|
|
|
beige: '#f5f5dc',
|
|
|
|
bisque: '#ffe4c4',
|
|
|
|
black: '#000000',
|
|
|
|
blanchedalmond: '#ffebcd',
|
|
|
|
blue: '#0000ff',
|
|
|
|
blueviolet: '#8a2be2',
|
|
|
|
brown: '#a52a2a',
|
|
|
|
burlywood: '#deb887',
|
|
|
|
cadetblue: '#5f9ea0',
|
|
|
|
chartreuse: '#7fff00',
|
|
|
|
chocolate: '#d2691e',
|
|
|
|
coral: '#ff7f50',
|
|
|
|
cornflowerblue: '#6495ed',
|
|
|
|
cornsilk: '#fff8dc',
|
|
|
|
crimson: '#dc143c',
|
|
|
|
cyan: '#00ffff',
|
|
|
|
darkblue: '#00008b',
|
|
|
|
darkcyan: '#008b8b',
|
|
|
|
darkgoldenrod: '#b8860b',
|
|
|
|
darkgray: '#a9a9a9',
|
|
|
|
darkgrey: '#a9a9a9',
|
|
|
|
darkgreen: '#006400',
|
|
|
|
darkkhaki: '#bdb76b',
|
|
|
|
darkmagenta: '#8b008b',
|
|
|
|
darkolivegreen: '#556b2f',
|
|
|
|
darkorange: '#ff8c00',
|
|
|
|
darkorchid: '#9932cc',
|
|
|
|
darkred: '#8b0000',
|
|
|
|
darksalmon: '#e9967a',
|
|
|
|
darkseagreen: '#8fbc8f',
|
|
|
|
darkslateblue: '#483d8b',
|
|
|
|
darkslategray: '#2f4f4f',
|
|
|
|
darkslategrey: '#2f4f4f',
|
|
|
|
darkturquoise: '#00ced1',
|
|
|
|
darkviolet: '#9400d3',
|
|
|
|
deeppink: '#ff1493',
|
|
|
|
deepskyblue: '#00bfff',
|
|
|
|
dimgray: '#696969',
|
|
|
|
dimgrey: '#696969',
|
|
|
|
dodgerblue: '#1e90ff',
|
|
|
|
firebrick: '#b22222',
|
|
|
|
floralwhite: '#fffaf0',
|
|
|
|
forestgreen: '#228b22',
|
|
|
|
fuchsia: '#ff00ff',
|
|
|
|
gainsboro: '#dcdcdc',
|
|
|
|
ghostwhite: '#f8f8ff',
|
|
|
|
gold: '#ffd700',
|
|
|
|
goldenrod: '#daa520',
|
|
|
|
gray: '#808080',
|
|
|
|
grey: '#808080',
|
|
|
|
green: '#008000',
|
|
|
|
greenyellow: '#adff2f',
|
|
|
|
honeydew: '#f0fff0',
|
|
|
|
hotpink: '#ff69b4',
|
|
|
|
indianred: '#cd5c5c',
|
|
|
|
indigo: '#4b0082',
|
|
|
|
ivory: '#fffff0',
|
|
|
|
khaki: '#f0e68c',
|
|
|
|
lavender: '#e6e6fa',
|
|
|
|
lavenderblush: '#fff0f5',
|
|
|
|
lawngreen: '#7cfc00',
|
|
|
|
lemonchiffon: '#fffacd',
|
|
|
|
lightblue: '#add8e6',
|
|
|
|
lightcoral: '#f08080',
|
|
|
|
lightcyan: '#e0ffff',
|
|
|
|
lightgoldenrodyellow: '#fafad2',
|
|
|
|
lightgray: '#d3d3d3',
|
|
|
|
lightgrey: '#d3d3d3',
|
|
|
|
lightgreen: '#90ee90',
|
|
|
|
lightpink: '#ffb6c1',
|
|
|
|
lightsalmon: '#ffa07a',
|
|
|
|
lightseagreen: '#20b2aa',
|
|
|
|
lightskyblue: '#87cefa',
|
|
|
|
lightslategray: '#778899',
|
|
|
|
lightslategrey: '#778899',
|
|
|
|
lightsteelblue: '#b0c4de',
|
|
|
|
lightyellow: '#ffffe0',
|
|
|
|
lime: '#00ff00',
|
|
|
|
limegreen: '#32cd32',
|
|
|
|
linen: '#faf0e6',
|
|
|
|
magenta: '#ff00ff',
|
|
|
|
maroon: '#800000',
|
|
|
|
mediumaquamarine: '#66cdaa',
|
|
|
|
mediumblue: '#0000cd',
|
|
|
|
mediumorchid: '#ba55d3',
|
|
|
|
mediumpurple: '#9370db',
|
|
|
|
mediumseagreen: '#3cb371',
|
|
|
|
mediumslateblue: '#7b68ee',
|
|
|
|
mediumspringgreen: '#00fa9a',
|
|
|
|
mediumturquoise: '#48d1cc',
|
|
|
|
mediumvioletred: '#c71585',
|
|
|
|
midnightblue: '#191970',
|
|
|
|
mintcream: '#f5fffa',
|
|
|
|
mistyrose: '#ffe4e1',
|
|
|
|
moccasin: '#ffe4b5',
|
|
|
|
navajowhite: '#ffdead',
|
|
|
|
navy: '#000080',
|
|
|
|
oldlace: '#fdf5e6',
|
|
|
|
olive: '#808000',
|
|
|
|
olivedrab: '#6b8e23',
|
|
|
|
orange: '#ffa500',
|
|
|
|
orangered: '#ff4500',
|
|
|
|
orchid: '#da70d6',
|
|
|
|
palegoldenrod: '#eee8aa',
|
|
|
|
palegreen: '#98fb98',
|
|
|
|
paleturquoise: '#afeeee',
|
|
|
|
palevioletred: '#db7093',
|
|
|
|
papayawhip: '#ffefd5',
|
|
|
|
peachpuff: '#ffdab9',
|
|
|
|
peru: '#cd853f',
|
|
|
|
pink: '#ffc0cb',
|
|
|
|
plum: '#dda0dd',
|
|
|
|
powderblue: '#b0e0e6',
|
|
|
|
purple: '#800080',
|
|
|
|
rebeccapurple: '#663399',
|
|
|
|
red: '#ff0000',
|
|
|
|
rosybrown: '#bc8f8f',
|
|
|
|
royalblue: '#4169e1',
|
|
|
|
saddlebrown: '#8b4513',
|
|
|
|
salmon: '#fa8072',
|
|
|
|
sandybrown: '#f4a460',
|
|
|
|
seagreen: '#2e8b57',
|
|
|
|
seashell: '#fff5ee',
|
|
|
|
sienna: '#a0522d',
|
|
|
|
silver: '#c0c0c0',
|
|
|
|
skyblue: '#87ceeb',
|
|
|
|
slateblue: '#6a5acd',
|
|
|
|
slategray: '#708090',
|
|
|
|
slategrey: '#708090',
|
|
|
|
snow: '#fffafa',
|
|
|
|
springgreen: '#00ff7f',
|
|
|
|
steelblue: '#4682b4',
|
|
|
|
tan: '#d2b48c',
|
|
|
|
teal: '#008080',
|
|
|
|
thistle: '#d8bfd8',
|
|
|
|
tomato: '#ff6347',
|
|
|
|
turquoise: '#40e0d0',
|
|
|
|
violet: '#ee82ee',
|
|
|
|
wheat: '#f5deb3',
|
|
|
|
white: '#ffffff',
|
|
|
|
whitesmoke: '#f5f5f5',
|
|
|
|
yellow: '#ffff00',
|
|
|
|
yellowgreen: '#9acd32',
|
|
|
|
|
2021-03-14 05:33:26 +00:00
|
|
|
// old = CSS2 system colors: https://www.w3.org/TR/css3-color/#css2-system
|
|
|
|
// new = CSS4 system colors: https://drafts.csswg.org/css-color-4/#css-system-colors
|
|
|
|
ActiveBorder: '',
|
|
|
|
ActiveCaption: '',
|
|
|
|
ActiveText: '', // new
|
|
|
|
AppWorkspace: '',
|
|
|
|
Background: '',
|
|
|
|
ButtonBorder: '', // new
|
|
|
|
ButtonFace: '', // old+new
|
|
|
|
ButtonHighlight: '',
|
|
|
|
ButtonShadow: '',
|
|
|
|
ButtonText: '', // old+new
|
|
|
|
Canvas: '', // new
|
|
|
|
CanvasText: '', // new
|
|
|
|
CaptionText: '',
|
|
|
|
Field: '', // new
|
|
|
|
FieldText: '', // new
|
|
|
|
GrayText: '', // old+new
|
|
|
|
Highlight: '', // old+new
|
|
|
|
HighlightText: '', // old+new
|
|
|
|
InactiveBorder: '',
|
|
|
|
InactiveCaption: '',
|
|
|
|
InactiveCaptionText: '',
|
|
|
|
InfoBackground: '',
|
|
|
|
InfoText: '',
|
|
|
|
LinkText: '', // new
|
|
|
|
Mark: '', // new
|
|
|
|
MarkText: '', // new
|
|
|
|
Menu: '',
|
|
|
|
MenuText: '',
|
|
|
|
Scrollbar: '',
|
|
|
|
ThreeDDarkShadow: '',
|
|
|
|
ThreeDFace: '',
|
|
|
|
ThreeDHighlight: '',
|
|
|
|
ThreeDLightShadow: '',
|
|
|
|
ThreeDShadow: '',
|
|
|
|
VisitedText: '', // new
|
|
|
|
Window: '',
|
|
|
|
WindowFrame: '',
|
|
|
|
WindowText: '',
|
|
|
|
});
|
|
|
|
const ColorsLC = new Set(Object.keys(Colors).map(lower));
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region Tokens
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/* https://www.w3.org/TR/css3-syntax/#lexical */
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @type {Object<string,number|Object>} */
|
2020-06-17 13:13:28 +00:00
|
|
|
const Tokens = Object.assign([], {
|
|
|
|
EOF: {}, // must be the first token
|
|
|
|
}, {
|
2017-12-26 20:39:52 +00:00
|
|
|
// HTML-style comments
|
2020-06-17 13:13:28 +00:00
|
|
|
CDC: {},
|
2020-11-25 18:01:36 +00:00
|
|
|
CDO: {},
|
2017-12-26 20:39:52 +00:00
|
|
|
// ignorables
|
2020-11-25 18:01:36 +00:00
|
|
|
COMMENT: {hide: true},
|
|
|
|
S: {},
|
2017-12-26 20:39:52 +00:00
|
|
|
// attribute equality
|
2020-06-17 13:13:28 +00:00
|
|
|
DASHMATCH: {text: '|='},
|
2020-11-25 18:01:36 +00:00
|
|
|
INCLUDES: {text: '~='},
|
2020-06-17 13:13:28 +00:00
|
|
|
PREFIXMATCH: {text: '^='},
|
|
|
|
SUBSTRINGMATCH: {text: '*='},
|
2020-11-25 18:01:36 +00:00
|
|
|
SUFFIXMATCH: {text: '$='},
|
2017-12-26 20:39:52 +00:00
|
|
|
// identifier types
|
2020-06-17 13:13:28 +00:00
|
|
|
HASH: {},
|
2020-11-25 18:01:36 +00:00
|
|
|
IDENT: {},
|
|
|
|
STRING: {},
|
2017-12-26 20:39:52 +00:00
|
|
|
// at-keywords
|
2020-11-25 18:01:36 +00:00
|
|
|
CHARSET_SYM: {text: '@charset'},
|
|
|
|
DOCUMENT_SYM: {text: ['@document', '@-moz-document']},
|
|
|
|
FONT_FACE_SYM: {text: '@font-face'},
|
2022-04-01 13:05:45 +00:00
|
|
|
FONT_PALETTE_VALUES_SYM: {text: '@font-palette-values'},
|
2020-06-17 13:13:28 +00:00
|
|
|
IMPORT_SYM: {text: '@import'},
|
2020-11-25 18:01:36 +00:00
|
|
|
KEYFRAMES_SYM: {text: ['@keyframes', '@-webkit-keyframes', '@-moz-keyframes', '@-o-keyframes']},
|
2022-02-03 21:43:08 +00:00
|
|
|
LAYER_SYM: {text: '@layer'},
|
2020-06-17 13:13:28 +00:00
|
|
|
MEDIA_SYM: {text: '@media'},
|
|
|
|
NAMESPACE_SYM: {text: '@namespace'},
|
2020-11-25 18:01:36 +00:00
|
|
|
PAGE_SYM: {text: '@page'},
|
2020-06-17 13:13:28 +00:00
|
|
|
SUPPORTS_SYM: {text: '@supports'},
|
2020-11-25 18:01:36 +00:00
|
|
|
UNKNOWN_SYM: {},
|
2020-06-17 13:13:28 +00:00
|
|
|
VIEWPORT_SYM: {text: ['@viewport', '@-ms-viewport', '@-o-viewport']},
|
2017-12-26 20:39:52 +00:00
|
|
|
// measurements
|
2020-06-17 13:13:28 +00:00
|
|
|
ANGLE: {},
|
|
|
|
DIMENSION: {},
|
2020-11-25 18:01:36 +00:00
|
|
|
FREQ: {},
|
|
|
|
LENGTH: {},
|
2020-06-17 13:13:28 +00:00
|
|
|
NUMBER: {},
|
2020-11-25 18:01:36 +00:00
|
|
|
PERCENTAGE: {},
|
|
|
|
TIME: {},
|
2017-12-26 20:39:52 +00:00
|
|
|
// functions
|
2020-06-17 13:13:28 +00:00
|
|
|
FUNCTION: {},
|
2020-11-25 18:01:36 +00:00
|
|
|
URI: {},
|
2017-12-26 20:39:52 +00:00
|
|
|
// Unicode ranges
|
2020-06-17 13:13:28 +00:00
|
|
|
UNICODE_RANGE: {},
|
2017-12-26 20:39:52 +00:00
|
|
|
// invalid string
|
2020-06-17 13:13:28 +00:00
|
|
|
INVALID: {},
|
2017-12-26 20:39:52 +00:00
|
|
|
// combinators
|
2020-11-25 18:01:36 +00:00
|
|
|
COLUMN: {text: '||'},
|
2020-06-17 13:13:28 +00:00
|
|
|
COMMA: {text: ','},
|
2020-11-25 18:01:36 +00:00
|
|
|
GREATER: {text: '>'},
|
|
|
|
PLUS: {text: '+'},
|
2020-06-17 13:13:28 +00:00
|
|
|
TILDE: {text: '~'},
|
2017-12-26 20:39:52 +00:00
|
|
|
// modifier
|
2020-06-17 13:13:28 +00:00
|
|
|
ANY: {text: ['any', '-webkit-any', '-moz-any']},
|
2022-06-30 10:36:40 +00:00
|
|
|
HAS: {},
|
2020-06-17 13:13:28 +00:00
|
|
|
IS: {},
|
2020-11-25 18:01:36 +00:00
|
|
|
NOT: {},
|
2020-06-26 16:47:12 +00:00
|
|
|
WHERE: {},
|
2020-11-25 18:01:36 +00:00
|
|
|
// CSS3 Paged Media
|
|
|
|
BOTTOMCENTER_SYM: {text: '@bottom-center'},
|
2020-06-17 13:13:28 +00:00
|
|
|
BOTTOMLEFTCORNER_SYM: {text: '@bottom-left-corner'},
|
|
|
|
BOTTOMLEFT_SYM: {text: '@bottom-left'},
|
|
|
|
BOTTOMRIGHTCORNER_SYM: {text: '@bottom-right-corner'},
|
2020-11-25 18:01:36 +00:00
|
|
|
BOTTOMRIGHT_SYM: {text: '@bottom-right'},
|
2020-06-17 13:13:28 +00:00
|
|
|
LEFTBOTTOM_SYM: {text: '@left-bottom'},
|
2020-11-25 18:01:36 +00:00
|
|
|
LEFTMIDDLE_SYM: {text: '@left-middle'},
|
|
|
|
LEFTTOP_SYM: {text: '@left-top'},
|
2020-06-17 13:13:28 +00:00
|
|
|
RIGHTBOTTOM_SYM: {text: '@right-bottom'},
|
2020-11-25 18:01:36 +00:00
|
|
|
RIGHTMIDDLE_SYM: {text: '@right-middle'},
|
|
|
|
RIGHTTOP_SYM: {text: '@right-top'},
|
|
|
|
TOPCENTER_SYM: {text: '@top-center'},
|
|
|
|
TOPLEFTCORNER_SYM: {text: '@top-left-corner'},
|
|
|
|
TOPLEFT_SYM: {text: '@top-left'},
|
|
|
|
TOPRIGHTCORNER_SYM: {text: '@top-right-corner'},
|
|
|
|
TOPRIGHT_SYM: {text: '@top-right'},
|
|
|
|
/* CSS3 Media Queries */
|
2020-06-17 13:13:28 +00:00
|
|
|
RESOLUTION: {state: 'media'},
|
2017-12-26 20:39:52 +00:00
|
|
|
/*
|
2020-11-25 18:01:36 +00:00
|
|
|
* The following token names are not defined in any CSS specification.
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
2020-06-17 13:13:28 +00:00
|
|
|
CHAR: {},
|
2020-11-25 18:01:36 +00:00
|
|
|
COLON: {text: ':'},
|
|
|
|
DOT: {text: '.'},
|
|
|
|
EQUALS: {text: '='},
|
|
|
|
IE_FUNCTION: {},
|
|
|
|
IMPORTANT: {},
|
2020-06-17 13:13:28 +00:00
|
|
|
LBRACE: {text: '{', endChar: '}'},
|
|
|
|
LBRACKET: {text: '[', endChar: ']'},
|
|
|
|
LPAREN: {text: '(', endChar: ')'},
|
2020-11-25 18:01:36 +00:00
|
|
|
MINUS: {text: '-'},
|
|
|
|
PIPE: {text: '|'},
|
|
|
|
RBRACE: {text: '}'},
|
|
|
|
RBRACKET: {text: ']'},
|
2020-06-17 13:13:28 +00:00
|
|
|
RPAREN: {text: ')'},
|
2020-11-25 18:01:36 +00:00
|
|
|
SEMICOLON: {text: ';'},
|
|
|
|
SLASH: {text: '/'},
|
|
|
|
STAR: {text: '*'},
|
|
|
|
USO_VAR: {},
|
2020-06-17 13:13:28 +00:00
|
|
|
});
|
|
|
|
// make Tokens an array of tokens, store the index in original prop, add 'name' to each token
|
|
|
|
const typeMap = new Map();
|
|
|
|
for (const [k, val] of Object.entries(Tokens)) {
|
|
|
|
const index = Tokens[k] = Tokens.length;
|
|
|
|
val.name = k;
|
|
|
|
Tokens.push(val);
|
|
|
|
const {text} = val;
|
|
|
|
if (text) {
|
|
|
|
for (const item of Array.isArray(text) ? text : [text]) {
|
|
|
|
typeMap.set(item, index);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 13:13:28 +00:00
|
|
|
Tokens.UNKNOWN = -1;
|
|
|
|
Tokens.name = index => (Tokens[index] || {}).name;
|
2020-11-25 18:01:36 +00:00
|
|
|
Tokens.type = text => typeMap.get(text) || Tokens.UNKNOWN;
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-10-24 10:18:17 +00:00
|
|
|
const TT = {
|
|
|
|
attrMatch: [
|
|
|
|
Tokens.PREFIXMATCH,
|
|
|
|
Tokens.SUFFIXMATCH,
|
|
|
|
Tokens.SUBSTRINGMATCH,
|
|
|
|
Tokens.EQUALS,
|
|
|
|
Tokens.INCLUDES,
|
|
|
|
Tokens.DASHMATCH,
|
|
|
|
],
|
|
|
|
combinator: [
|
|
|
|
Tokens.PLUS,
|
|
|
|
Tokens.GREATER,
|
|
|
|
Tokens.TILDE,
|
|
|
|
Tokens.COLUMN,
|
|
|
|
],
|
|
|
|
cruft: [
|
|
|
|
Tokens.S,
|
|
|
|
Tokens.CDO,
|
|
|
|
Tokens.CDC,
|
|
|
|
],
|
|
|
|
expression: [
|
|
|
|
Tokens.PLUS,
|
|
|
|
Tokens.MINUS,
|
|
|
|
Tokens.DIMENSION,
|
|
|
|
Tokens.NUMBER,
|
|
|
|
Tokens.STRING,
|
|
|
|
Tokens.IDENT,
|
|
|
|
Tokens.LENGTH,
|
|
|
|
Tokens.FREQ,
|
|
|
|
Tokens.ANGLE,
|
|
|
|
Tokens.TIME,
|
|
|
|
Tokens.RESOLUTION,
|
|
|
|
Tokens.SLASH,
|
|
|
|
],
|
|
|
|
identString: [
|
|
|
|
Tokens.IDENT,
|
|
|
|
Tokens.STRING,
|
2021-08-24 11:38:19 +00:00
|
|
|
Tokens.USO_VAR,
|
2020-10-24 10:18:17 +00:00
|
|
|
],
|
|
|
|
LParenBracket: [
|
|
|
|
Tokens.LPAREN,
|
|
|
|
Tokens.LBRACKET,
|
|
|
|
],
|
|
|
|
LParenBracketBrace: [
|
|
|
|
Tokens.LPAREN,
|
|
|
|
Tokens.LBRACKET,
|
|
|
|
Tokens.LBRACE,
|
|
|
|
],
|
|
|
|
margins: [
|
|
|
|
Tokens.TOPLEFTCORNER_SYM,
|
|
|
|
Tokens.TOPLEFT_SYM,
|
|
|
|
Tokens.TOPCENTER_SYM,
|
|
|
|
Tokens.TOPRIGHT_SYM,
|
|
|
|
Tokens.TOPRIGHTCORNER_SYM,
|
|
|
|
Tokens.BOTTOMLEFTCORNER_SYM,
|
|
|
|
Tokens.BOTTOMLEFT_SYM,
|
|
|
|
Tokens.BOTTOMCENTER_SYM,
|
|
|
|
Tokens.BOTTOMRIGHT_SYM,
|
|
|
|
Tokens.BOTTOMRIGHTCORNER_SYM,
|
|
|
|
Tokens.LEFTTOP_SYM,
|
|
|
|
Tokens.LEFTMIDDLE_SYM,
|
|
|
|
Tokens.LEFTBOTTOM_SYM,
|
|
|
|
Tokens.RIGHTTOP_SYM,
|
|
|
|
Tokens.RIGHTMIDDLE_SYM,
|
|
|
|
Tokens.RIGHTBOTTOM_SYM,
|
|
|
|
],
|
2022-06-24 07:22:49 +00:00
|
|
|
mediaValue: [
|
|
|
|
Tokens.IDENT,
|
|
|
|
Tokens.NUMBER,
|
|
|
|
Tokens.DIMENSION,
|
|
|
|
Tokens.LENGTH,
|
|
|
|
],
|
2020-10-24 10:18:17 +00:00
|
|
|
op: [
|
|
|
|
Tokens.SLASH,
|
|
|
|
Tokens.COMMA,
|
|
|
|
],
|
|
|
|
opInFunc: [
|
|
|
|
Tokens.SLASH,
|
|
|
|
Tokens.COMMA,
|
|
|
|
Tokens.PLUS,
|
|
|
|
Tokens.STAR,
|
|
|
|
Tokens.MINUS,
|
|
|
|
],
|
|
|
|
plusMinus: [
|
|
|
|
Tokens.MINUS,
|
|
|
|
Tokens.PLUS,
|
|
|
|
],
|
2020-11-25 18:01:36 +00:00
|
|
|
pseudo: [
|
|
|
|
Tokens.FUNCTION,
|
|
|
|
Tokens.IDENT,
|
|
|
|
],
|
|
|
|
semiS: [
|
|
|
|
Tokens.SEMICOLON,
|
|
|
|
Tokens.S,
|
|
|
|
],
|
2020-10-24 10:18:17 +00:00
|
|
|
stringUri: [
|
|
|
|
Tokens.STRING,
|
|
|
|
Tokens.URI,
|
2021-08-24 11:38:19 +00:00
|
|
|
Tokens.USO_VAR,
|
2020-10-24 10:18:17 +00:00
|
|
|
],
|
|
|
|
term: [
|
|
|
|
Tokens.NUMBER,
|
|
|
|
Tokens.PERCENTAGE,
|
|
|
|
Tokens.LENGTH,
|
|
|
|
Tokens.ANGLE,
|
|
|
|
Tokens.TIME,
|
|
|
|
Tokens.DIMENSION,
|
|
|
|
Tokens.FREQ,
|
|
|
|
Tokens.STRING,
|
|
|
|
Tokens.IDENT,
|
|
|
|
Tokens.URI,
|
|
|
|
Tokens.UNICODE_RANGE,
|
|
|
|
Tokens.USO_VAR,
|
|
|
|
],
|
|
|
|
usoS: [
|
|
|
|
Tokens.USO_VAR,
|
|
|
|
Tokens.S,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region StringReader
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
class StringReader {
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
constructor(text) {
|
|
|
|
this._input = text.replace(/\r\n?/g, '\n');
|
|
|
|
this._line = 1;
|
|
|
|
this._col = 1;
|
|
|
|
this._cursor = 0;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
eof() {
|
|
|
|
return this._cursor >= this._input.length;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
peek(count = 1) {
|
|
|
|
return this._input[this._cursor + count - 1] || null;
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
peekTest(stickyRx) {
|
2020-12-09 07:38:28 +00:00
|
|
|
stickyRx.lastIndex = this._cursor;
|
|
|
|
return stickyRx.test(this._input);
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
read() {
|
|
|
|
const c = this._input[this._cursor];
|
|
|
|
if (!c) return null;
|
|
|
|
if (c === '\n') {
|
|
|
|
this._line++;
|
|
|
|
this._col = 1;
|
|
|
|
} else {
|
|
|
|
this._col++;
|
2018-01-06 06:48:56 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
this._cursor++;
|
|
|
|
return c;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
mark() {
|
|
|
|
this._bookmark = {
|
|
|
|
cursor: this._cursor,
|
|
|
|
line: this._line,
|
|
|
|
col: this._col,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
if (this._bookmark) {
|
|
|
|
this._cursor = this._bookmark.cursor;
|
|
|
|
this._line = this._bookmark.line;
|
|
|
|
this._col = this._bookmark.col;
|
|
|
|
delete this._bookmark;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* Reads up to and including the given string.
|
|
|
|
* @param {String} pattern The string to read.
|
|
|
|
* @return {String} The string when it is found.
|
|
|
|
* @throws Error when the string pattern is not found.
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
2018-01-06 11:05:20 +00:00
|
|
|
readTo(pattern) {
|
|
|
|
const i = this._input.indexOf(pattern, this._cursor);
|
2020-11-25 18:01:36 +00:00
|
|
|
if (i < 0) throw new Error(`Expected '${pattern}'.`);
|
2018-01-06 11:05:20 +00:00
|
|
|
return this.readCount(i - this._cursor + pattern.length);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* Reads characters that match either text or a regular expression and returns those characters.
|
|
|
|
* If a match is found, the row and column are adjusted.
|
|
|
|
* @param {String|RegExp} matcher
|
|
|
|
* @return {String} string or null if there was no match.
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
2018-01-06 11:05:20 +00:00
|
|
|
readMatch(matcher) {
|
|
|
|
if (matcher.sticky) {
|
2020-12-09 07:38:28 +00:00
|
|
|
matcher.lastIndex = this._cursor;
|
|
|
|
return matcher.test(this._input) ?
|
2018-01-06 11:05:20 +00:00
|
|
|
this.readCount(RegExp.lastMatch.length) :
|
|
|
|
null;
|
|
|
|
}
|
|
|
|
if (typeof matcher === 'string') {
|
|
|
|
if (this._input[this._cursor] === matcher[0] &&
|
|
|
|
this._input.substr(this._cursor, matcher.length) === matcher) {
|
|
|
|
return this.readCount(matcher.length);
|
|
|
|
}
|
|
|
|
} else if (matcher instanceof RegExp) {
|
|
|
|
if (matcher.test(this._input.substr(this._cursor))) {
|
|
|
|
return this.readCount(RegExp.lastMatch.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* Reads a given number of characters. If the end of the input is reached,
|
|
|
|
* it reads only the remaining characters and does not throw an error.
|
|
|
|
* @param {int} count The number of characters to read.
|
|
|
|
* @return {String} string or null if already at EOF
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
2018-01-06 11:05:20 +00:00
|
|
|
readCount(count) {
|
|
|
|
const len = this._input.length;
|
|
|
|
if (this._cursor >= len) return null;
|
|
|
|
if (!count) return '';
|
|
|
|
const text = this._input.substr(this._cursor, count);
|
|
|
|
this._cursor = Math.min(this._cursor + count, len);
|
|
|
|
let prev = -1;
|
|
|
|
for (let i = 0; (i = text.indexOf('\n', i)) >= 0; prev = i, i++) this._line++;
|
|
|
|
this._col = prev < 0 ? this._col + count : count - prev;
|
|
|
|
return text;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region Matcher
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* Reuses a Matcher for a ValidationTypes definition string instead of reparsing it.
|
|
|
|
* @type {Map<string, Matcher>}
|
|
|
|
*/
|
2020-11-25 18:01:36 +00:00
|
|
|
const matcherCache = new Map();
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* This class implements a combinator library for matcher functions.
|
|
|
|
* https://developer.mozilla.org/docs/Web/CSS/Value_definition_syntax#Component_value_combinators
|
|
|
|
*/
|
|
|
|
class Matcher {
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-07 20:26:47 +00:00
|
|
|
constructor(matchFunc, toString, options) {
|
2018-01-06 11:05:20 +00:00
|
|
|
this.matchFunc = matchFunc;
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @type {function(?number):string} */
|
2018-01-06 11:05:20 +00:00
|
|
|
this.toString = typeof toString === 'function' ? toString : () => toString;
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @type {?Matcher[]} */
|
|
|
|
this.options = options;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/**
|
|
|
|
* @param {PropertyValueIterator} e
|
|
|
|
* @return {?boolean}
|
|
|
|
*/
|
|
|
|
match(e) {
|
2021-09-07 09:36:36 +00:00
|
|
|
e._marks.push(e._i);
|
|
|
|
return e.popMark(this.matchFunc(e));
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
braces(min, max, marker, sep) {
|
|
|
|
return new Matcher(Matcher.funcBraces, Matcher.toStringBraces, {
|
|
|
|
min, max, marker,
|
|
|
|
sep: sep && Matcher.seq(sep, this),
|
|
|
|
embraced: this,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
static parse(str) {
|
2020-11-25 18:01:36 +00:00
|
|
|
let m = matcherCache.get(str);
|
2018-01-06 11:05:20 +00:00
|
|
|
if (m) return m;
|
|
|
|
m = Matcher.doParse(str);
|
2020-11-25 18:01:36 +00:00
|
|
|
matcherCache.set(str, m);
|
2018-01-06 11:05:20 +00:00
|
|
|
return m;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/** Simple recursive-descent grammar to build matchers from strings. */
|
|
|
|
static doParse(str) {
|
|
|
|
const reader = new StringReader(str);
|
2020-10-24 10:18:17 +00:00
|
|
|
const result = Matcher.parseGrammar(reader);
|
2018-01-06 11:05:20 +00:00
|
|
|
if (!reader.eof()) {
|
2020-11-25 18:01:36 +00:00
|
|
|
throw new Error('Internal grammar error. ' +
|
|
|
|
`Expected end of string at ${reader._cursor}: ${reader._input}.`);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
return result;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
static cast(m) {
|
|
|
|
return m instanceof Matcher ? m : Matcher.parse(m);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
// Matcher for a single type.
|
|
|
|
static fromType(type) {
|
2020-11-25 18:01:36 +00:00
|
|
|
let m = matcherCache.get(type);
|
2018-01-06 11:05:20 +00:00
|
|
|
if (m) return m;
|
2020-11-25 18:01:36 +00:00
|
|
|
m = new Matcher(Matcher.funcFromType, type, type);
|
|
|
|
matcherCache.set(type, m);
|
2018-01-06 11:05:20 +00:00
|
|
|
return m;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
/**
|
|
|
|
* @param {string} name - functio name
|
|
|
|
* @param {Matcher} body - matcher for function body
|
|
|
|
* @returns {Matcher}
|
|
|
|
*/
|
|
|
|
static func(name, body) {
|
|
|
|
return new Matcher(Matcher.funcFunc, Matcher.toStringFunc, {name, body});
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
// Matcher for one or more juxtaposed words, which all must occur, in the given order.
|
|
|
|
static seq(...args) {
|
|
|
|
const ms = args.map(Matcher.cast);
|
|
|
|
if (ms.length === 1) return ms[0];
|
2020-11-25 18:01:36 +00:00
|
|
|
return new Matcher(Matcher.funcSeq, Matcher.toStringSeq, ms);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
// Matcher for one or more alternatives, where exactly one must occur.
|
|
|
|
static alt(...args) {
|
|
|
|
const ms = args.map(Matcher.cast);
|
|
|
|
if (ms.length === 1) return ms[0];
|
2020-11-25 18:01:36 +00:00
|
|
|
return new Matcher(Matcher.funcAlt, Matcher.toStringAlt, ms);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Matcher for two or more options: double bar (||) and double ampersand (&&) operators,
|
|
|
|
* as well as variants of && where some of the alternatives are optional.
|
|
|
|
* This will backtrack through even successful matches to try to
|
|
|
|
* maximize the number of items matched.
|
|
|
|
*/
|
|
|
|
static many(required, ...args) {
|
2020-11-25 18:01:36 +00:00
|
|
|
const ms = args.map(Matcher.cast);
|
|
|
|
const m = new Matcher(Matcher.funcMany, Matcher.toStringMany, ms);
|
|
|
|
m.required = required === true ? Array(ms.length).fill(true) : required;
|
2018-01-07 20:26:47 +00:00
|
|
|
return m;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/**************************** matchFunc **********************/
|
2018-01-06 11:05:20 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/**
|
|
|
|
* @this {Matcher}
|
|
|
|
* @param {PropertyValueIterator} expr
|
|
|
|
*/
|
|
|
|
static funcAlt(expr) {
|
|
|
|
return this.options.some(Matcher.invoke, expr);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
/**
|
|
|
|
* @this {Matcher}
|
|
|
|
* @param {PropertyValueIterator} expr
|
|
|
|
*/
|
|
|
|
static funcBraces(expr) {
|
|
|
|
const {min, max, sep, embraced} = this.options;
|
|
|
|
let i = 0;
|
|
|
|
while (i < max && (i && sep || embraced).match(expr)) {
|
|
|
|
i++;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
return i >= min;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
/**
|
|
|
|
* @this {Matcher}
|
|
|
|
* @param {PropertyValueIterator} expr
|
|
|
|
*/
|
|
|
|
static funcFromType(expr) {
|
2020-12-09 07:38:28 +00:00
|
|
|
const part = expr.peek();
|
|
|
|
if (!part) return;
|
|
|
|
const type = this.options;
|
|
|
|
let result, m;
|
|
|
|
if (part.isVar) {
|
|
|
|
result = true;
|
|
|
|
} else if (!type.startsWith('<')) {
|
|
|
|
result = vtIsLiteral(type, part);
|
|
|
|
} else if ((m = VTSimple[type])) {
|
|
|
|
result = m.call(VTSimple, part);
|
|
|
|
} else {
|
|
|
|
m = VTComplex[type];
|
|
|
|
return m instanceof Matcher ?
|
|
|
|
m.match(expr) :
|
|
|
|
m.call(VTComplex, expr);
|
|
|
|
}
|
|
|
|
if (!result && expr.tryAttr && part.isAttr) {
|
|
|
|
result = vtIsAttr(part);
|
|
|
|
}
|
|
|
|
if (result) expr.next();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @this {Matcher}
|
|
|
|
* @param {PropertyValueIterator} expr
|
|
|
|
*/
|
|
|
|
static funcFunc(expr) {
|
|
|
|
const p = expr.peek();
|
|
|
|
if (p && p.expr && p.tokenType === Tokens.FUNCTION && lowerCmp(p.name, this.options.name)) {
|
|
|
|
let res = hasVarParts(p.expr);
|
|
|
|
if (!res) {
|
|
|
|
const vi = new PropertyValueIterator(p.expr); // eslint-disable-line no-use-before-define
|
|
|
|
res = this.options.body.match(vi) && !vi.hasNext;
|
|
|
|
}
|
|
|
|
return res && expr.next();
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @this {PropertyValueIterator}
|
|
|
|
* @param {Matcher} m
|
|
|
|
*/
|
|
|
|
static invoke(m) {
|
|
|
|
return m.match(this);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @this {Matcher}
|
|
|
|
* @param {PropertyValueIterator} expr
|
|
|
|
*/
|
|
|
|
static funcMany(expr) {
|
2018-01-07 20:26:47 +00:00
|
|
|
const seen = [];
|
2020-11-25 18:01:36 +00:00
|
|
|
const {/** @type {Matcher[]} */options: ms, required} = this;
|
2018-01-07 20:26:47 +00:00
|
|
|
let max = 0;
|
|
|
|
let pass = 0;
|
|
|
|
// If couldn't get a complete match, retrace our steps to make the
|
|
|
|
// match with the maximum # of required elements.
|
|
|
|
if (!tryMatch(0)) {
|
|
|
|
pass++;
|
|
|
|
tryMatch(0);
|
|
|
|
}
|
|
|
|
if (required === false) {
|
|
|
|
return max > 0;
|
|
|
|
}
|
|
|
|
// Use finer-grained specification of which matchers are required.
|
|
|
|
for (let i = 0; i < ms.length; i++) {
|
|
|
|
if (required[i] && !seen[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
function tryMatch(matchCount) {
|
|
|
|
for (let i = 0; i < ms.length; i++) {
|
|
|
|
if (seen[i]) continue;
|
2020-11-25 18:01:36 +00:00
|
|
|
expr.mark();
|
|
|
|
if (!ms[i].matchFunc(expr)) {
|
|
|
|
expr.popMark(true);
|
2018-01-07 20:26:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
seen[i] = true;
|
|
|
|
// Increase matchCount if this was a required element
|
|
|
|
// (or if all the elements are optional)
|
|
|
|
if (tryMatch(matchCount + (required === false || required[i] ? 1 : 0))) {
|
2020-11-25 18:01:36 +00:00
|
|
|
expr.popMark(true);
|
2018-01-07 20:26:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Backtrack: try *not* matching using this rule, and
|
|
|
|
// let's see if it leads to a better overall match.
|
2020-11-25 18:01:36 +00:00
|
|
|
expr.popMark();
|
2018-01-07 20:26:47 +00:00
|
|
|
seen[i] = false;
|
|
|
|
}
|
|
|
|
if (pass === 0) {
|
|
|
|
max = Math.max(matchCount, max);
|
|
|
|
return matchCount === ms.length;
|
|
|
|
} else {
|
|
|
|
return matchCount === max;
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @this {Matcher}
|
|
|
|
* @param {PropertyValueIterator} expr
|
|
|
|
*/
|
|
|
|
static funcSeq(expr) {
|
|
|
|
return this.options.every(Matcher.invoke, expr);
|
|
|
|
}
|
2018-01-07 20:26:47 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/**************************** toStringFunc **********************/
|
2018-01-07 20:26:47 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @this {Matcher} */
|
|
|
|
static toStringAlt(prec) {
|
2018-01-07 20:26:47 +00:00
|
|
|
const p = Matcher.prec.ALT;
|
|
|
|
const s = this.options.map(m => m.toString(p)).join(' | ');
|
|
|
|
return prec > p ? `[ ${s} ]` : s;
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
/** @this {Matcher} */
|
|
|
|
static toStringBraces() {
|
|
|
|
const {marker, min, max, embraced} = this.options;
|
|
|
|
return embraced.toString(Matcher.prec.MOD) + (
|
|
|
|
!marker || marker === '#'
|
|
|
|
? `${marker || ''}{${min}${min === max ? '' : ',' + max}}`
|
|
|
|
: marker);
|
|
|
|
}
|
|
|
|
/** @this {Matcher} */
|
2020-12-09 07:38:28 +00:00
|
|
|
static toStringFunc() {
|
|
|
|
const {name, body} = this.options;
|
|
|
|
return `${name}( ${body} )`;
|
|
|
|
}
|
|
|
|
/** @this {Matcher} */
|
2020-11-25 18:01:36 +00:00
|
|
|
static toStringMany(prec) {
|
2018-01-07 20:26:47 +00:00
|
|
|
const {options: ms, required} = this;
|
|
|
|
const p = required === false ? Matcher.prec.OROR : Matcher.prec.ANDAND;
|
|
|
|
const s = ms.map((m, i) => {
|
|
|
|
if (required !== false && !required[i]) {
|
2019-03-03 22:55:15 +00:00
|
|
|
const str = m.toString(Matcher.prec.MOD);
|
|
|
|
return str.endsWith('?') ? str : str + '?';
|
2018-01-07 20:26:47 +00:00
|
|
|
}
|
|
|
|
return m.toString(p);
|
|
|
|
}).join(required === false ? ' || ' : ' && ');
|
|
|
|
return prec > p ? `[ ${s} ]` : s;
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
/** @this {Matcher} */
|
|
|
|
static toStringSeq(prec) {
|
|
|
|
const p = Matcher.prec.SEQ;
|
|
|
|
const s = this.options.map(m => m.toString(p)).join(' ');
|
|
|
|
return prec > p ? `[ ${s} ]` : s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precedence table of combinators.
|
|
|
|
Matcher.prec = {
|
|
|
|
MOD: 5,
|
|
|
|
SEQ: 4,
|
|
|
|
ANDAND: 3,
|
|
|
|
OROR: 2,
|
|
|
|
ALT: 1,
|
2018-01-07 20:26:47 +00:00
|
|
|
};
|
|
|
|
|
2020-10-24 10:18:17 +00:00
|
|
|
Matcher.parseGrammar = (() => {
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @type {StringReader} */
|
2018-01-07 20:26:47 +00:00
|
|
|
let reader;
|
2020-10-24 10:18:17 +00:00
|
|
|
return newReader => {
|
2018-01-07 20:26:47 +00:00
|
|
|
reader = newReader;
|
2020-11-25 18:01:36 +00:00
|
|
|
return alt();
|
2020-10-24 10:18:17 +00:00
|
|
|
};
|
2020-11-25 18:01:36 +00:00
|
|
|
function alt() {
|
|
|
|
// alt = oror (" | " oror)*
|
2018-01-07 20:26:47 +00:00
|
|
|
const m = [oror()];
|
|
|
|
while (reader.readMatch(' | ')) {
|
|
|
|
m.push(oror());
|
|
|
|
}
|
2020-10-24 10:18:17 +00:00
|
|
|
return m.length === 1 ? m[0] : Matcher.alt(...m);
|
2018-01-07 20:26:47 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
// Matcher for two or more options in any order, at least one must be present.
|
2018-01-07 20:26:47 +00:00
|
|
|
function oror() {
|
|
|
|
// oror = andand ( " || " andand)*
|
|
|
|
const m = [andand()];
|
|
|
|
while (reader.readMatch(' || ')) {
|
|
|
|
m.push(andand());
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
return m.length === 1 ? m[0] : Matcher.many(false, ...m);
|
2018-01-07 20:26:47 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
// Matcher for two or more options in any order, all mandatory.
|
2018-01-07 20:26:47 +00:00
|
|
|
function andand() {
|
|
|
|
// andand = seq ( " && " seq)*
|
|
|
|
const m = [seq()];
|
2019-03-03 22:55:15 +00:00
|
|
|
let reqPrev = !isOptional(m[0]);
|
|
|
|
const required = [reqPrev];
|
2018-01-07 20:26:47 +00:00
|
|
|
while (reader.readMatch(' && ')) {
|
2019-03-03 22:55:15 +00:00
|
|
|
const item = seq();
|
|
|
|
const req = !isOptional(item);
|
|
|
|
// Matcher.many apparently can't handle optional items first
|
|
|
|
if (req && !reqPrev) {
|
|
|
|
m.unshift(item);
|
|
|
|
required.unshift(req);
|
|
|
|
} else {
|
|
|
|
m.push(item);
|
|
|
|
required.push(req);
|
|
|
|
reqPrev = req;
|
|
|
|
}
|
2018-01-07 20:26:47 +00:00
|
|
|
}
|
2019-03-03 22:55:15 +00:00
|
|
|
return m.length === 1 ? m[0] : Matcher.many(required, ...m);
|
2018-01-07 20:26:47 +00:00
|
|
|
}
|
|
|
|
function seq() {
|
|
|
|
// seq = mod ( " " mod)*
|
2020-11-25 18:01:36 +00:00
|
|
|
const ms = [mod()];
|
2020-12-09 07:38:28 +00:00
|
|
|
while (reader.readMatch(/\s(?![&|)\]])/y)) {
|
2020-11-25 18:01:36 +00:00
|
|
|
ms.push(mod());
|
2018-01-07 20:26:47 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
return Matcher.seq(...ms);
|
2018-01-07 20:26:47 +00:00
|
|
|
}
|
|
|
|
function mod() {
|
|
|
|
// mod = term ( "?" | "*" | "+" | "#" | "{<num>,<num>}" )?
|
2020-12-09 07:38:28 +00:00
|
|
|
// term = <nt> | literal | "[ " expression " ]" | fn "( " alt " )"
|
|
|
|
let m, fn;
|
2020-11-25 18:01:36 +00:00
|
|
|
if (reader.readMatch('[ ')) {
|
|
|
|
m = alt();
|
|
|
|
eat(' ]');
|
2020-12-09 07:38:28 +00:00
|
|
|
} else if ((fn = reader.readMatch(/[-\w]+(?=\(\s)/y))) {
|
|
|
|
reader.readCount(2);
|
|
|
|
m = alt();
|
|
|
|
eat(' )');
|
|
|
|
return Matcher.func(fn, m);
|
2020-11-25 18:01:36 +00:00
|
|
|
} else {
|
2021-09-07 09:36:36 +00:00
|
|
|
m = Matcher.fromType(eat(/<[^>]+>|[^\s?*+#{]+/y));
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
2018-01-07 20:26:47 +00:00
|
|
|
reader.mark();
|
|
|
|
let hash;
|
|
|
|
switch (reader.read()) {
|
2020-11-25 18:01:36 +00:00
|
|
|
case '?': return m.braces(0, 1, '?');
|
|
|
|
case '*': return m.braces(0, Infinity, '*');
|
|
|
|
case '+': return m.braces(1, Infinity, '+');
|
2018-01-07 20:26:47 +00:00
|
|
|
case '#':
|
2020-11-25 18:01:36 +00:00
|
|
|
if (reader.peek() !== '{') return m.braces(1, Infinity, '#', ',');
|
2018-01-07 20:26:47 +00:00
|
|
|
reader.read();
|
|
|
|
hash = '#';
|
|
|
|
// fallthrough
|
|
|
|
case '{': {
|
2020-11-25 18:01:36 +00:00
|
|
|
const [min, max] = eat(/\s*\d+\s*(,\s*\d+\s*)?}/y).trim().split(/\s+|,|}/);
|
|
|
|
return m.braces(min | 0, max | min | 0, hash, hash && ',');
|
2018-01-07 20:26:47 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
reader.reset();
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
function eat(pattern) {
|
|
|
|
const s = reader.readMatch(pattern);
|
|
|
|
if (s != null) return s;
|
|
|
|
throw new Error('Internal grammar error. ' +
|
|
|
|
`Expected ${pattern} at ${reader._cursor} in ${reader._input}`);
|
2018-01-07 20:26:47 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
function isOptional({options}) {
|
|
|
|
return options && options.marker === '?';
|
2019-03-03 22:55:15 +00:00
|
|
|
}
|
2018-01-07 20:26:47 +00:00
|
|
|
})();
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region EventTarget
|
2018-01-06 11:05:20 +00:00
|
|
|
|
|
|
|
class EventTarget {
|
|
|
|
constructor() {
|
|
|
|
this._listeners = new Map();
|
|
|
|
}
|
|
|
|
addListener(type, fn) {
|
2020-11-25 18:01:36 +00:00
|
|
|
let list = this._listeners.get(type);
|
|
|
|
if (!list) this._listeners.set(type, (list = new Set()));
|
|
|
|
list.add(fn);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
fire(event) {
|
|
|
|
if (typeof event === 'string') {
|
|
|
|
event = {type: event};
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
event.target = this;
|
|
|
|
const list = this._listeners.get(event.type);
|
|
|
|
if (list) {
|
|
|
|
for (const fn of list) {
|
|
|
|
fn.call(this, event);
|
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
removeListener(type, fn) {
|
2020-11-25 18:01:36 +00:00
|
|
|
const list = this._listeners.get(type);
|
|
|
|
if (list) list.delete(fn);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region Syntax units
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean|number} [_isAttr]
|
|
|
|
* @property {boolean|number} [_isCalc]
|
|
|
|
* @property {boolean|number} [_isVar]
|
|
|
|
*/
|
2017-12-26 20:39:52 +00:00
|
|
|
class SyntaxUnit {
|
2020-11-25 18:01:36 +00:00
|
|
|
constructor(text, pos, type, extras) {
|
|
|
|
this.col = pos.col;
|
|
|
|
this.line = pos.line;
|
2017-12-26 20:39:52 +00:00
|
|
|
this.offset = pos.offset;
|
|
|
|
this.text = text;
|
|
|
|
this.type = type;
|
2020-11-25 18:01:36 +00:00
|
|
|
if (extras) Object.assign(this, extras);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
valueOf() {
|
|
|
|
return this.text;
|
|
|
|
}
|
|
|
|
toString() {
|
|
|
|
return this.text;
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
get isAttr() {
|
|
|
|
let res = this._isAttr;
|
|
|
|
if (res === 0) res = this._isAttr = lowerCmp(this.name, 'attr');
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
get isCalc() {
|
|
|
|
let res = this._isCalc;
|
|
|
|
if (res === 0) res = this._isCalc = rxCalc.test(this.text);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
get isVar() {
|
|
|
|
let res = this._isVar;
|
|
|
|
if (res === 0) {
|
|
|
|
const pp = this.expr && this.expr.parts;
|
|
|
|
res = this._isVar = pp && pp.length > 0 && (
|
|
|
|
(pp.length === 1 || pp[1].text === ',') && (
|
|
|
|
pp[0].type === 'custom-property' && lowerCmp(this.name, 'var') ||
|
|
|
|
pp[0].type === 'identifier' && lowerCmp(this.name, 'env')));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
static fromToken(token) {
|
2020-11-25 18:01:36 +00:00
|
|
|
return token && new SyntaxUnit(token.value, token);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/**
|
|
|
|
* @param {SyntaxUnit} unit
|
|
|
|
* @param {SyntaxUnit|parserlib.Token} token
|
|
|
|
* @returns {SyntaxUnit}
|
|
|
|
*/
|
|
|
|
static addFuncInfo(unit, {expr, name} = unit) {
|
2022-04-01 13:05:45 +00:00
|
|
|
const isColor = expr && expr.parts && /^((rgb|hsl)a?|hwb)$/i.test(name);
|
2020-11-25 18:01:36 +00:00
|
|
|
if (isColor) unit.type = 'color';
|
|
|
|
unit._isAttr =
|
|
|
|
unit._isCalc =
|
|
|
|
unit._isVar = isColor ? false : 0;
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
class SyntaxError extends Error {
|
|
|
|
constructor(message, pos) {
|
|
|
|
super();
|
|
|
|
this.name = this.constructor.name;
|
2020-11-25 18:01:36 +00:00
|
|
|
this.col = pos.col;
|
|
|
|
this.line = pos.line;
|
2022-01-19 11:42:10 +00:00
|
|
|
this.offset = pos.offset;
|
2018-01-06 11:05:20 +00:00
|
|
|
this.message = message;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
class ValidationError extends Error {
|
|
|
|
constructor(message, pos) {
|
|
|
|
super();
|
2020-11-25 18:01:36 +00:00
|
|
|
this.col = pos.col;
|
|
|
|
this.line = pos.line;
|
2022-01-19 11:42:10 +00:00
|
|
|
this.offset = pos.offset;
|
2018-01-06 11:05:20 +00:00
|
|
|
this.message = message;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
// individual media query
|
|
|
|
class MediaQuery extends SyntaxUnit {
|
|
|
|
constructor(modifier, mediaType, features, pos) {
|
|
|
|
const text = (modifier ? modifier + ' ' : '') +
|
|
|
|
(mediaType ? mediaType : '') +
|
|
|
|
(mediaType && features.length > 0 ? ' and ' : '') +
|
|
|
|
features.join(' and ');
|
|
|
|
super(text, pos, TYPES.MEDIA_QUERY_TYPE);
|
|
|
|
this.modifier = modifier;
|
|
|
|
this.mediaType = mediaType;
|
|
|
|
this.features = features;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
// e.g. max-width:500.
|
|
|
|
class MediaFeature extends SyntaxUnit {
|
|
|
|
constructor(name, value) {
|
2020-10-24 10:18:17 +00:00
|
|
|
const text = `(${name}${value != null ? ':' + value : ''})`;
|
2018-01-06 11:05:20 +00:00
|
|
|
super(text, name, TYPES.MEDIA_FEATURE_TYPE);
|
|
|
|
this.name = name;
|
|
|
|
this.value = value;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* An entire single selector, including all parts but not
|
|
|
|
* including multiple selectors (those separated by commas).
|
|
|
|
*/
|
|
|
|
class Selector extends SyntaxUnit {
|
|
|
|
constructor(parts, pos) {
|
|
|
|
super(parts.join(' '), pos, TYPES.SELECTOR_TYPE);
|
|
|
|
this.parts = parts;
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
this.specificity = Specificity.calculate(this);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* A single part of a selector string i.e. element name and modifiers.
|
|
|
|
* Does not include combinators such as spaces, +, >, etc.
|
|
|
|
*/
|
2017-12-26 20:39:52 +00:00
|
|
|
class SelectorPart extends SyntaxUnit {
|
|
|
|
constructor(elementName, modifiers, text, pos) {
|
|
|
|
super(text, pos, TYPES.SELECTOR_PART_TYPE);
|
|
|
|
this.elementName = elementName;
|
|
|
|
this.modifiers = modifiers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* Selector modifier string
|
|
|
|
*/
|
|
|
|
class SelectorSubPart extends SyntaxUnit {
|
|
|
|
constructor(text, type, pos) {
|
|
|
|
super(text, pos, TYPES.SELECTOR_SUB_PART_TYPE);
|
|
|
|
this.type = type;
|
|
|
|
// Some subparts have arguments
|
|
|
|
this.args = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A selector combinator (whitespace, +, >).
|
|
|
|
*/
|
|
|
|
class Combinator extends SyntaxUnit {
|
|
|
|
constructor(token) {
|
|
|
|
const {value} = token;
|
|
|
|
super(value, token, TYPES.COMBINATOR_TYPE);
|
|
|
|
this.type =
|
|
|
|
value === '>' ? 'child' :
|
|
|
|
value === '+' ? 'adjacent-sibling' :
|
|
|
|
value === '~' ? 'sibling' :
|
2020-06-26 16:47:12 +00:00
|
|
|
value === '||' ? 'column' :
|
2018-01-06 11:05:20 +00:00
|
|
|
!value.trim() ? 'descendant' :
|
|
|
|
'unknown';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A selector specificity.
|
|
|
|
*/
|
2017-12-26 20:39:52 +00:00
|
|
|
class Specificity {
|
|
|
|
/**
|
|
|
|
* @param {int} a Should be 1 for inline styles, zero for stylesheet styles
|
|
|
|
* @param {int} b Number of ID selectors
|
|
|
|
* @param {int} c Number of classes and pseudo classes
|
|
|
|
* @param {int} d Number of element names and pseudo elements
|
|
|
|
*/
|
|
|
|
constructor(a, b, c, d) {
|
|
|
|
this.a = a;
|
|
|
|
this.b = b;
|
|
|
|
this.c = c;
|
|
|
|
this.d = d;
|
|
|
|
this.constructor = Specificity;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param {Specificity} other The other specificity to compare to.
|
|
|
|
* @return {int} -1 if the other specificity is larger, 1 if smaller, 0 if equal.
|
|
|
|
*/
|
|
|
|
compare(other) {
|
|
|
|
const comps = ['a', 'b', 'c', 'd'];
|
|
|
|
for (let i = 0, len = comps.length; i < len; i++) {
|
|
|
|
if (this[comps[i]] < other[comps[i]]) {
|
|
|
|
return -1;
|
|
|
|
} else if (this[comps[i]] > other[comps[i]]) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
valueOf() {
|
2021-01-01 14:27:58 +00:00
|
|
|
return this.a * 1000 + this.b * 100 + this.c * 10 + this.d;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
toString() {
|
2021-01-01 14:27:58 +00:00
|
|
|
return `${this.a},${this.b},${this.c},${this.d}`;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Calculates the specificity of the given selector.
|
2021-01-01 14:27:58 +00:00
|
|
|
* @param {Selector} selector The selector to calculate specificity for.
|
2018-01-06 11:05:20 +00:00
|
|
|
* @return {Specificity} The specificity of the selector.
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
|
|
|
static calculate(selector) {
|
|
|
|
let b = 0;
|
|
|
|
let c = 0;
|
|
|
|
let d = 0;
|
2018-01-06 11:05:20 +00:00
|
|
|
selector.parts.forEach(updateValues);
|
|
|
|
return new Specificity(0, b, c, d);
|
2017-12-26 20:39:52 +00:00
|
|
|
function updateValues(part) {
|
2018-01-06 11:05:20 +00:00
|
|
|
if (!(part instanceof SelectorPart)) return;
|
2017-12-26 20:39:52 +00:00
|
|
|
const elementName = part.elementName ? part.elementName.text : '';
|
2018-01-06 11:05:20 +00:00
|
|
|
if (elementName && !elementName.endsWith('*')) {
|
2017-12-26 20:39:52 +00:00
|
|
|
d++;
|
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
for (const modifier of part.modifiers) {
|
2017-12-26 20:39:52 +00:00
|
|
|
switch (modifier.type) {
|
|
|
|
case 'class':
|
|
|
|
case 'attribute':
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
case 'id':
|
|
|
|
b++;
|
|
|
|
break;
|
|
|
|
case 'pseudo':
|
|
|
|
if (isPseudoElement(modifier.text)) {
|
|
|
|
d++;
|
|
|
|
} else {
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'not':
|
2018-01-06 11:05:20 +00:00
|
|
|
modifier.args.forEach(updateValues);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
class PropertyName extends SyntaxUnit {
|
|
|
|
constructor(text, hack, pos) {
|
|
|
|
super(text, pos, TYPES.PROPERTY_NAME_TYPE);
|
|
|
|
this.hack = hack;
|
|
|
|
}
|
|
|
|
toString() {
|
|
|
|
return (this.hack || '') + this.text;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* A single value between ":" and ";", that is if there are multiple values
|
|
|
|
* separated by commas, this type represents just one of the values.
|
|
|
|
*/
|
|
|
|
class PropertyValue extends SyntaxUnit {
|
2017-12-26 20:39:52 +00:00
|
|
|
constructor(parts, pos) {
|
2018-01-06 11:05:20 +00:00
|
|
|
super(parts.join(' '), pos, TYPES.PROPERTY_VALUE_TYPE);
|
2017-12-26 20:39:52 +00:00
|
|
|
this.parts = parts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* A single part of a value
|
|
|
|
* e.g. '1px solid rgb(1, 2, 3)' has 3 parts
|
2020-11-25 18:01:36 +00:00
|
|
|
* @property {PropertyValue} expr
|
2018-01-06 11:05:20 +00:00
|
|
|
*/
|
|
|
|
class PropertyValuePart extends SyntaxUnit {
|
2020-12-09 07:38:28 +00:00
|
|
|
/** @param {parserlib.Token} token */
|
2018-01-06 11:05:20 +00:00
|
|
|
constructor(token) {
|
|
|
|
const {value, type} = token;
|
|
|
|
super(value, token, TYPES.PROPERTY_VALUE_PART_TYPE);
|
|
|
|
this.tokenType = type;
|
2021-01-01 14:27:58 +00:00
|
|
|
this.expr = token.expr || null;
|
2020-11-25 18:01:36 +00:00
|
|
|
switch (type) {
|
|
|
|
case Tokens.ANGLE:
|
|
|
|
case Tokens.DIMENSION:
|
|
|
|
case Tokens.FREQ:
|
|
|
|
case Tokens.LENGTH:
|
|
|
|
case Tokens.NUMBER:
|
|
|
|
case Tokens.PERCENTAGE:
|
|
|
|
case Tokens.TIME:
|
|
|
|
this.value = token.number;
|
|
|
|
this.units = token.units;
|
|
|
|
this.type = token.unitsType;
|
|
|
|
this.isInt = this.type === 'number' && !value.includes('.');
|
|
|
|
break;
|
|
|
|
case Tokens.HASH:
|
|
|
|
this.type = 'color';
|
|
|
|
this.value = value;
|
|
|
|
break;
|
2020-12-09 07:38:28 +00:00
|
|
|
case Tokens.IDENT:
|
|
|
|
if (value.startsWith('--')) {
|
|
|
|
this.type = 'custom-property';
|
2020-11-25 18:01:36 +00:00
|
|
|
this.value = value;
|
2020-12-09 07:38:28 +00:00
|
|
|
} else {
|
|
|
|
const namedColor = Colors[value] || Colors[lower(value)];
|
|
|
|
this.type = namedColor ? 'color' : 'identifier';
|
|
|
|
this.value = namedColor || value;
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Tokens.FUNCTION: {
|
|
|
|
this.name = token.name;
|
|
|
|
SyntaxUnit.addFuncInfo(this, token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Tokens.STRING:
|
|
|
|
this.type = 'string';
|
|
|
|
this.value = parseString(value);
|
|
|
|
break;
|
|
|
|
case Tokens.URI:
|
|
|
|
this.type = 'uri';
|
|
|
|
this.name = token.name;
|
|
|
|
this.uri = token.uri;
|
|
|
|
break;
|
|
|
|
case Tokens.USO_VAR:
|
|
|
|
this._isVar = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (value === ',' || value === '/') {
|
|
|
|
this.type = 'operator';
|
|
|
|
this.value = value;
|
|
|
|
} else {
|
|
|
|
this.type = 'unknown';
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class PropertyValueIterator {
|
2017-12-26 20:39:52 +00:00
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* @param {PropertyValue} value
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
2018-01-06 11:05:20 +00:00
|
|
|
constructor(value) {
|
|
|
|
this._i = 0;
|
|
|
|
this._parts = value.parts;
|
|
|
|
this._marks = [];
|
|
|
|
this.value = value;
|
2020-11-25 18:01:36 +00:00
|
|
|
this.hasNext = this._parts.length > 0;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @returns {PropertyValuePart|null} */
|
2018-01-06 11:05:20 +00:00
|
|
|
peek(count) {
|
|
|
|
return this._parts[this._i + (count || 0)] || null;
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @returns {?PropertyValuePart} */
|
2018-01-06 11:05:20 +00:00
|
|
|
next() {
|
2020-11-25 18:01:36 +00:00
|
|
|
if (this.hasNext) {
|
|
|
|
this.hasNext = this._i + 1 < this._parts.length;
|
|
|
|
return this._parts[this._i++];
|
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @returns {PropertyValueIterator} */
|
2018-01-06 11:05:20 +00:00
|
|
|
mark() {
|
|
|
|
this._marks.push(this._i);
|
2020-10-24 10:18:17 +00:00
|
|
|
return this;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2020-10-24 10:18:17 +00:00
|
|
|
popMark(success) {
|
|
|
|
const i = this._marks.pop();
|
|
|
|
if (!success && i != null) {
|
|
|
|
this._i = i;
|
2020-11-25 18:01:36 +00:00
|
|
|
this.hasNext = i < this._parts.length;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2020-10-24 10:18:17 +00:00
|
|
|
return success;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
resetTo(i) {
|
|
|
|
this._i = i;
|
|
|
|
this.hasNext = this._parts.length > i;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region ValidationTypes - implementation
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
for (const obj of [VTSimple, VTComplex]) {
|
2020-11-25 18:01:36 +00:00
|
|
|
const action = obj === VTSimple
|
|
|
|
? rule => vtIsLiteral.bind(obj, rule)
|
|
|
|
: Matcher.parse;
|
|
|
|
for (const [id, rule] of Object.entries(obj)) {
|
|
|
|
if (typeof rule === 'string') {
|
|
|
|
obj[id] = Object.defineProperty(action(rule), 'originalText', {value: rule});
|
|
|
|
} else if (/^Matcher\s/.test(rule)) {
|
|
|
|
obj[id] = rule(Matcher);
|
2020-10-24 10:18:17 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
function vtDescribe(type) {
|
|
|
|
const complex = VTComplex[type];
|
|
|
|
const text = complex instanceof Matcher ? complex.toString(0) : type;
|
|
|
|
return vtExplode(text);
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
function vtExplode(text) {
|
|
|
|
if (!text.includes('<')) return text;
|
2020-12-09 07:38:28 +00:00
|
|
|
return text.replace(/(<.*?>)([{#?]?)/g, (s, rule, mod) => {
|
2020-11-25 18:01:36 +00:00
|
|
|
const ref = VTSimple[rule] || VTComplex[rule];
|
2020-12-09 07:38:28 +00:00
|
|
|
if (!ref || !ref.originalText) return s;
|
2020-11-25 18:01:36 +00:00
|
|
|
const full = vtExplode(ref.originalText);
|
|
|
|
const brace = mod || full.includes(' ');
|
|
|
|
return ((brace ? '[ ' : '') + full + (brace ? ' ]' : '')) + mod;
|
|
|
|
});
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
/** @param {PropertyValuePart} p */
|
|
|
|
function vtIsAttr(p) {
|
|
|
|
return p.isAttr && (p = p.expr) && (p = p.parts) && p.length && vtIsIdent(p[0]);
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @param {PropertyValuePart} p */
|
|
|
|
function vtIsIdent(p) {
|
2021-01-01 14:27:58 +00:00
|
|
|
return p.tokenType === Tokens.IDENT;
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @param {PropertyValuePart} p */
|
|
|
|
function vtIsLength(p) {
|
2021-01-01 16:43:28 +00:00
|
|
|
return p.text === '0' || p.type === 'length' || p.isCalc;
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/**
|
|
|
|
* @param {string} literals
|
|
|
|
* @param {PropertyValuePart} part
|
|
|
|
* @return {?boolean}
|
|
|
|
*/
|
|
|
|
function vtIsLiteral(literals, part) {
|
|
|
|
let text;
|
|
|
|
for (const arg of literals.includes(' | ') ? literals.split(' | ') : [literals]) {
|
|
|
|
if (arg.startsWith('<')) {
|
|
|
|
const vt = VTSimple[arg];
|
|
|
|
if (vt && vt(part)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg.endsWith('()') &&
|
2021-09-07 09:36:36 +00:00
|
|
|
part.name &&
|
2020-12-09 07:38:28 +00:00
|
|
|
part.name.length === arg.length - 2 &&
|
|
|
|
lowerCmp(part.name, arg.slice(0, -2))) {
|
|
|
|
return true;
|
2020-10-24 10:18:17 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
if ((text || part.text) === arg ||
|
|
|
|
(text || part.text).length >= arg.length &&
|
|
|
|
lowerCmp(arg, text || (text = rxVendorPrefix.test(part.text) ? RegExp.$2 : part.text))) {
|
|
|
|
return true;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2020-10-24 10:18:17 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @param {PropertyValuePart} p */
|
|
|
|
function vtIsPct(p) {
|
|
|
|
return p.text === '0' || p.type === 'percentage' || p.isCalc;
|
|
|
|
}
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
//#region Validation
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
const validationCache = new Map();
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2021-09-07 09:36:36 +00:00
|
|
|
function validateProperty(name, property, value, Props = Properties) {
|
2022-07-01 07:26:46 +00:00
|
|
|
if (rxGlobalKeyword.test(value.parts[0])) {
|
2018-01-06 11:05:20 +00:00
|
|
|
if (value.parts.length > 1) {
|
|
|
|
throwEndExpected(value.parts[1], true);
|
|
|
|
}
|
|
|
|
return;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2021-08-20 10:01:56 +00:00
|
|
|
const prop = lower(name);
|
2021-09-07 09:36:36 +00:00
|
|
|
const spec = Props[prop] || rxVendorPrefix.test(prop) && Props[RegExp.$2];
|
2020-11-25 18:01:36 +00:00
|
|
|
if (typeof spec === 'number' || !spec && prop.startsWith('-')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!spec) {
|
2021-09-07 09:36:36 +00:00
|
|
|
const problem = Props === Properties || !Properties[prop] ? 'Unknown' : 'Misplaced';
|
|
|
|
throw new ValidationError(`${problem} property '${name}'.`, value);
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
2021-08-20 10:03:23 +00:00
|
|
|
if (hasVarParts(value)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-07 09:36:36 +00:00
|
|
|
let known = validationCache.get(prop);
|
|
|
|
if (known && known.has(value.text)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
// Property-specific validation.
|
2020-11-02 19:08:14 +00:00
|
|
|
const expr = new PropertyValueIterator(value);
|
|
|
|
const m = Matcher.parse(spec);
|
|
|
|
let result = m.match(expr);
|
2020-11-25 18:01:36 +00:00
|
|
|
if (/\battr\(/i.test(value.text)) {
|
2020-11-02 19:08:14 +00:00
|
|
|
if (!result) {
|
|
|
|
expr.tryAttr = true;
|
2020-11-25 18:01:36 +00:00
|
|
|
expr.resetTo(0);
|
2020-11-02 19:08:14 +00:00
|
|
|
result = m.match(expr);
|
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
for (let p; (p = expr.peek()) && p.isAttr && vtIsAttr(p);) {
|
2020-11-02 19:08:14 +00:00
|
|
|
expr.next();
|
|
|
|
}
|
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
if (result) {
|
2020-11-25 18:01:36 +00:00
|
|
|
if (expr.hasNext) throwEndExpected(expr.next());
|
|
|
|
} else if (expr.hasNext && expr._i) {
|
2020-11-02 19:08:14 +00:00
|
|
|
throwEndExpected(expr.peek());
|
2018-01-06 11:05:20 +00:00
|
|
|
} else {
|
2020-11-02 19:08:14 +00:00
|
|
|
const {text} = expr.value;
|
2020-11-25 18:01:36 +00:00
|
|
|
throw new ValidationError(`Expected '${vtDescribe(spec)}' but found '${text}'.`,
|
2020-11-02 19:08:14 +00:00
|
|
|
expr.value);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
if (!known) validationCache.set(prop, (known = new Set()));
|
|
|
|
known.add(value.text);
|
2020-11-25 18:01:36 +00:00
|
|
|
function throwEndExpected(unit, force) {
|
|
|
|
if (force || !unit.isVar) {
|
|
|
|
throw new ValidationError(`Expected end of value but found '${unit.text}'.`, unit);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region TokenStreamBase
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/** lookup table size for TokenStreamBase */
|
2017-12-26 20:39:52 +00:00
|
|
|
const LT_SIZE = 5;
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* Generic TokenStream providing base functionality.
|
2020-11-25 18:01:36 +00:00
|
|
|
* @typedef TokenStream
|
2018-01-06 11:05:20 +00:00
|
|
|
*/
|
2017-12-26 20:39:52 +00:00
|
|
|
class TokenStreamBase {
|
2018-01-06 11:05:20 +00:00
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
constructor(input) {
|
|
|
|
this._reader = new StringReader(input ? input.toString() : '');
|
2020-08-27 19:39:49 +00:00
|
|
|
this.resetLT();
|
|
|
|
}
|
|
|
|
|
|
|
|
resetLT() {
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @type {parserlib.Token} Last consumed token object */
|
2017-12-26 20:39:52 +00:00
|
|
|
this._token = null;
|
|
|
|
// Lookahead token buffer.
|
2020-11-25 18:01:36 +00:00
|
|
|
this._lt = Array(LT_SIZE).fill(null);
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ltIndex = 0;
|
2020-11-25 18:01:36 +00:00
|
|
|
this._ltAhead = 0;
|
|
|
|
this._ltShift = 0;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* Consumes the next token if that matches any of the given token type(s).
|
|
|
|
* @param {int|int[]} tokenTypes
|
2020-11-25 18:01:36 +00:00
|
|
|
* @param {string|string[]} [values]
|
|
|
|
* @return {parserlib.Token|boolean} token or `false`
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
2020-11-25 18:01:36 +00:00
|
|
|
match(tokenTypes, values) {
|
2020-10-24 10:18:17 +00:00
|
|
|
const isArray = typeof tokenTypes === 'object';
|
2020-11-25 18:01:36 +00:00
|
|
|
for (let token, tt; (tt = (token = this.get(true)).type);) {
|
|
|
|
if ((isArray ? tokenTypes.includes(tt) : tt === tokenTypes) &&
|
|
|
|
(!values || values.some(lowerCmpThis, token.value))) {
|
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-10-24 10:18:17 +00:00
|
|
|
if (tt !== Tokens.COMMENT) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
this.unget();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* Consumes the next token if that matches the given token type(s).
|
|
|
|
* Otherwise an error is thrown.
|
|
|
|
* @param {int|int[]} tokenTypes
|
|
|
|
* @throws {SyntaxError}
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
|
|
|
mustMatch(tokenTypes) {
|
2020-11-25 18:01:36 +00:00
|
|
|
return this.match(tokenTypes) ||
|
|
|
|
this.throwUnexpected(this.LT(1), tokenTypes);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* Keeps reading until one of the specified token types is found or EOF.
|
|
|
|
* @param {int|int[]} tokenTypes
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
|
|
|
advance(tokenTypes) {
|
|
|
|
while (this.LA(0) !== 0 && !this.match(tokenTypes)) {
|
|
|
|
this.get();
|
|
|
|
}
|
|
|
|
return this.LA(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Consumes the next token from the token stream.
|
2020-11-25 18:01:36 +00:00
|
|
|
* @param {boolean} [asToken]
|
|
|
|
* @return {int|parserlib.Token} The token type
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
2020-11-25 18:01:36 +00:00
|
|
|
get(asToken) {
|
|
|
|
const i = this._ltIndex;
|
|
|
|
const next = i + 1;
|
|
|
|
const slot = (i + this._ltShift) % LT_SIZE;
|
|
|
|
if (i < this._ltAhead) {
|
|
|
|
this._ltIndex = next;
|
|
|
|
const token = this._token = this._lt[slot];
|
|
|
|
return asToken ? token : token.type;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
const token = this._getToken();
|
2020-11-25 18:01:36 +00:00
|
|
|
const {type} = token;
|
|
|
|
const hide = type && (Tokens[type] || {}).hide;
|
|
|
|
if (type >= 0 && !hide) {
|
2017-12-26 20:39:52 +00:00
|
|
|
this._token = token;
|
2020-11-25 18:01:36 +00:00
|
|
|
this._lt[slot] = token;
|
|
|
|
if (this._ltAhead < LT_SIZE) {
|
|
|
|
this._ltIndex = next;
|
|
|
|
this._ltAhead++;
|
|
|
|
} else {
|
|
|
|
this._ltShift = (this._ltShift + 1) % LT_SIZE;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
// Skip to the next token if the token type is marked as hidden.
|
2020-11-25 18:01:36 +00:00
|
|
|
return hide ? this.get(asToken) :
|
|
|
|
asToken ? token : type;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* Looks ahead a certain number of tokens and returns the token type at that position.
|
|
|
|
* @param {int} index The index of the token type to retrieve.
|
|
|
|
* 0 for the current token, 1 for the next, -1 for the previous, etc.
|
|
|
|
* @return {int} The token type
|
|
|
|
* @throws if you lookahead past EOF, past the size of the lookahead buffer,
|
|
|
|
* or back past the first token in the lookahead buffer.
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
|
|
|
LA(index) {
|
2020-11-25 18:01:36 +00:00
|
|
|
return (index ? this.LT(index) : this._token).type;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* Looks ahead a certain number of tokens and returns the token at that position.
|
|
|
|
* @param {int} index The index of the token type to retrieve.
|
|
|
|
* 0 for the current token, 1 for the next, -1 for the previous, etc.
|
2020-11-25 18:01:36 +00:00
|
|
|
* @param {boolean} [forceCache] won't call get() so it's useful in fast tentative checks
|
2018-01-06 11:05:20 +00:00
|
|
|
* @return {Object} The token
|
|
|
|
* @throws if you lookahead past EOF, past the size of the lookahead buffer,
|
|
|
|
* or back past the first token in the lookahead buffer.
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
2020-11-25 18:01:36 +00:00
|
|
|
LT(index, forceCache) {
|
|
|
|
if (!index) {
|
|
|
|
return this._token;
|
|
|
|
}
|
|
|
|
let i = index + this._ltIndex - (index > 0);
|
|
|
|
if (index < 0 ? i >= 0 : i < this._ltAhead) {
|
|
|
|
return this._lt[(i + this._ltShift) % LT_SIZE];
|
|
|
|
} else if (forceCache) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (index < 0) {
|
|
|
|
throw new Error('Too much lookbehind.');
|
|
|
|
}
|
|
|
|
if (index > LT_SIZE) {
|
|
|
|
throw new Error('Too much lookahead.');
|
|
|
|
}
|
|
|
|
i = index;
|
|
|
|
const oldToken = this._token;
|
|
|
|
while (i && i--) this.get();
|
|
|
|
const token = this._token;
|
|
|
|
this._ltIndex -= index;
|
|
|
|
this._token = oldToken;
|
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/** Returns the token type for the next token in the stream without consuming it. */
|
2017-12-26 20:39:52 +00:00
|
|
|
peek() {
|
2020-11-25 18:01:36 +00:00
|
|
|
return this.LT(1).type;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/** Restores the last consumed token to the token stream. */
|
2017-12-26 20:39:52 +00:00
|
|
|
unget() {
|
2020-11-25 18:01:36 +00:00
|
|
|
if (this._ltIndex) {
|
|
|
|
this._ltIndex--;
|
|
|
|
this._token = this._lt[(this._ltIndex - 1 + this._ltShift + LT_SIZE) % LT_SIZE];
|
2017-12-26 20:39:52 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Too much lookahead.');
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
|
|
|
|
throwUnexpected(token = this._token, expected = []) {
|
|
|
|
expected = (Array.isArray(expected) ? expected : [expected])
|
|
|
|
.map(e => typeof e === 'string' ? e : Tokens.name(e))
|
|
|
|
.join(', ');
|
|
|
|
const msg = expected
|
|
|
|
? `Expected ${expected} but found '${token.value}'.`
|
|
|
|
: `Unexpected '${token.value}'.`;
|
|
|
|
throw new SyntaxError(msg, token);
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region TokenStream
|
2017-12-26 20:39:52 +00:00
|
|
|
|
|
|
|
class TokenStream extends TokenStreamBase {
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* @param {Number|Number[]} tokenTypes
|
2021-08-24 11:38:19 +00:00
|
|
|
* @param {Boolean} [skipCruftBefore=true] - skip comments/whitespace before matching
|
2018-01-06 11:05:20 +00:00
|
|
|
* @returns {Object} token
|
|
|
|
*/
|
|
|
|
mustMatch(tokenTypes, skipCruftBefore = true) {
|
|
|
|
if (skipCruftBefore && tokenTypes !== Tokens.S) {
|
|
|
|
this.skipComment(true);
|
|
|
|
}
|
2020-10-24 10:18:17 +00:00
|
|
|
return super.mustMatch(tokenTypes);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* @param {Boolean} [skipWS] - skip whitespace too
|
2021-08-24 11:38:19 +00:00
|
|
|
* @param {Boolean} [skipUsoVar] - skip USO_VAR too
|
2018-01-06 11:05:20 +00:00
|
|
|
*/
|
2021-08-24 11:38:19 +00:00
|
|
|
skipComment(skipWS, skipUsoVar) {
|
2020-11-25 18:01:36 +00:00
|
|
|
const tt = this.LT(1, true).type;
|
|
|
|
if (skipWS && tt === Tokens.S ||
|
2021-08-24 11:38:19 +00:00
|
|
|
skipUsoVar && tt === Tokens.USO_VAR ||
|
2018-01-06 11:05:20 +00:00
|
|
|
tt === Tokens.COMMENT ||
|
2020-11-25 18:01:36 +00:00
|
|
|
tt == null && this._ltIndex === this._ltAhead && (
|
2020-12-09 07:38:28 +00:00
|
|
|
skipWS && this._reader.readMatch(/\s+/y),
|
2020-11-25 18:01:36 +00:00
|
|
|
this._reader.peekTest(/\/\*/y))) {
|
2021-08-24 11:38:19 +00:00
|
|
|
while (this.match(skipUsoVar ? TT.usoS : Tokens.S)) { /*NOP*/ }
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* @returns {Object} token
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
|
|
|
_getToken() {
|
|
|
|
const reader = this._reader;
|
2020-12-09 07:38:28 +00:00
|
|
|
/** @namespace parserlib.Token */
|
|
|
|
const tok = {
|
2021-01-01 14:27:58 +00:00
|
|
|
value: '',
|
2020-12-09 07:38:28 +00:00
|
|
|
type: Tokens.CHAR,
|
2017-12-26 20:39:52 +00:00
|
|
|
col: reader._col,
|
2020-12-09 07:38:28 +00:00
|
|
|
line: reader._line,
|
2017-12-26 20:39:52 +00:00
|
|
|
offset: reader._cursor,
|
|
|
|
};
|
2021-01-01 14:27:58 +00:00
|
|
|
let a = tok.value = reader.read();
|
|
|
|
let b = reader.peek();
|
|
|
|
if (a === '\\') {
|
|
|
|
if (b === '\n' || b === '\f') return tok;
|
|
|
|
a = this.readEscape();
|
|
|
|
b = reader.peek();
|
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
switch (a) {
|
2017-12-26 20:39:52 +00:00
|
|
|
case ' ':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\t':
|
|
|
|
case '\f':
|
2020-12-09 07:38:28 +00:00
|
|
|
tok.type = Tokens.S;
|
|
|
|
if (/\s/.test(b)) {
|
|
|
|
tok.value += reader.readMatch(/\s+/y) || '';
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
case '{':
|
|
|
|
tok.type = Tokens.LBRACE;
|
|
|
|
tok.endChar = '}';
|
|
|
|
return tok;
|
|
|
|
case '(':
|
|
|
|
tok.type = Tokens.LPAREN;
|
|
|
|
tok.endChar = ')';
|
|
|
|
return tok;
|
|
|
|
case '[':
|
|
|
|
tok.type = Tokens.LBRACKET;
|
|
|
|
tok.endChar = ']';
|
|
|
|
return tok;
|
2017-12-26 20:39:52 +00:00
|
|
|
case '/':
|
2020-12-09 07:38:28 +00:00
|
|
|
if (b === '*') {
|
|
|
|
const str = tok.value = this.readComment(a);
|
|
|
|
tok.type = str.startsWith('/*[[') && str.endsWith(']]*/')
|
|
|
|
? Tokens.USO_VAR
|
|
|
|
: Tokens.COMMENT;
|
|
|
|
} else {
|
|
|
|
tok.type = Tokens.SLASH;
|
|
|
|
}
|
|
|
|
return tok;
|
2017-12-26 20:39:52 +00:00
|
|
|
case '|':
|
|
|
|
case '~':
|
|
|
|
case '^':
|
|
|
|
case '$':
|
|
|
|
case '*':
|
2020-12-09 07:38:28 +00:00
|
|
|
if (b === '=') {
|
|
|
|
tok.value = a + reader.read();
|
|
|
|
tok.type = typeMap.get(tok.value) || Tokens.CHAR;
|
|
|
|
} else if (a === '|' && b === '|') {
|
|
|
|
reader.read();
|
|
|
|
tok.value = '||';
|
|
|
|
tok.type = Tokens.COLUMN;
|
|
|
|
} else {
|
|
|
|
tok.type = typeMap.get(a) || Tokens.CHAR;
|
|
|
|
}
|
|
|
|
return tok;
|
2017-12-26 20:39:52 +00:00
|
|
|
case '"':
|
|
|
|
case "'":
|
2020-12-09 07:38:28 +00:00
|
|
|
return this.stringToken(a, tok);
|
2017-12-26 20:39:52 +00:00
|
|
|
case '#':
|
2021-01-01 14:27:58 +00:00
|
|
|
if (rxNameChar.test(b)) {
|
2020-12-09 07:38:28 +00:00
|
|
|
tok.type = Tokens.HASH;
|
|
|
|
tok.value = this.readName(a);
|
|
|
|
}
|
|
|
|
return tok;
|
2017-12-26 20:39:52 +00:00
|
|
|
case '.':
|
2020-12-09 07:38:28 +00:00
|
|
|
if (b >= '0' && b <= '9') {
|
|
|
|
this.numberToken(a, tok);
|
|
|
|
} else {
|
|
|
|
tok.type = Tokens.DOT;
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
case '-':
|
2017-12-26 20:39:52 +00:00
|
|
|
// could be closing HTML-style comment or CSS variable
|
2020-12-09 07:38:28 +00:00
|
|
|
if (b === '-') {
|
|
|
|
if (reader.peekTest(/-\w/yu)) {
|
|
|
|
this.identOrFunctionToken(a, tok);
|
|
|
|
} else if (reader.readMatch('->')) {
|
|
|
|
tok.type = Tokens.CDC;
|
|
|
|
tok.value = '-->';
|
|
|
|
}
|
|
|
|
} else if (b >= '0' && b <= '9' || b === '.' && reader.peekTest(/\.\d/y)) {
|
|
|
|
this.numberToken(a, tok);
|
2021-01-01 14:27:58 +00:00
|
|
|
} else if (rxIdentStart.test(b)) {
|
2020-12-09 07:38:28 +00:00
|
|
|
this.identOrFunctionToken(a, tok);
|
|
|
|
} else {
|
|
|
|
tok.type = Tokens.MINUS;
|
|
|
|
}
|
|
|
|
return tok;
|
2020-11-25 18:01:36 +00:00
|
|
|
case '+':
|
2020-12-09 07:38:28 +00:00
|
|
|
if (b >= '0' && b <= '9' || b === '.' && reader.peekTest(/\.\d/y)) {
|
|
|
|
this.numberToken(a, tok);
|
|
|
|
} else {
|
|
|
|
tok.type = Tokens.PLUS;
|
|
|
|
}
|
|
|
|
return tok;
|
2017-12-26 20:39:52 +00:00
|
|
|
case '!':
|
2020-12-09 07:38:28 +00:00
|
|
|
return this.importantToken(a, tok);
|
2017-12-26 20:39:52 +00:00
|
|
|
case '@':
|
2020-12-09 07:38:28 +00:00
|
|
|
return this.atRuleToken(a, tok);
|
|
|
|
case ':': {
|
2022-06-30 10:36:40 +00:00
|
|
|
const func = /[-hniw]/i.test(b) &&
|
|
|
|
reader.readMatch(/(has|not|is|where|(-(moz|webkit)-)?any)\(/iy);
|
2020-12-09 07:38:28 +00:00
|
|
|
if (func) {
|
|
|
|
const first = b.toLowerCase();
|
|
|
|
tok.type =
|
2022-06-30 10:36:40 +00:00
|
|
|
first === 'h' ? Tokens.HAS :
|
2020-12-09 07:38:28 +00:00
|
|
|
first === 'n' ? Tokens.NOT :
|
|
|
|
first === 'i' ? Tokens.IS :
|
|
|
|
first === 'w' ? Tokens.WHERE : Tokens.ANY;
|
|
|
|
tok.value += func;
|
|
|
|
} else {
|
|
|
|
tok.type = Tokens.COLON;
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
case '<':
|
2020-12-09 07:38:28 +00:00
|
|
|
if (b === '!' && reader.readMatch('!--')) {
|
|
|
|
tok.type = Tokens.CDO;
|
|
|
|
tok.value = '<!--';
|
|
|
|
}
|
|
|
|
return tok;
|
2018-01-06 11:05:20 +00:00
|
|
|
// EOF
|
|
|
|
case null:
|
2020-12-09 07:38:28 +00:00
|
|
|
tok.type = Tokens.EOF;
|
|
|
|
return tok;
|
2017-12-26 20:39:52 +00:00
|
|
|
case 'U':
|
|
|
|
case 'u':
|
2020-12-09 07:38:28 +00:00
|
|
|
return b === '+'
|
|
|
|
? this.unicodeRangeToken(a, tok)
|
|
|
|
: this.identOrFunctionToken(a, tok);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
if (a >= '0' && a <= '9') {
|
|
|
|
this.numberToken(a, tok);
|
2021-01-01 14:27:58 +00:00
|
|
|
} else if (rxIdentStart.test(a)) {
|
2020-12-09 07:38:28 +00:00
|
|
|
this.identOrFunctionToken(a, tok);
|
|
|
|
} else {
|
|
|
|
tok.type = typeMap.get(a) || Tokens.CHAR;
|
|
|
|
}
|
|
|
|
return tok;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
atRuleToken(first, token) {
|
2018-01-06 11:05:20 +00:00
|
|
|
this._reader.mark();
|
2020-08-27 19:39:49 +00:00
|
|
|
let rule = first + this.readName();
|
2018-01-06 11:05:20 +00:00
|
|
|
let tt = Tokens.type(lower(rule));
|
2017-12-26 20:39:52 +00:00
|
|
|
// if it's not valid, use the first character only and reset the reader
|
|
|
|
if (tt === Tokens.CHAR || tt === Tokens.UNKNOWN) {
|
|
|
|
if (rule.length > 1) {
|
|
|
|
tt = Tokens.UNKNOWN_SYM;
|
|
|
|
} else {
|
|
|
|
tt = Tokens.CHAR;
|
|
|
|
rule = first;
|
2018-01-06 11:05:20 +00:00
|
|
|
this._reader.reset();
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
token.type = tt;
|
|
|
|
token.value = rule;
|
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
identOrFunctionToken(first, token) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const reader = this._reader;
|
2020-12-14 18:18:15 +00:00
|
|
|
const name = this.readChunksWithEscape(first, rxNameCharNoEsc);
|
2018-01-06 11:05:20 +00:00
|
|
|
const next = reader.peek();
|
2020-12-09 07:38:28 +00:00
|
|
|
token.value = name;
|
2018-01-06 11:05:20 +00:00
|
|
|
// might be a URI or function
|
|
|
|
if (next === '(') {
|
|
|
|
reader.read();
|
2020-10-24 10:18:17 +00:00
|
|
|
if (/^(url(-prefix)?|domain)$/i.test(name)) {
|
2018-01-06 11:05:20 +00:00
|
|
|
reader.mark();
|
|
|
|
const uri = this.readURI(name + '(');
|
|
|
|
if (uri) {
|
2020-12-09 07:38:28 +00:00
|
|
|
token.type = Tokens.URI;
|
|
|
|
token.value = uri.text;
|
2018-01-06 11:05:20 +00:00
|
|
|
token.name = name;
|
|
|
|
token.uri = uri.value;
|
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
reader.reset();
|
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
token.type = Tokens.FUNCTION;
|
|
|
|
token.value += '(';
|
|
|
|
} else if (next === ':' && lowerCmp(name, 'progid')) {
|
|
|
|
token.type = Tokens.IE_FUNCTION;
|
|
|
|
token.value += reader.readTo('(');
|
|
|
|
} else {
|
|
|
|
token.type = Tokens.IDENT;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
importantToken(first, token) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const reader = this._reader;
|
|
|
|
let text = first;
|
|
|
|
reader.mark();
|
|
|
|
for (let pass = 1; pass++ <= 2;) {
|
|
|
|
const important = reader.readMatch(/\s*important\b/iy);
|
|
|
|
if (important) {
|
2020-12-09 07:38:28 +00:00
|
|
|
token.type = Tokens.IMPORTANT;
|
|
|
|
token.value = text + important;
|
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
const comment = reader.readMatch('/*');
|
|
|
|
if (!comment) break;
|
2020-12-09 07:38:28 +00:00
|
|
|
text += this.readComment(comment);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
reader.reset();
|
2020-12-09 07:38:28 +00:00
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
numberToken(first, token) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const reader = this._reader;
|
2020-12-09 07:38:28 +00:00
|
|
|
const value = first + (
|
|
|
|
this._reader.readMatch(
|
|
|
|
first === '.' ?
|
|
|
|
/\d+(e[+-]?\d+)?/iy :
|
|
|
|
first >= '0' && first <= '9' ?
|
|
|
|
/\d*\.?\d*(e[+-]?\d+)?/iy :
|
|
|
|
/(\d*\.\d+|\d+\.?\d*)(e[+-]?\d+)?/iy
|
|
|
|
) || '');
|
2017-12-26 20:39:52 +00:00
|
|
|
let tt = Tokens.NUMBER;
|
|
|
|
let units, type;
|
|
|
|
const c = reader.peek();
|
2021-01-01 14:27:58 +00:00
|
|
|
if (rxIdentStart.test(c)) {
|
2017-12-26 20:39:52 +00:00
|
|
|
units = this.readName(reader.read());
|
2020-12-09 07:38:28 +00:00
|
|
|
type = UNITS[units] || UNITS[lower(units)];
|
2017-12-26 20:39:52 +00:00
|
|
|
tt = type && Tokens[type.toUpperCase()] ||
|
|
|
|
type === 'frequency' && Tokens.FREQ ||
|
|
|
|
Tokens.DIMENSION;
|
|
|
|
} else if (c === '%') {
|
|
|
|
units = reader.read();
|
|
|
|
type = 'percentage';
|
|
|
|
tt = Tokens.PERCENTAGE;
|
|
|
|
} else {
|
|
|
|
type = 'number';
|
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
token.type = tt;
|
|
|
|
token.value = units ? value + units : value;
|
2017-12-26 20:39:52 +00:00
|
|
|
token.number = parseFloat(value);
|
|
|
|
if (units) token.units = units;
|
|
|
|
if (type) token.unitsType = type;
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
stringToken(first, token) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const delim = first;
|
2018-01-06 11:05:20 +00:00
|
|
|
const string = first ? [first] : [];
|
2017-12-26 20:39:52 +00:00
|
|
|
const reader = this._reader;
|
|
|
|
let tt = Tokens.STRING;
|
2020-11-25 18:01:36 +00:00
|
|
|
let c;
|
|
|
|
while (true) {
|
|
|
|
c = reader.readMatch(/[^\n\r\f\\'"]+|./y);
|
|
|
|
if (!c) break;
|
2018-01-06 11:05:20 +00:00
|
|
|
string.push(c);
|
2017-12-26 20:39:52 +00:00
|
|
|
if (c === '\\') {
|
|
|
|
c = reader.read();
|
2020-10-24 10:18:17 +00:00
|
|
|
if (c == null) {
|
2017-12-26 20:39:52 +00:00
|
|
|
break; // premature EOF after backslash
|
|
|
|
} else if (/[^\r\n\f0-9a-f]/i.test(c)) {
|
|
|
|
// single-character escape
|
2018-01-06 11:05:20 +00:00
|
|
|
string.push(c);
|
2017-12-26 20:39:52 +00:00
|
|
|
} else {
|
2020-11-25 18:01:36 +00:00
|
|
|
// read up to six hex digits + newline
|
|
|
|
string.push(c, reader.readMatch(/[0-9a-f]{1,6}\n?/yi));
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
} else if (c === delim) {
|
|
|
|
break; // delimiter found.
|
2020-11-25 18:01:36 +00:00
|
|
|
} else if (reader.peekTest(/[\n\r\f]/y)) {
|
2017-12-26 20:39:52 +00:00
|
|
|
// newline without an escapement: it's an invalid string
|
|
|
|
tt = Tokens.INVALID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
token.type = c ? tt : Tokens.INVALID; // if the string wasn't closed
|
|
|
|
token.value = fastJoin(string);
|
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
unicodeRangeToken(first, token) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const reader = this._reader;
|
|
|
|
reader.mark();
|
2020-12-09 07:38:28 +00:00
|
|
|
token.value += reader.read(); // +
|
2017-12-26 20:39:52 +00:00
|
|
|
let chunk = this.readUnicodeRangePart(true);
|
|
|
|
if (!chunk) {
|
|
|
|
reader.reset();
|
2020-12-09 07:38:28 +00:00
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
token.value += chunk;
|
2017-12-26 20:39:52 +00:00
|
|
|
// if there's a ? in the first part, there can't be a second part
|
2020-12-09 07:38:28 +00:00
|
|
|
if (!token.value.includes('?') && reader.peek() === '-') {
|
2017-12-26 20:39:52 +00:00
|
|
|
reader.mark();
|
|
|
|
reader.read();
|
|
|
|
chunk = this.readUnicodeRangePart(false);
|
|
|
|
if (!chunk) {
|
|
|
|
reader.reset();
|
|
|
|
} else {
|
2020-12-09 07:38:28 +00:00
|
|
|
token.value += '-' + chunk;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
token.type = Tokens.UNICODE_RANGE;
|
|
|
|
return token;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readUnicodeRangePart(allowQuestionMark) {
|
|
|
|
const reader = this._reader;
|
|
|
|
let part = reader.readMatch(/[0-9a-f]{1,6}/iy);
|
2020-12-09 07:38:28 +00:00
|
|
|
while (allowQuestionMark && part.length < 6 && reader.peek() === '?') {
|
|
|
|
part += reader.read();
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
return part;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns null w/o resetting reader if string is invalid.
|
2020-11-25 18:01:36 +00:00
|
|
|
readString(first = this._reader.read()) {
|
2020-12-09 07:38:28 +00:00
|
|
|
const token = this.stringToken(first, {});
|
2017-12-26 20:39:52 +00:00
|
|
|
return token.type !== Tokens.INVALID ? token.value : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns null w/o resetting reader if URI is invalid.
|
|
|
|
readURI(first) {
|
|
|
|
const reader = this._reader;
|
|
|
|
const uri = first;
|
|
|
|
let value = '';
|
2020-12-09 07:38:28 +00:00
|
|
|
this._reader.readMatch(/\s+/y);
|
2020-11-25 18:01:36 +00:00
|
|
|
if (reader.peekTest(/['"]/y)) {
|
2017-12-26 20:39:52 +00:00
|
|
|
value = this.readString();
|
2020-10-24 10:18:17 +00:00
|
|
|
if (value == null) return null;
|
2018-01-06 11:05:20 +00:00
|
|
|
value = parseString(value);
|
2017-12-26 20:39:52 +00:00
|
|
|
} else {
|
2020-12-14 18:18:15 +00:00
|
|
|
value = this.readChunksWithEscape('', rxUnquotedUrlCharNoEsc);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
this._reader.readMatch(/\s+/y);
|
2017-12-26 20:39:52 +00:00
|
|
|
// Ensure argument to URL is always double-quoted
|
|
|
|
// (This simplifies later processing in PropertyValuePart.)
|
2020-11-25 18:01:36 +00:00
|
|
|
return reader.peek() !== ')' ? null : {
|
|
|
|
value,
|
|
|
|
text: uri + serializeString(value) + reader.read(),
|
|
|
|
};
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readName(first) {
|
2020-12-14 18:18:15 +00:00
|
|
|
return this.readChunksWithEscape(first, rxNameCharNoEsc);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readEscape() {
|
2021-04-20 17:27:07 +00:00
|
|
|
let res = this._reader.readMatch(/[0-9a-f]{1,6}\s?/iy);
|
|
|
|
if (res) {
|
|
|
|
res = parseInt(res, 16);
|
|
|
|
res = String.fromCodePoint(res && res <= 0x10FFFF ? res : 0xFFFD);
|
|
|
|
} else {
|
|
|
|
res = this._reader.read();
|
|
|
|
}
|
|
|
|
return res;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 18:18:15 +00:00
|
|
|
/**
|
|
|
|
* @param {?string} first
|
|
|
|
* @param {RegExp} rx - must not match \\
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-01-06 11:05:20 +00:00
|
|
|
readChunksWithEscape(first, rx) {
|
|
|
|
const reader = this._reader;
|
|
|
|
const url = first ? [first] : [];
|
|
|
|
while (true) {
|
|
|
|
const chunk = reader.readMatch(rx);
|
|
|
|
if (chunk) url.push(chunk);
|
2020-11-25 18:01:36 +00:00
|
|
|
if (reader.peekTest(/\\[^\r\n\f]/y)) {
|
|
|
|
reader.read();
|
|
|
|
url.push(this.readEscape());
|
2018-01-06 11:05:20 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
return fastJoin(url);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readComment(first) {
|
|
|
|
return first +
|
|
|
|
this._reader.readCount(2 - first.length) +
|
2018-02-28 02:15:28 +00:00
|
|
|
this._reader.readMatch(/([^*]|\*(?!\/))*(\*\/|$)/y);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} [omitComments]
|
|
|
|
* @param {string} [stopOn] - goes to the parent if used at the top nesting level of the value,
|
|
|
|
specifying an empty string will stop after consuming the first encountered top block.
|
|
|
|
* @returns {?string}
|
|
|
|
*/
|
|
|
|
readDeclValue({omitComments, stopOn = ';!})'} = {}) {
|
|
|
|
const reader = this._reader;
|
|
|
|
const value = [];
|
|
|
|
const endings = [];
|
|
|
|
let end = stopOn;
|
|
|
|
const rx = stopOn.includes(';')
|
|
|
|
? /([^;!'"{}()[\]/\\]|\/(?!\*))+/y
|
|
|
|
: /([^'"{}()[\]/\\]|\/(?!\*))+/y;
|
|
|
|
while (!reader.eof()) {
|
|
|
|
const chunk = reader.readMatch(rx);
|
|
|
|
if (chunk) {
|
|
|
|
value.push(chunk);
|
|
|
|
}
|
|
|
|
reader.mark();
|
|
|
|
const c = reader.read();
|
|
|
|
if (!endings.length && stopOn.includes(c)) {
|
|
|
|
reader.reset();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
value.push(c);
|
|
|
|
if (c === '\\') {
|
|
|
|
value[value.length - 1] = this.readEscape();
|
|
|
|
} else if (c === '/') {
|
|
|
|
value[value.length - 1] = this.readComment(c);
|
|
|
|
if (omitComments) value.pop();
|
|
|
|
} else if (c === '"' || c === "'") {
|
|
|
|
value[value.length - 1] = this.readString(c);
|
|
|
|
} else if (c === '{' || c === '(' || c === '[') {
|
|
|
|
endings.push(end);
|
|
|
|
end = c === '{' ? '}' : c === '(' ? ')' : ']';
|
|
|
|
} else if (c === '}' || c === ')' || c === ']') {
|
|
|
|
if (!end.includes(c)) {
|
|
|
|
reader.reset();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
end = endings.pop();
|
|
|
|
if (!end && !stopOn) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fastJoin(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
readUnknownSym() {
|
|
|
|
const reader = this._reader;
|
2022-06-26 15:13:13 +00:00
|
|
|
let prelude = '';
|
2021-01-01 14:27:58 +00:00
|
|
|
let block;
|
|
|
|
while (true) {
|
2022-06-26 15:13:13 +00:00
|
|
|
let c = reader.peek();
|
|
|
|
if (!c) this.throwUnexpected();
|
2021-01-01 14:27:58 +00:00
|
|
|
if (c === '{') {
|
|
|
|
block = this.readDeclValue({stopOn: ''});
|
|
|
|
break;
|
|
|
|
} else if (c === ';') {
|
|
|
|
reader.read();
|
|
|
|
break;
|
|
|
|
} else {
|
2022-06-26 15:13:13 +00:00
|
|
|
c = this.readDeclValue({omitComments: true, stopOn: ';{}'});
|
|
|
|
if (!c) break;
|
|
|
|
prelude += c;
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-26 15:13:13 +00:00
|
|
|
return {prelude: prelude.replace(/^\s+/, ''), block};
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region parserCache
|
2018-01-06 11:05:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Caches the results and reuses them on subsequent parsing of the same code
|
|
|
|
*/
|
|
|
|
const parserCache = (() => {
|
|
|
|
const MAX_DURATION = 10 * 60e3;
|
|
|
|
const TRIM_DELAY = 10e3;
|
|
|
|
// all blocks since page load; key = text between block start and { inclusive
|
|
|
|
const data = new Map();
|
|
|
|
// nested block stack
|
|
|
|
const stack = [];
|
|
|
|
// performance.now() of the current parser
|
|
|
|
let generation = null;
|
|
|
|
// performance.now() of the first parser after reset or page load,
|
|
|
|
// used for weighted sorting in getBlock()
|
|
|
|
let generationBase = null;
|
|
|
|
// true on page load, first run is pure analysis
|
|
|
|
let firstRun = true;
|
|
|
|
let parser = null;
|
|
|
|
let stream = null;
|
|
|
|
|
|
|
|
return {
|
2020-11-25 18:01:36 +00:00
|
|
|
start(newParser) {
|
|
|
|
parser = newParser;
|
|
|
|
if (!parser) {
|
|
|
|
data.clear();
|
|
|
|
stack.length = 0;
|
|
|
|
generationBase = performance.now();
|
2018-01-06 11:05:20 +00:00
|
|
|
return;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
if (firstRun) firstRun = false;
|
|
|
|
stream = parser._tokenStream;
|
|
|
|
generation = performance.now();
|
|
|
|
trim();
|
|
|
|
},
|
|
|
|
addEvent(event) {
|
|
|
|
if (!parser) return;
|
|
|
|
for (let i = stack.length; --i >= 0;) {
|
|
|
|
const {offset, endOffset, events} = stack[i];
|
|
|
|
if (event.offset >= offset && (!endOffset || event.offset <= endOffset)) {
|
|
|
|
events.push(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
findBlock(token = getToken()) {
|
|
|
|
if (!parser || firstRun || !token) return;
|
|
|
|
|
|
|
|
const reader = stream._reader;
|
|
|
|
const input = reader._input;
|
|
|
|
let start = token.offset;
|
|
|
|
const c = input[start];
|
|
|
|
if (c === ' ' || c === '\t' || c === '\n' || c === '\f' || c === '\r') {
|
|
|
|
const rx = /\s*/y;
|
|
|
|
rx.lastIndex = start;
|
|
|
|
rx.exec(input);
|
|
|
|
start = rx.lastIndex;
|
|
|
|
}
|
|
|
|
const key = input.slice(start, input.indexOf('{', start) + 1);
|
|
|
|
const blocks = data.get(key);
|
|
|
|
if (!blocks) return;
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
const block = getBlock(blocks, input, start, key);
|
|
|
|
if (!block) return;
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
reader.readCount(start - reader._cursor);
|
|
|
|
shiftBlock(reader, start, block);
|
|
|
|
shiftStream(reader, block);
|
|
|
|
parser._ws();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
startBlock(start = getToken()) {
|
|
|
|
if (!parser) return;
|
|
|
|
stack.push({
|
|
|
|
text: '',
|
|
|
|
events: [],
|
|
|
|
generation: generation,
|
|
|
|
line: start.line,
|
|
|
|
col: start.col,
|
|
|
|
offset: start.offset,
|
|
|
|
endLine: undefined,
|
|
|
|
endCol: undefined,
|
|
|
|
endOffset: undefined,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
adjustBlockStart(start = getToken()) {
|
|
|
|
if (!parser) return;
|
|
|
|
const block = stack[stack.length - 1];
|
|
|
|
block.line = start.line;
|
|
|
|
block.col = start.col;
|
|
|
|
block.offset = start.offset;
|
|
|
|
},
|
|
|
|
endBlock(end = getToken()) {
|
|
|
|
if (!parser) return;
|
|
|
|
const block = stack.pop();
|
|
|
|
block.endLine = end.line;
|
|
|
|
block.endCol = end.col + end.value.length;
|
|
|
|
block.endOffset = end.offset + end.value.length;
|
|
|
|
|
|
|
|
const input = stream._reader._input;
|
|
|
|
const key = input.slice(block.offset, input.indexOf('{', block.offset) + 1);
|
|
|
|
block.text = input.slice(block.offset, block.endOffset);
|
|
|
|
|
|
|
|
let blocks = data.get(key);
|
|
|
|
if (!blocks) data.set(key, (blocks = []));
|
|
|
|
blocks.push(block);
|
|
|
|
},
|
|
|
|
cancelBlock: () => stack.pop(),
|
|
|
|
feedback({messages}) {
|
|
|
|
messages = new Set(messages);
|
|
|
|
for (const blocks of data.values()) {
|
|
|
|
for (const block of blocks) {
|
|
|
|
if (!block.events.length) continue;
|
|
|
|
if (block.generation !== generation) continue;
|
|
|
|
const {
|
|
|
|
line: L1,
|
|
|
|
col: C1,
|
|
|
|
endLine: L2,
|
|
|
|
endCol: C2,
|
|
|
|
} = block;
|
|
|
|
let isClean = true;
|
|
|
|
for (const msg of messages) {
|
|
|
|
const {line, col} = msg;
|
|
|
|
if (L1 === L2 && line === L1 && C1 <= col && col <= C2 ||
|
2021-01-01 14:27:58 +00:00
|
|
|
line === L1 && col >= C1 ||
|
|
|
|
line === L2 && col <= C2 ||
|
|
|
|
line > L1 && line < L2) {
|
2020-11-25 18:01:36 +00:00
|
|
|
messages.delete(msg);
|
|
|
|
isClean = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isClean) block.events.length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
|
|
|
* Removes old entries from the cache.
|
|
|
|
* 'Old' means older than MAX_DURATION or half the blocks from the previous generation(s).
|
|
|
|
* @param {Boolean} [immediately] - set internally when debounced by TRIM_DELAY
|
|
|
|
*/
|
|
|
|
function trim(immediately) {
|
|
|
|
if (!immediately) {
|
|
|
|
clearTimeout(trim.timer);
|
|
|
|
trim.timer = setTimeout(trim, TRIM_DELAY, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const cutoff = performance.now() - MAX_DURATION;
|
|
|
|
for (const [key, blocks] of data.entries()) {
|
|
|
|
const halfLen = blocks.length >> 1;
|
|
|
|
const newBlocks = blocks
|
|
|
|
.sort((a, b) => a.time - b.time)
|
|
|
|
.filter((block, i) => block.generation > cutoff ||
|
|
|
|
block.generation !== generation && i < halfLen);
|
|
|
|
if (!newBlocks.length) {
|
|
|
|
data.delete(key);
|
|
|
|
} else if (newBlocks.length !== blocks.length) {
|
|
|
|
data.set(key, newBlocks);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
// gets the matching block
|
|
|
|
function getBlock(blocks, input, start, key) {
|
|
|
|
// extracted to prevent V8 deopt
|
|
|
|
const keyLast = Math.max(key.length - 1);
|
|
|
|
const check1 = input[start];
|
|
|
|
const check2 = input[start + keyLast];
|
|
|
|
const generationSpan = performance.now() - generationBase;
|
|
|
|
blocks = blocks
|
|
|
|
.filter(({text, offset, endOffset}) =>
|
|
|
|
text[0] === check1 &&
|
|
|
|
text[keyLast] === check2 &&
|
|
|
|
text[text.length - 1] === input[start + text.length - 1] &&
|
|
|
|
text.startsWith(key) &&
|
|
|
|
text === input.substr(start, endOffset - offset))
|
|
|
|
.sort((a, b) =>
|
|
|
|
// newest and closest will be the first element
|
|
|
|
(a.generation - b.generation) / generationSpan +
|
|
|
|
(Math.abs(a.offset - start) - Math.abs(b.offset - start)) / input.length);
|
|
|
|
// identical blocks may produce different reports in CSSLint
|
|
|
|
// so we need to either hijack an older generation block or make a clone
|
|
|
|
const block = blocks.find(b => b.generation !== generation);
|
|
|
|
return block || deepCopy(blocks[0]);
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
// Shifts positions of the block and its events, also fires the events
|
|
|
|
function shiftBlock(reader, start, block) {
|
|
|
|
// extracted to prevent V8 deopt
|
2020-11-25 18:01:36 +00:00
|
|
|
const deltaLines = reader._line - block.line;
|
|
|
|
const deltaCols = block.col === 1 && reader._col === 1 ? 0 : reader._col - block.col;
|
2018-01-06 11:05:20 +00:00
|
|
|
const deltaOffs = reader._cursor - block.offset;
|
|
|
|
const hasDelta = deltaLines || deltaCols || deltaOffs;
|
|
|
|
const shifted = new Set();
|
|
|
|
for (const e of block.events) {
|
|
|
|
if (hasDelta) {
|
2020-11-25 18:01:36 +00:00
|
|
|
applyDelta(e, shifted, block.line, deltaLines, deltaCols, deltaOffs);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
parser.fire(e, false);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
block.generation = generation;
|
2020-11-25 18:01:36 +00:00
|
|
|
block.endCol += block.endLine === block.line ? deltaCols : 0;
|
2018-01-06 11:05:20 +00:00
|
|
|
block.endLine += deltaLines;
|
|
|
|
block.endOffset = reader._cursor + block.text.length;
|
2020-11-25 18:01:36 +00:00
|
|
|
block.line += deltaLines;
|
|
|
|
block.col += deltaCols;
|
2018-01-06 11:05:20 +00:00
|
|
|
block.offset = reader._cursor;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
function shiftStream(reader, block) {
|
|
|
|
reader._line = block.endLine;
|
|
|
|
reader._col = block.endCol;
|
|
|
|
reader._cursor = block.endOffset;
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
stream.resetLT();
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
// Recursively applies the delta to the event and all its nested parts
|
2020-11-25 18:01:36 +00:00
|
|
|
function applyDelta(obj, seen, line, lines, cols, offs) {
|
2018-01-06 11:05:20 +00:00
|
|
|
if (seen.has(obj)) return;
|
|
|
|
seen.add(obj);
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
for (const item of obj) {
|
|
|
|
if ((typeof item === 'object' || Array.isArray(item)) && item) {
|
2020-11-25 18:01:36 +00:00
|
|
|
applyDelta(item, seen, line, lines, cols, offs);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// applyDelta may get surpisingly slow on complex objects so we're using an array
|
|
|
|
// because in js an array lookup is much faster than a property lookup
|
|
|
|
const keys = Object.keys(obj);
|
|
|
|
if (cols !== 0) {
|
2020-11-25 18:01:36 +00:00
|
|
|
if (keys.includes('col') && obj.line === line) obj.col += cols;
|
|
|
|
if (keys.includes('endCol') && obj.endLine === line) obj.endCol += cols;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
if (lines !== 0) {
|
|
|
|
if (keys.includes('line')) obj.line += lines;
|
|
|
|
if (keys.includes('endLine')) obj.endLine += lines;
|
|
|
|
}
|
|
|
|
if (offs !== 0 && keys.includes('offset')) obj.offset += offs;
|
|
|
|
for (const k of keys) {
|
2020-11-25 18:01:36 +00:00
|
|
|
if (k !== 'col' && k !== 'endCol' &&
|
|
|
|
k !== 'line' && k !== 'endLine' &&
|
2018-01-06 11:05:20 +00:00
|
|
|
k !== 'offset') {
|
|
|
|
const v = obj[k];
|
|
|
|
if (v && typeof v === 'object') {
|
2020-11-25 18:01:36 +00:00
|
|
|
applyDelta(v, seen, line, lines, cols, offs);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
// returns next token if it's already seen or the current token
|
2018-01-06 11:05:20 +00:00
|
|
|
function getToken() {
|
2020-11-25 18:01:36 +00:00
|
|
|
return parser && (stream.LT(1, true) || stream._token);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
function deepCopy(obj) {
|
2020-11-25 18:01:36 +00:00
|
|
|
if (!obj || typeof obj !== 'object') {
|
|
|
|
return obj;
|
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
if (Array.isArray(obj)) {
|
2020-11-25 18:01:36 +00:00
|
|
|
return obj.map(deepCopy);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
const copy = {};
|
2020-12-09 07:38:28 +00:00
|
|
|
for (const k in obj) {
|
|
|
|
copy[k] = deepCopy(obj[k]);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
return copy;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
})();
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region Parser
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2022-02-03 21:43:08 +00:00
|
|
|
const ParserRoute = {};
|
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
class Parser extends EventTarget {
|
|
|
|
/**
|
2018-01-06 11:05:20 +00:00
|
|
|
* @param {Object} [options]
|
2021-01-01 14:27:58 +00:00
|
|
|
* @param {boolean} [options.ieFilters] - accepts IE < 8 filters instead of throwing
|
|
|
|
* @param {boolean} [options.skipValidation] - skip syntax validation
|
|
|
|
* @param {boolean} [options.starHack] - allows IE6 star hack
|
|
|
|
* @param {boolean} [options.strict] - stop on errors instead of reporting them and continuing
|
|
|
|
* @param {boolean} [options.topDocOnly] - quickly extract all top-level @-moz-document,
|
|
|
|
their {}-block contents is retrieved as text using _simpleBlock()
|
|
|
|
* @param {boolean} [options.underscoreHack] - interprets leading _ as IE6-7 for known props
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
|
|
|
constructor(options) {
|
|
|
|
super();
|
|
|
|
this.options = options || {};
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @type {TokenStream|TokenStreamBase} */
|
2017-12-26 20:39:52 +00:00
|
|
|
this._tokenStream = null;
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:05:20 +00:00
|
|
|
/**
|
2021-01-01 14:27:58 +00:00
|
|
|
* @param {string|Object} event
|
|
|
|
* @param {parserlib.Token|SyntaxUnit} [token=this._tokenStream._token] - sets the position
|
2018-01-06 11:05:20 +00:00
|
|
|
*/
|
2022-01-19 11:42:10 +00:00
|
|
|
fire(event, token = event.offset != null ? event : this._tokenStream._token) {
|
2017-12-26 20:39:52 +00:00
|
|
|
if (typeof event === 'string') {
|
|
|
|
event = {type: event};
|
|
|
|
}
|
|
|
|
if (event.offset === undefined && token) {
|
|
|
|
event.offset = token.offset;
|
2020-11-25 18:01:36 +00:00
|
|
|
if (event.line === undefined) event.line = token.line;
|
|
|
|
if (event.col === undefined) event.col = token.col;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
if (token !== false) parserCache.addEvent(event);
|
2018-01-06 11:05:20 +00:00
|
|
|
super.fire(event);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
|
2022-02-03 21:43:08 +00:00
|
|
|
/**
|
|
|
|
* @layer <layer-name>#;
|
|
|
|
* @layer <layer-name>? { <stylesheet> };
|
|
|
|
*/
|
|
|
|
_layer(start) {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
const ids = [];
|
|
|
|
let t, val;
|
|
|
|
do {
|
|
|
|
this._ws();
|
|
|
|
if ((t = stream.get(true)).type === Tokens.IDENT) {
|
|
|
|
ids.push(t.value);
|
|
|
|
this._ws();
|
|
|
|
t = stream.get(true);
|
|
|
|
}
|
|
|
|
if ((val = t.value) === '{') {
|
|
|
|
if (ids[1]) this.fire({type: 'error', message: '@layer block cannot have multiple ids'}, start);
|
2022-02-11 22:18:03 +00:00
|
|
|
this.fire({type: 'startlayer', id: ids[0] || null}, start);
|
|
|
|
this._rulesetBlock(start);
|
|
|
|
this.fire('endlayer');
|
|
|
|
this._ws();
|
2022-02-03 21:43:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (val === ',');
|
|
|
|
if (val !== ';') stream.mustMatch(Tokens.SEMICOLON);
|
|
|
|
this.fire({type: 'layer', ids}, start);
|
|
|
|
this._ws();
|
|
|
|
}
|
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
_stylesheet() {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
this.fire('startstylesheet');
|
2021-04-28 14:33:27 +00:00
|
|
|
this._sheetGlobals();
|
2021-01-01 14:27:58 +00:00
|
|
|
const {topDocOnly} = this.options;
|
2022-02-03 21:43:08 +00:00
|
|
|
const allowedActions = topDocOnly ? ParserRoute.topDoc : ParserRoute.stylesheet;
|
2021-01-01 14:27:58 +00:00
|
|
|
for (let tt, token; (tt = (token = stream.get(true)).type); this._skipCruft()) {
|
2018-01-06 11:05:20 +00:00
|
|
|
try {
|
2022-02-03 21:43:08 +00:00
|
|
|
const action = allowedActions[tt];
|
2018-01-06 11:05:20 +00:00
|
|
|
if (action) {
|
2021-01-01 14:27:58 +00:00
|
|
|
action.call(this, token);
|
2018-01-06 11:05:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (topDocOnly) {
|
|
|
|
stream.readDeclValue({stopOn: '{}'});
|
|
|
|
if (stream._reader.peek() === '{') {
|
|
|
|
stream.readDeclValue({stopOn: ''});
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stream.unget();
|
2020-11-25 18:01:36 +00:00
|
|
|
if (!this._ruleset() && stream.peek() !== Tokens.EOF) {
|
|
|
|
stream.throwUnexpected(stream.get(true));
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
if (ex instanceof SyntaxError && !this.options.strict) {
|
|
|
|
this.fire(Object.assign({}, ex, {type: 'error', error: ex}));
|
|
|
|
} else {
|
2020-12-09 07:38:28 +00:00
|
|
|
ex.message = ex.stack;
|
|
|
|
ex.line = token.line;
|
|
|
|
ex.col = token.col;
|
2017-12-26 20:39:52 +00:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.fire('endstylesheet');
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:33:27 +00:00
|
|
|
_sheetGlobals() {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
this._skipCruft();
|
|
|
|
for (const [type, fn, max = Infinity] of [
|
|
|
|
[Tokens.CHARSET_SYM, this._charset, 1],
|
2022-02-03 21:43:08 +00:00
|
|
|
[Tokens.LAYER_SYM, this._layer],
|
2021-04-28 14:33:27 +00:00
|
|
|
[Tokens.IMPORT_SYM, this._import],
|
|
|
|
[Tokens.NAMESPACE_SYM, this._namespace],
|
|
|
|
]) {
|
|
|
|
for (let i = 0; i++ < max && stream.peek() === type;) {
|
|
|
|
fn.call(this, stream.get(true));
|
|
|
|
this._skipCruft();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_charset(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2018-01-06 11:05:20 +00:00
|
|
|
const charset = stream.mustMatch(Tokens.STRING).value;
|
2017-12-26 20:39:52 +00:00
|
|
|
stream.mustMatch(Tokens.SEMICOLON);
|
2021-04-28 14:33:27 +00:00
|
|
|
this.fire({type: 'charset', charset}, start);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:33:27 +00:00
|
|
|
_import(start) {
|
2022-02-03 21:43:08 +00:00
|
|
|
let t, layer;
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2022-02-03 21:43:08 +00:00
|
|
|
t = stream.mustMatch(TT.stringUri);
|
|
|
|
const uri = t.uri || t.value.replace(/^["']|["']$/g, '');
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
2022-02-03 21:43:08 +00:00
|
|
|
t = stream.get(true);
|
|
|
|
if (/^layer(\()?$/i.test(t.value)) {
|
|
|
|
layer = RegExp.$1 ? stream.mustMatch(Tokens.IDENT) : '';
|
|
|
|
if (layer) stream.mustMatch(Tokens.RPAREN);
|
|
|
|
this._ws();
|
|
|
|
t = stream.get(true);
|
|
|
|
}
|
|
|
|
if (lowerCmp('supports(', t.value)) {
|
|
|
|
this._ws();
|
|
|
|
if (!this._declaration()) this._supportsCondition();
|
|
|
|
stream.mustMatch(Tokens.RPAREN);
|
|
|
|
} else {
|
|
|
|
stream.unget();
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
const media = this._mediaQueryList();
|
2017-12-26 20:39:52 +00:00
|
|
|
stream.mustMatch(Tokens.SEMICOLON);
|
2022-02-03 21:43:08 +00:00
|
|
|
this.fire({type: 'import', layer, media, uri}, start);
|
2018-01-06 11:05:20 +00:00
|
|
|
this._ws();
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:33:27 +00:00
|
|
|
_namespace(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
|
|
|
this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
const prefix = stream.match(Tokens.IDENT).value;
|
|
|
|
if (prefix) this._ws();
|
|
|
|
const token = stream.mustMatch(TT.stringUri);
|
|
|
|
const uri = token.uri || token.value.replace(/^["']|["']$/g, '');
|
2017-12-26 20:39:52 +00:00
|
|
|
stream.mustMatch(Tokens.SEMICOLON);
|
2021-04-28 14:33:27 +00:00
|
|
|
this.fire({type: 'namespace', prefix, uri}, start);
|
2018-01-06 11:05:20 +00:00
|
|
|
this._ws();
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:33:27 +00:00
|
|
|
_supports(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
|
|
|
this._ws();
|
|
|
|
this._supportsCondition();
|
|
|
|
stream.mustMatch(Tokens.LBRACE);
|
2021-04-28 14:33:27 +00:00
|
|
|
this.fire('startsupports', start);
|
2022-02-03 21:43:08 +00:00
|
|
|
this._rulesetBlock(start);
|
2021-04-28 14:33:27 +00:00
|
|
|
this.fire('endsupports');
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
|
|
|
}
|
|
|
|
|
|
|
|
_supportsCondition() {
|
|
|
|
const stream = this._tokenStream;
|
2020-11-25 18:01:36 +00:00
|
|
|
if (stream.match(Tokens.IDENT, ['not'])) {
|
2020-10-14 17:17:57 +00:00
|
|
|
stream.mustMatch(Tokens.S);
|
|
|
|
this._supportsConditionInParens();
|
2017-12-26 20:39:52 +00:00
|
|
|
} else {
|
|
|
|
this._supportsConditionInParens();
|
2020-11-25 18:01:36 +00:00
|
|
|
while (stream.match(Tokens.IDENT, ['and', 'or'])) {
|
|
|
|
this._ws();
|
|
|
|
this._supportsConditionInParens();
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_supportsConditionInParens() {
|
|
|
|
const stream = this._tokenStream;
|
2020-10-14 17:17:57 +00:00
|
|
|
const next = stream.LT(1);
|
|
|
|
if (next.type === Tokens.LPAREN) {
|
|
|
|
stream.get();
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
2020-10-14 17:17:57 +00:00
|
|
|
const {type, value} = stream.LT(1);
|
2020-12-09 07:38:28 +00:00
|
|
|
if (type === Tokens.IDENT) {
|
2020-10-24 10:18:17 +00:00
|
|
|
if (lowerCmp(value, 'not')) {
|
2017-12-26 20:39:52 +00:00
|
|
|
this._supportsCondition();
|
|
|
|
stream.mustMatch(Tokens.RPAREN);
|
|
|
|
} else {
|
2020-10-14 17:17:57 +00:00
|
|
|
this._supportsDecl(false);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._supportsCondition();
|
|
|
|
stream.mustMatch(Tokens.RPAREN);
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
} else if (stream.match(Tokens.FUNCTION, ['selector('])) {
|
2020-10-14 17:17:57 +00:00
|
|
|
this._ws();
|
2021-09-01 21:12:29 +00:00
|
|
|
const selector = this._selector();
|
|
|
|
this.fire({type: 'supportsSelector', selector}, selector);
|
2020-10-14 17:17:57 +00:00
|
|
|
stream.mustMatch(Tokens.RPAREN);
|
2017-12-26 20:39:52 +00:00
|
|
|
} else {
|
2020-10-14 17:17:57 +00:00
|
|
|
this._supportsDecl();
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-10-14 17:17:57 +00:00
|
|
|
this._ws();
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 17:17:57 +00:00
|
|
|
_supportsDecl(requireStartParen = true) {
|
|
|
|
const stream = this._tokenStream;
|
2018-01-06 11:05:20 +00:00
|
|
|
if (requireStartParen) {
|
2020-10-14 17:17:57 +00:00
|
|
|
stream.mustMatch(Tokens.LPAREN);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
this._ws();
|
|
|
|
this._declaration();
|
2020-10-14 17:17:57 +00:00
|
|
|
stream.mustMatch(Tokens.RPAREN);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_media(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
|
|
|
this._ws();
|
|
|
|
const mediaList = this._mediaQueryList();
|
|
|
|
stream.mustMatch(Tokens.LBRACE);
|
|
|
|
this.fire({
|
|
|
|
type: 'startmedia',
|
|
|
|
media: mediaList,
|
|
|
|
}, start);
|
2022-02-03 21:43:08 +00:00
|
|
|
this._rulesetBlock(start);
|
2017-12-26 20:39:52 +00:00
|
|
|
this.fire({
|
|
|
|
type: 'endmedia',
|
|
|
|
media: mediaList,
|
|
|
|
});
|
|
|
|
this._ws();
|
|
|
|
}
|
|
|
|
|
|
|
|
_mediaQueryList() {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
const mediaList = [];
|
|
|
|
this._ws();
|
|
|
|
if ([Tokens.IDENT, Tokens.LPAREN].includes(stream.peek())) {
|
|
|
|
mediaList.push(this._mediaQuery());
|
|
|
|
}
|
|
|
|
while (stream.match(Tokens.COMMA)) {
|
|
|
|
this._ws();
|
|
|
|
mediaList.push(this._mediaQuery());
|
|
|
|
}
|
|
|
|
return mediaList;
|
|
|
|
}
|
|
|
|
|
|
|
|
_mediaQuery() {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
const expressions = [];
|
2022-06-24 07:22:49 +00:00
|
|
|
const mod = stream.match(Tokens.IDENT, ['only', 'not']);
|
2020-10-24 10:18:17 +00:00
|
|
|
let type = null;
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
2020-10-24 10:18:17 +00:00
|
|
|
const next = stream.LT(1);
|
2022-06-24 07:22:49 +00:00
|
|
|
if (next.type === Tokens.IDENT) {
|
|
|
|
type = this._mediaFeature();
|
|
|
|
} else if (next.value === '(') {
|
2022-06-26 18:37:43 +00:00
|
|
|
expressions.push(this._mediaExpression());
|
2022-06-24 07:22:49 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
this._ws();
|
2022-06-24 07:22:49 +00:00
|
|
|
const c = stream.match(Tokens.IDENT).value;
|
|
|
|
if (c) {
|
|
|
|
if (lowerCmp(c, 'and') || !type && lowerCmp(c, 'or')) {
|
2020-10-24 10:18:17 +00:00
|
|
|
this._ws();
|
|
|
|
expressions.push(this._mediaExpression());
|
|
|
|
} else {
|
2022-06-24 07:22:49 +00:00
|
|
|
stream.throwUnexpected(undefined, ["'and'", !type && "'or'"].filter(Boolean));
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-24 07:22:49 +00:00
|
|
|
return new MediaQuery(mod.value || null, type, expressions, mod || next);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_mediaExpression() {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
stream.mustMatch(Tokens.LPAREN);
|
2022-06-24 07:22:49 +00:00
|
|
|
const feature = this._mediaFeature(TT.mediaValue);
|
|
|
|
let b;
|
|
|
|
for (let pass = 0; ++pass <= 2;) {
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
2022-06-24 07:22:49 +00:00
|
|
|
b = stream.get(true).value;
|
|
|
|
if (/^[:=<>]$/.test(b)) {
|
|
|
|
const isRange = /[<>]/.test(b);
|
|
|
|
if (isRange) stream.match(Tokens.EQUALS);
|
|
|
|
this._ws();
|
|
|
|
b = this._expression({calc: true});
|
|
|
|
if (!isRange) break;
|
|
|
|
} else {
|
|
|
|
stream.unget();
|
|
|
|
b = null;
|
|
|
|
break;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
stream.mustMatch(Tokens.RPAREN);
|
|
|
|
this._ws();
|
2022-06-24 07:22:49 +00:00
|
|
|
return new MediaFeature(feature); // TODO: construct the value properly
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 07:22:49 +00:00
|
|
|
_mediaFeature(type = Tokens.IDENT) {
|
|
|
|
return SyntaxUnit.fromToken(this._tokenStream.mustMatch(type));
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_page(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
|
|
|
this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
const id = stream.match(Tokens.IDENT).value || null;
|
|
|
|
if (id && lowerCmp(id, 'auto')) {
|
|
|
|
stream.throwUnexpected();
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
const pseudo = stream.match(Tokens.COLON)
|
|
|
|
? stream.mustMatch(Tokens.IDENT, false).value
|
|
|
|
: null;
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'startpage', id, pseudo}, start);
|
2017-12-26 20:39:52 +00:00
|
|
|
this._readDeclarations({readMargins: true});
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'endpage', id, pseudo});
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_margin() {
|
2020-11-25 18:01:36 +00:00
|
|
|
const margin = SyntaxUnit.fromToken(this._tokenStream.match(TT.margins));
|
2017-12-26 20:39:52 +00:00
|
|
|
if (!margin) return false;
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'startpagemargin', margin});
|
2017-12-26 20:39:52 +00:00
|
|
|
this._readDeclarations();
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'endpagemargin', margin});
|
2017-12-26 20:39:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_fontFace(start) {
|
|
|
|
this.fire('startfontface', start);
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
2021-09-07 09:36:36 +00:00
|
|
|
this._readDeclarations({Props: ScopedProperties['@font-face']});
|
2017-12-26 20:39:52 +00:00
|
|
|
this.fire('endfontface');
|
|
|
|
}
|
|
|
|
|
2022-04-01 13:05:45 +00:00
|
|
|
_fontPaletteValues(start) {
|
|
|
|
this.fire({
|
|
|
|
type: 'startfontpalettevalues',
|
|
|
|
id: this._tokenStream.mustMatch(Tokens.IDENT),
|
|
|
|
}, start);
|
|
|
|
this._readDeclarations({Props: ScopedProperties['@font-palette-values']});
|
|
|
|
this.fire('endfontpalettevalues');
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_viewport(start) {
|
2019-03-03 22:55:15 +00:00
|
|
|
// only viewport-fit is allowed but we're reusing MediaQuery syntax unit,
|
|
|
|
// and accept anything for the sake of simplicity since the spec isn't yet final:
|
|
|
|
// https://drafts.csswg.org/css-round-display/#extending-viewport-rule
|
|
|
|
const descriptors = this._mediaQueryList();
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'startviewport', descriptors}, start);
|
|
|
|
this._ws();
|
|
|
|
this._readDeclarations();
|
|
|
|
this.fire({type: 'endviewport', descriptors});
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_document(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
|
|
|
const functions = [];
|
2020-10-15 11:36:15 +00:00
|
|
|
const prefix = start.value.split('-')[1] || '';
|
2017-12-26 20:39:52 +00:00
|
|
|
do {
|
|
|
|
this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
const uri = stream.match(Tokens.URI);
|
|
|
|
const fn = uri ? new PropertyValuePart(uri) : this._function() || stream.LT(1);
|
|
|
|
functions.push(fn);
|
|
|
|
if (uri) this._ws();
|
2017-12-26 20:39:52 +00:00
|
|
|
} while (stream.match(Tokens.COMMA));
|
2020-11-08 08:12:42 +00:00
|
|
|
for (const fn of functions) {
|
|
|
|
if ((fn.type !== 'function' || !/^(url(-prefix)?|domain|regexp)$/i.test(fn.name)) &&
|
|
|
|
fn.type !== 'uri') {
|
|
|
|
this.fire({
|
|
|
|
type: 'error',
|
|
|
|
message: 'Expected url( or url-prefix( or domain( or regexp(, instead saw ' +
|
|
|
|
Tokens.name(fn.tokenType || fn.type) + ' ' + (fn.text || fn.value),
|
|
|
|
}, fn);
|
|
|
|
}
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
stream.mustMatch(Tokens.LBRACE);
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'startdocument', functions, prefix}, start);
|
2021-01-01 14:27:58 +00:00
|
|
|
if (this.options.topDocOnly) {
|
|
|
|
stream.readDeclValue({stopOn: '}'});
|
2022-02-03 21:43:08 +00:00
|
|
|
stream.mustMatch(Tokens.RBRACE);
|
2021-01-01 14:27:58 +00:00
|
|
|
} else {
|
2021-04-28 14:33:27 +00:00
|
|
|
/* We allow @import and such inside document sections because the final generated CSS for
|
|
|
|
* a given page may be valid e.g. if this section is the first one that matched the URL */
|
|
|
|
this._sheetGlobals();
|
2022-02-03 21:43:08 +00:00
|
|
|
this._rulesetBlock(start);
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'enddocument', functions, prefix});
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_documentMisplaced(start) {
|
2018-03-21 06:32:32 +00:00
|
|
|
this.fire({
|
2020-11-25 18:01:36 +00:00
|
|
|
type: 'error',
|
2018-03-21 06:32:32 +00:00
|
|
|
message: 'Nested @document produces broken code',
|
2020-11-25 18:01:36 +00:00
|
|
|
}, start);
|
|
|
|
this._document(start);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_combinator() {
|
2020-11-25 18:01:36 +00:00
|
|
|
const token = this._tokenStream.match(TT.combinator);
|
|
|
|
if (token) {
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
return new Combinator(token);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_property() {
|
|
|
|
const stream = this._tokenStream;
|
2020-11-25 18:01:36 +00:00
|
|
|
let token = stream.get(true);
|
2017-12-26 20:39:52 +00:00
|
|
|
let value = null;
|
|
|
|
let hack = null;
|
2020-11-25 18:01:36 +00:00
|
|
|
let start;
|
|
|
|
if (this.options.starHack && token.type === Tokens.STAR) {
|
|
|
|
hack = '*';
|
2017-12-26 20:39:52 +00:00
|
|
|
start = token;
|
2020-11-25 18:01:36 +00:00
|
|
|
token = stream.get(true);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-12-09 07:38:28 +00:00
|
|
|
if (token.type === Tokens.IDENT) {
|
2020-11-25 18:01:36 +00:00
|
|
|
let tokenValue = token.value;
|
2017-12-26 20:39:52 +00:00
|
|
|
// check for underscore hack - no error if not allowed because it's valid CSS syntax
|
2020-11-25 18:01:36 +00:00
|
|
|
if (this.options.underscoreHack && tokenValue.startsWith('_')) {
|
2017-12-26 20:39:52 +00:00
|
|
|
hack = '_';
|
2020-11-25 18:01:36 +00:00
|
|
|
tokenValue = tokenValue.slice(1);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
value = new PropertyName(tokenValue, hack, start || token);
|
|
|
|
this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
} else {
|
|
|
|
stream.unget();
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ruleset() {
|
2018-01-06 11:05:20 +00:00
|
|
|
const stream = this._tokenStream;
|
|
|
|
let braceOpened;
|
2017-12-26 20:39:52 +00:00
|
|
|
try {
|
2021-08-24 11:38:19 +00:00
|
|
|
stream.skipComment(undefined, true);
|
2020-11-25 18:01:36 +00:00
|
|
|
if (parserCache.findBlock()) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
parserCache.startBlock();
|
|
|
|
const selectors = this._selectorsGroup();
|
|
|
|
if (!selectors) {
|
|
|
|
parserCache.cancelBlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
parserCache.adjustBlockStart(selectors[0]);
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'startrule', selectors}, selectors[0]);
|
2017-12-26 20:39:52 +00:00
|
|
|
this._readDeclarations({stopAfterBrace: true});
|
2018-01-06 11:05:20 +00:00
|
|
|
braceOpened = true;
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'endrule', selectors});
|
2017-12-26 20:39:52 +00:00
|
|
|
parserCache.endBlock();
|
|
|
|
this._ws();
|
|
|
|
return true;
|
|
|
|
} catch (ex) {
|
|
|
|
parserCache.cancelBlock();
|
|
|
|
if (!(ex instanceof SyntaxError) || this.options.strict) throw ex;
|
|
|
|
this.fire(Object.assign({}, ex, {type: 'error', error: ex}));
|
|
|
|
// if there's a right brace, the rule is finished so don't do anything
|
|
|
|
// otherwise, rethrow the error because it wasn't handled properly
|
2020-10-24 10:18:17 +00:00
|
|
|
if (braceOpened && stream.advance(Tokens.RBRACE) !== Tokens.RBRACE) throw ex;
|
2017-12-26 20:39:52 +00:00
|
|
|
// If even a single selector fails to parse, the entire ruleset should be thrown away,
|
|
|
|
// so we let the parser continue with the next one
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 21:43:08 +00:00
|
|
|
/** @param {parserlib.Token} start */
|
|
|
|
_rulesetBlock(start) {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
const map = ParserRoute[start.type];
|
|
|
|
this._ws();
|
|
|
|
while (true) {
|
|
|
|
const fn = map[stream.LT(1).type];
|
|
|
|
if (fn) fn.call(this, stream.get(true));
|
|
|
|
else if (!this._ruleset()) break;
|
|
|
|
stream.skipComment();
|
|
|
|
}
|
|
|
|
stream.mustMatch(Tokens.RBRACE);
|
|
|
|
}
|
|
|
|
|
2022-06-30 10:36:40 +00:00
|
|
|
_selectorsGroup(relative) {
|
2020-11-25 18:01:36 +00:00
|
|
|
const stream = this._tokenStream;
|
2017-12-26 20:39:52 +00:00
|
|
|
const selectors = [];
|
2020-11-25 18:01:36 +00:00
|
|
|
let comma;
|
2022-06-30 10:36:40 +00:00
|
|
|
for (let sel; (sel = this._selector(!sel && relative));) {
|
2020-11-25 18:01:36 +00:00
|
|
|
selectors.push(sel);
|
|
|
|
this._ws(null, true);
|
|
|
|
comma = stream.match(Tokens.COMMA);
|
2017-12-26 20:39:52 +00:00
|
|
|
if (!comma) break;
|
2020-11-25 18:01:36 +00:00
|
|
|
this._ws(null, true);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
if (comma) stream.throwUnexpected(stream.LT(1));
|
2017-12-26 20:39:52 +00:00
|
|
|
return selectors.length ? selectors : null;
|
|
|
|
}
|
|
|
|
|
2022-06-30 10:36:40 +00:00
|
|
|
_selector(relative) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2020-11-25 18:01:36 +00:00
|
|
|
const sel = [];
|
|
|
|
let nextSel = null;
|
2017-12-26 20:39:52 +00:00
|
|
|
let combinator = null;
|
2022-06-30 10:36:40 +00:00
|
|
|
if (!relative || !TT.combinator.includes(stream.LT(1).type)) {
|
|
|
|
nextSel = this._simpleSelectorSequence();
|
|
|
|
if (!nextSel) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
sel.push(nextSel);
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
while (true) {
|
|
|
|
combinator = this._combinator();
|
|
|
|
if (combinator) {
|
2020-11-25 18:01:36 +00:00
|
|
|
sel.push(combinator);
|
|
|
|
nextSel = this._simpleSelectorSequence();
|
|
|
|
if (nextSel) {
|
|
|
|
sel.push(nextSel);
|
2017-12-26 20:39:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
stream.throwUnexpected(stream.LT(1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!this._ws(null, true)) {
|
2017-12-26 20:39:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// make a fallback whitespace combinator
|
|
|
|
const ws = new Combinator(stream._token);
|
|
|
|
// look for an explicit combinator
|
|
|
|
combinator = this._combinator();
|
|
|
|
// selector is required if there's a combinator
|
2020-11-25 18:01:36 +00:00
|
|
|
nextSel = this._simpleSelectorSequence();
|
|
|
|
if (nextSel) {
|
|
|
|
sel.push(combinator || ws);
|
|
|
|
sel.push(nextSel);
|
2017-12-26 20:39:52 +00:00
|
|
|
} else if (combinator) {
|
2020-11-25 18:01:36 +00:00
|
|
|
stream.throwUnexpected(stream.LT(1));
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
return new Selector(sel, sel[0]);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_simpleSelectorSequence() {
|
|
|
|
const stream = this._tokenStream;
|
2020-11-25 18:01:36 +00:00
|
|
|
const start = stream.LT(1);
|
2017-12-26 20:39:52 +00:00
|
|
|
const modifiers = [];
|
2020-11-25 18:01:36 +00:00
|
|
|
const seq = [];
|
|
|
|
const ns = this._namespacePrefix(start.type);
|
2017-12-26 20:39:52 +00:00
|
|
|
const elementName = this._typeSelector(ns) || this._universal(ns);
|
|
|
|
if (elementName) {
|
2020-11-25 18:01:36 +00:00
|
|
|
seq.push(elementName);
|
2017-12-26 20:39:52 +00:00
|
|
|
} else if (ns) {
|
|
|
|
stream.unget();
|
|
|
|
}
|
|
|
|
while (true) {
|
2020-11-25 18:01:36 +00:00
|
|
|
const token = stream.get(true);
|
2022-02-03 21:43:08 +00:00
|
|
|
const action = ParserRoute.simpleSelectorSequence[token.type];
|
2020-11-25 18:01:36 +00:00
|
|
|
const component = action ? action.call(this, token) : (stream.unget(), 0);
|
2017-12-26 20:39:52 +00:00
|
|
|
if (!component) break;
|
|
|
|
modifiers.push(component);
|
2020-11-25 18:01:36 +00:00
|
|
|
seq.push(component);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
const text = fastJoin(seq);
|
2017-12-26 20:39:52 +00:00
|
|
|
return text && new SelectorPart(elementName, modifiers, text, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
_typeSelector(ns) {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
const nsSupplied = ns !== undefined;
|
2020-11-25 18:01:36 +00:00
|
|
|
if (!nsSupplied) ns = this._namespacePrefix();
|
|
|
|
const name = stream.match(Tokens.IDENT) &&
|
|
|
|
new SelectorSubPart(stream._token.value, 'elementName', stream._token);
|
|
|
|
if (!name) {
|
2017-12-26 20:39:52 +00:00
|
|
|
if (!nsSupplied && ns && ns.length > 0) stream.unget();
|
|
|
|
if (!nsSupplied && ns && ns.length > 1) stream.unget();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (ns) {
|
2020-11-25 18:01:36 +00:00
|
|
|
name.text = ns + name.text;
|
|
|
|
name.col -= ns.length;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
return name;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_hash(start) {
|
|
|
|
return new SelectorSubPart(start.value, 'id', start);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_class(start) {
|
|
|
|
const name = this._tokenStream.mustMatch(Tokens.IDENT, false).value;
|
|
|
|
return new SelectorSubPart('.' + name, 'class', start);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_namespacePrefix(next) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2020-11-25 18:01:36 +00:00
|
|
|
if (!next) next = stream.LA(1);
|
|
|
|
return next === Tokens.PIPE ? '|' :
|
|
|
|
(next === Tokens.IDENT || next === Tokens.STAR) && stream.LA(2) === Tokens.PIPE
|
|
|
|
? stream.get().value + stream.get().value
|
|
|
|
: null;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_universal(ns = this._namespacePrefix()) {
|
2020-10-24 10:18:17 +00:00
|
|
|
return `${ns || ''}${this._tokenStream.match(Tokens.STAR).value || ''}` || null;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_attrib(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2020-11-25 18:01:36 +00:00
|
|
|
const value = [
|
|
|
|
start.value,
|
|
|
|
this._ws(),
|
|
|
|
this._namespacePrefix() || '',
|
|
|
|
stream.mustMatch(Tokens.IDENT, false).value,
|
|
|
|
this._ws(),
|
|
|
|
];
|
2020-10-24 10:18:17 +00:00
|
|
|
if (stream.match(TT.attrMatch)) {
|
2020-11-25 18:01:36 +00:00
|
|
|
value.push(
|
|
|
|
stream._token.value,
|
|
|
|
this._ws(),
|
|
|
|
stream.mustMatch(TT.identString).value,
|
|
|
|
this._ws());
|
|
|
|
if (stream.match(Tokens.IDENT, ['i', 's'])) {
|
|
|
|
value.push(
|
|
|
|
stream._token.value,
|
|
|
|
this._ws());
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
value.push(stream.mustMatch(Tokens.RBRACKET).value);
|
|
|
|
return new SelectorSubPart(fastJoin(value), 'attribute', start);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_pseudo(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2020-11-25 18:01:36 +00:00
|
|
|
const colons = start.value + (stream.match(Tokens.COLON).value || '');
|
|
|
|
const t = stream.mustMatch(TT.pseudo);
|
|
|
|
const pseudo = t.type === Tokens.IDENT ? t.value :
|
|
|
|
t.value +
|
|
|
|
this._ws() +
|
|
|
|
(this._expression({list: true}) || '') +
|
|
|
|
stream.mustMatch(Tokens.RPAREN).value;
|
|
|
|
return new SelectorSubPart(colons + pseudo, 'pseudo', {
|
|
|
|
line: t.line,
|
|
|
|
col: t.col - colons.length,
|
|
|
|
offset: t.offset - colons.length,
|
|
|
|
});
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 10:18:17 +00:00
|
|
|
_expression({calc, list} = {}) {
|
|
|
|
const chunks = [];
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2020-10-24 10:18:17 +00:00
|
|
|
while (stream.get()) {
|
|
|
|
const {type, value} = stream._token;
|
|
|
|
if (calc && type === Tokens.FUNCTION) {
|
2020-11-25 18:01:36 +00:00
|
|
|
if (!rxCalc.test(value)) {
|
|
|
|
stream.throwUnexpected();
|
|
|
|
}
|
2020-10-24 10:18:17 +00:00
|
|
|
chunks.push(value,
|
|
|
|
this._expr('calc').text,
|
|
|
|
stream.mustMatch(Tokens.RPAREN).value);
|
|
|
|
} else if (TT.expression.includes(type) || list && type === Tokens.COMMA) {
|
|
|
|
chunks.push(value, this._ws());
|
|
|
|
} else if (type !== Tokens.COMMENT) {
|
|
|
|
stream.unget();
|
|
|
|
break;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
return fastJoin(chunks) || null;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_is(start) {
|
|
|
|
let args;
|
2018-03-01 18:04:39 +00:00
|
|
|
const value =
|
|
|
|
start.value +
|
|
|
|
this._ws() +
|
2022-06-30 10:36:40 +00:00
|
|
|
(args = this._selectorsGroup(start.type === Tokens.HAS)) +
|
2018-03-01 18:04:39 +00:00
|
|
|
this._ws() +
|
2020-11-25 18:01:36 +00:00
|
|
|
this._tokenStream.mustMatch(Tokens.RPAREN).value;
|
|
|
|
const type = lower(Tokens.name(start.type));
|
|
|
|
return Object.assign(new SelectorSubPart(value, type, start), {args});
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_negation(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2021-01-01 14:27:58 +00:00
|
|
|
const value = [start.value, this._ws()];
|
2020-11-25 18:01:36 +00:00
|
|
|
const args = this._selectorsGroup();
|
|
|
|
if (!args) stream.throwUnexpected(stream.LT(1));
|
2021-01-01 14:27:58 +00:00
|
|
|
value.push(...args, this._ws(), stream.mustMatch(Tokens.RPAREN).value);
|
|
|
|
return Object.assign(new SelectorSubPart(fastJoin(value), 'not', start), {args});
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 09:36:36 +00:00
|
|
|
_declaration(consumeSemicolon, Props) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
|
|
|
const property = this._property();
|
|
|
|
if (!property) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stream.mustMatch(Tokens.COLON);
|
2020-11-25 18:01:36 +00:00
|
|
|
const value = property.text.startsWith('--')
|
|
|
|
? this._customProperty() // whitespace is a part of custom property value
|
|
|
|
: (this._ws(), this._expr());
|
2017-12-26 20:39:52 +00:00
|
|
|
// if there's no parts for the value, it's an error
|
|
|
|
if (!value || value.length === 0) {
|
2020-11-25 18:01:36 +00:00
|
|
|
stream.throwUnexpected(stream.LT(1));
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
let invalid;
|
|
|
|
if (!this.options.skipValidation) {
|
|
|
|
try {
|
|
|
|
/* If hacks are allowed, then only check the root property.
|
|
|
|
Otherwise treat `_property` or `*property` as invalid */
|
|
|
|
const name =
|
|
|
|
this.options.starHack && property.hack === '*' ||
|
|
|
|
this.options.underscoreHack && property.hack === '_'
|
|
|
|
? property.text
|
|
|
|
: property.toString();
|
2021-09-07 09:36:36 +00:00
|
|
|
validateProperty(name, property, value, Props);
|
2020-11-25 18:01:36 +00:00
|
|
|
} catch (ex) {
|
2020-12-09 07:38:28 +00:00
|
|
|
if (!(ex instanceof ValidationError)) {
|
|
|
|
ex.message = ex.stack;
|
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
invalid = ex;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
const event = {
|
|
|
|
type: 'property',
|
2020-11-25 18:01:36 +00:00
|
|
|
important: stream.match(Tokens.IMPORTANT),
|
2017-12-26 20:39:52 +00:00
|
|
|
property,
|
|
|
|
value,
|
|
|
|
};
|
2020-11-25 18:01:36 +00:00
|
|
|
this._ws();
|
2017-12-26 20:39:52 +00:00
|
|
|
if (invalid) {
|
|
|
|
event.invalid = invalid;
|
|
|
|
event.message = invalid.message;
|
|
|
|
}
|
|
|
|
this.fire(event, property);
|
2020-11-25 18:01:36 +00:00
|
|
|
if (consumeSemicolon) {
|
|
|
|
while (stream.match(TT.semiS)) {/*NOP*/}
|
2022-01-11 18:18:37 +00:00
|
|
|
this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
return true;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 05:13:33 +00:00
|
|
|
_expr(inFunction, endToken = Tokens.RPAREN) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
|
|
|
const values = [];
|
|
|
|
while (true) {
|
|
|
|
let value = this._term(inFunction);
|
2020-11-25 18:01:36 +00:00
|
|
|
if (!value && !values.length) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
// get everything inside the parens and let validateProperty handle that
|
2018-11-25 05:13:33 +00:00
|
|
|
if (!value && inFunction && stream.peek() !== endToken) {
|
2017-12-26 20:39:52 +00:00
|
|
|
stream.get();
|
|
|
|
value = new PropertyValuePart(stream._token);
|
|
|
|
} else if (!value) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// TODO: remove this hack
|
|
|
|
const last = values[values.length - 1];
|
|
|
|
if (last && last.offset === value.offset && last.text === value.text) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
values.push(value);
|
|
|
|
this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
const operator = this._tokenStream.match(inFunction ? TT.opInFunc : TT.op);
|
|
|
|
if (operator) {
|
|
|
|
this._ws();
|
|
|
|
values.push(new PropertyValuePart(operator));
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
return values[0] ? new PropertyValue(values, values[0]) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_customProperty() {
|
2021-01-01 14:27:58 +00:00
|
|
|
const value = this._tokenStream.readDeclValue();
|
|
|
|
if (value) {
|
|
|
|
const token = this._tokenStream._token;
|
|
|
|
token.value = value;
|
|
|
|
token.type = Tokens.IDENT;
|
|
|
|
return new PropertyValue([new PropertyValuePart(token)], token);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_term(inFunction) {
|
|
|
|
const stream = this._tokenStream;
|
2020-10-24 10:18:17 +00:00
|
|
|
const unary = stream.match(TT.plusMinus) && stream._token;
|
2017-12-26 20:39:52 +00:00
|
|
|
const finalize = (token, value) => {
|
2018-01-07 16:11:00 +00:00
|
|
|
if (!token && unary) stream.unget();
|
2017-12-26 20:39:52 +00:00
|
|
|
if (!token) return null;
|
|
|
|
if (token instanceof SyntaxUnit) return token;
|
|
|
|
if (unary) {
|
2020-11-25 18:01:36 +00:00
|
|
|
token.line = unary.line;
|
|
|
|
token.col = unary.col;
|
2017-12-26 20:39:52 +00:00
|
|
|
token.value = unary.value + (value || token.value);
|
|
|
|
} else if (value) {
|
|
|
|
token.value = value;
|
|
|
|
}
|
|
|
|
return new PropertyValuePart(token);
|
|
|
|
};
|
2021-08-26 19:03:05 +00:00
|
|
|
const next = this.options.ieFilters && stream.LT(1);
|
|
|
|
if (next && next.type === Tokens.IE_FUNCTION) {
|
|
|
|
return finalize(next, this._ieFunction());
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
// see if it's a simple block
|
2020-10-24 10:18:17 +00:00
|
|
|
if (stream.match(inFunction ? TT.LParenBracketBrace : TT.LParenBracket)) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const token = stream._token;
|
2018-11-25 05:13:33 +00:00
|
|
|
const endToken = Tokens.type(token.endChar);
|
|
|
|
token.expr = this._expr(inFunction, endToken);
|
|
|
|
stream.mustMatch(endToken);
|
2018-01-07 15:59:31 +00:00
|
|
|
return finalize(token, token.value + (token.expr || '') + token.endChar);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
return finalize(
|
|
|
|
// see if there's a simple match
|
2020-12-09 07:38:28 +00:00
|
|
|
stream.match(TT.term) && stream._token ||
|
2017-12-26 20:39:52 +00:00
|
|
|
this._hexcolor() ||
|
2018-01-06 11:05:20 +00:00
|
|
|
this._function({asText: Boolean(unary)}));
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_function({asText} = {}) {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
if (!stream.match(Tokens.FUNCTION)) return null;
|
|
|
|
const start = stream._token;
|
|
|
|
const name = start.value.slice(0, -1);
|
2018-06-12 09:51:04 +00:00
|
|
|
this._ws();
|
2018-01-07 15:59:31 +00:00
|
|
|
const expr = this._expr(lower(name));
|
2017-12-26 20:39:52 +00:00
|
|
|
const ieFilter = this.options.ieFilters && stream.peek() === Tokens.EQUALS ?
|
|
|
|
this._functionIeFilter() : '';
|
2018-01-07 15:59:31 +00:00
|
|
|
const text = name + '(' + (expr || '') + ieFilter + ')';
|
2017-12-26 20:39:52 +00:00
|
|
|
stream.mustMatch(Tokens.RPAREN);
|
|
|
|
this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
if (asText) {
|
|
|
|
return text;
|
|
|
|
}
|
2021-03-14 05:33:26 +00:00
|
|
|
const m = rxVendorPrefix.exec(name) || [];
|
2020-11-25 18:01:36 +00:00
|
|
|
return SyntaxUnit.addFuncInfo(
|
|
|
|
new SyntaxUnit(text, start, 'function', {
|
|
|
|
expr,
|
2021-03-14 05:33:26 +00:00
|
|
|
name: m[2] || name,
|
|
|
|
prefix: m[1] || '',
|
2020-11-25 18:01:36 +00:00
|
|
|
tokenType: Tokens.FUNCTION,
|
|
|
|
}));
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_functionIeFilter() {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
const text = [];
|
|
|
|
do {
|
|
|
|
if (this._ws()) {
|
|
|
|
text.push(stream._token.value);
|
|
|
|
}
|
|
|
|
// might be second time in the loop
|
|
|
|
if (stream.LA(0) === Tokens.COMMA) {
|
|
|
|
text.push(stream._token.value);
|
|
|
|
}
|
|
|
|
stream.match(Tokens.IDENT);
|
|
|
|
text.push(stream._token.value);
|
|
|
|
stream.match(Tokens.EQUALS);
|
|
|
|
text.push(stream._token.value);
|
|
|
|
let lt = stream.peek();
|
|
|
|
while (lt !== Tokens.COMMA &&
|
|
|
|
lt !== Tokens.S &&
|
|
|
|
lt !== Tokens.RPAREN &&
|
|
|
|
lt !== Tokens.EOF) {
|
|
|
|
stream.get();
|
|
|
|
text.push(stream._token.value);
|
|
|
|
lt = stream.peek();
|
|
|
|
}
|
|
|
|
} while (stream.match([Tokens.COMMA, Tokens.S]));
|
2020-11-25 18:01:36 +00:00
|
|
|
return fastJoin(text);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ieFunction() {
|
|
|
|
const stream = this._tokenStream;
|
2021-08-26 19:03:05 +00:00
|
|
|
const text = [];
|
2017-12-26 20:39:52 +00:00
|
|
|
// IE function can begin like a regular function, too
|
|
|
|
if (stream.match([Tokens.IE_FUNCTION, Tokens.FUNCTION])) {
|
2021-08-26 19:03:05 +00:00
|
|
|
text.push(stream._token.value);
|
2017-12-26 20:39:52 +00:00
|
|
|
do {
|
2021-08-26 19:03:05 +00:00
|
|
|
text.push(
|
|
|
|
stream._token.value === ',' ? ',' : '', // subsequent loops
|
|
|
|
this._ws(),
|
|
|
|
stream.match(Tokens.IDENT).value || '',
|
|
|
|
stream.match(Tokens.EQUALS).value || '');
|
|
|
|
// functionText.push(this._term());
|
|
|
|
while (!/^([,)]|\s+)$/.test(stream.LT(1).value)) {
|
|
|
|
text.push(stream.get(true).value);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
} while (stream.match([Tokens.COMMA, Tokens.S]));
|
2021-08-26 19:03:05 +00:00
|
|
|
text.push(stream.match(Tokens.RPAREN).value);
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
|
|
|
}
|
2021-08-26 19:03:05 +00:00
|
|
|
return fastJoin(text) || null;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_hexcolor() {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
if (!stream.match(Tokens.HASH)) return null;
|
|
|
|
const token = stream._token;
|
|
|
|
const color = token.value;
|
|
|
|
const len = color.length;
|
|
|
|
if (len !== 4 && len !== 5 && len !== 7 && len !== 9 ||
|
|
|
|
!/^#([a-f\d]{3}(?:[a-f\d](?:[a-f\d]{2}){0,2})?)$/i.test(color)) {
|
2020-11-25 18:01:36 +00:00
|
|
|
throw new SyntaxError(`Expected a hex color but found '${color}'.`, token);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
this._ws();
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_keyframes(start) {
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2021-08-24 11:38:19 +00:00
|
|
|
const prefix = rxVendorPrefix.test(start.value) ? RegExp.$1 : '';
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
|
|
|
const name = this._keyframeName();
|
|
|
|
stream.mustMatch(Tokens.LBRACE);
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'startkeyframes', name, prefix}, start);
|
2017-12-26 20:39:52 +00:00
|
|
|
// check for key
|
|
|
|
while (true) {
|
|
|
|
this._ws();
|
|
|
|
const tt = stream.peek();
|
|
|
|
if (tt !== Tokens.IDENT && tt !== Tokens.PERCENTAGE) break;
|
|
|
|
this._keyframeRule();
|
|
|
|
}
|
|
|
|
stream.mustMatch(Tokens.RBRACE);
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'endkeyframes', name, prefix});
|
2017-12-26 20:39:52 +00:00
|
|
|
this._ws();
|
|
|
|
}
|
|
|
|
|
|
|
|
_keyframeName() {
|
|
|
|
const stream = this._tokenStream;
|
2020-10-24 10:18:17 +00:00
|
|
|
stream.mustMatch(TT.identString);
|
2017-12-26 20:39:52 +00:00
|
|
|
return SyntaxUnit.fromToken(stream._token);
|
|
|
|
}
|
|
|
|
|
|
|
|
_keyframeRule() {
|
2020-11-25 18:01:36 +00:00
|
|
|
const keys = this._keyList();
|
|
|
|
this.fire({type: 'startkeyframerule', keys}, keys[0]);
|
2017-12-26 20:39:52 +00:00
|
|
|
this._readDeclarations();
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire({type: 'endkeyframerule', keys});
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_keyList() {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
const keyList = [];
|
|
|
|
// must be least one key
|
|
|
|
keyList.push(this._key());
|
|
|
|
this._ws();
|
|
|
|
while (stream.match(Tokens.COMMA)) {
|
|
|
|
this._ws();
|
|
|
|
keyList.push(this._key());
|
|
|
|
this._ws();
|
|
|
|
}
|
|
|
|
return keyList;
|
|
|
|
}
|
|
|
|
|
|
|
|
_key() {
|
|
|
|
const stream = this._tokenStream;
|
|
|
|
if (stream.match(Tokens.PERCENTAGE)) {
|
|
|
|
return SyntaxUnit.fromToken(stream._token);
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
|
|
|
if (stream.match(Tokens.IDENT)) {
|
|
|
|
if (/^(from|to)$/i.test(stream._token.value)) {
|
|
|
|
return SyntaxUnit.fromToken(stream._token);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
stream.unget();
|
|
|
|
}
|
|
|
|
// if it gets here, there wasn't a valid token, so time to explode
|
2020-11-25 18:01:36 +00:00
|
|
|
stream.throwUnexpected(stream.LT(1), ['%', "'from'", "'to'"]);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_skipCruft() {
|
2020-10-24 10:18:17 +00:00
|
|
|
while (this._tokenStream.match(TT.cruft)) { /*NOP*/ }
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-07 09:36:36 +00:00
|
|
|
* @param {{}} [_]
|
|
|
|
* @param {Boolean} [_.checkStart] - check for the left brace at the beginning.
|
|
|
|
* @param {Boolean} [_.readMargins] - check for margin patterns.
|
|
|
|
* @param {Boolean} [_.stopAfterBrace] - stop after the final } without consuming whitespace
|
|
|
|
* @param {{}} [_.Props] - definitions of valid properties
|
2017-12-26 20:39:52 +00:00
|
|
|
*/
|
|
|
|
_readDeclarations({
|
|
|
|
checkStart = true,
|
|
|
|
readMargins = false,
|
2020-11-18 11:17:15 +00:00
|
|
|
stopAfterBrace = false,
|
2021-09-07 09:36:36 +00:00
|
|
|
Props,
|
2017-12-26 20:39:52 +00:00
|
|
|
} = {}) {
|
|
|
|
const stream = this._tokenStream;
|
2018-01-06 11:05:20 +00:00
|
|
|
if (checkStart) stream.mustMatch(Tokens.LBRACE);
|
2021-09-07 09:36:36 +00:00
|
|
|
let next, tt;
|
|
|
|
while ((next = stream.get(true)).value !== '}' && (tt = next.type)) {
|
2020-11-25 18:01:36 +00:00
|
|
|
try {
|
|
|
|
if (tt === Tokens.SEMICOLON ||
|
2022-01-18 17:24:41 +00:00
|
|
|
this._ws(next, true) ||
|
2020-11-25 18:01:36 +00:00
|
|
|
readMargins && this._margin() ||
|
2022-01-18 17:24:41 +00:00
|
|
|
(stream.unget(), this._declaration(true, Props))) {
|
2020-11-25 18:01:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} catch (ex) {
|
|
|
|
this._readDeclarationsRecovery(ex, arguments[0]);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
2021-09-07 09:36:36 +00:00
|
|
|
if (next.value !== '}') stream.mustMatch(Tokens.RBRACE);
|
|
|
|
if (!stopAfterBrace) this._ws();
|
2020-11-25 18:01:36 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_readDeclarationsRecovery(ex) {
|
|
|
|
if (ex) {
|
|
|
|
if (this.options.strict || !(ex instanceof SyntaxError)) {
|
|
|
|
throw ex; // if not a syntax error, rethrow it
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
2020-11-25 18:01:36 +00:00
|
|
|
this.fire(Object.assign({}, ex, {
|
|
|
|
type: ex.type || 'error',
|
|
|
|
recoverable: true,
|
|
|
|
error: ex,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
switch (this._tokenStream.advance([Tokens.SEMICOLON, Tokens.RBRACE])) {
|
|
|
|
case Tokens.SEMICOLON:
|
|
|
|
return true; // continue to the next declaration
|
|
|
|
case Tokens.RBRACE:
|
|
|
|
this._tokenStream.unget();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
throw ex;
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_ws(start, skipUsoVar) {
|
2022-01-18 17:24:41 +00:00
|
|
|
const tt = start && start.type;
|
|
|
|
if (tt && !(
|
|
|
|
tt === Tokens.S ||
|
|
|
|
tt === Tokens.COMMENT ||
|
|
|
|
tt === Tokens.USO_VAR && skipUsoVar
|
|
|
|
)) {
|
|
|
|
return '';
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
const stream = this._tokenStream;
|
2020-10-24 10:18:17 +00:00
|
|
|
const tokens = skipUsoVar ? TT.usoS : Tokens.S;
|
2020-11-25 18:01:36 +00:00
|
|
|
let ws = start ? start.value : '';
|
|
|
|
for (let t; (t = stream.LT(1, true)) && t.type === Tokens.S;) {
|
|
|
|
ws += stream.get(true).value;
|
|
|
|
}
|
|
|
|
if (stream._ltIndex === stream._ltAhead) {
|
2020-12-09 07:38:28 +00:00
|
|
|
ws += stream._reader.readMatch(/\s+/y) || '';
|
2020-11-25 18:01:36 +00:00
|
|
|
if (!stream._reader.peekTest(/\/\*/y)) {
|
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
}
|
2018-01-06 11:05:20 +00:00
|
|
|
while (stream.match(tokens)) {
|
2017-12-26 20:39:52 +00:00
|
|
|
ws += stream._token.value;
|
|
|
|
}
|
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
_unknownSym(start) {
|
2018-03-01 18:26:57 +00:00
|
|
|
if (this.options.strict) {
|
2020-11-25 18:01:36 +00:00
|
|
|
throw new SyntaxError('Unknown @ rule.', start);
|
2018-03-01 18:26:57 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
const {prelude, block} = this._tokenStream.readUnknownSym();
|
|
|
|
this.fire({type: 'unknown-at-rule', name: start.value, prelude, block}, start);
|
2020-08-27 19:39:49 +00:00
|
|
|
this._ws();
|
2018-03-01 18:26:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
_verifyEnd() {
|
2020-11-25 18:01:36 +00:00
|
|
|
const stream = this._tokenStream;
|
|
|
|
if (stream.peek() !== Tokens.EOF) {
|
|
|
|
stream.throwUnexpected(stream.LT(1));
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parse(input, {reuseCache} = {}) {
|
|
|
|
this._tokenStream = new TokenStream(input);
|
|
|
|
parserCache.start(reuseCache && this);
|
|
|
|
this._stylesheet();
|
|
|
|
}
|
|
|
|
|
|
|
|
parseStyleSheet(input) {
|
|
|
|
return this.parse(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
parseMediaQuery(input, {reuseCache} = {}) {
|
|
|
|
this._tokenStream = new TokenStream(input);
|
|
|
|
parserCache.start(reuseCache && this);
|
|
|
|
const result = this._mediaQuery();
|
|
|
|
this._verifyEnd();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a property value (everything after the semicolon).
|
2018-01-06 11:05:20 +00:00
|
|
|
* @return {PropertyValue} The property value.
|
2017-12-26 20:39:52 +00:00
|
|
|
* @throws parserlib.util.SyntaxError If an unexpected token is found.
|
|
|
|
*/
|
|
|
|
parsePropertyValue(input) {
|
|
|
|
this._tokenStream = new TokenStream(input);
|
|
|
|
this._ws();
|
|
|
|
const result = this._expr();
|
|
|
|
this._ws();
|
|
|
|
this._verifyEnd();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a complete CSS rule, including selectors and
|
|
|
|
* properties.
|
|
|
|
* @param {String} input The text to parser.
|
|
|
|
* @return {Boolean} True if the parse completed successfully, false if not.
|
|
|
|
*/
|
|
|
|
parseRule(input, {reuseCache} = {}) {
|
|
|
|
this._tokenStream = new TokenStream(input);
|
|
|
|
parserCache.start(reuseCache && this);
|
|
|
|
this._ws();
|
|
|
|
const result = this._ruleset();
|
|
|
|
this._ws();
|
|
|
|
this._verifyEnd();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a single CSS selector (no comma)
|
|
|
|
* @param {String} input The text to parse as a CSS selector.
|
|
|
|
* @return {Selector} An object representing the selector.
|
|
|
|
* @throws parserlib.util.SyntaxError If an unexpected token is found.
|
|
|
|
*/
|
|
|
|
parseSelector(input) {
|
|
|
|
this._tokenStream = new TokenStream(input);
|
|
|
|
this._ws();
|
|
|
|
const result = this._selector();
|
|
|
|
this._ws();
|
|
|
|
this._verifyEnd();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses an HTML style attribute: a set of CSS declarations
|
|
|
|
* separated by semicolons.
|
|
|
|
* @param {String} input The text to parse as a style attribute
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
parseStyleAttribute(input) {
|
|
|
|
// help error recovery in _readDeclarations()
|
|
|
|
this._tokenStream = new TokenStream(input + '}');
|
|
|
|
this._readDeclarations({checkStart: false});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(Parser, TYPES);
|
|
|
|
Object.assign(Parser.prototype, TYPES);
|
|
|
|
Parser.prototype._readWhitespace = Parser.prototype._ws;
|
2018-03-01 18:26:57 +00:00
|
|
|
|
2022-02-03 21:43:08 +00:00
|
|
|
ParserRoute[Tokens.DOCUMENT_SYM] =
|
|
|
|
ParserRoute[Tokens.LAYER_SYM] =
|
|
|
|
ParserRoute[Tokens.MEDIA_SYM] =
|
|
|
|
ParserRoute[Tokens.SUPPORTS_SYM] = {
|
|
|
|
[Tokens.DOCUMENT_SYM]: Parser.prototype._documentMisplaced,
|
|
|
|
[Tokens.FONT_FACE_SYM]: Parser.prototype._fontFace,
|
2022-04-01 13:05:45 +00:00
|
|
|
[Tokens.FONT_PALETTE_VALUES_SYM]: Parser.prototype._fontPaletteValues,
|
2022-02-03 21:43:08 +00:00
|
|
|
[Tokens.KEYFRAMES_SYM]: Parser.prototype._keyframes,
|
2022-02-11 22:18:03 +00:00
|
|
|
[Tokens.LAYER_SYM]: Parser.prototype._layer,
|
2022-02-03 21:43:08 +00:00
|
|
|
[Tokens.MEDIA_SYM]: Parser.prototype._media,
|
|
|
|
[Tokens.PAGE_SYM]: Parser.prototype._page,
|
|
|
|
[Tokens.SUPPORTS_SYM]: Parser.prototype._supports,
|
|
|
|
[Tokens.UNKNOWN_SYM]: Parser.prototype._unknownSym,
|
|
|
|
[Tokens.VIEWPORT_SYM]: Parser.prototype._viewport,
|
|
|
|
};
|
|
|
|
ParserRoute.stylesheet = Object.assign({}, ParserRoute[Tokens.DOCUMENT_SYM], {
|
|
|
|
[Tokens.DOCUMENT_SYM]: Parser.prototype._document,
|
|
|
|
[Tokens.S]: Parser.prototype._ws,
|
|
|
|
});
|
|
|
|
ParserRoute.topDoc = {
|
|
|
|
[Tokens.DOCUMENT_SYM]: Parser.prototype._document,
|
|
|
|
[Tokens.UNKNOWN_SYM]: Parser.prototype._unknownSym,
|
|
|
|
[Tokens.S]: Parser.prototype._ws,
|
|
|
|
};
|
|
|
|
ParserRoute.simpleSelectorSequence = {
|
|
|
|
[Tokens.HASH]: Parser.prototype._hash,
|
|
|
|
[Tokens.DOT]: Parser.prototype._class,
|
|
|
|
[Tokens.LBRACKET]: Parser.prototype._attrib,
|
|
|
|
[Tokens.COLON]: Parser.prototype._pseudo,
|
|
|
|
[Tokens.IS]: Parser.prototype._is,
|
2022-06-30 10:36:40 +00:00
|
|
|
[Tokens.HAS]: Parser.prototype._is,
|
2022-02-03 21:43:08 +00:00
|
|
|
[Tokens.ANY]: Parser.prototype._is,
|
|
|
|
[Tokens.WHERE]: Parser.prototype._is,
|
|
|
|
[Tokens.NOT]: Parser.prototype._negation,
|
2018-03-01 18:04:39 +00:00
|
|
|
};
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
|
|
|
//#region Helper functions
|
2018-01-06 11:05:20 +00:00
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
function escapeChar(c) {
|
|
|
|
return c === '"' ? '\\' + c : `\\${c.codePointAt(0).toString(16)} `;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
function fastJoin(arr) {
|
|
|
|
return !arr.length ? '' :
|
|
|
|
arr.length === 1 ? `${arr[0]}` :
|
|
|
|
arr.length === 2 ? `${arr[0]}${arr[1]}` :
|
|
|
|
arr.join('');
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:38:28 +00:00
|
|
|
/**
|
|
|
|
* vars can span any number of grammar parts so not gonna try to guess. KISS.
|
|
|
|
* @param {PropertyValue} value
|
|
|
|
*/
|
|
|
|
function hasVarParts(value) {
|
|
|
|
return value.parts.some(p => p.isVar);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
function isPseudoElement(pseudo) {
|
|
|
|
return pseudo.startsWith('::') ||
|
|
|
|
/^:(first-(letter|line)|before|after)$/i.test(pseudo);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
function lower(text) {
|
|
|
|
if (typeof text !== 'string') text = `${text}`;
|
|
|
|
let result = lowercaseCache.get(text);
|
|
|
|
if (result) return result;
|
|
|
|
result = text.toLowerCase();
|
|
|
|
lowercaseCache.set(text, result);
|
|
|
|
return result;
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
function lowerCmp(a, b) {
|
|
|
|
return a.length === b.length && (a === b || lower(a) === lower(b));
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
/** @this {String} */
|
|
|
|
function lowerCmpThis(a) {
|
|
|
|
return a.length === this.length && (a === this || lower(a) === lower(this));
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseString(str) {
|
2020-11-25 18:01:36 +00:00
|
|
|
return str.slice(1, -1) // strip surrounding quotes
|
|
|
|
.replace(/\\(\r\n|[^\r0-9a-f]|[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?)/ig, unescapeChar);
|
2018-01-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function serializeString(value) {
|
2020-11-25 18:01:36 +00:00
|
|
|
return `"${value.replace(/["\r\n\f]/g, escapeChar)}"`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unescapeChar(m, c) {
|
|
|
|
if (c === '\n' || c === '\r\n' || c === '\r' || c === '\f') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
m = /^[0-9a-f]{1,6}/i.exec(c);
|
|
|
|
return m ? String.fromCodePoint(parseInt(m[0], 16)) : c;
|
|
|
|
}
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
//#region PUBLIC API
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2021-11-14 08:00:12 +00:00
|
|
|
/** @namespace parserlib */
|
2017-12-26 20:39:52 +00:00
|
|
|
return {
|
|
|
|
css: {
|
|
|
|
Colors,
|
|
|
|
Combinator,
|
2021-08-20 10:01:56 +00:00
|
|
|
GlobalKeywords,
|
|
|
|
Matcher,
|
|
|
|
MediaFeature,
|
|
|
|
MediaQuery,
|
2017-12-26 20:39:52 +00:00
|
|
|
Parser,
|
2021-03-14 05:33:26 +00:00
|
|
|
Properties,
|
2017-12-26 20:39:52 +00:00
|
|
|
PropertyName,
|
|
|
|
PropertyValue,
|
|
|
|
PropertyValuePart,
|
2021-09-07 09:36:36 +00:00
|
|
|
ScopedProperties,
|
2017-12-26 20:39:52 +00:00
|
|
|
Selector,
|
|
|
|
SelectorPart,
|
|
|
|
SelectorSubPart,
|
|
|
|
Specificity,
|
|
|
|
TokenStream,
|
|
|
|
Tokens,
|
|
|
|
ValidationError,
|
|
|
|
},
|
|
|
|
util: {
|
2021-03-14 05:33:26 +00:00
|
|
|
EventTarget,
|
2017-12-26 20:39:52 +00:00
|
|
|
StringReader,
|
|
|
|
SyntaxError,
|
|
|
|
SyntaxUnit,
|
|
|
|
TokenStreamBase,
|
2021-09-01 21:12:29 +00:00
|
|
|
fastJoin,
|
|
|
|
isPseudoElement,
|
|
|
|
lower,
|
2021-01-05 13:46:21 +00:00
|
|
|
rxVendorPrefix,
|
2021-03-14 05:33:26 +00:00
|
|
|
describeProp: vtExplode,
|
2017-12-26 20:39:52 +00:00
|
|
|
},
|
|
|
|
cache: parserCache,
|
|
|
|
};
|
|
|
|
|
2020-11-25 18:01:36 +00:00
|
|
|
//#endregion
|
2017-12-26 20:39:52 +00:00
|
|
|
})();
|