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
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
//! 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-06-22 17:43:20 +03:00
|
|
|
|
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;
|
2017-06-14 05:25:09 +03:00
|
|
|
#[macro_use] extern crate bitflags;
|
2017-02-26 13:19:32 +03:00
|
|
|
#[macro_use] extern crate cssparser;
|
2015-09-04 15:46:11 +03:00
|
|
|
extern crate euclid;
|
2016-06-22 17:43:20 +03:00
|
|
|
#[cfg(feature = "servo")] extern crate heapsize;
|
2016-11-03 05:49:08 +03:00
|
|
|
#[cfg(feature = "servo")] #[macro_use] extern crate heapsize_derive;
|
2017-09-14 10:10:40 +03:00
|
|
|
#[cfg(feature = "gecko")] extern crate malloc_size_of;
|
|
|
|
#[cfg(feature = "gecko")] #[macro_use] extern crate malloc_size_of_derive;
|
2017-06-10 00:31:48 +03:00
|
|
|
extern crate selectors;
|
2017-06-16 15:03:18 +03:00
|
|
|
#[cfg(feature = "servo")] #[macro_use] extern crate serde;
|
2017-07-29 15:38:23 +03:00
|
|
|
#[cfg(feature = "servo")] extern crate webrender_api;
|
2017-10-09 17:29:18 +03:00
|
|
|
extern crate servo_arc;
|
2017-07-31 21:13:26 +03:00
|
|
|
#[cfg(feature = "servo")] extern crate servo_atoms;
|
2017-07-29 15:38:23 +03:00
|
|
|
|
|
|
|
#[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;
|
2017-07-31 21:13:26 +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)]
|
|
|
|
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize, HeapSizeOf))]
|
|
|
|
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
|
|
|
|
2016-02-16 14:20:20 +03:00
|
|
|
pub mod cursor;
|
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;
|
2015-11-27 01:26:08 +03:00
|
|
|
|
2017-06-28 01:34:15 +03:00
|
|
|
pub use values::{Comma, CommaWithSpace, 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
|
|
|
/// Expected identifier not found.
|
|
|
|
ExpectedIdentifier(Token<'i>),
|
|
|
|
/// Missing or invalid media feature name.
|
|
|
|
MediaQueryExpectedFeatureName(CowRcStr<'i>),
|
|
|
|
/// Missing or invalid media feature value.
|
|
|
|
MediaQueryExpectedFeatureValue,
|
|
|
|
/// 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
|
|
|
|
pub fn new_invalid(name: CowRcStr<'i>, value_error: ParseError<'i>) -> ParseError<'i> {
|
|
|
|
let variant = match value_error.kind {
|
|
|
|
cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => {
|
|
|
|
match e {
|
|
|
|
ValueParseErrorKind::InvalidColor(token) => {
|
|
|
|
StyleParseErrorKind::InvalidColor(name, token)
|
|
|
|
}
|
|
|
|
ValueParseErrorKind::InvalidFilter(token) => {
|
|
|
|
StyleParseErrorKind::InvalidFilter(name, token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => 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.
|
|
|
|
pub flags 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-06-14 05:25:09 +03:00
|
|
|
const PARSING_MODE_DEFAULT = 0x00,
|
|
|
|
/// In SVG, a coordinate or length value without a unit identifier (e.g., "25") is assumed
|
|
|
|
/// to be in user units (px).
|
2017-10-18 00:04:10 +03:00
|
|
|
/// <https://www.w3.org/TR/SVG/coords.html#Units>
|
2017-06-14 05:25:09 +03:00
|
|
|
const PARSING_MODE_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-06-14 05:25:09 +03:00
|
|
|
const PARSING_MODE_ALLOW_ALL_NUMERIC_VALUES = 0x02,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParsingMode {
|
|
|
|
/// Whether the parsing mode allows unitless lengths for non-zero values to be intpreted as px.
|
|
|
|
pub fn allows_unitless_lengths(&self) -> bool {
|
|
|
|
self.intersects(PARSING_MODE_ALLOW_UNITLESS_LENGTH)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether the parsing mode allows all numeric values.
|
|
|
|
pub fn allows_all_numeric_values(&self) -> bool {
|
|
|
|
self.intersects(PARSING_MODE_ALLOW_ALL_NUMERIC_VALUES)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>
|
2017-07-31 21:13:26 +03:00
|
|
|
fn speculatively_draw_a_paint_image(&self, properties: Vec<(Atom, String)>, arguments: Vec<String>);
|
|
|
|
}
|