@moondraft/import 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +394 -0
- package/dist/adapter/convert.d.ts +144 -0
- package/dist/adapter/convert.js +2811 -0
- package/dist/adapter/figma-descriptions.d.ts +7 -0
- package/dist/adapter/figma-descriptions.js +34 -0
- package/dist/adapter/figma-grids.d.ts +12 -0
- package/dist/adapter/figma-grids.js +175 -0
- package/dist/adapter/figma-handoff.d.ts +12 -0
- package/dist/adapter/figma-handoff.js +167 -0
- package/dist/adapter/figma-hyperlinks.d.ts +13 -0
- package/dist/adapter/figma-hyperlinks.js +77 -0
- package/dist/adapter/figma.d.ts +12 -0
- package/dist/adapter/figma.js +40 -0
- package/dist/adapter/file-components.d.ts +20 -0
- package/dist/adapter/file-components.js +352 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +851 -0
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +49 -0
- package/dist/adapter/link.js +892 -0
- package/dist/adapter/payload.d.ts +31 -0
- package/dist/adapter/payload.js +78 -0
- package/dist/adapter/shapes.d.ts +31 -0
- package/dist/adapter/shapes.js +75 -0
- package/dist/adapter/variables.d.ts +45 -0
- package/dist/adapter/variables.js +106 -0
- package/dist/figma-file.d.ts +38 -0
- package/dist/figma-file.js +97 -0
- package/dist/figma-worker.d.ts +11 -0
- package/dist/figma-worker.js +12 -0
- package/dist/figma.d.ts +26 -0
- package/dist/figma.js +230 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +50 -0
- package/dist/report.d.ts +42 -0
- package/dist/report.js +142 -0
- package/dist/serialize.d.ts +7 -0
- package/dist/serialize.js +79 -0
- package/dist/svg.d.ts +33 -0
- package/dist/svg.js +44 -0
- package/dist/syntax.d.ts +8 -0
- package/dist/syntax.js +19 -0
- package/dist/taco/figma-import/clipboard.d.ts +25 -0
- package/dist/taco/figma-import/clipboard.js +87 -0
- package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
- package/dist/taco/figma-import/clipboardSize.js +107 -0
- package/dist/taco/figma-import/container/compression.d.ts +9 -0
- package/dist/taco/figma-import/container/compression.js +174 -0
- package/dist/taco/figma-import/container/error.d.ts +15 -0
- package/dist/taco/figma-import/container/error.js +19 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +41 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
- package/dist/taco/figma-import/container/limits.d.ts +22 -0
- package/dist/taco/figma-import/container/limits.js +22 -0
- package/dist/taco/figma-import/container/zip.d.ts +14 -0
- package/dist/taco/figma-import/container/zip.js +154 -0
- package/dist/taco/figma-import/figFile.d.ts +34 -0
- package/dist/taco/figma-import/figFile.js +91 -0
- package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
- package/dist/taco/figma-import/figFileDocument.js +100 -0
- package/dist/taco/figma-import/import/blobs.d.ts +10 -0
- package/dist/taco/figma-import/import/blobs.js +23 -0
- package/dist/taco/figma-import/import/carry.d.ts +24 -0
- package/dist/taco/figma-import/import/carry.js +66 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
- package/dist/taco/figma-import/import/componentProps.js +329 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +85 -0
- package/dist/taco/figma-import/import/componentSets.js +647 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
- package/dist/taco/figma-import/import/converters/effects.js +127 -0
- package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
- package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
- package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +63 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
- package/dist/taco/figma-import/import/converters/gradient.js +108 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
- package/dist/taco/figma-import/import/converters/group.js +30 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +94 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
- package/dist/taco/figma-import/import/converters/layout.js +187 -0
- package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
- package/dist/taco/figma-import/import/converters/line.js +40 -0
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
- package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
- package/dist/taco/figma-import/import/converters/section.d.ts +8 -0
- package/dist/taco/figma-import/import/converters/section.js +29 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
- package/dist/taco/figma-import/import/converters/shared.js +372 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
- package/dist/taco/figma-import/import/converters/text.js +707 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
- package/dist/taco/figma-import/import/converters/vector.js +609 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -0
- package/dist/taco/figma-import/import/fields.d.ts +15 -0
- package/dist/taco/figma-import/import/fields.js +29 -0
- package/dist/taco/figma-import/import/instances.d.ts +184 -0
- package/dist/taco/figma-import/import/instances.js +1530 -0
- package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
- package/dist/taco/figma-import/import/nestingCycles.js +488 -0
- package/dist/taco/figma-import/import/slots.d.ts +56 -0
- package/dist/taco/figma-import/import/slots.js +96 -0
- package/dist/taco/figma-import/import/styles.d.ts +48 -0
- package/dist/taco/figma-import/import/styles.js +48 -0
- package/dist/taco/figma-import/import/tree.d.ts +66 -0
- package/dist/taco/figma-import/import/tree.js +194 -0
- package/dist/taco/figma-import/import/types.d.ts +232 -0
- package/dist/taco/figma-import/import/types.js +33 -0
- package/dist/taco/figma-import/import/variables.d.ts +137 -0
- package/dist/taco/figma-import/import/variables.js +769 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +520 -0
- package/dist/taco/figma-import/import/walk.d.ts +207 -0
- package/dist/taco/figma-import/import/walk.js +1464 -0
- package/dist/taco/figma-import/index.d.ts +22 -0
- package/dist/taco/figma-import/index.js +13 -0
- package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
- package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
- package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
- package/dist/taco/figma-import/kiwi/error.js +29 -0
- package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
- package/dist/taco/figma-import/kiwi/limits.js +18 -0
- package/dist/taco/figma-import/kiwi/message.d.ts +12 -0
- package/dist/taco/figma-import/kiwi/message.js +147 -0
- package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
- package/dist/taco/figma-import/kiwi/schema.js +206 -0
- package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
- package/dist/taco/figma-import/kiwi/types.js +18 -0
- package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
- package/dist/taco/model/agent-hidden-fields.js +29 -0
- package/dist/taco/model/asset-users.d.ts +23 -0
- package/dist/taco/model/asset-users.js +128 -0
- package/dist/taco/model/auto-layout-lines.d.ts +32 -0
- package/dist/taco/model/auto-layout-lines.js +70 -0
- package/dist/taco/model/codec.d.ts +444 -0
- package/dist/taco/model/codec.js +5665 -0
- package/dist/taco/model/color.d.ts +11 -0
- package/dist/taco/model/color.js +61 -0
- package/dist/taco/model/comment-operations.d.ts +102 -0
- package/dist/taco/model/comment-operations.js +227 -0
- package/dist/taco/model/component-evaluation.d.ts +96 -0
- package/dist/taco/model/component-evaluation.js +555 -0
- package/dist/taco/model/component-expressions.d.ts +242 -0
- package/dist/taco/model/component-expressions.js +1082 -0
- package/dist/taco/model/component-properties.d.ts +111 -0
- package/dist/taco/model/component-properties.js +339 -0
- package/dist/taco/model/component-samples.d.ts +83 -0
- package/dist/taco/model/component-samples.js +290 -0
- package/dist/taco/model/component-validity.d.ts +85 -0
- package/dist/taco/model/component-validity.js +397 -0
- package/dist/taco/model/components.d.ts +155 -0
- package/dist/taco/model/components.js +505 -0
- package/dist/taco/model/connector-ends.d.ts +20 -0
- package/dist/taco/model/connector-ends.js +188 -0
- package/dist/taco/model/connector-geometry.d.ts +74 -0
- package/dist/taco/model/connector-geometry.js +293 -0
- package/dist/taco/model/corner-radii.d.ts +26 -0
- package/dist/taco/model/corner-radii.js +47 -0
- package/dist/taco/model/derivations.d.ts +81 -0
- package/dist/taco/model/derivations.js +151 -0
- package/dist/taco/model/export-settings.d.ts +53 -0
- package/dist/taco/model/export-settings.js +156 -0
- package/dist/taco/model/expressions/ast.d.ts +153 -0
- package/dist/taco/model/expressions/ast.js +6 -0
- package/dist/taco/model/expressions/cache.d.ts +23 -0
- package/dist/taco/model/expressions/cache.js +43 -0
- package/dist/taco/model/expressions/check.d.ts +72 -0
- package/dist/taco/model/expressions/check.js +517 -0
- package/dist/taco/model/expressions/completion.d.ts +61 -0
- package/dist/taco/model/expressions/completion.js +421 -0
- package/dist/taco/model/expressions/evaluate.d.ts +32 -0
- package/dist/taco/model/expressions/evaluate.js +519 -0
- package/dist/taco/model/expressions/forms.d.ts +84 -0
- package/dist/taco/model/expressions/forms.js +200 -0
- package/dist/taco/model/expressions/index.d.ts +14 -0
- package/dist/taco/model/expressions/index.js +17 -0
- package/dist/taco/model/expressions/lexer.d.ts +42 -0
- package/dist/taco/model/expressions/lexer.js +213 -0
- package/dist/taco/model/expressions/library.d.ts +33 -0
- package/dist/taco/model/expressions/library.js +117 -0
- package/dist/taco/model/expressions/names.d.ts +43 -0
- package/dist/taco/model/expressions/names.js +99 -0
- package/dist/taco/model/expressions/parser.d.ts +15 -0
- package/dist/taco/model/expressions/parser.js +438 -0
- package/dist/taco/model/expressions/rename.d.ts +34 -0
- package/dist/taco/model/expressions/rename.js +46 -0
- package/dist/taco/model/expressions/source.d.ts +15 -0
- package/dist/taco/model/expressions/source.js +161 -0
- package/dist/taco/model/expressions/types.d.ts +119 -0
- package/dist/taco/model/expressions/types.js +102 -0
- package/dist/taco/model/expressions/values.d.ts +97 -0
- package/dist/taco/model/expressions/values.js +160 -0
- package/dist/taco/model/geometry-limits.d.ts +16 -0
- package/dist/taco/model/geometry-limits.js +67 -0
- package/dist/taco/model/geometry.d.ts +47 -0
- package/dist/taco/model/geometry.js +193 -0
- package/dist/taco/model/group-bounds.d.ts +38 -0
- package/dist/taco/model/group-bounds.js +144 -0
- package/dist/taco/model/id-table.d.ts +37 -0
- package/dist/taco/model/id-table.js +80 -0
- package/dist/taco/model/index.d.ts +25 -0
- package/dist/taco/model/index.js +27 -0
- package/dist/taco/model/instance-resolver.d.ts +376 -0
- package/dist/taco/model/instance-resolver.js +1778 -0
- package/dist/taco/model/layer-syntax.d.ts +340 -0
- package/dist/taco/model/layer-syntax.js +3506 -0
- package/dist/taco/model/layout/constraints.d.ts +84 -0
- package/dist/taco/model/layout/constraints.js +445 -0
- package/dist/taco/model/layout/frame-layout.d.ts +46 -0
- package/dist/taco/model/layout/frame-layout.js +618 -0
- package/dist/taco/model/layout/node-sizing.d.ts +163 -0
- package/dist/taco/model/layout/node-sizing.js +486 -0
- package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
- package/dist/taco/model/legacy-connector-caps.js +70 -0
- package/dist/taco/model/linear-call.d.ts +48 -0
- package/dist/taco/model/linear-call.js +58 -0
- package/dist/taco/model/new-nodes.d.ts +145 -0
- package/dist/taco/model/new-nodes.js +277 -0
- package/dist/taco/model/node-builder.d.ts +449 -0
- package/dist/taco/model/node-builder.js +2294 -0
- package/dist/taco/model/node-table-watch.d.ts +36 -0
- package/dist/taco/model/node-table-watch.js +63 -0
- package/dist/taco/model/node-transform.d.ts +39 -0
- package/dist/taco/model/node-transform.js +79 -0
- package/dist/taco/model/node-users-index.d.ts +10 -0
- package/dist/taco/model/node-users-index.js +82 -0
- package/dist/taco/model/operation-references.d.ts +80 -0
- package/dist/taco/model/operation-references.js +399 -0
- package/dist/taco/model/operations.d.ts +610 -0
- package/dist/taco/model/operations.js +2882 -0
- package/dist/taco/model/overlay-table.d.ts +71 -0
- package/dist/taco/model/overlay-table.js +171 -0
- package/dist/taco/model/override-remap.d.ts +69 -0
- package/dist/taco/model/override-remap.js +230 -0
- package/dist/taco/model/parent-table.d.ts +53 -0
- package/dist/taco/model/parent-table.js +193 -0
- package/dist/taco/model/problem-collector.d.ts +27 -0
- package/dist/taco/model/problem-collector.js +101 -0
- package/dist/taco/model/sha256.d.ts +10 -0
- package/dist/taco/model/sha256.js +79 -0
- package/dist/taco/model/shape-geometry.d.ts +96 -0
- package/dist/taco/model/shape-geometry.js +368 -0
- package/dist/taco/model/shared-node-table.d.ts +97 -0
- package/dist/taco/model/shared-node-table.js +546 -0
- package/dist/taco/model/slots.d.ts +113 -0
- package/dist/taco/model/slots.js +281 -0
- package/dist/taco/model/stroke-settings.d.ts +37 -0
- package/dist/taco/model/stroke-settings.js +90 -0
- package/dist/taco/model/svg-path-import.d.ts +89 -0
- package/dist/taco/model/svg-path-import.js +219 -0
- package/dist/taco/model/text-measurement.d.ts +142 -0
- package/dist/taco/model/text-measurement.js +846 -0
- package/dist/taco/model/text-reads.d.ts +66 -0
- package/dist/taco/model/text-reads.js +128 -0
- package/dist/taco/model/text-sizer.d.ts +358 -0
- package/dist/taco/model/text-sizer.js +996 -0
- package/dist/taco/model/theme-users.d.ts +59 -0
- package/dist/taco/model/theme-users.js +312 -0
- package/dist/taco/model/thumbnail.d.ts +15 -0
- package/dist/taco/model/thumbnail.js +34 -0
- package/dist/taco/model/types.d.ts +2369 -0
- package/dist/taco/model/types.js +366 -0
- package/dist/taco/model/unknown-property.d.ts +5 -0
- package/dist/taco/model/unknown-property.js +7 -0
- package/dist/taco/model/validated-documents.d.ts +22 -0
- package/dist/taco/model/validated-documents.js +22 -0
- package/dist/taco/model/variable-modes.d.ts +61 -0
- package/dist/taco/model/variable-modes.js +438 -0
- package/dist/taco/model/variable-references.d.ts +54 -0
- package/dist/taco/model/variable-references.js +183 -0
- package/dist/taco/model/variables.d.ts +24 -0
- package/dist/taco/model/variables.js +151 -0
- package/dist/taco/model/vector-network.d.ts +34 -0
- package/dist/taco/model/vector-network.js +411 -0
- package/dist/taco/svg-import/import-svg.d.ts +36 -0
- package/dist/taco/svg-import/import-svg.js +729 -0
- package/dist/taco/svg-import/index.d.ts +3 -0
- package/dist/taco/svg-import/index.js +3 -0
- package/dist/taco/svg-import/limits/limits.d.ts +79 -0
- package/dist/taco/svg-import/limits/limits.js +160 -0
- package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
- package/dist/taco/svg-import/paint/gradients.js +429 -0
- package/dist/taco/svg-import/paint/paint.d.ts +32 -0
- package/dist/taco/svg-import/paint/paint.js +162 -0
- package/dist/taco/svg-import/paths/paths.d.ts +58 -0
- package/dist/taco/svg-import/paths/paths.js +205 -0
- package/dist/taco/svg-import/public-types.d.ts +279 -0
- package/dist/taco/svg-import/public-types.js +56 -0
- package/dist/taco/svg-import/report/report.d.ts +44 -0
- package/dist/taco/svg-import/report/report.js +79 -0
- package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
- package/dist/taco/svg-import/reuse/reuse.js +840 -0
- package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
- package/dist/taco/svg-import/shapes/shapes.js +386 -0
- package/dist/taco/svg-import/structure/effects.d.ts +26 -0
- package/dist/taco/svg-import/structure/effects.js +618 -0
- package/dist/taco/svg-import/structure/image.d.ts +24 -0
- package/dist/taco/svg-import/structure/image.js +495 -0
- package/dist/taco/svg-import/structure/walk.d.ts +85 -0
- package/dist/taco/svg-import/structure/walk.js +544 -0
- package/dist/taco/svg-import/style/color.d.ts +40 -0
- package/dist/taco/svg-import/style/color.js +275 -0
- package/dist/taco/svg-import/style/computed.d.ts +214 -0
- package/dist/taco/svg-import/style/computed.js +1534 -0
- package/dist/taco/svg-import/style/css.d.ts +127 -0
- package/dist/taco/svg-import/style/css.js +541 -0
- package/dist/taco/svg-import/style/media.d.ts +25 -0
- package/dist/taco/svg-import/style/media.js +307 -0
- package/dist/taco/svg-import/style/properties.d.ts +43 -0
- package/dist/taco/svg-import/style/properties.js +135 -0
- package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
- package/dist/taco/svg-import/style/selector-reach.js +169 -0
- package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
- package/dist/taco/svg-import/style/selector-validity.js +307 -0
- package/dist/taco/svg-import/text/text.d.ts +29 -0
- package/dist/taco/svg-import/text/text.js +545 -0
- package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
- package/dist/taco/svg-import/transform/matrix.js +229 -0
- package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
- package/dist/taco/svg-import/tree/element-data.js +155 -0
- package/dist/taco/svg-import/tree/elements.d.ts +36 -0
- package/dist/taco/svg-import/tree/elements.js +121 -0
- package/dist/taco/svg-import/units/length.d.ts +43 -0
- package/dist/taco/svg-import/units/length.js +125 -0
- package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
- package/dist/taco/svg-import/units/viewbox.js +77 -0
- package/package.json +55 -4
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The small CSS subset that SVG `<style>` elements and `style="..."`
|
|
3
|
+
* attributes use: rules whose selectors are a single type, class or id,
|
|
4
|
+
* and flat `property: value` declarations. Anything outside the subset is
|
|
5
|
+
* skipped and reported, never thrown on. No dates, randomness or globals:
|
|
6
|
+
* the same input always parses to the same output.
|
|
7
|
+
*/
|
|
8
|
+
import { type MediaEnvironment } from "./media.ts";
|
|
9
|
+
/** One `property: value` pair, the property name lowercased, the value trimmed but verbatim. */
|
|
10
|
+
export interface CssDeclaration {
|
|
11
|
+
readonly property: string;
|
|
12
|
+
readonly value: string;
|
|
13
|
+
/** Set when the value ended in an unescaped `!important`, which is stripped from `value`. */
|
|
14
|
+
readonly important?: true;
|
|
15
|
+
/**
|
|
16
|
+
* Set when a quoted string in the value is broken by a line break, which
|
|
17
|
+
* makes the declaration invalid (CSS Syntax 3, bad string): the cascade
|
|
18
|
+
* ignores and reports it, whatever the property.
|
|
19
|
+
*/
|
|
20
|
+
readonly brokenString?: true;
|
|
21
|
+
}
|
|
22
|
+
/** The supported selectors: a type, a class, or an id. Nothing else. */
|
|
23
|
+
export type CssSelector = {
|
|
24
|
+
readonly kind: "type";
|
|
25
|
+
readonly name: string;
|
|
26
|
+
} | {
|
|
27
|
+
readonly kind: "class";
|
|
28
|
+
readonly name: string;
|
|
29
|
+
} | {
|
|
30
|
+
readonly kind: "id";
|
|
31
|
+
readonly name: string;
|
|
32
|
+
};
|
|
33
|
+
/** One rule of a style sheet: a simple selector and the declarations it applies. */
|
|
34
|
+
export interface CssRule {
|
|
35
|
+
readonly selector: CssSelector;
|
|
36
|
+
readonly declarations: readonly CssDeclaration[];
|
|
37
|
+
/** Position of the rule in the source, for cascade tie-breaking. */
|
|
38
|
+
readonly order: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* What kind of problem a rejection is. Rejections of one style sheet with
|
|
42
|
+
* the same `group` share one report line with a count.
|
|
43
|
+
*/
|
|
44
|
+
export type CssRejectionCause =
|
|
45
|
+
/** A rule whose selector is outside the subset and might match an element. */
|
|
46
|
+
"selector"
|
|
47
|
+
/** A rule whose selector list is invalid CSS, so a browser drops the whole rule. */
|
|
48
|
+
| "invalid-selector"
|
|
49
|
+
/** An at-rule other than `@media`; `text` is its name, such as `@font-face`. */
|
|
50
|
+
| "at-rule"
|
|
51
|
+
/** An `@media` query the importer cannot evaluate; `text` is the query. */
|
|
52
|
+
| "media-query"
|
|
53
|
+
/** A declaration without a colon or property. */
|
|
54
|
+
| "declaration"
|
|
55
|
+
/** Broken structure: a stray `}`, a rule without a block or an empty selector list. */
|
|
56
|
+
| "syntax";
|
|
57
|
+
/** A selector list, at-rule or declaration the subset cannot apply. The caller reports these. */
|
|
58
|
+
export interface CssRejection {
|
|
59
|
+
/** The rejected source text, trimmed (a selector list, an at-rule name, a media query or a declaration). */
|
|
60
|
+
readonly text: string;
|
|
61
|
+
readonly reason: string;
|
|
62
|
+
readonly cause: CssRejectionCause;
|
|
63
|
+
}
|
|
64
|
+
/** How a style sheet is read for one SVG. Without these, every unsupported rule is rejected. */
|
|
65
|
+
export interface StyleSheetOptions {
|
|
66
|
+
/**
|
|
67
|
+
* The screen `@media` queries are evaluated for. A rule inside a query
|
|
68
|
+
* that does not apply is skipped without a rejection; without an
|
|
69
|
+
* environment every `@media` rule is rejected as an unsupported at-rule.
|
|
70
|
+
*/
|
|
71
|
+
readonly media?: MediaEnvironment;
|
|
72
|
+
/**
|
|
73
|
+
* Whether a selector surely matches no element of the SVG. A selector
|
|
74
|
+
* outside the subset that cannot match is skipped without a rejection.
|
|
75
|
+
*/
|
|
76
|
+
readonly cannotMatch?: (selector: string) => boolean;
|
|
77
|
+
}
|
|
78
|
+
/** What a `<style>` element parsed to: the applicable rules and the rejected pieces. */
|
|
79
|
+
export interface ParsedStyleSheet {
|
|
80
|
+
readonly rules: readonly CssRule[];
|
|
81
|
+
/** The rejected pieces, in source order, capped: only the first MAX_KEPT_REJECTIONS are kept. */
|
|
82
|
+
readonly rejected: readonly CssRejection[];
|
|
83
|
+
/** How many rejected pieces past the kept ones there were. */
|
|
84
|
+
readonly rejectedOverflow: number;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Parses the content of an SVG `<style>` element. Only rules whose every
|
|
88
|
+
* selector is a single type, class or id selector become rules: a selector
|
|
89
|
+
* list like `rect, .icon` splits into one rule per selector when all of
|
|
90
|
+
* them are simple. Anything else — combinators, pseudo classes or
|
|
91
|
+
* elements, attribute selectors, the universal selector, at-rules — goes
|
|
92
|
+
* to `rejected` with its source text, except what `options` shows cannot
|
|
93
|
+
* apply: a selector that matches no element and an `@media` block whose
|
|
94
|
+
* query is false for the import's screen are skipped silently, and the
|
|
95
|
+
* rules of an `@media` block whose query is true are read as if they stood
|
|
96
|
+
* outside it. CSS comments are stripped first.
|
|
97
|
+
* A trailing unescaped `!important` on a value is stripped (any case) and
|
|
98
|
+
* kept as the declaration's `important` flag.
|
|
99
|
+
* `charge` is called with the cost of each declaration — one plus its
|
|
100
|
+
* value's length — as it is parsed, before it joins its rule's list, so the
|
|
101
|
+
* caller's cap can refuse a huge sheet instead of building a huge list.
|
|
102
|
+
*/
|
|
103
|
+
export declare function parseStyleSheet(css: string, options?: StyleSheetOptions, charge?: (units: number) => void): ParsedStyleSheet;
|
|
104
|
+
/**
|
|
105
|
+
* Parses a `style="..."` attribute into declarations. A declaration
|
|
106
|
+
* without a colon or with an empty property is returned in `rejected`
|
|
107
|
+
* instead of being applied. CSS comments are stripped first, and a
|
|
108
|
+
* trailing unescaped `!important` on a value is stripped (any case) and
|
|
109
|
+
* kept as the declaration's `important` flag.
|
|
110
|
+
* `charge` is called with the cost of each declaration — one plus its
|
|
111
|
+
* value's length — as it is parsed, before it joins the list, so the
|
|
112
|
+
* caller's cap can refuse a huge attribute instead of building a huge list.
|
|
113
|
+
*/
|
|
114
|
+
export declare function parseInlineStyle(style: string, charge?: (units: number) => void): {
|
|
115
|
+
readonly declarations: readonly CssDeclaration[];
|
|
116
|
+
readonly rejected: readonly CssRejection[];
|
|
117
|
+
readonly rejectedOverflow: number;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* The report lines of one style sheet's rejections: rejections with the
|
|
121
|
+
* same cause (and, for at-rules, the same name) share one line that says
|
|
122
|
+
* how many there are and quotes the first, in the order first met.
|
|
123
|
+
* `overflow` is how many rejections past the kept ones were only counted;
|
|
124
|
+
* their count joins the last kept rejection's group, so the line still says
|
|
125
|
+
* how many there were.
|
|
126
|
+
*/
|
|
127
|
+
export declare function describeStyleRejections(rejected: readonly CssRejection[], overflow?: number): string[];
|
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The small CSS subset that SVG `<style>` elements and `style="..."`
|
|
3
|
+
* attributes use: rules whose selectors are a single type, class or id,
|
|
4
|
+
* and flat `property: value` declarations. Anything outside the subset is
|
|
5
|
+
* skipped and reported, never thrown on. No dates, randomness or globals:
|
|
6
|
+
* the same input always parses to the same output.
|
|
7
|
+
*/
|
|
8
|
+
import { evaluateMediaQueryList } from "./media.js";
|
|
9
|
+
import { isValidSelector } from "./selector-validity.js";
|
|
10
|
+
/**
|
|
11
|
+
* Most rejections kept whole per style sheet or style attribute; past the
|
|
12
|
+
* cap only the count grows. The report collapses duplicates into one grouped
|
|
13
|
+
* line, so a hundred samples are plenty — and a sheet of one stray `}` per
|
|
14
|
+
* byte cannot grow memory without end.
|
|
15
|
+
*/
|
|
16
|
+
const MAX_KEPT_REJECTIONS = 100;
|
|
17
|
+
/** The rejections of one style sheet or style attribute: the first few kept whole, the rest only counted. */
|
|
18
|
+
class RejectionSink {
|
|
19
|
+
kept = [];
|
|
20
|
+
overflow = 0;
|
|
21
|
+
push(rejection) {
|
|
22
|
+
if (this.kept.length < MAX_KEPT_REJECTIONS)
|
|
23
|
+
this.kept.push(rejection);
|
|
24
|
+
else
|
|
25
|
+
this.overflow += 1;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Parses the content of an SVG `<style>` element. Only rules whose every
|
|
30
|
+
* selector is a single type, class or id selector become rules: a selector
|
|
31
|
+
* list like `rect, .icon` splits into one rule per selector when all of
|
|
32
|
+
* them are simple. Anything else — combinators, pseudo classes or
|
|
33
|
+
* elements, attribute selectors, the universal selector, at-rules — goes
|
|
34
|
+
* to `rejected` with its source text, except what `options` shows cannot
|
|
35
|
+
* apply: a selector that matches no element and an `@media` block whose
|
|
36
|
+
* query is false for the import's screen are skipped silently, and the
|
|
37
|
+
* rules of an `@media` block whose query is true are read as if they stood
|
|
38
|
+
* outside it. CSS comments are stripped first.
|
|
39
|
+
* A trailing unescaped `!important` on a value is stripped (any case) and
|
|
40
|
+
* kept as the declaration's `important` flag.
|
|
41
|
+
* `charge` is called with the cost of each declaration — one plus its
|
|
42
|
+
* value's length — as it is parsed, before it joins its rule's list, so the
|
|
43
|
+
* caller's cap can refuse a huge sheet instead of building a huge list.
|
|
44
|
+
*/
|
|
45
|
+
export function parseStyleSheet(css, options = {}, charge) {
|
|
46
|
+
const rules = [];
|
|
47
|
+
const rejections = new RejectionSink();
|
|
48
|
+
parseRules(stripComments(normalizeLineBreaks(css)), options, rules, rejections, 0, charge);
|
|
49
|
+
return { rules, rejected: rejections.kept, rejectedOverflow: rejections.overflow };
|
|
50
|
+
}
|
|
51
|
+
/** Maximum `@media` nesting read; deeper blocks are rejected so a hostile sheet cannot recurse without end. */
|
|
52
|
+
const MAX_MEDIA_DEPTH = 8;
|
|
53
|
+
/** Parses a list of rules (a whole sheet or the block of an applying `@media`) into `rules` and `rejected`. */
|
|
54
|
+
function parseRules(source, options, rules, rejected, depth, charge) {
|
|
55
|
+
let pos = 0;
|
|
56
|
+
while (pos < source.length) {
|
|
57
|
+
while (pos < source.length && isWhitespace(source.charAt(pos)))
|
|
58
|
+
pos += 1;
|
|
59
|
+
if (pos >= source.length)
|
|
60
|
+
break;
|
|
61
|
+
const first = source.charAt(pos);
|
|
62
|
+
if (first === "}") {
|
|
63
|
+
rejected.push({ text: "}", reason: "unexpected `}` outside a rule", cause: "syntax" });
|
|
64
|
+
pos += 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (first === "@") {
|
|
68
|
+
pos = readAtRule(source, pos, options, rules, rejected, depth, charge);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const open = findOpenBrace(source, pos);
|
|
72
|
+
if (open === -1) {
|
|
73
|
+
rejected.push({ text: source.slice(pos).trim(), reason: "unterminated rule (no `{` block)", cause: "syntax" });
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
const close = findCloseBrace(source, open);
|
|
77
|
+
if (close === -1) {
|
|
78
|
+
rejected.push({ text: source.slice(pos).trim(), reason: "unterminated rule (no closing `}`)", cause: "syntax" });
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
const selectorText = source.slice(pos, open).trim();
|
|
82
|
+
const body = source.slice(open + 1, close);
|
|
83
|
+
pos = close + 1;
|
|
84
|
+
const selectors = [];
|
|
85
|
+
let problem;
|
|
86
|
+
let skipped = 0;
|
|
87
|
+
if (selectorText.length === 0) {
|
|
88
|
+
problem = { reason: "empty selector list", cause: "syntax" };
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
for (const part of splitTopLevel(selectorText, ",")) {
|
|
92
|
+
const candidate = part.trim();
|
|
93
|
+
const selector = parseSelector(candidate);
|
|
94
|
+
if (selector !== undefined) {
|
|
95
|
+
selectors.push(selector);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (candidate.length === 0) {
|
|
99
|
+
problem = { reason: "empty selector in selector list", cause: "syntax" };
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
// An invalid branch makes the whole selector list invalid, so a
|
|
103
|
+
// browser drops the rule (Selectors 4); the importer does the same.
|
|
104
|
+
if (!isValidSelector(candidate)) {
|
|
105
|
+
problem = { reason: `invalid selector \`${candidate}\``, cause: "invalid-selector" };
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
// A branch that can never match an element of this SVG is dropped:
|
|
109
|
+
// the rule loses nothing visible by leaving it out.
|
|
110
|
+
if (options.cannotMatch?.(candidate) === true) {
|
|
111
|
+
skipped += 1;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
problem = { reason: `unsupported selector \`${candidate}\``, cause: "selector" };
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (problem !== undefined) {
|
|
119
|
+
rejected.push({ text: selectorText, ...problem });
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (selectors.length === 0 && skipped > 0)
|
|
123
|
+
continue;
|
|
124
|
+
const declarations = parseDeclarations(body, rejected, charge);
|
|
125
|
+
for (const selector of selectors) {
|
|
126
|
+
rules.push({ selector, declarations, order: rules.length });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Parses a `style="..."` attribute into declarations. A declaration
|
|
132
|
+
* without a colon or with an empty property is returned in `rejected`
|
|
133
|
+
* instead of being applied. CSS comments are stripped first, and a
|
|
134
|
+
* trailing unescaped `!important` on a value is stripped (any case) and
|
|
135
|
+
* kept as the declaration's `important` flag.
|
|
136
|
+
* `charge` is called with the cost of each declaration — one plus its
|
|
137
|
+
* value's length — as it is parsed, before it joins the list, so the
|
|
138
|
+
* caller's cap can refuse a huge attribute instead of building a huge list.
|
|
139
|
+
*/
|
|
140
|
+
export function parseInlineStyle(style, charge) {
|
|
141
|
+
const rejections = new RejectionSink();
|
|
142
|
+
const declarations = parseDeclarations(stripComments(normalizeLineBreaks(style)), rejections, charge);
|
|
143
|
+
return { declarations, rejected: rejections.kept, rejectedOverflow: rejections.overflow };
|
|
144
|
+
}
|
|
145
|
+
/** A name: a letter or `_`, then word characters or `-`. Matching against elements is the caller's job. */
|
|
146
|
+
const NAME_PATTERN = /^[a-zA-Z_][\w-]*$/;
|
|
147
|
+
/** CSS reads `\r\n`, `\r` and `\f` as one `\n` before anything else (CSS Syntax 3, preprocessing). */
|
|
148
|
+
function normalizeLineBreaks(source) {
|
|
149
|
+
return source.replace(/\r\n?|\f/g, "\n");
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Whether a quoted string in `text` is broken by a line break: an unescaped
|
|
153
|
+
* `\n` inside quotes ends the string as a bad string (CSS Syntax 3, consume
|
|
154
|
+
* a string token). A backslash before the line break continues the string.
|
|
155
|
+
*/
|
|
156
|
+
function hasBrokenString(text) {
|
|
157
|
+
let quote = "";
|
|
158
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
159
|
+
const ch = text.charAt(i);
|
|
160
|
+
if (quote !== "") {
|
|
161
|
+
if (ch === "\\")
|
|
162
|
+
i += 1;
|
|
163
|
+
else if (ch === "\n")
|
|
164
|
+
return true;
|
|
165
|
+
else if (ch === quote)
|
|
166
|
+
quote = "";
|
|
167
|
+
}
|
|
168
|
+
else if (ch === '"' || ch === "'") {
|
|
169
|
+
quote = ch;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
function isWhitespace(ch) {
|
|
175
|
+
return ch === " " || ch === "\t" || ch === "\n" || ch === "\r" || ch === "\f";
|
|
176
|
+
}
|
|
177
|
+
function isNameChar(ch) {
|
|
178
|
+
return /[\w-]/.test(ch);
|
|
179
|
+
}
|
|
180
|
+
function asciiLower(text) {
|
|
181
|
+
return text.replace(/[A-Z]/g, (ch) => ch.toLowerCase());
|
|
182
|
+
}
|
|
183
|
+
/** Parses one trimmed selector candidate, or `undefined` when it is not a single type, class or id. */
|
|
184
|
+
function parseSelector(text) {
|
|
185
|
+
if (text.length === 0)
|
|
186
|
+
return undefined;
|
|
187
|
+
const first = text.charAt(0);
|
|
188
|
+
if (first === "." || first === "#") {
|
|
189
|
+
const name = text.slice(1);
|
|
190
|
+
if (!NAME_PATTERN.test(name))
|
|
191
|
+
return undefined;
|
|
192
|
+
return first === "." ? { kind: "class", name } : { kind: "id", name };
|
|
193
|
+
}
|
|
194
|
+
return NAME_PATTERN.test(text) ? { kind: "type", name: text } : undefined;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Removes block comments, each replaced by one space so tokens on either
|
|
198
|
+
* side stay separate. An unterminated comment swallows the rest of the
|
|
199
|
+
* input, which then simply parses as less source. Comment markers inside a
|
|
200
|
+
* quoted string are ordinary characters (CSS Syntax 3 reads comments only
|
|
201
|
+
* between tokens), so the scan skips strings: a backslash keeps the next
|
|
202
|
+
* character, and a line break ends the string as CSS ends a bad string.
|
|
203
|
+
*/
|
|
204
|
+
function stripComments(source) {
|
|
205
|
+
let out = "";
|
|
206
|
+
let pos = 0;
|
|
207
|
+
let quote = "";
|
|
208
|
+
for (let i = 0; i < source.length; i += 1) {
|
|
209
|
+
const ch = source.charAt(i);
|
|
210
|
+
if (quote !== "") {
|
|
211
|
+
if (ch === "\\")
|
|
212
|
+
i += 1;
|
|
213
|
+
else if (ch === quote || ch === "\n")
|
|
214
|
+
quote = "";
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (ch === '"' || ch === "'") {
|
|
218
|
+
quote = ch;
|
|
219
|
+
}
|
|
220
|
+
else if (ch === "/" && source.charAt(i + 1) === "*") {
|
|
221
|
+
out += source.slice(pos, i) + " ";
|
|
222
|
+
const end = source.indexOf("*/", i + 2);
|
|
223
|
+
if (end === -1)
|
|
224
|
+
return out;
|
|
225
|
+
pos = end + 2;
|
|
226
|
+
i = end + 1;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return out + source.slice(pos);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Calls `fn` with each part of `text` split on `separator` occurrences that
|
|
233
|
+
* sit outside parentheses, brackets and quoted strings, so commas and
|
|
234
|
+
* semicolons inside `url(...)` or string values do not split. An escaped
|
|
235
|
+
* character is content, never a separator: a backslash keeps the next
|
|
236
|
+
* character (CSS Syntax 3); the white space a hex escape may swallow after
|
|
237
|
+
* its digits is never a separator, so one character is enough.
|
|
238
|
+
* The parts are handed over one at a time, never as one list, so a caller
|
|
239
|
+
* that charges each part against a cap can stop (by throwing) before the
|
|
240
|
+
* whole input is materialized.
|
|
241
|
+
*/
|
|
242
|
+
function eachTopLevel(text, separator, fn) {
|
|
243
|
+
let depth = 0;
|
|
244
|
+
let quote = "";
|
|
245
|
+
let start = 0;
|
|
246
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
247
|
+
const ch = text.charAt(i);
|
|
248
|
+
if (quote !== "") {
|
|
249
|
+
if (ch === "\\")
|
|
250
|
+
i += 1;
|
|
251
|
+
else if (ch === quote || ch === "\n")
|
|
252
|
+
quote = "";
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (ch === '"' || ch === "'")
|
|
256
|
+
quote = ch;
|
|
257
|
+
else if (ch === "\\")
|
|
258
|
+
i += 1;
|
|
259
|
+
else if (ch === "(" || ch === "[")
|
|
260
|
+
depth += 1;
|
|
261
|
+
else if (ch === ")" || ch === "]")
|
|
262
|
+
depth = depth > 0 ? depth - 1 : 0;
|
|
263
|
+
else if (ch === separator && depth === 0) {
|
|
264
|
+
fn(text.slice(start, i));
|
|
265
|
+
start = i + 1;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
fn(text.slice(start));
|
|
269
|
+
}
|
|
270
|
+
/** The parts of `text` split on `separator`, as `eachTopLevel` hands them over. */
|
|
271
|
+
function splitTopLevel(text, separator) {
|
|
272
|
+
const parts = [];
|
|
273
|
+
eachTopLevel(text, separator, (part) => parts.push(part));
|
|
274
|
+
return parts;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Finds the next `{` outside parentheses, brackets and strings, or -1. An
|
|
278
|
+
* escaped character is content, never a delimiter: a backslash keeps the
|
|
279
|
+
* next character (CSS Syntax 3); the white space a hex escape may swallow
|
|
280
|
+
* after its digits is never a brace, so one character is enough.
|
|
281
|
+
*/
|
|
282
|
+
function findOpenBrace(source, from) {
|
|
283
|
+
let depth = 0;
|
|
284
|
+
let quote = "";
|
|
285
|
+
for (let i = from; i < source.length; i += 1) {
|
|
286
|
+
const ch = source.charAt(i);
|
|
287
|
+
if (quote !== "") {
|
|
288
|
+
if (ch === "\\")
|
|
289
|
+
i += 1;
|
|
290
|
+
else if (ch === quote || ch === "\n")
|
|
291
|
+
quote = "";
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
if (ch === '"' || ch === "'")
|
|
295
|
+
quote = ch;
|
|
296
|
+
else if (ch === "\\")
|
|
297
|
+
i += 1;
|
|
298
|
+
else if (ch === "(" || ch === "[")
|
|
299
|
+
depth += 1;
|
|
300
|
+
else if (ch === ")" || ch === "]")
|
|
301
|
+
depth = depth > 0 ? depth - 1 : 0;
|
|
302
|
+
else if (ch === "{" && depth === 0)
|
|
303
|
+
return i;
|
|
304
|
+
}
|
|
305
|
+
return -1;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Finds the `}` balancing the `{` at `open`, ignoring braces inside strings
|
|
309
|
+
* or escaped, or -1.
|
|
310
|
+
*/
|
|
311
|
+
function findCloseBrace(source, open) {
|
|
312
|
+
let depth = 0;
|
|
313
|
+
let quote = "";
|
|
314
|
+
for (let i = open; i < source.length; i += 1) {
|
|
315
|
+
const ch = source.charAt(i);
|
|
316
|
+
if (quote !== "") {
|
|
317
|
+
if (ch === "\\")
|
|
318
|
+
i += 1;
|
|
319
|
+
else if (ch === quote || ch === "\n")
|
|
320
|
+
quote = "";
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
if (ch === '"' || ch === "'")
|
|
324
|
+
quote = ch;
|
|
325
|
+
else if (ch === "\\")
|
|
326
|
+
i += 1;
|
|
327
|
+
else if (ch === "{")
|
|
328
|
+
depth += 1;
|
|
329
|
+
else if (ch === "}") {
|
|
330
|
+
depth -= 1;
|
|
331
|
+
if (depth === 0)
|
|
332
|
+
return i;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return -1;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Reads the at-rule starting at `at` (the position of `@`) and returns the
|
|
339
|
+
* position after it. An `@media` block whose query applies to
|
|
340
|
+
* `options.media` is parsed as rules; one whose query does not apply is
|
|
341
|
+
* skipped silently; one whose query cannot be evaluated is rejected with
|
|
342
|
+
* the query. Every other at-rule is skipped with one rejection naming it.
|
|
343
|
+
* An at-rule ends at the next top-level `;` or at its balanced block; a
|
|
344
|
+
* semicolon or brace inside a quoted string (a backslash escapes the next
|
|
345
|
+
* character, a line break ends the string) or inside parentheses or
|
|
346
|
+
* brackets is part of the prelude, not the end, and so is an escaped
|
|
347
|
+
* character outside a string. An unterminated one ends
|
|
348
|
+
* parsing, so this returns the source end.
|
|
349
|
+
*/
|
|
350
|
+
function readAtRule(source, at, options, rules, rejected, depth, charge) {
|
|
351
|
+
let pos = at + 1;
|
|
352
|
+
while (pos < source.length && isNameChar(source.charAt(pos)))
|
|
353
|
+
pos += 1;
|
|
354
|
+
const name = source.slice(at, pos);
|
|
355
|
+
const unsupported = { text: name, reason: `unsupported at-rule \`${name}\``, cause: "at-rule" };
|
|
356
|
+
let quote = "";
|
|
357
|
+
let bracketDepth = 0;
|
|
358
|
+
for (let i = pos; i < source.length; i += 1) {
|
|
359
|
+
const ch = source.charAt(i);
|
|
360
|
+
if (quote !== "") {
|
|
361
|
+
if (ch === "\\")
|
|
362
|
+
i += 1;
|
|
363
|
+
else if (ch === quote || ch === "\n")
|
|
364
|
+
quote = "";
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if (ch === '"' || ch === "'")
|
|
368
|
+
quote = ch;
|
|
369
|
+
else if (ch === "\\")
|
|
370
|
+
i += 1;
|
|
371
|
+
else if (ch === "(" || ch === "[")
|
|
372
|
+
bracketDepth += 1;
|
|
373
|
+
else if (ch === ")" || ch === "]")
|
|
374
|
+
bracketDepth = bracketDepth > 0 ? bracketDepth - 1 : 0;
|
|
375
|
+
else if (ch === ";" && bracketDepth === 0) {
|
|
376
|
+
rejected.push(unsupported);
|
|
377
|
+
return i + 1;
|
|
378
|
+
}
|
|
379
|
+
else if (ch === "{" && bracketDepth === 0) {
|
|
380
|
+
const close = findCloseBrace(source, i);
|
|
381
|
+
if (close === -1)
|
|
382
|
+
break;
|
|
383
|
+
if (asciiLower(name) !== "@media" || options.media === undefined || depth >= MAX_MEDIA_DEPTH) {
|
|
384
|
+
rejected.push(unsupported);
|
|
385
|
+
return close + 1;
|
|
386
|
+
}
|
|
387
|
+
const query = source.slice(pos, i).trim();
|
|
388
|
+
const applies = evaluateMediaQueryList(query, options.media);
|
|
389
|
+
if (applies === null) {
|
|
390
|
+
rejected.push({ text: query, reason: `\`@media ${query}\` cannot be evaluated`, cause: "media-query" });
|
|
391
|
+
}
|
|
392
|
+
else if (applies) {
|
|
393
|
+
parseRules(source.slice(i + 1, close), options, rules, rejected, depth + 1, charge);
|
|
394
|
+
}
|
|
395
|
+
return close + 1;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
rejected.push({ text: name, reason: `unterminated at-rule \`${name}\``, cause: "syntax" });
|
|
399
|
+
return source.length;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* The position of the first `:` outside a quoted string and unescaped, or
|
|
403
|
+
* -1: an escaped colon is content, never the property/value delimiter (CSS
|
|
404
|
+
* Syntax 3, consume an escaped code point).
|
|
405
|
+
*/
|
|
406
|
+
function findColon(text) {
|
|
407
|
+
let quote = "";
|
|
408
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
409
|
+
const ch = text.charAt(i);
|
|
410
|
+
if (quote !== "") {
|
|
411
|
+
if (ch === "\\")
|
|
412
|
+
i += 1;
|
|
413
|
+
else if (ch === quote || ch === "\n")
|
|
414
|
+
quote = "";
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
if (ch === '"' || ch === "'")
|
|
418
|
+
quote = ch;
|
|
419
|
+
else if (ch === "\\")
|
|
420
|
+
i += 1;
|
|
421
|
+
else if (ch === ":")
|
|
422
|
+
return i;
|
|
423
|
+
}
|
|
424
|
+
return -1;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Where a value's trailing `!important` starts — the position of its `!` —
|
|
428
|
+
* or -1 when the value does not end in one. The `!` must be a delimiter
|
|
429
|
+
* token of its own: one inside a quoted string is text, and `\!` is an
|
|
430
|
+
* escaped bang, part of an identifier — `Foo\!important` is the family
|
|
431
|
+
* `Foo!important` with no priority (probed in Chrome for Testing). White
|
|
432
|
+
* space between `!` and `important` is allowed, as between any two tokens,
|
|
433
|
+
* and the word matches in any letter case.
|
|
434
|
+
*/
|
|
435
|
+
function findImportantBang(value) {
|
|
436
|
+
let quote = "";
|
|
437
|
+
let bang = -1;
|
|
438
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
439
|
+
const ch = value.charAt(i);
|
|
440
|
+
if (quote !== "") {
|
|
441
|
+
if (ch === "\\")
|
|
442
|
+
i += 1;
|
|
443
|
+
else if (ch === quote || ch === "\n")
|
|
444
|
+
quote = "";
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
if (ch === '"' || ch === "'")
|
|
448
|
+
quote = ch;
|
|
449
|
+
else if (ch === "\\")
|
|
450
|
+
i += 1;
|
|
451
|
+
else if (ch === "!")
|
|
452
|
+
bang = i;
|
|
453
|
+
}
|
|
454
|
+
if (bang === -1)
|
|
455
|
+
return -1;
|
|
456
|
+
return asciiLower(value.slice(bang + 1).trim()) === "important" ? bang : -1;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Parses a declaration block (or a `style="..."` value): pieces split on
|
|
460
|
+
* top-level `;`, each `property : value` on its first `:`, with escaped
|
|
461
|
+
* characters read as content, never as either delimiter. Bad pieces go
|
|
462
|
+
* to `rejected`; the rest come back in source order, duplicates kept.
|
|
463
|
+
* The pieces are read one at a time and each parsed declaration is charged
|
|
464
|
+
* to `charge` — one plus its value's length — before it joins the list, so
|
|
465
|
+
* a cap that throws here refuses the block before the list grows without
|
|
466
|
+
* end (a 10 MB attribute holds millions of declarations).
|
|
467
|
+
*/
|
|
468
|
+
function parseDeclarations(body, rejected, charge) {
|
|
469
|
+
const declarations = [];
|
|
470
|
+
eachTopLevel(body, ";", (piece) => {
|
|
471
|
+
const text = piece.trim();
|
|
472
|
+
if (text.length === 0)
|
|
473
|
+
return;
|
|
474
|
+
const colon = findColon(text);
|
|
475
|
+
if (colon === -1) {
|
|
476
|
+
rejected.push({ text, reason: `declaration \`${text}\` has no colon`, cause: "declaration" });
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const property = asciiLower(text.slice(0, colon).trim());
|
|
480
|
+
if (property.length === 0) {
|
|
481
|
+
rejected.push({ text, reason: `declaration \`${text}\` has an empty property`, cause: "declaration" });
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const value = text.slice(colon + 1).trim();
|
|
485
|
+
const bang = findImportantBang(value);
|
|
486
|
+
const declaration = bang === -1
|
|
487
|
+
? { property, value }
|
|
488
|
+
: { property, value: value.slice(0, bang).trim(), important: true };
|
|
489
|
+
charge?.(1 + declaration.value.length);
|
|
490
|
+
// The untrimmed piece: a line break that ends the declaration still breaks its string.
|
|
491
|
+
declarations.push(hasBrokenString(piece) ? { ...declaration, brokenString: true } : declaration);
|
|
492
|
+
});
|
|
493
|
+
return declarations;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* The report lines of one style sheet's rejections: rejections with the
|
|
497
|
+
* same cause (and, for at-rules, the same name) share one line that says
|
|
498
|
+
* how many there are and quotes the first, in the order first met.
|
|
499
|
+
* `overflow` is how many rejections past the kept ones were only counted;
|
|
500
|
+
* their count joins the last kept rejection's group, so the line still says
|
|
501
|
+
* how many there were.
|
|
502
|
+
*/
|
|
503
|
+
export function describeStyleRejections(rejected, overflow = 0) {
|
|
504
|
+
const keyOf = (rejection) => rejection.cause === "at-rule" ? `at-rule ${asciiLower(rejection.text)}` : rejection.cause;
|
|
505
|
+
const groups = new Map();
|
|
506
|
+
for (const rejection of rejected) {
|
|
507
|
+
const key = keyOf(rejection);
|
|
508
|
+
const group = groups.get(key);
|
|
509
|
+
if (group === undefined)
|
|
510
|
+
groups.set(key, [rejection]);
|
|
511
|
+
else
|
|
512
|
+
group.push(rejection);
|
|
513
|
+
}
|
|
514
|
+
const lastKey = rejected.length === 0 ? undefined : keyOf(rejected[rejected.length - 1]);
|
|
515
|
+
return [...groups.entries()].map(([key, group]) => describeGroup(group[0], group.length + (key === lastKey ? overflow : 0)));
|
|
516
|
+
}
|
|
517
|
+
function describeGroup(first, count) {
|
|
518
|
+
const more = count === 1 ? "" : ` and ${String(count - 1)} more`;
|
|
519
|
+
switch (first.cause) {
|
|
520
|
+
case "selector":
|
|
521
|
+
return count === 1
|
|
522
|
+
? `a style rule uses an unsupported selector: ${first.text}`
|
|
523
|
+
: `${String(count)} style rules use unsupported selectors: ${first.text}${more}`;
|
|
524
|
+
case "invalid-selector":
|
|
525
|
+
return count === 1
|
|
526
|
+
? `a style rule's selector list is invalid, so the rule is dropped: ${first.text}`
|
|
527
|
+
: `${String(count)} style rules have invalid selector lists, so they are dropped: ${first.text}${more}`;
|
|
528
|
+
case "at-rule":
|
|
529
|
+
return count === 1
|
|
530
|
+
? `unsupported at-rule ${first.text}`
|
|
531
|
+
: `${String(count)} unsupported ${first.text} at-rules`;
|
|
532
|
+
case "media-query":
|
|
533
|
+
return count === 1
|
|
534
|
+
? `an @media query cannot be evaluated: ${first.text}`
|
|
535
|
+
: `${String(count)} @media queries cannot be evaluated: ${first.text}${more}`;
|
|
536
|
+
case "declaration":
|
|
537
|
+
return count === 1 ? first.reason : `${String(count)} declarations cannot be read: ${first.reason}${more}`;
|
|
538
|
+
case "syntax":
|
|
539
|
+
return count === 1 ? first.reason : `${String(count)} parts of the style sheet cannot be read: ${first.reason}${more}`;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evaluates the query of an `@media` rule for the one screen an import
|
|
3
|
+
* draws on: a light-scheme screen the size of the SVG's root, with no
|
|
4
|
+
* reduced motion, contrast or forced colours asked for. That is the look
|
|
5
|
+
* the import keeps (an import takes the light look, as Figma does).
|
|
6
|
+
*
|
|
7
|
+
* Only a small part of Media Queries is read: media types, `not`/`only`,
|
|
8
|
+
* `and`/`or` chains, and the features listed in `DISCRETE_FEATURES`.
|
|
9
|
+
* Anything else makes the answer `null` (cannot be evaluated), so the
|
|
10
|
+
* caller reports the rule instead of guessing. An unknown media type or
|
|
11
|
+
* feature name is not "anything else": it is a valid query that is false —
|
|
12
|
+
* a feature even under `not` (Media Queries 4 §4.1).
|
|
13
|
+
*/
|
|
14
|
+
/** The screen the import draws on. `size` is null when the root has no size. */
|
|
15
|
+
export interface MediaEnvironment {
|
|
16
|
+
readonly size: {
|
|
17
|
+
readonly width: number;
|
|
18
|
+
readonly height: number;
|
|
19
|
+
} | null;
|
|
20
|
+
}
|
|
21
|
+
/** True, false, or null when the query cannot be evaluated. */
|
|
22
|
+
type Truth = boolean | null;
|
|
23
|
+
/** Whether the media query list applies; null when it cannot be evaluated. */
|
|
24
|
+
export declare function evaluateMediaQueryList(prelude: string, environment: MediaEnvironment): Truth;
|
|
25
|
+
export {};
|