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-06-22 17:43:20 +03:00
|
|
|
#![cfg_attr(feature = "servo", feature(plugin))]
|
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
#[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-06-10 00:31:48 +03:00
|
|
|
use selectors::parser::SelectorParseError;
|
|
|
|
|
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"))]
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum DevicePixel {}
|
|
|
|
|
2016-10-12 10:08:37 +03:00
|
|
|
/// Opaque type stored in type-unsafe work queues for parallel layout.
|
2016-11-14 15:59:35 +03:00
|
|
|
/// Must be transmutable to and from `TNode`.
|
2016-10-12 10:08:37 +03:00
|
|
|
pub type UnsafeNode = (usize, usize);
|
|
|
|
|
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":
|
|
|
|
/// http://www.w3.org/TR/css-device-adapt/#initial-viewport
|
|
|
|
///
|
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-05-20 19:34:36 +03:00
|
|
|
pub use viewport::HasViewportPercentage;
|
2017-06-10 00:31:48 +03:00
|
|
|
|
|
|
|
/// The error type for all CSS parsing routines.
|
|
|
|
pub type ParseError<'i> = cssparser::ParseError<'i, SelectorParseError<'i, StyleParseError<'i>>>;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
/// Errors that can be encountered while parsing CSS values.
|
|
|
|
pub enum StyleParseError<'i> {
|
|
|
|
/// 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 parsing error.
|
2017-07-17 17:10:11 +03:00
|
|
|
PropertyDeclaration(PropertyDeclarationParseError<'i>),
|
2017-06-10 00:31:48 +03:00
|
|
|
/// 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-06-10 00:31:48 +03:00
|
|
|
/// A media query using a ranged expression with no value was encountered.
|
|
|
|
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-06-10 00:31:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The result of parsing a property declaration.
|
2017-07-11 04:13:44 +03:00
|
|
|
#[derive(Eq, PartialEq, Clone, Debug)]
|
2017-07-17 17:10:11 +03:00
|
|
|
pub enum PropertyDeclarationParseError<'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,
|
|
|
|
/// The property declaration contained an invalid value.
|
2017-07-24 16:27:00 +03:00
|
|
|
InvalidValue(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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<StyleParseError<'a>> for ParseError<'a> {
|
|
|
|
fn from(this: StyleParseError<'a>) -> Self {
|
|
|
|
cssparser::ParseError::Custom(SelectorParseError::Custom(this))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:10:11 +03:00
|
|
|
impl<'a> From<PropertyDeclarationParseError<'a>> for ParseError<'a> {
|
|
|
|
fn from(this: PropertyDeclarationParseError<'a>) -> Self {
|
2017-06-10 00:31:48 +03:00
|
|
|
cssparser::ParseError::Custom(SelectorParseError::Custom(StyleParseError::PropertyDeclaration(this)))
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
/// https://www.w3.org/TR/css3-values/#lengths
|
|
|
|
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).
|
|
|
|
/// https://www.w3.org/TR/SVG/coords.html#Units
|
|
|
|
const PARSING_MODE_ALLOW_UNITLESS_LENGTH = 0x01,
|
|
|
|
/// In SVG, out-of-range values are not treated as an error in parsing.
|
|
|
|
/// https://www.w3.org/TR/SVG/implnote.html#RangeClamping
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|