2015-09-04 15:46:11 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2018-12-02 21:46:06 +03:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
2015-09-04 15:46:11 +03:00
|
|
|
|
|
|
|
//! This module contains shared types and messages for use by devtools/script.
|
|
|
|
//! The traits are here instead of in script so that the devtools crate can be
|
|
|
|
//! modified independently of the rest of Servo.
|
|
|
|
|
|
|
|
#![crate_name = "style_traits"]
|
|
|
|
#![crate_type = "rlib"]
|
2016-12-31 14:19:02 +03:00
|
|
|
#![deny(unsafe_code, missing_docs)]
|
2015-09-04 15:46:11 +03:00
|
|
|
|
2016-09-02 19:10:01 +03:00
|
|
|
extern crate app_units;
|
2018-09-05 18:32:36 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate bitflags;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate cssparser;
|
2015-09-04 15:46:11 +03:00
|
|
|
extern crate euclid;
|
servo: Merge #18938 - Replace all uses of the `heapsize` crate with `malloc_size_of` (from nnethercote:bug-1409255); r=SimonSapin
Servo currently uses `heapsize`, but Stylo/Gecko use `malloc_size_of`.
`malloc_size_of` is better -- it handles various cases that `heapsize` does not
-- so this patch changes Servo to use `malloc_size_of`.
This patch makes the following changes to the `malloc_size_of` crate.
- Adds `MallocSizeOf` trait implementations for numerous types, some built-in
(e.g. `VecDeque`), some external and Servo-only (e.g. `string_cache`).
- Makes `enclosing_size_of_op` optional, because vanilla jemalloc doesn't
support that operation.
- For `HashSet`/`HashMap`, falls back to a computed estimate when
`enclosing_size_of_op` isn't available.
- Adds an extern "C" `malloc_size_of` function that does the actual heap
measurement; this is based on the same functions from the `heapsize` crate.
This patch makes the following changes elsewhere.
- Converts all the uses of `heapsize` to instead use `malloc_size_of`.
- Disables the "heapsize"/"heap_size" feature for the external crates that
provide it.
- Removes the `HeapSizeOf` implementation from `hashglobe`.
- Adds `ignore` annotations to a few `Rc`/`Arc`, because `malloc_size_of`
doesn't derive those types, unlike `heapsize`.
<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [ ] These changes fix https://bugzilla.mozilla.org/show_bug.cgi?id=1409255
<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because testing is on the Gecko side.
<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 4c538b642e4bdfbf42c522c5a59c258a6d14546e
--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : f9a6feed1088d0b0be2b55d7f0c2ec9c594ac33b
2017-10-18 21:56:05 +03:00
|
|
|
extern crate malloc_size_of;
|
2018-09-05 18:32:36 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate malloc_size_of_derive;
|
2017-06-10 00:31:48 +03:00
|
|
|
extern crate selectors;
|
2018-09-05 18:32:36 +03:00
|
|
|
#[cfg(feature = "servo")]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde;
|
2017-10-09 17:29:18 +03:00
|
|
|
extern crate servo_arc;
|
2018-09-05 18:32:36 +03:00
|
|
|
#[cfg(feature = "servo")]
|
|
|
|
extern crate servo_atoms;
|
|
|
|
#[cfg(feature = "servo")]
|
|
|
|
extern crate servo_url;
|
2019-03-30 03:16:25 +03:00
|
|
|
extern crate to_shmem;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate to_shmem_derive;
|
2018-09-05 18:32:36 +03:00
|
|
|
#[cfg(feature = "servo")]
|
|
|
|
extern crate webrender_api;
|
|
|
|
#[cfg(feature = "servo")]
|
|
|
|
pub use webrender_api::DevicePixel;
|
2016-07-11 13:17:55 +03:00
|
|
|
|
2017-07-24 16:27:00 +03:00
|
|
|
use cssparser::{CowRcStr, Token};
|
2017-10-10 20:31:24 +03:00
|
|
|
use selectors::parser::SelectorParseErrorKind;
|
2018-09-05 18:32:36 +03:00
|
|
|
#[cfg(feature = "servo")]
|
|
|
|
use servo_atoms::Atom;
|
2017-06-10 00:31:48 +03:00
|
|
|
|
2017-07-29 15:38:23 +03:00
|
|
|
/// One hardware pixel.
|
|
|
|
///
|
|
|
|
/// This unit corresponds to the smallest addressable element of the display hardware.
|
|
|
|
#[cfg(not(feature = "servo"))]
|
2017-08-24 01:18:31 +03:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2017-07-29 15:38:23 +03:00
|
|
|
pub enum DevicePixel {}
|
|
|
|
|
2017-02-24 03:01:16 +03:00
|
|
|
/// Represents a mobile style pinch zoom factor.
|
2017-07-13 07:52:27 +03:00
|
|
|
/// TODO(gw): Once WR supports pinch zoom, use a type directly from webrender_api.
|
2017-02-24 03:01:16 +03:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2018-12-14 10:31:30 +03:00
|
|
|
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize, MallocSizeOf))]
|
2017-02-24 03:01:16 +03:00
|
|
|
pub struct PinchZoomFactor(f32);
|
|
|
|
|
|
|
|
impl PinchZoomFactor {
|
|
|
|
/// Construct a new pinch zoom factor.
|
|
|
|
pub fn new(scale: f32) -> PinchZoomFactor {
|
|
|
|
PinchZoomFactor(scale)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the pinch zoom factor as an untyped float.
|
|
|
|
pub fn get(&self) -> f32 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 13:17:55 +03:00
|
|
|
/// One CSS "px" in the coordinate system of the "initial viewport":
|
2017-10-18 00:04:10 +03:00
|
|
|
/// <http://www.w3.org/TR/css-device-adapt/#initial-viewport>
|
2016-07-11 13:17:55 +03:00
|
|
|
///
|
2017-02-24 03:01:16 +03:00
|
|
|
/// `CSSPixel` is equal to `DeviceIndependentPixel` times a "page zoom" factor controlled by the user. This is
|
2016-07-11 13:17:55 +03:00
|
|
|
/// the desktop-style "full page" zoom that enlarges content but then reflows the layout viewport
|
|
|
|
/// so it still exactly fits the visible area.
|
|
|
|
///
|
2017-02-24 03:01:16 +03:00
|
|
|
/// At the default zoom level of 100%, one `CSSPixel` is equal to one `DeviceIndependentPixel`. However, if the
|
2016-07-11 13:17:55 +03:00
|
|
|
/// document is zoomed in or out then this scale may be larger or smaller.
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
2017-02-24 03:01:16 +03:00
|
|
|
pub enum CSSPixel {}
|
2016-07-11 13:17:55 +03:00
|
|
|
|
|
|
|
// In summary, the hierarchy of pixel units and the factors to convert from one to the next:
|
|
|
|
//
|
|
|
|
// DevicePixel
|
2017-02-22 07:45:20 +03:00
|
|
|
// / hidpi_ratio => DeviceIndependentPixel
|
2017-02-24 03:01:16 +03:00
|
|
|
// / desktop_zoom => CSSPixel
|
2015-09-04 15:46:11 +03:00
|
|
|
|
2018-04-26 02:01:02 +03:00
|
|
|
pub mod specified_value_info;
|
2015-09-04 15:46:11 +03:00
|
|
|
#[macro_use]
|
|
|
|
pub mod values;
|
2017-05-20 19:34:36 +03:00
|
|
|
#[macro_use]
|
2015-09-04 15:46:11 +03:00
|
|
|
pub mod viewport;
|
2019-05-09 13:49:50 +03:00
|
|
|
pub mod owned_slice;
|
2015-11-27 01:26:08 +03:00
|
|
|
|
2018-11-01 16:09:54 +03:00
|
|
|
pub use crate::specified_value_info::{CssType, KeywordsCollectFn, SpecifiedValueInfo};
|
|
|
|
pub use crate::values::{
|
|
|
|
Comma, CommaWithSpace, CssWriter, OneOrMoreSeparated, Separator, Space, ToCss,
|
|
|
|
};
|
2017-06-10 00:31:48 +03:00
|
|
|
|
|
|
|
/// The error type for all CSS parsing routines.
|
2017-10-10 20:31:24 +03:00
|
|
|
pub type ParseError<'i> = cssparser::ParseError<'i, StyleParseErrorKind<'i>>;
|
|
|
|
|
|
|
|
/// Error in property value parsing
|
|
|
|
pub type ValueParseError<'i> = cssparser::ParseError<'i, ValueParseErrorKind<'i>>;
|
2017-06-10 00:31:48 +03:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
/// Errors that can be encountered while parsing CSS values.
|
2017-10-10 20:31:24 +03:00
|
|
|
pub enum StyleParseErrorKind<'i> {
|
2017-06-10 00:31:48 +03:00
|
|
|
/// A bad URL token in a DVB.
|
2017-07-24 16:27:00 +03:00
|
|
|
BadUrlInDeclarationValueBlock(CowRcStr<'i>),
|
2017-06-10 00:31:48 +03:00
|
|
|
/// A bad string token in a DVB.
|
2017-07-24 16:27:00 +03:00
|
|
|
BadStringInDeclarationValueBlock(CowRcStr<'i>),
|
2017-06-10 00:31:48 +03:00
|
|
|
/// Unexpected closing parenthesis in a DVB.
|
|
|
|
UnbalancedCloseParenthesisInDeclarationValueBlock,
|
|
|
|
/// Unexpected closing bracket in a DVB.
|
|
|
|
UnbalancedCloseSquareBracketInDeclarationValueBlock,
|
|
|
|
/// Unexpected closing curly bracket in a DVB.
|
|
|
|
UnbalancedCloseCurlyBracketInDeclarationValueBlock,
|
|
|
|
/// A property declaration value had input remaining after successfully parsing.
|
|
|
|
PropertyDeclarationValueNotExhausted,
|
|
|
|
/// An unexpected dimension token was encountered.
|
2017-07-24 16:27:00 +03:00
|
|
|
UnexpectedDimension(CowRcStr<'i>),
|
2017-09-08 19:14:51 +03:00
|
|
|
/// Missing or invalid media feature name.
|
|
|
|
MediaQueryExpectedFeatureName(CowRcStr<'i>),
|
|
|
|
/// Missing or invalid media feature value.
|
|
|
|
MediaQueryExpectedFeatureValue,
|
2018-06-21 20:35:35 +03:00
|
|
|
/// A media feature range operator was not expected.
|
|
|
|
MediaQueryUnexpectedOperator,
|
2017-09-08 19:14:51 +03:00
|
|
|
/// min- or max- properties must have a value.
|
2017-06-10 00:31:48 +03:00
|
|
|
RangedExpressionWithNoValue,
|
|
|
|
/// A function was encountered that was not expected.
|
2017-07-24 16:27:00 +03:00
|
|
|
UnexpectedFunction(CowRcStr<'i>),
|
2017-06-10 00:31:48 +03:00
|
|
|
/// @namespace must be before any rule but @charset and @import
|
|
|
|
UnexpectedNamespaceRule,
|
|
|
|
/// @import must be before any rule but @charset
|
|
|
|
UnexpectedImportRule,
|
|
|
|
/// Unexpected @charset rule encountered.
|
|
|
|
UnexpectedCharsetRule,
|
|
|
|
/// Unsupported @ rule
|
2017-07-24 16:27:00 +03:00
|
|
|
UnsupportedAtRule(CowRcStr<'i>),
|
2017-06-10 00:31:48 +03:00
|
|
|
/// A placeholder for many sources of errors that require more specific variants.
|
|
|
|
UnspecifiedError,
|
2017-07-11 04:13:44 +03:00
|
|
|
/// An unexpected token was found within a namespace rule.
|
|
|
|
UnexpectedTokenWithinNamespace(Token<'i>),
|
2017-07-31 23:50:14 +03:00
|
|
|
/// An error was encountered while parsing a property value.
|
2017-10-10 20:31:24 +03:00
|
|
|
ValueError(ValueParseErrorKind<'i>),
|
|
|
|
/// An error was encountered while parsing a selector
|
|
|
|
SelectorError(SelectorParseErrorKind<'i>),
|
2017-06-10 00:31:48 +03:00
|
|
|
|
|
|
|
/// The property declaration was for an unknown property.
|
2017-07-24 16:27:00 +03:00
|
|
|
UnknownProperty(CowRcStr<'i>),
|
2017-07-17 17:10:11 +03:00
|
|
|
/// An unknown vendor-specific identifier was encountered.
|
|
|
|
UnknownVendorProperty,
|
2017-06-10 00:31:48 +03:00
|
|
|
/// The property declaration was for a disabled experimental property.
|
|
|
|
ExperimentalProperty,
|
2017-10-10 20:31:24 +03:00
|
|
|
/// The property declaration contained an invalid color value.
|
|
|
|
InvalidColor(CowRcStr<'i>, Token<'i>),
|
|
|
|
/// The property declaration contained an invalid filter value.
|
|
|
|
InvalidFilter(CowRcStr<'i>, Token<'i>),
|
2017-06-10 00:31:48 +03:00
|
|
|
/// The property declaration contained an invalid value.
|
2017-10-10 20:31:24 +03:00
|
|
|
OtherInvalidValue(CowRcStr<'i>),
|
2017-06-10 00:31:48 +03:00
|
|
|
/// The declaration contained an animation property, and we were parsing
|
|
|
|
/// this as a keyframe block (so that property should be ignored).
|
|
|
|
///
|
|
|
|
/// See: https://drafts.csswg.org/css-animations/#keyframes
|
|
|
|
AnimationPropertyInKeyframeBlock,
|
|
|
|
/// The property is not allowed within a page rule.
|
|
|
|
NotAllowedInPageRule,
|
|
|
|
}
|
|
|
|
|
2017-10-10 20:31:24 +03:00
|
|
|
impl<'i> From<ValueParseErrorKind<'i>> for StyleParseErrorKind<'i> {
|
|
|
|
fn from(this: ValueParseErrorKind<'i>) -> Self {
|
|
|
|
StyleParseErrorKind::ValueError(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'i> From<SelectorParseErrorKind<'i>> for StyleParseErrorKind<'i> {
|
|
|
|
fn from(this: SelectorParseErrorKind<'i>) -> Self {
|
|
|
|
StyleParseErrorKind::SelectorError(this)
|
2017-06-10 00:31:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 20:31:24 +03:00
|
|
|
/// Specific errors that can be encountered while parsing property values.
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum ValueParseErrorKind<'i> {
|
|
|
|
/// An invalid token was encountered while parsing a color value.
|
|
|
|
InvalidColor(Token<'i>),
|
|
|
|
/// An invalid filter value was encountered.
|
|
|
|
InvalidFilter(Token<'i>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'i> StyleParseErrorKind<'i> {
|
|
|
|
/// Create an InvalidValue parse error
|
2018-06-04 21:57:49 +03:00
|
|
|
pub fn new_invalid<S>(name: S, value_error: ParseError<'i>) -> ParseError<'i>
|
|
|
|
where
|
|
|
|
S: Into<CowRcStr<'i>>,
|
|
|
|
{
|
|
|
|
let name = name.into();
|
2017-10-10 20:31:24 +03:00
|
|
|
let variant = match value_error.kind {
|
2018-09-05 18:32:36 +03:00
|
|
|
cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => match e {
|
|
|
|
ValueParseErrorKind::InvalidColor(token) => {
|
|
|
|
StyleParseErrorKind::InvalidColor(name, token)
|
|
|
|
},
|
|
|
|
ValueParseErrorKind::InvalidFilter(token) => {
|
|
|
|
StyleParseErrorKind::InvalidFilter(name, token)
|
|
|
|
},
|
|
|
|
},
|
2017-10-10 20:31:24 +03:00
|
|
|
_ => StyleParseErrorKind::OtherInvalidValue(name),
|
|
|
|
};
|
|
|
|
cssparser::ParseError {
|
|
|
|
kind: cssparser::ParseErrorKind::Custom(variant),
|
|
|
|
location: value_error.location,
|
|
|
|
}
|
2017-06-10 00:31:48 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-14 05:25:09 +03:00
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
/// The mode to use when parsing values.
|
2017-10-31 02:25:45 +03:00
|
|
|
pub struct ParsingMode: u8 {
|
|
|
|
/// In CSS; lengths must have units, except for zero values, where the unit can be omitted.
|
2017-10-18 00:04:10 +03:00
|
|
|
/// <https://www.w3.org/TR/css3-values/#lengths>
|
2017-10-31 02:25:45 +03:00
|
|
|
const DEFAULT = 0x00;
|
|
|
|
/// In SVG; a coordinate or length value without a unit identifier (e.g., "25") is assumed
|
2017-06-14 05:25:09 +03:00
|
|
|
/// to be in user units (px).
|
2017-10-18 00:04:10 +03:00
|
|
|
/// <https://www.w3.org/TR/SVG/coords.html#Units>
|
2017-10-31 02:25:45 +03:00
|
|
|
const ALLOW_UNITLESS_LENGTH = 0x01;
|
|
|
|
/// In SVG; out-of-range values are not treated as an error in parsing.
|
2017-10-18 00:04:10 +03:00
|
|
|
/// <https://www.w3.org/TR/SVG/implnote.html#RangeClamping>
|
2017-10-31 02:25:45 +03:00
|
|
|
const ALLOW_ALL_NUMERIC_VALUES = 0x02;
|
2017-06-14 05:25:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParsingMode {
|
|
|
|
/// Whether the parsing mode allows unitless lengths for non-zero values to be intpreted as px.
|
2018-02-16 22:43:50 +03:00
|
|
|
#[inline]
|
2017-06-14 05:25:09 +03:00
|
|
|
pub fn allows_unitless_lengths(&self) -> bool {
|
2017-10-31 02:25:45 +03:00
|
|
|
self.intersects(ParsingMode::ALLOW_UNITLESS_LENGTH)
|
2017-06-14 05:25:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether the parsing mode allows all numeric values.
|
2018-02-16 22:43:50 +03:00
|
|
|
#[inline]
|
2017-06-14 05:25:09 +03:00
|
|
|
pub fn allows_all_numeric_values(&self) -> bool {
|
2017-10-31 02:25:45 +03:00
|
|
|
self.intersects(ParsingMode::ALLOW_ALL_NUMERIC_VALUES)
|
2017-06-14 05:25:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 21:13:26 +03:00
|
|
|
#[cfg(feature = "servo")]
|
|
|
|
/// Speculatively execute paint code in the worklet thread pool.
|
|
|
|
pub trait SpeculativePainter: Send + Sync {
|
2017-10-18 00:04:10 +03:00
|
|
|
/// <https://drafts.css-houdini.org/css-paint-api/#draw-a-paint-image>
|
2018-09-05 18:32:36 +03:00
|
|
|
fn speculatively_draw_a_paint_image(
|
|
|
|
&self,
|
|
|
|
properties: Vec<(Atom, String)>,
|
|
|
|
arguments: Vec<String>,
|
|
|
|
);
|
2017-07-31 21:13:26 +03:00
|
|
|
}
|