blitz-pdf 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +355 -2
- package/THIRD_PARTY_NOTICES.md +25 -0
- package/assets/icon.svg +4 -0
- package/dist/api.d.ts +7 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +46 -0
- package/dist/api.js.map +1 -0
- package/dist/assets.d.ts +7 -0
- package/dist/assets.d.ts.map +1 -0
- package/dist/assets.js +52 -0
- package/dist/assets.js.map +1 -0
- package/dist/browser.d.ts +3 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +3 -0
- package/dist/browser.js.map +1 -0
- package/dist/node.d.ts +3 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +10 -0
- package/dist/node.js.map +1 -0
- package/dist/options.d.ts +24 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +135 -0
- package/dist/options.js.map +1 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/examples/bun.ts +8 -0
- package/examples/german-invoice/README.md +70 -0
- package/examples/german-invoice/data.mjs +83 -0
- package/examples/german-invoice/invoice.css +59 -0
- package/examples/german-invoice/template.mjs +49 -0
- package/examples/german-invoice.mjs +20 -0
- package/examples/invoice.html +29 -0
- package/examples/node.mjs +8 -0
- package/examples/pagination.html +40 -0
- package/examples/pagination.mjs +8 -0
- package/licenses/DejaVu.txt +187 -0
- package/licenses/RUST_DEPENDENCIES.txt +11577 -0
- package/package.json +89 -4
- package/src/api.ts +44 -0
- package/src/assets.ts +43 -0
- package/src/browser.ts +6 -0
- package/src/node.ts +12 -0
- package/src/options.ts +101 -0
- package/src/types.ts +49 -0
- package/vendor/README.md +17 -0
- package/vendor/stylo/.cargo-ok +1 -0
- package/vendor/stylo/.cargo_vcs_info.json +6 -0
- package/vendor/stylo/Cargo.lock +1485 -0
- package/vendor/stylo/Cargo.toml +274 -0
- package/vendor/stylo/LICENSE +373 -0
- package/vendor/stylo/README.md +6 -0
- package/vendor/stylo/applicable_declarations.rs +328 -0
- package/vendor/stylo/author_styles.rs +79 -0
- package/vendor/stylo/bezier.rs +176 -0
- package/vendor/stylo/bloom.rs +414 -0
- package/vendor/stylo/build.rs +91 -0
- package/vendor/stylo/build_gecko.rs +393 -0
- package/vendor/stylo/color/color_function.rs +601 -0
- package/vendor/stylo/color/component.rs +207 -0
- package/vendor/stylo/color/convert.rs +935 -0
- package/vendor/stylo/color/gamut/raytrace.rs +265 -0
- package/vendor/stylo/color/gamut.rs +227 -0
- package/vendor/stylo/color/mix.rs +645 -0
- package/vendor/stylo/color/mod.rs +750 -0
- package/vendor/stylo/color/parsing.rs +770 -0
- package/vendor/stylo/color/to_css.rs +297 -0
- package/vendor/stylo/context.rs +716 -0
- package/vendor/stylo/counter_style/mod.rs +754 -0
- package/vendor/stylo/counter_style/predefined.rs +61 -0
- package/vendor/stylo/counter_style/update_predefined.py +35 -0
- package/vendor/stylo/custom_properties.rs +2023 -0
- package/vendor/stylo/custom_properties_map.rs +220 -0
- package/vendor/stylo/data.rs +693 -0
- package/vendor/stylo/device/gecko.rs +486 -0
- package/vendor/stylo/device/mod.rs +304 -0
- package/vendor/stylo/device/servo.rs +507 -0
- package/vendor/stylo/dom.rs +1164 -0
- package/vendor/stylo/dom_apis.rs +937 -0
- package/vendor/stylo/driver.rs +172 -0
- package/vendor/stylo/error_reporting.rs +484 -0
- package/vendor/stylo/font_face.rs +558 -0
- package/vendor/stylo/font_metrics.rs +124 -0
- package/vendor/stylo/gecko/anon_boxes.toml +113 -0
- package/vendor/stylo/gecko/arc_types.rs +211 -0
- package/vendor/stylo/gecko/conversions.rs +59 -0
- package/vendor/stylo/gecko/data.rs +222 -0
- package/vendor/stylo/gecko/media_features.rs +971 -0
- package/vendor/stylo/gecko/mod.rs +20 -0
- package/vendor/stylo/gecko/non_ts_pseudo_class_list.rs +119 -0
- package/vendor/stylo/gecko/pseudo_element.rs +640 -0
- package/vendor/stylo/gecko/pseudo_element_definition.mako.rs +298 -0
- package/vendor/stylo/gecko/pseudo_elements.py +137 -0
- package/vendor/stylo/gecko/pseudo_elements.toml +197 -0
- package/vendor/stylo/gecko/regen_pseudo_elements.py +33 -0
- package/vendor/stylo/gecko/restyle_damage.rs +127 -0
- package/vendor/stylo/gecko/selector_parser.rs +627 -0
- package/vendor/stylo/gecko/snapshot.rs +202 -0
- package/vendor/stylo/gecko/snapshot_helpers.rs +334 -0
- package/vendor/stylo/gecko/traversal.rs +52 -0
- package/vendor/stylo/gecko/wrapper.rs +2258 -0
- package/vendor/stylo/gecko_bindings/mod.rs +34 -0
- package/vendor/stylo/gecko_bindings/sugar/mod.rs +12 -0
- package/vendor/stylo/gecko_bindings/sugar/ns_com_ptr.rs +15 -0
- package/vendor/stylo/gecko_bindings/sugar/ns_compatibility.rs +19 -0
- package/vendor/stylo/gecko_bindings/sugar/ns_style_auto_array.rs +111 -0
- package/vendor/stylo/gecko_bindings/sugar/origin_flags.rs +31 -0
- package/vendor/stylo/gecko_bindings/sugar/ownership.rs +70 -0
- package/vendor/stylo/gecko_bindings/sugar/refptr.rs +299 -0
- package/vendor/stylo/gecko_string_cache/mod.rs +543 -0
- package/vendor/stylo/gecko_string_cache/namespace.rs +106 -0
- package/vendor/stylo/global_style_data.rs +224 -0
- package/vendor/stylo/invalidation/element/document_state.rs +156 -0
- package/vendor/stylo/invalidation/element/element_wrapper.rs +432 -0
- package/vendor/stylo/invalidation/element/invalidation_map.rs +1631 -0
- package/vendor/stylo/invalidation/element/invalidator.rs +1470 -0
- package/vendor/stylo/invalidation/element/mod.rs +13 -0
- package/vendor/stylo/invalidation/element/relative_selector.rs +1399 -0
- package/vendor/stylo/invalidation/element/restyle_hints.rs +226 -0
- package/vendor/stylo/invalidation/element/state_and_attributes.rs +756 -0
- package/vendor/stylo/invalidation/media_queries.rs +144 -0
- package/vendor/stylo/invalidation/mod.rs +10 -0
- package/vendor/stylo/invalidation/stylesheets.rs +745 -0
- package/vendor/stylo/invalidation/viewport_units.rs +71 -0
- package/vendor/stylo/lib.rs +336 -0
- package/vendor/stylo/logical_geometry.rs +1684 -0
- package/vendor/stylo/macros.rs +70 -0
- package/vendor/stylo/matching.rs +1226 -0
- package/vendor/stylo/media_queries/media_list.rs +168 -0
- package/vendor/stylo/media_queries/media_query.rs +190 -0
- package/vendor/stylo/media_queries/mod.rs +13 -0
- package/vendor/stylo/parallel.rs +205 -0
- package/vendor/stylo/parser.rs +276 -0
- package/vendor/stylo/piecewise_linear.rs +282 -0
- package/vendor/stylo/prefs.rs +33 -0
- package/vendor/stylo/properties/build.py +119 -0
- package/vendor/stylo/properties/cascade.rs +2704 -0
- package/vendor/stylo/properties/computed_value_flags.rs +206 -0
- package/vendor/stylo/properties/counted_unknown_properties.py +104 -0
- package/vendor/stylo/properties/counter_style_descriptors.toml +45 -0
- package/vendor/stylo/properties/data.py +1375 -0
- package/vendor/stylo/properties/declaration_block.rs +1788 -0
- package/vendor/stylo/properties/font_face_descriptors.toml +50 -0
- package/vendor/stylo/properties/gecko.mako.rs +1555 -0
- package/vendor/stylo/properties/helpers/animated_properties.mako.rs +800 -0
- package/vendor/stylo/properties/helpers.mako.rs +812 -0
- package/vendor/stylo/properties/longhands.toml +4305 -0
- package/vendor/stylo/properties/mod.rs +1857 -0
- package/vendor/stylo/properties/properties.html.mako +31 -0
- package/vendor/stylo/properties/properties.mako.rs +3103 -0
- package/vendor/stylo/properties/property_descriptors.toml +18 -0
- package/vendor/stylo/properties/shorthands.rs +4241 -0
- package/vendor/stylo/properties/shorthands.toml +575 -0
- package/vendor/stylo/properties/vendored_python/mako-1.3.10-py3-none-any.whl +0 -0
- package/vendor/stylo/properties/vendored_python/markupsafe/LICENSE.txt +28 -0
- package/vendor/stylo/properties/vendored_python/markupsafe/__init__.py +384 -0
- package/vendor/stylo/properties/vendored_python/toml-0.10.2-py2.py3-none-any.whl +0 -0
- package/vendor/stylo/properties/view_transition_descriptors.toml +11 -0
- package/vendor/stylo/properties_and_values/mod.rs +12 -0
- package/vendor/stylo/properties_and_values/registry.rs +65 -0
- package/vendor/stylo/properties_and_values/rule.rs +331 -0
- package/vendor/stylo/properties_and_values/syntax/ascii.rs +60 -0
- package/vendor/stylo/properties_and_values/syntax/data_type.rs +146 -0
- package/vendor/stylo/properties_and_values/syntax/mod.rs +437 -0
- package/vendor/stylo/properties_and_values/value.rs +758 -0
- package/vendor/stylo/queries/condition.rs +996 -0
- package/vendor/stylo/queries/feature.rs +199 -0
- package/vendor/stylo/queries/feature_expression.rs +1170 -0
- package/vendor/stylo/queries/mod.rs +19 -0
- package/vendor/stylo/queries/values.rs +46 -0
- package/vendor/stylo/rule_cache.rs +267 -0
- package/vendor/stylo/rule_collector.rs +554 -0
- package/vendor/stylo/rule_tree/core.rs +787 -0
- package/vendor/stylo/rule_tree/level.rs +396 -0
- package/vendor/stylo/rule_tree/map.rs +201 -0
- package/vendor/stylo/rule_tree/mod.rs +427 -0
- package/vendor/stylo/rule_tree/source.rs +80 -0
- package/vendor/stylo/rule_tree/unsafe_box.rs +78 -0
- package/vendor/stylo/scoped_tls.rs +80 -0
- package/vendor/stylo/selector_map.rs +1050 -0
- package/vendor/stylo/selector_parser.rs +305 -0
- package/vendor/stylo/servo/animation.rs +1922 -0
- package/vendor/stylo/servo/attr.rs +714 -0
- package/vendor/stylo/servo/encoding_support.rs +81 -0
- package/vendor/stylo/servo/media_features.rs +254 -0
- package/vendor/stylo/servo/mod.rs +16 -0
- package/vendor/stylo/servo/restyle_damage.rs +215 -0
- package/vendor/stylo/servo/selector_parser.rs +1036 -0
- package/vendor/stylo/servo/shadow_parts.rs +207 -0
- package/vendor/stylo/shared_lock.rs +271 -0
- package/vendor/stylo/sharing/checks.rs +281 -0
- package/vendor/stylo/sharing/mod.rs +1000 -0
- package/vendor/stylo/simple_buckets_map.rs +52 -0
- package/vendor/stylo/str.rs +168 -0
- package/vendor/stylo/style_adjuster.rs +1086 -0
- package/vendor/stylo/style_resolver.rs +658 -0
- package/vendor/stylo/stylesheet_set.rs +658 -0
- package/vendor/stylo/stylesheets/appearance_base_rule.rs +51 -0
- package/vendor/stylo/stylesheets/container_rule.rs +769 -0
- package/vendor/stylo/stylesheets/counter_style_rule.rs +7 -0
- package/vendor/stylo/stylesheets/document_rule.rs +272 -0
- package/vendor/stylo/stylesheets/font_face_rule.rs +7 -0
- package/vendor/stylo/stylesheets/font_feature_values_rule.rs +481 -0
- package/vendor/stylo/stylesheets/font_palette_values_rule.rs +304 -0
- package/vendor/stylo/stylesheets/import_rule.rs +259 -0
- package/vendor/stylo/stylesheets/keyframes_rule.rs +733 -0
- package/vendor/stylo/stylesheets/layer_rule.rs +221 -0
- package/vendor/stylo/stylesheets/loader.rs +29 -0
- package/vendor/stylo/stylesheets/margin_rule.rs +190 -0
- package/vendor/stylo/stylesheets/media_rule.rs +191 -0
- package/vendor/stylo/stylesheets/mod.rs +918 -0
- package/vendor/stylo/stylesheets/namespace_rule.rs +43 -0
- package/vendor/stylo/stylesheets/nested_declarations_rule.rs +51 -0
- package/vendor/stylo/stylesheets/origin.rs +249 -0
- package/vendor/stylo/stylesheets/page_rule.rs +354 -0
- package/vendor/stylo/stylesheets/position_try_rule.rs +68 -0
- package/vendor/stylo/stylesheets/property_rule.rs +5 -0
- package/vendor/stylo/stylesheets/rule_list.rs +167 -0
- package/vendor/stylo/stylesheets/rule_parser.rs +1189 -0
- package/vendor/stylo/stylesheets/rules_iterator.rs +397 -0
- package/vendor/stylo/stylesheets/scope_rule.rs +511 -0
- package/vendor/stylo/stylesheets/starting_style_rule.rs +54 -0
- package/vendor/stylo/stylesheets/style_rule.rs +80 -0
- package/vendor/stylo/stylesheets/stylesheet.rs +570 -0
- package/vendor/stylo/stylesheets/supports_rule.rs +486 -0
- package/vendor/stylo/stylesheets/view_transition_rule.rs +81 -0
- package/vendor/stylo/stylist.rs +5076 -0
- package/vendor/stylo/thread_state.rs +87 -0
- package/vendor/stylo/traversal.rs +765 -0
- package/vendor/stylo/traversal_flags.rs +68 -0
- package/vendor/stylo/typed_om/mod.rs +981 -0
- package/vendor/stylo/typed_om/numeric.rs +128 -0
- package/vendor/stylo/typed_om/numeric_declaration.rs +93 -0
- package/vendor/stylo/typed_om/numeric_type.rs +640 -0
- package/vendor/stylo/typed_om/sum_value.rs +426 -0
- package/vendor/stylo/url/gecko.rs +367 -0
- package/vendor/stylo/url/mod.rs +71 -0
- package/vendor/stylo/url/servo.rs +239 -0
- package/vendor/stylo/use_counters/mod.rs +137 -0
- package/vendor/stylo/values/animated/color.rs +103 -0
- package/vendor/stylo/values/animated/effects.rs +26 -0
- package/vendor/stylo/values/animated/font.rs +37 -0
- package/vendor/stylo/values/animated/grid.rs +161 -0
- package/vendor/stylo/values/animated/lists.rs +141 -0
- package/vendor/stylo/values/animated/mod.rs +550 -0
- package/vendor/stylo/values/animated/svg.rs +46 -0
- package/vendor/stylo/values/animated/text.rs +58 -0
- package/vendor/stylo/values/animated/transform.rs +1592 -0
- package/vendor/stylo/values/computed/align.rs +90 -0
- package/vendor/stylo/values/computed/angle.rs +130 -0
- package/vendor/stylo/values/computed/animation.rs +124 -0
- package/vendor/stylo/values/computed/background.rs +13 -0
- package/vendor/stylo/values/computed/basic_shape.rs +267 -0
- package/vendor/stylo/values/computed/border.rs +163 -0
- package/vendor/stylo/values/computed/box.rs +267 -0
- package/vendor/stylo/values/computed/calc.rs +334 -0
- package/vendor/stylo/values/computed/color.rs +161 -0
- package/vendor/stylo/values/computed/column.rs +11 -0
- package/vendor/stylo/values/computed/corner_shape.rs +155 -0
- package/vendor/stylo/values/computed/counters.rs +26 -0
- package/vendor/stylo/values/computed/easing.rs +109 -0
- package/vendor/stylo/values/computed/effects.rs +30 -0
- package/vendor/stylo/values/computed/flex.rs +19 -0
- package/vendor/stylo/values/computed/font.rs +1517 -0
- package/vendor/stylo/values/computed/image.rs +273 -0
- package/vendor/stylo/values/computed/length.rs +576 -0
- package/vendor/stylo/values/computed/length_percentage.rs +1055 -0
- package/vendor/stylo/values/computed/list.rs +16 -0
- package/vendor/stylo/values/computed/mod.rs +1141 -0
- package/vendor/stylo/values/computed/motion.rs +73 -0
- package/vendor/stylo/values/computed/outline.rs +7 -0
- package/vendor/stylo/values/computed/page.rs +77 -0
- package/vendor/stylo/values/computed/param.rs +9 -0
- package/vendor/stylo/values/computed/percentage.rs +143 -0
- package/vendor/stylo/values/computed/position.rs +273 -0
- package/vendor/stylo/values/computed/ratio.rs +88 -0
- package/vendor/stylo/values/computed/rect.rs +19 -0
- package/vendor/stylo/values/computed/resolution.rs +76 -0
- package/vendor/stylo/values/computed/svg.rs +66 -0
- package/vendor/stylo/values/computed/table.rs +7 -0
- package/vendor/stylo/values/computed/text.rs +199 -0
- package/vendor/stylo/values/computed/time.rs +83 -0
- package/vendor/stylo/values/computed/transform.rs +559 -0
- package/vendor/stylo/values/computed/tree_counting.rs +30 -0
- package/vendor/stylo/values/computed/ui.rs +23 -0
- package/vendor/stylo/values/computed/url.rs +12 -0
- package/vendor/stylo/values/distance.rs +139 -0
- package/vendor/stylo/values/generics/animation.rs +363 -0
- package/vendor/stylo/values/generics/background.rs +65 -0
- package/vendor/stylo/values/generics/basic_shape.rs +1389 -0
- package/vendor/stylo/values/generics/border.rs +303 -0
- package/vendor/stylo/values/generics/box.rs +552 -0
- package/vendor/stylo/values/generics/calc.rs +2986 -0
- package/vendor/stylo/values/generics/color.rs +318 -0
- package/vendor/stylo/values/generics/column.rs +44 -0
- package/vendor/stylo/values/generics/counters.rs +320 -0
- package/vendor/stylo/values/generics/easing.rs +132 -0
- package/vendor/stylo/values/generics/effects.rs +131 -0
- package/vendor/stylo/values/generics/flex.rs +34 -0
- package/vendor/stylo/values/generics/font.rs +376 -0
- package/vendor/stylo/values/generics/grid.rs +962 -0
- package/vendor/stylo/values/generics/image.rs +653 -0
- package/vendor/stylo/values/generics/length.rs +656 -0
- package/vendor/stylo/values/generics/mod.rs +303 -0
- package/vendor/stylo/values/generics/motion.rs +274 -0
- package/vendor/stylo/values/generics/page.rs +167 -0
- package/vendor/stylo/values/generics/position.rs +669 -0
- package/vendor/stylo/values/generics/ratio.rs +88 -0
- package/vendor/stylo/values/generics/rect.rs +147 -0
- package/vendor/stylo/values/generics/size.rs +102 -0
- package/vendor/stylo/values/generics/svg.rs +226 -0
- package/vendor/stylo/values/generics/text.rs +268 -0
- package/vendor/stylo/values/generics/transform.rs +1157 -0
- package/vendor/stylo/values/generics/ui.rs +147 -0
- package/vendor/stylo/values/generics/url.rs +51 -0
- package/vendor/stylo/values/mod.rs +820 -0
- package/vendor/stylo/values/resolved/animation.rs +33 -0
- package/vendor/stylo/values/resolved/color.rs +53 -0
- package/vendor/stylo/values/resolved/counters.rs +45 -0
- package/vendor/stylo/values/resolved/mod.rs +316 -0
- package/vendor/stylo/values/specified/align.rs +711 -0
- package/vendor/stylo/values/specified/angle.rs +407 -0
- package/vendor/stylo/values/specified/animation.rs +921 -0
- package/vendor/stylo/values/specified/background.rs +270 -0
- package/vendor/stylo/values/specified/basic_shape.rs +986 -0
- package/vendor/stylo/values/specified/border.rs +430 -0
- package/vendor/stylo/values/specified/box.rs +2231 -0
- package/vendor/stylo/values/specified/calc.rs +1657 -0
- package/vendor/stylo/values/specified/color.rs +1210 -0
- package/vendor/stylo/values/specified/column.rs +11 -0
- package/vendor/stylo/values/specified/corner_shape.rs +152 -0
- package/vendor/stylo/values/specified/counters.rs +242 -0
- package/vendor/stylo/values/specified/easing.rs +225 -0
- package/vendor/stylo/values/specified/effects.rs +421 -0
- package/vendor/stylo/values/specified/flex.rs +42 -0
- package/vendor/stylo/values/specified/font.rs +2103 -0
- package/vendor/stylo/values/specified/frequency.rs +39 -0
- package/vendor/stylo/values/specified/grid.rs +407 -0
- package/vendor/stylo/values/specified/image.rs +1385 -0
- package/vendor/stylo/values/specified/intersection_observer.rs +80 -0
- package/vendor/stylo/values/specified/length.rs +2024 -0
- package/vendor/stylo/values/specified/list.rs +190 -0
- package/vendor/stylo/values/specified/mod.rs +531 -0
- package/vendor/stylo/values/specified/motion.rs +316 -0
- package/vendor/stylo/values/specified/number.rs +603 -0
- package/vendor/stylo/values/specified/outline.rs +69 -0
- package/vendor/stylo/values/specified/page.rs +94 -0
- package/vendor/stylo/values/specified/param.rs +121 -0
- package/vendor/stylo/values/specified/percentage.rs +364 -0
- package/vendor/stylo/values/specified/position.rs +2247 -0
- package/vendor/stylo/values/specified/ratio.rs +29 -0
- package/vendor/stylo/values/specified/rect.rs +28 -0
- package/vendor/stylo/values/specified/resolution.rs +237 -0
- package/vendor/stylo/values/specified/source_size_list.rs +135 -0
- package/vendor/stylo/values/specified/svg.rs +427 -0
- package/vendor/stylo/values/specified/svg_path.rs +1115 -0
- package/vendor/stylo/values/specified/table.rs +38 -0
- package/vendor/stylo/values/specified/text.rs +1561 -0
- package/vendor/stylo/values/specified/time.rs +313 -0
- package/vendor/stylo/values/specified/transform.rs +599 -0
- package/vendor/stylo/values/specified/tree_counting.rs +32 -0
- package/vendor/stylo/values/specified/ui.rs +344 -0
- package/vendor/stylo/values/specified/url.rs +12 -0
- package/vendor/stylo/values/tagged_numeric.rs +253 -0
- package/wasm/blitz_pdf.d.ts +67 -0
- package/wasm/blitz_pdf.js +699 -0
- package/wasm/blitz_pdf_bg.wasm +0 -0
- package/wasm/blitz_pdf_bg.wasm.d.ts +20 -0
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
4
|
+
|
|
5
|
+
#![deny(missing_docs)]
|
|
6
|
+
|
|
7
|
+
//! Parsing for CSS colors.
|
|
8
|
+
|
|
9
|
+
use std::fmt::Write;
|
|
10
|
+
|
|
11
|
+
use super::{
|
|
12
|
+
color_function::ColorFunction,
|
|
13
|
+
component::{ColorComponent, ColorComponentType},
|
|
14
|
+
AbsoluteColor,
|
|
15
|
+
};
|
|
16
|
+
use crate::derives::*;
|
|
17
|
+
use crate::typed_om::{NumericBaseType, NumericType};
|
|
18
|
+
use crate::{
|
|
19
|
+
parser::{Parse, ParserContext},
|
|
20
|
+
values::{
|
|
21
|
+
computed::Color as ComputedColor,
|
|
22
|
+
generics::{calc::CalcType, Optional},
|
|
23
|
+
specified::{
|
|
24
|
+
angle::NoCalcAngle,
|
|
25
|
+
calc::{Leaf, PercentageContext},
|
|
26
|
+
color::Color as SpecifiedColor,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
use cssparser::{
|
|
31
|
+
color::{parse_hash_color, PredefinedColorSpace, OPAQUE},
|
|
32
|
+
match_ignore_ascii_case, CowRcStr, Parser, Token,
|
|
33
|
+
};
|
|
34
|
+
use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
|
|
35
|
+
|
|
36
|
+
/// Represents a channel keyword inside a color.
|
|
37
|
+
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToShmem)]
|
|
38
|
+
#[repr(C)]
|
|
39
|
+
pub struct ChannelKeyword(u16);
|
|
40
|
+
bitflags! {
|
|
41
|
+
impl ChannelKeyword: u16 {
|
|
42
|
+
/// alpha
|
|
43
|
+
const ALPHA = 1 << 0;
|
|
44
|
+
/// a
|
|
45
|
+
const A = 1 << 1;
|
|
46
|
+
/// b, blackness, blue
|
|
47
|
+
const B = 1 << 2;
|
|
48
|
+
/// chroma
|
|
49
|
+
const C = 1 << 3;
|
|
50
|
+
/// green
|
|
51
|
+
const G = 1 << 4;
|
|
52
|
+
/// hue
|
|
53
|
+
const H = 1 << 5;
|
|
54
|
+
/// lightness
|
|
55
|
+
const L = 1 << 6;
|
|
56
|
+
/// red
|
|
57
|
+
const R = 1 << 7;
|
|
58
|
+
/// saturation
|
|
59
|
+
const S = 1 << 8;
|
|
60
|
+
/// whiteness
|
|
61
|
+
const W = 1 << 9;
|
|
62
|
+
/// x
|
|
63
|
+
const X = 1 << 10;
|
|
64
|
+
/// y
|
|
65
|
+
const Y = 1 << 11;
|
|
66
|
+
/// z
|
|
67
|
+
const Z = 1 << 12;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
impl ChannelKeyword {
|
|
72
|
+
/// Channel keywords allowed in sRGB colors.
|
|
73
|
+
/// https://drafts.csswg.org/css-color-5/#relative-RGB
|
|
74
|
+
pub fn rgb() -> Self {
|
|
75
|
+
Self::R | Self::G | Self::B | Self::ALPHA
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// Channel keywords allowed in HSL colors.
|
|
79
|
+
/// https://drafts.csswg.org/css-color-5/#relative-HSL
|
|
80
|
+
pub fn hsl() -> Self {
|
|
81
|
+
Self::H | Self::S | Self::L | Self::ALPHA
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// Channel keywords allowed in HWB colors.
|
|
85
|
+
/// https://drafts.csswg.org/css-color-5/#relative-HWB
|
|
86
|
+
pub fn hwb() -> Self {
|
|
87
|
+
Self::H | Self::W | Self::B | Self::ALPHA
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Channel keywords allowed in Lab and Oklab colors.
|
|
91
|
+
/// https://drafts.csswg.org/css-color-5/#relative-Lab
|
|
92
|
+
pub fn lab() -> Self {
|
|
93
|
+
Self::L | Self::A | Self::B | Self::ALPHA
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// Channel keywords allowed in LCH and OkLCh colors.
|
|
97
|
+
/// https://drafts.csswg.org/css-color-5/#relative-LCH
|
|
98
|
+
pub fn lch() -> Self {
|
|
99
|
+
Self::L | Self::C | Self::H | Self::ALPHA
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// Channel keywords allowed in XYZ colors.
|
|
103
|
+
/// https://drafts.csswg.org/css-color-5/#relative-color-function
|
|
104
|
+
pub fn xyz() -> Self {
|
|
105
|
+
Self::X | Self::Y | Self::Z | Self::ALPHA
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// Parse a channel keyword from an ident.
|
|
109
|
+
pub fn from_ident(ident: &str) -> Result<Self, ()> {
|
|
110
|
+
Ok(match_ignore_ascii_case! { ident,
|
|
111
|
+
"alpha" => Self::ALPHA,
|
|
112
|
+
"a" => Self::A,
|
|
113
|
+
"b" => Self::B,
|
|
114
|
+
"c" => Self::C,
|
|
115
|
+
"g" => Self::G,
|
|
116
|
+
"h" => Self::H,
|
|
117
|
+
"l" => Self::L,
|
|
118
|
+
"r" => Self::R,
|
|
119
|
+
"s" => Self::S,
|
|
120
|
+
"w" => Self::W,
|
|
121
|
+
"x" => Self::X,
|
|
122
|
+
"y" => Self::Y,
|
|
123
|
+
"z" => Self::Z,
|
|
124
|
+
_ => return Err(())
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
impl Parse for ChannelKeyword {
|
|
130
|
+
fn parse(_: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
|
|
131
|
+
let ident = input.expect_ident()?;
|
|
132
|
+
Self::from_ident(ident.as_ref()).map_err(|()| ParseError::unexpected_token())
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
impl ToCss for ChannelKeyword {
|
|
137
|
+
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> std::fmt::Result
|
|
138
|
+
where
|
|
139
|
+
W: std::fmt::Write,
|
|
140
|
+
{
|
|
141
|
+
dest.write_str(match *self {
|
|
142
|
+
Self::ALPHA => "alpha",
|
|
143
|
+
Self::A => "a",
|
|
144
|
+
Self::B => "b",
|
|
145
|
+
Self::C => "c",
|
|
146
|
+
Self::G => "g",
|
|
147
|
+
Self::H => "h",
|
|
148
|
+
Self::L => "l",
|
|
149
|
+
Self::R => "r",
|
|
150
|
+
Self::S => "s",
|
|
151
|
+
Self::W => "w",
|
|
152
|
+
Self::X => "x",
|
|
153
|
+
Self::Y => "y",
|
|
154
|
+
Self::Z => "z",
|
|
155
|
+
_ => {
|
|
156
|
+
debug_assert!(
|
|
157
|
+
false,
|
|
158
|
+
"tried to serialize unexpected multi-value ChannelKeyword"
|
|
159
|
+
);
|
|
160
|
+
""
|
|
161
|
+
},
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/// Return the named color with the given name.
|
|
167
|
+
///
|
|
168
|
+
/// Matching is case-insensitive in the ASCII range.
|
|
169
|
+
/// CSS escaping (if relevant) should be resolved before calling this function.
|
|
170
|
+
/// (For example, the value of an `Ident` token is fine.)
|
|
171
|
+
#[inline]
|
|
172
|
+
pub fn parse_color_keyword(ident: &str) -> Result<SpecifiedColor, ()> {
|
|
173
|
+
Ok(match_ignore_ascii_case! { ident,
|
|
174
|
+
"transparent" => {
|
|
175
|
+
SpecifiedColor::from_absolute_color(AbsoluteColor::srgb_legacy(0u8, 0u8, 0u8, 0.0))
|
|
176
|
+
},
|
|
177
|
+
"currentcolor" => SpecifiedColor::CurrentColor,
|
|
178
|
+
_ => {
|
|
179
|
+
let (r, g, b) = cssparser::color::parse_named_color(ident)?;
|
|
180
|
+
SpecifiedColor::from_absolute_color(AbsoluteColor::srgb_legacy(r, g, b, OPAQUE))
|
|
181
|
+
},
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/// Parse a CSS color using the specified [`ColorParser`] and return a new color
|
|
186
|
+
/// value on success.
|
|
187
|
+
pub fn parse_color_with(
|
|
188
|
+
context: &ParserContext,
|
|
189
|
+
input: &mut Parser,
|
|
190
|
+
) -> Result<SpecifiedColor, ParseError> {
|
|
191
|
+
let token = input.next()?;
|
|
192
|
+
match *token {
|
|
193
|
+
Token::Hash(ref value) | Token::IDHash(ref value) => parse_hash_color(value.as_bytes())
|
|
194
|
+
.map(|(r, g, b, a)| {
|
|
195
|
+
SpecifiedColor::from_absolute_color(AbsoluteColor::srgb_legacy(r, g, b, a))
|
|
196
|
+
}),
|
|
197
|
+
Token::Ident(ref value) => parse_color_keyword(value),
|
|
198
|
+
Token::Function(ref name) => {
|
|
199
|
+
let name = name.clone();
|
|
200
|
+
return input.parse_nested_block(|arguments| {
|
|
201
|
+
let color_function = parse_color_function(context, name, arguments)?;
|
|
202
|
+
if !color_function.has_origin_color() {
|
|
203
|
+
if let Ok(ComputedColor::Absolute(resolved)) =
|
|
204
|
+
color_function.to_computed_color(None)
|
|
205
|
+
{
|
|
206
|
+
return Ok(SpecifiedColor::from_absolute_color(resolved));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// Preserve the color as it was parsed.
|
|
210
|
+
Ok(SpecifiedColor::ColorFunction(Box::new(color_function)))
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
_ => Err(()),
|
|
214
|
+
}
|
|
215
|
+
.map_err(|()| ParseError::unexpected_token())
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/// Parse one of the color functions: rgba(), lab(), color(), etc.
|
|
219
|
+
#[inline]
|
|
220
|
+
fn parse_color_function<'i>(
|
|
221
|
+
context: &ParserContext,
|
|
222
|
+
name: CowRcStr<'i>,
|
|
223
|
+
arguments: &mut Parser<'i>,
|
|
224
|
+
) -> Result<ColorFunction<SpecifiedColor>, ParseError> {
|
|
225
|
+
let origin_color = parse_origin_color(context, arguments)?;
|
|
226
|
+
let color = match_ignore_ascii_case! { &name,
|
|
227
|
+
"rgb" | "rgba" => parse_rgb(context, arguments, origin_color),
|
|
228
|
+
"hsl" | "hsla" => parse_hsl(context, arguments, origin_color),
|
|
229
|
+
"hwb" => parse_hwb(context, arguments, origin_color),
|
|
230
|
+
"lab" => parse_lab_like(context, arguments, origin_color, ColorFunction::Lab),
|
|
231
|
+
"lch" => parse_lch_like(context, arguments, origin_color, ColorFunction::Lch),
|
|
232
|
+
"oklab" => parse_lab_like(context, arguments, origin_color, ColorFunction::Oklab),
|
|
233
|
+
"oklch" => parse_lch_like(context, arguments, origin_color, ColorFunction::Oklch),
|
|
234
|
+
"color" => parse_color_with_color_space(context, arguments, origin_color),
|
|
235
|
+
"alpha" if crate::pref!("layout.css.alpha-color-function.enabled") => {
|
|
236
|
+
parse_relative_alpha(
|
|
237
|
+
context,
|
|
238
|
+
arguments,
|
|
239
|
+
origin_color.ok_or_else(|| ParseError::custom(StyleParseErrorKind::UnspecifiedError))?
|
|
240
|
+
)
|
|
241
|
+
},
|
|
242
|
+
_ => return Err(ParseError::unexpected_token()),
|
|
243
|
+
}?;
|
|
244
|
+
arguments.expect_exhausted()?;
|
|
245
|
+
Ok(color)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// Parse the relative color syntax "from" syntax `from <color>`.
|
|
249
|
+
fn parse_origin_color(
|
|
250
|
+
context: &ParserContext,
|
|
251
|
+
arguments: &mut Parser,
|
|
252
|
+
) -> Result<Option<SpecifiedColor>, ParseError> {
|
|
253
|
+
// Not finding the from keyword is not an error, it just means we don't
|
|
254
|
+
// have an origin color.
|
|
255
|
+
if arguments
|
|
256
|
+
.try_parse(|p| p.expect_ident_matching("from"))
|
|
257
|
+
.is_err()
|
|
258
|
+
{
|
|
259
|
+
return Ok(None);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
SpecifiedColor::parse(context, arguments).map(Some)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
#[inline]
|
|
266
|
+
fn parse_rgb(
|
|
267
|
+
context: &ParserContext,
|
|
268
|
+
arguments: &mut Parser,
|
|
269
|
+
origin_color: Option<SpecifiedColor>,
|
|
270
|
+
) -> Result<ColorFunction<SpecifiedColor>, ParseError> {
|
|
271
|
+
let allowed_channel_keywords = if origin_color.is_some() {
|
|
272
|
+
ChannelKeyword::rgb()
|
|
273
|
+
} else {
|
|
274
|
+
ChannelKeyword::empty()
|
|
275
|
+
};
|
|
276
|
+
let maybe_red = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
277
|
+
|
|
278
|
+
// If the first component is not "none" and is followed by a comma, then we
|
|
279
|
+
// are parsing the legacy syntax. Legacy syntax also doesn't support an
|
|
280
|
+
// origin color.
|
|
281
|
+
let is_legacy_syntax = origin_color.is_none()
|
|
282
|
+
&& !maybe_red.is_none()
|
|
283
|
+
&& arguments.try_parse(|p| p.expect_comma()).is_ok();
|
|
284
|
+
|
|
285
|
+
Ok(if is_legacy_syntax {
|
|
286
|
+
let (green, blue) = if maybe_red.could_be_percentage() {
|
|
287
|
+
let green = parse_percentage(context, arguments, false, allowed_channel_keywords)?;
|
|
288
|
+
arguments.expect_comma()?;
|
|
289
|
+
let blue = parse_percentage(context, arguments, false, allowed_channel_keywords)?;
|
|
290
|
+
(green, blue)
|
|
291
|
+
} else {
|
|
292
|
+
let green = parse_number(context, arguments, false, allowed_channel_keywords)?;
|
|
293
|
+
arguments.expect_comma()?;
|
|
294
|
+
let blue = parse_number(context, arguments, false, allowed_channel_keywords)?;
|
|
295
|
+
(green, blue)
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
let alpha = parse_legacy_alpha(context, arguments)?;
|
|
299
|
+
|
|
300
|
+
ColorFunction::Rgb(origin_color.into(), maybe_red, green, blue, alpha)
|
|
301
|
+
} else {
|
|
302
|
+
let green = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
303
|
+
let blue = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
304
|
+
|
|
305
|
+
let alpha = parse_modern_alpha(context, arguments, allowed_channel_keywords)?;
|
|
306
|
+
|
|
307
|
+
ColorFunction::Rgb(origin_color.into(), maybe_red, green, blue, alpha)
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/// Parses hsl syntax.
|
|
312
|
+
///
|
|
313
|
+
/// <https://drafts.csswg.org/css-color/#the-hsl-notation>
|
|
314
|
+
#[inline]
|
|
315
|
+
fn parse_hsl(
|
|
316
|
+
context: &ParserContext,
|
|
317
|
+
arguments: &mut Parser,
|
|
318
|
+
origin_color: Option<SpecifiedColor>,
|
|
319
|
+
) -> Result<ColorFunction<SpecifiedColor>, ParseError> {
|
|
320
|
+
let allowed_channel_keywords = if origin_color.is_some() {
|
|
321
|
+
ChannelKeyword::hsl()
|
|
322
|
+
} else {
|
|
323
|
+
ChannelKeyword::empty()
|
|
324
|
+
};
|
|
325
|
+
let hue = parse_number_or_angle(context, arguments, true, allowed_channel_keywords)?;
|
|
326
|
+
|
|
327
|
+
// If the hue is not "none" and is followed by a comma, then we are parsing
|
|
328
|
+
// the legacy syntax. Legacy syntax also doesn't support an origin color.
|
|
329
|
+
let is_legacy_syntax = origin_color.is_none()
|
|
330
|
+
&& !hue.is_none()
|
|
331
|
+
&& arguments.try_parse(|p| p.expect_comma()).is_ok();
|
|
332
|
+
|
|
333
|
+
let (saturation, lightness, alpha) = if is_legacy_syntax {
|
|
334
|
+
let saturation = parse_percentage(context, arguments, false, allowed_channel_keywords)?;
|
|
335
|
+
arguments.expect_comma()?;
|
|
336
|
+
let lightness = parse_percentage(context, arguments, false, allowed_channel_keywords)?;
|
|
337
|
+
let alpha = parse_legacy_alpha(context, arguments)?;
|
|
338
|
+
(saturation, lightness, alpha)
|
|
339
|
+
} else {
|
|
340
|
+
let saturation =
|
|
341
|
+
parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
342
|
+
let lightness =
|
|
343
|
+
parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
344
|
+
let alpha = parse_modern_alpha(context, arguments, allowed_channel_keywords)?;
|
|
345
|
+
(saturation, lightness, alpha)
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
Ok(ColorFunction::Hsl(
|
|
349
|
+
origin_color.into(),
|
|
350
|
+
hue,
|
|
351
|
+
saturation,
|
|
352
|
+
lightness,
|
|
353
|
+
alpha,
|
|
354
|
+
))
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/// Parses hwb syntax.
|
|
358
|
+
///
|
|
359
|
+
/// <https://drafts.csswg.org/css-color/#the-hbw-notation>
|
|
360
|
+
#[inline]
|
|
361
|
+
fn parse_hwb(
|
|
362
|
+
context: &ParserContext,
|
|
363
|
+
arguments: &mut Parser,
|
|
364
|
+
origin_color: Option<SpecifiedColor>,
|
|
365
|
+
) -> Result<ColorFunction<SpecifiedColor>, ParseError> {
|
|
366
|
+
let allowed_channel_keywords = if origin_color.is_some() {
|
|
367
|
+
ChannelKeyword::hwb()
|
|
368
|
+
} else {
|
|
369
|
+
ChannelKeyword::empty()
|
|
370
|
+
};
|
|
371
|
+
let hue = parse_number_or_angle(context, arguments, true, allowed_channel_keywords)?;
|
|
372
|
+
let whiteness = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
373
|
+
let blackness = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
374
|
+
|
|
375
|
+
let alpha = parse_modern_alpha(context, arguments, allowed_channel_keywords)?;
|
|
376
|
+
|
|
377
|
+
Ok(ColorFunction::Hwb(
|
|
378
|
+
origin_color.into(),
|
|
379
|
+
hue,
|
|
380
|
+
whiteness,
|
|
381
|
+
blackness,
|
|
382
|
+
alpha,
|
|
383
|
+
))
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
type IntoLabFn<Output> = fn(
|
|
387
|
+
origin: Optional<SpecifiedColor>,
|
|
388
|
+
l: ColorComponent<NumberOrPercentageComponent>,
|
|
389
|
+
a: ColorComponent<NumberOrPercentageComponent>,
|
|
390
|
+
b: ColorComponent<NumberOrPercentageComponent>,
|
|
391
|
+
alpha: ColorComponent<NumberOrPercentageComponent>,
|
|
392
|
+
) -> Output;
|
|
393
|
+
|
|
394
|
+
#[inline]
|
|
395
|
+
fn parse_lab_like(
|
|
396
|
+
context: &ParserContext,
|
|
397
|
+
arguments: &mut Parser,
|
|
398
|
+
origin_color: Option<SpecifiedColor>,
|
|
399
|
+
into_color: IntoLabFn<ColorFunction<SpecifiedColor>>,
|
|
400
|
+
) -> Result<ColorFunction<SpecifiedColor>, ParseError> {
|
|
401
|
+
let allowed_channel_keywords = if origin_color.is_some() {
|
|
402
|
+
ChannelKeyword::lab()
|
|
403
|
+
} else {
|
|
404
|
+
ChannelKeyword::empty()
|
|
405
|
+
};
|
|
406
|
+
let lightness = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
407
|
+
let a = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
408
|
+
let b = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
409
|
+
|
|
410
|
+
let alpha = parse_modern_alpha(context, arguments, allowed_channel_keywords)?;
|
|
411
|
+
|
|
412
|
+
Ok(into_color(origin_color.into(), lightness, a, b, alpha))
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
type IntoLchFn<Output> = fn(
|
|
416
|
+
origin: Optional<SpecifiedColor>,
|
|
417
|
+
l: ColorComponent<NumberOrPercentageComponent>,
|
|
418
|
+
a: ColorComponent<NumberOrPercentageComponent>,
|
|
419
|
+
b: ColorComponent<NumberOrAngleComponent>,
|
|
420
|
+
alpha: ColorComponent<NumberOrPercentageComponent>,
|
|
421
|
+
) -> Output;
|
|
422
|
+
|
|
423
|
+
#[inline]
|
|
424
|
+
fn parse_lch_like(
|
|
425
|
+
context: &ParserContext,
|
|
426
|
+
arguments: &mut Parser,
|
|
427
|
+
origin_color: Option<SpecifiedColor>,
|
|
428
|
+
into_color: IntoLchFn<ColorFunction<SpecifiedColor>>,
|
|
429
|
+
) -> Result<ColorFunction<SpecifiedColor>, ParseError> {
|
|
430
|
+
let allowed_channel_keywords = if origin_color.is_some() {
|
|
431
|
+
ChannelKeyword::lch()
|
|
432
|
+
} else {
|
|
433
|
+
ChannelKeyword::empty()
|
|
434
|
+
};
|
|
435
|
+
let lightness = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
436
|
+
let chroma = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
437
|
+
let hue = parse_number_or_angle(context, arguments, true, allowed_channel_keywords)?;
|
|
438
|
+
|
|
439
|
+
let alpha = parse_modern_alpha(context, arguments, allowed_channel_keywords)?;
|
|
440
|
+
|
|
441
|
+
Ok(into_color(
|
|
442
|
+
origin_color.into(),
|
|
443
|
+
lightness,
|
|
444
|
+
chroma,
|
|
445
|
+
hue,
|
|
446
|
+
alpha,
|
|
447
|
+
))
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/// Parse the color() function.
|
|
451
|
+
#[inline]
|
|
452
|
+
fn parse_color_with_color_space(
|
|
453
|
+
context: &ParserContext,
|
|
454
|
+
arguments: &mut Parser,
|
|
455
|
+
origin_color: Option<SpecifiedColor>,
|
|
456
|
+
) -> Result<ColorFunction<SpecifiedColor>, ParseError> {
|
|
457
|
+
let color_space = PredefinedColorSpace::parse(arguments)?;
|
|
458
|
+
let allowed_channel_keywords = if origin_color.is_some() {
|
|
459
|
+
match color_space {
|
|
460
|
+
PredefinedColorSpace::Srgb
|
|
461
|
+
| PredefinedColorSpace::SrgbLinear
|
|
462
|
+
| PredefinedColorSpace::DisplayP3
|
|
463
|
+
| PredefinedColorSpace::DisplayP3Linear
|
|
464
|
+
| PredefinedColorSpace::A98Rgb
|
|
465
|
+
| PredefinedColorSpace::ProphotoRgb
|
|
466
|
+
| PredefinedColorSpace::Rec2020 => ChannelKeyword::rgb(),
|
|
467
|
+
PredefinedColorSpace::XyzD50 | PredefinedColorSpace::XyzD65 => ChannelKeyword::xyz(),
|
|
468
|
+
}
|
|
469
|
+
} else {
|
|
470
|
+
ChannelKeyword::empty()
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
let c1 = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
474
|
+
let c2 = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
475
|
+
let c3 = parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)?;
|
|
476
|
+
|
|
477
|
+
let alpha = parse_modern_alpha(context, arguments, allowed_channel_keywords)?;
|
|
478
|
+
|
|
479
|
+
Ok(ColorFunction::Color(
|
|
480
|
+
origin_color.into(),
|
|
481
|
+
c1,
|
|
482
|
+
c2,
|
|
483
|
+
c3,
|
|
484
|
+
alpha,
|
|
485
|
+
color_space.into(),
|
|
486
|
+
))
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/// Parse the alpha() function.
|
|
490
|
+
#[inline]
|
|
491
|
+
fn parse_relative_alpha(
|
|
492
|
+
context: &ParserContext,
|
|
493
|
+
arguments: &mut Parser,
|
|
494
|
+
origin_color: SpecifiedColor,
|
|
495
|
+
) -> Result<ColorFunction<SpecifiedColor>, ParseError> {
|
|
496
|
+
let alpha = parse_modern_alpha(context, arguments, ChannelKeyword::ALPHA)?;
|
|
497
|
+
if matches!(alpha, ColorComponent::AlphaOmitted) {
|
|
498
|
+
// An alpha is required as it is the only controllable component.
|
|
499
|
+
return Err(ParseError::custom(StyleParseErrorKind::UnspecifiedError));
|
|
500
|
+
}
|
|
501
|
+
Ok(ColorFunction::Alpha(origin_color, alpha))
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/// Either a percentage or a number.
|
|
505
|
+
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)]
|
|
506
|
+
#[repr(u8)]
|
|
507
|
+
pub enum NumberOrPercentageComponent {
|
|
508
|
+
/// `<number>`.
|
|
509
|
+
Number(f32),
|
|
510
|
+
/// `<percentage>`
|
|
511
|
+
/// The value as a float, divided by 100 so that the nominal range is 0.0 to 1.0.
|
|
512
|
+
Percentage(f32),
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
impl NumberOrPercentageComponent {
|
|
516
|
+
/// Return the value as a number. Percentages will be adjusted to the range
|
|
517
|
+
/// [0..percent_basis].
|
|
518
|
+
pub fn to_number(&self, percentage_basis: f32) -> f32 {
|
|
519
|
+
match *self {
|
|
520
|
+
Self::Number(value) => value,
|
|
521
|
+
Self::Percentage(unit_value) => unit_value * percentage_basis,
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
impl ColorComponentType for NumberOrPercentageComponent {
|
|
527
|
+
fn from_value(value: f32) -> Self {
|
|
528
|
+
Self::Number(value)
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
fn is_valid_type(ty: &NumericType) -> bool {
|
|
532
|
+
ty.as_calc_type()
|
|
533
|
+
.is_ok_and(|ty| ty == CalcType::Number || ty == CalcType::Percentage)
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
fn try_from_token(token: &Token) -> Result<Self, ()> {
|
|
537
|
+
Ok(match *token {
|
|
538
|
+
Token::Number { value, .. } => Self::Number(value),
|
|
539
|
+
Token::Percentage { unit_value, .. } => Self::Percentage(unit_value),
|
|
540
|
+
_ => {
|
|
541
|
+
return Err(());
|
|
542
|
+
},
|
|
543
|
+
})
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
fn try_from_leaf(leaf: &Leaf) -> Result<Self, ()> {
|
|
547
|
+
Ok(match *leaf {
|
|
548
|
+
Leaf::Percentage(ref p) => Self::Percentage(p.get()),
|
|
549
|
+
Leaf::Number(n) => Self::Number(n.value()),
|
|
550
|
+
_ => return Err(()),
|
|
551
|
+
})
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/// Either an angle or a number.
|
|
556
|
+
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)]
|
|
557
|
+
#[repr(u8)]
|
|
558
|
+
pub enum NumberOrAngleComponent {
|
|
559
|
+
/// `<number>`.
|
|
560
|
+
Number(f32),
|
|
561
|
+
/// `<angle>`
|
|
562
|
+
/// The value as a number of degrees.
|
|
563
|
+
Angle(f32),
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
impl NumberOrAngleComponent {
|
|
567
|
+
/// Return the angle in degrees. `NumberOrAngle::Number` is returned as
|
|
568
|
+
/// degrees, because it is the canonical unit.
|
|
569
|
+
pub fn degrees(&self) -> f32 {
|
|
570
|
+
match *self {
|
|
571
|
+
Self::Number(value) => value,
|
|
572
|
+
Self::Angle(degrees) => degrees,
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
impl ColorComponentType for NumberOrAngleComponent {
|
|
578
|
+
fn from_value(value: f32) -> Self {
|
|
579
|
+
Self::Number(value)
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
fn is_valid_type(ty: &NumericType) -> bool {
|
|
583
|
+
ty.as_calc_type()
|
|
584
|
+
.is_ok_and(|ty| ty == CalcType::Number || ty == CalcType::Angle)
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
fn try_from_token(token: &Token) -> Result<Self, ()> {
|
|
588
|
+
Ok(match *token {
|
|
589
|
+
Token::Number { value, .. } => Self::Number(value),
|
|
590
|
+
Token::Dimension {
|
|
591
|
+
value, ref unit, ..
|
|
592
|
+
} => {
|
|
593
|
+
let degrees = NoCalcAngle::parse_dimension(value, unit)?.degrees();
|
|
594
|
+
NumberOrAngleComponent::Angle(degrees)
|
|
595
|
+
},
|
|
596
|
+
_ => {
|
|
597
|
+
return Err(());
|
|
598
|
+
},
|
|
599
|
+
})
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
fn try_from_leaf(leaf: &Leaf) -> Result<Self, ()> {
|
|
603
|
+
Ok(match *leaf {
|
|
604
|
+
Leaf::Angle(angle) => Self::Angle(angle.degrees()),
|
|
605
|
+
Leaf::Number(n) => Self::Number(n.value()),
|
|
606
|
+
_ => return Err(()),
|
|
607
|
+
})
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/// The raw f32 here is for <number>.
|
|
612
|
+
impl ColorComponentType for f32 {
|
|
613
|
+
fn from_value(value: f32) -> Self {
|
|
614
|
+
value
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
fn is_valid_type(ty: &NumericType) -> bool {
|
|
618
|
+
matches!(ty.as_calc_type(), Ok(CalcType::Number))
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
fn try_from_token(token: &Token) -> Result<Self, ()> {
|
|
622
|
+
if let Token::Number { value, .. } = *token {
|
|
623
|
+
Ok(value)
|
|
624
|
+
} else {
|
|
625
|
+
Err(())
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
fn try_from_leaf(leaf: &Leaf) -> Result<Self, ()> {
|
|
630
|
+
if let Leaf::Number(n) = *leaf {
|
|
631
|
+
Ok(n.value())
|
|
632
|
+
} else {
|
|
633
|
+
Err(())
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/// Parse an `<number>` or `<angle>` value.
|
|
639
|
+
fn parse_number_or_angle(
|
|
640
|
+
context: &ParserContext,
|
|
641
|
+
input: &mut Parser,
|
|
642
|
+
allow_none: bool,
|
|
643
|
+
allowed_channel_keywords: ChannelKeyword,
|
|
644
|
+
) -> Result<ColorComponent<NumberOrAngleComponent>, ParseError> {
|
|
645
|
+
ColorComponent::parse(
|
|
646
|
+
context,
|
|
647
|
+
input,
|
|
648
|
+
allow_none,
|
|
649
|
+
allowed_channel_keywords,
|
|
650
|
+
PercentageContext::not_allowed(),
|
|
651
|
+
)
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/// Parse a `<percentage>` value.
|
|
655
|
+
fn parse_percentage(
|
|
656
|
+
context: &ParserContext,
|
|
657
|
+
input: &mut Parser,
|
|
658
|
+
allow_none: bool,
|
|
659
|
+
allowed_channel_keywords: ChannelKeyword,
|
|
660
|
+
) -> Result<ColorComponent<NumberOrPercentageComponent>, ParseError> {
|
|
661
|
+
let value = ColorComponent::<NumberOrPercentageComponent>::parse(
|
|
662
|
+
context,
|
|
663
|
+
input,
|
|
664
|
+
allow_none,
|
|
665
|
+
allowed_channel_keywords,
|
|
666
|
+
PercentageContext::allowed_with_hint(NumericBaseType::Percent),
|
|
667
|
+
)?;
|
|
668
|
+
if !value.could_be_percentage() {
|
|
669
|
+
return Err(ParseError::custom(StyleParseErrorKind::UnspecifiedError));
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
Ok(value)
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/// Parse a `<number>` value.
|
|
676
|
+
fn parse_number(
|
|
677
|
+
context: &ParserContext,
|
|
678
|
+
input: &mut Parser,
|
|
679
|
+
allow_none: bool,
|
|
680
|
+
allowed_channel_keywords: ChannelKeyword,
|
|
681
|
+
) -> Result<ColorComponent<NumberOrPercentageComponent>, ParseError> {
|
|
682
|
+
let value = ColorComponent::<NumberOrPercentageComponent>::parse(
|
|
683
|
+
context,
|
|
684
|
+
input,
|
|
685
|
+
allow_none,
|
|
686
|
+
allowed_channel_keywords,
|
|
687
|
+
PercentageContext::not_allowed(),
|
|
688
|
+
)?;
|
|
689
|
+
|
|
690
|
+
if !value.could_be_number() {
|
|
691
|
+
return Err(ParseError::custom(StyleParseErrorKind::UnspecifiedError));
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
Ok(value)
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/// Parse a `<number>` or `<percentage>` value.
|
|
698
|
+
fn parse_number_or_percentage(
|
|
699
|
+
context: &ParserContext,
|
|
700
|
+
input: &mut Parser,
|
|
701
|
+
allow_none: bool,
|
|
702
|
+
allowed_channel_keywords: ChannelKeyword,
|
|
703
|
+
) -> Result<ColorComponent<NumberOrPercentageComponent>, ParseError> {
|
|
704
|
+
ColorComponent::parse(
|
|
705
|
+
context,
|
|
706
|
+
input,
|
|
707
|
+
allow_none,
|
|
708
|
+
allowed_channel_keywords,
|
|
709
|
+
PercentageContext::allowed_with_hint(NumericBaseType::Percent),
|
|
710
|
+
)
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
fn parse_legacy_alpha(
|
|
714
|
+
context: &ParserContext,
|
|
715
|
+
arguments: &mut Parser,
|
|
716
|
+
) -> Result<ColorComponent<NumberOrPercentageComponent>, ParseError> {
|
|
717
|
+
if !arguments.is_exhausted() {
|
|
718
|
+
arguments.expect_comma()?;
|
|
719
|
+
parse_number_or_percentage(context, arguments, false, ChannelKeyword::empty())
|
|
720
|
+
} else {
|
|
721
|
+
Ok(ColorComponent::AlphaOmitted)
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
fn parse_modern_alpha(
|
|
726
|
+
context: &ParserContext,
|
|
727
|
+
arguments: &mut Parser,
|
|
728
|
+
allowed_channel_keywords: ChannelKeyword,
|
|
729
|
+
) -> Result<ColorComponent<NumberOrPercentageComponent>, ParseError> {
|
|
730
|
+
if !arguments.is_exhausted() {
|
|
731
|
+
arguments.expect_delim('/')?;
|
|
732
|
+
parse_number_or_percentage(context, arguments, true, allowed_channel_keywords)
|
|
733
|
+
} else {
|
|
734
|
+
Ok(ColorComponent::AlphaOmitted)
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
impl ColorComponent<NumberOrPercentageComponent> {
|
|
739
|
+
/// Return true if the value contained inside is/can resolve to a number.
|
|
740
|
+
/// Also returns false if the node is invalid somehow.
|
|
741
|
+
fn could_be_number(&self) -> bool {
|
|
742
|
+
match self {
|
|
743
|
+
Self::None | Self::AlphaOmitted => true,
|
|
744
|
+
Self::Value(value) => matches!(value, NumberOrPercentageComponent::Number { .. }),
|
|
745
|
+
Self::ChannelKeyword(_) => {
|
|
746
|
+
// Channel keywords always resolve to numbers.
|
|
747
|
+
true
|
|
748
|
+
},
|
|
749
|
+
Self::Calc(node) => node
|
|
750
|
+
.numeric_type_as_calc_type()
|
|
751
|
+
.is_ok_and(|ty| ty == CalcType::Number),
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/// Return true if the value contained inside is/can resolve to a percentage.
|
|
756
|
+
/// Also returns false if the node is invalid somehow.
|
|
757
|
+
fn could_be_percentage(&self) -> bool {
|
|
758
|
+
match self {
|
|
759
|
+
Self::None | Self::AlphaOmitted => true,
|
|
760
|
+
Self::Value(value) => matches!(value, NumberOrPercentageComponent::Percentage { .. }),
|
|
761
|
+
Self::ChannelKeyword(_) => {
|
|
762
|
+
// Channel keywords always resolve to numbers.
|
|
763
|
+
false
|
|
764
|
+
},
|
|
765
|
+
Self::Calc(node) => node
|
|
766
|
+
.numeric_type_as_calc_type()
|
|
767
|
+
.is_ok_and(|ty| ty == CalcType::Percentage),
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|