mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
style: Separate out some of the declaration precedence components from @viewport rules
Differential Revision: https://phabricator.services.mozilla.com/D97817
This commit is contained in:
parent
55e9d8c214
commit
eb257a4dc3
3 changed files with 73 additions and 57 deletions
70
components/style/stylesheets/cascading_at_rule.rs
Normal file
70
components/style/stylesheets/cascading_at_rule.rs
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
/* 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 https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
//! Cascading at-rule types and traits
|
||||||
|
|
||||||
|
use crate::stylesheets::Origin;
|
||||||
|
use std::fmt::{self, Write};
|
||||||
|
use style_traits::{CssWriter, ToCss};
|
||||||
|
|
||||||
|
/// Computes the cascade precedence as according to
|
||||||
|
/// <http://dev.w3.org/csswg/css-cascade/#cascade-origin>
|
||||||
|
#[inline]
|
||||||
|
fn cascade_precendence(origin: Origin, important: bool) -> u8 {
|
||||||
|
match (origin, important) {
|
||||||
|
(Origin::UserAgent, true) => 1,
|
||||||
|
(Origin::User, true) => 2,
|
||||||
|
(Origin::Author, true) => 3,
|
||||||
|
(Origin::Author, false) => 4,
|
||||||
|
(Origin::User, false) => 5,
|
||||||
|
(Origin::UserAgent, false) => 6,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cascading rule descriptor implementation.
|
||||||
|
/// This is only used for at-rules which can cascade. These are @viewport and
|
||||||
|
/// @page, although we don't currently implement @page as such.
|
||||||
|
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToShmem)]
|
||||||
|
pub struct DescriptorDeclaration<T> {
|
||||||
|
/// Origin of the declaration
|
||||||
|
pub origin: Origin,
|
||||||
|
/// Declaration value
|
||||||
|
pub descriptor: T,
|
||||||
|
/// Indicates the presence of a !important property.
|
||||||
|
pub important: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> DescriptorDeclaration<T> {
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
pub fn new(origin: Origin, descriptor: T, important: bool) -> Self {
|
||||||
|
Self {
|
||||||
|
origin,
|
||||||
|
descriptor,
|
||||||
|
important,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Returns true iff self is equal or higher precedence to the other.
|
||||||
|
pub fn higher_or_equal_precendence(&self, other: &Self) -> bool {
|
||||||
|
let self_precedence = cascade_precendence(self.origin, self.important);
|
||||||
|
let other_precedence = cascade_precendence(other.origin, other.important);
|
||||||
|
|
||||||
|
self_precedence <= other_precedence
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> ToCss for DescriptorDeclaration<T>
|
||||||
|
where
|
||||||
|
T: ToCss,
|
||||||
|
{
|
||||||
|
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
|
||||||
|
where
|
||||||
|
W: Write,
|
||||||
|
{
|
||||||
|
self.descriptor.to_css(dest)?;
|
||||||
|
if self.important {
|
||||||
|
dest.write_str(" !important")?;
|
||||||
|
}
|
||||||
|
dest.write_char(';')
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
//! Style sheets and their CSS rules.
|
//! Style sheets and their CSS rules.
|
||||||
|
|
||||||
|
mod cascading_at_rule;
|
||||||
mod counter_style_rule;
|
mod counter_style_rule;
|
||||||
mod document_rule;
|
mod document_rule;
|
||||||
mod font_face_rule;
|
mod font_face_rule;
|
||||||
|
|
|
@ -17,6 +17,7 @@ use crate::rule_cache::RuleCacheConditions;
|
||||||
use crate::shared_lock::{SharedRwLockReadGuard, StylesheetGuards, ToCssWithGuard};
|
use crate::shared_lock::{SharedRwLockReadGuard, StylesheetGuards, ToCssWithGuard};
|
||||||
use crate::str::CssStringWriter;
|
use crate::str::CssStringWriter;
|
||||||
use crate::stylesheets::{Origin, StylesheetInDocument};
|
use crate::stylesheets::{Origin, StylesheetInDocument};
|
||||||
|
use crate::stylesheets::cascading_at_rule::DescriptorDeclaration;
|
||||||
use crate::values::computed::{Context, ToComputedValue};
|
use crate::values::computed::{Context, ToComputedValue};
|
||||||
use crate::values::generics::length::LengthPercentageOrAuto;
|
use crate::values::generics::length::LengthPercentageOrAuto;
|
||||||
use crate::values::generics::NonNegative;
|
use crate::values::generics::NonNegative;
|
||||||
|
@ -225,42 +226,8 @@ struct ViewportRuleParser<'a, 'b: 'a> {
|
||||||
context: &'a ParserContext<'b>,
|
context: &'a ParserContext<'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, ToShmem)]
|
|
||||||
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
|
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub struct ViewportDescriptorDeclaration {
|
pub type ViewportDescriptorDeclaration = DescriptorDeclaration<ViewportDescriptor>;
|
||||||
pub origin: Origin,
|
|
||||||
pub descriptor: ViewportDescriptor,
|
|
||||||
pub important: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ViewportDescriptorDeclaration {
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
pub fn new(
|
|
||||||
origin: Origin,
|
|
||||||
descriptor: ViewportDescriptor,
|
|
||||||
important: bool,
|
|
||||||
) -> ViewportDescriptorDeclaration {
|
|
||||||
ViewportDescriptorDeclaration {
|
|
||||||
origin: origin,
|
|
||||||
descriptor: descriptor,
|
|
||||||
important: important,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ToCss for ViewportDescriptorDeclaration {
|
|
||||||
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
|
|
||||||
where
|
|
||||||
W: Write,
|
|
||||||
{
|
|
||||||
self.descriptor.to_css(dest)?;
|
|
||||||
if self.important {
|
|
||||||
dest.write_str(" !important")?;
|
|
||||||
}
|
|
||||||
dest.write_str(";")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_shorthand<'i, 't>(
|
fn parse_shorthand<'i, 't>(
|
||||||
context: &ParserContext,
|
context: &ParserContext,
|
||||||
|
@ -554,28 +521,6 @@ impl ToCssWithGuard for ViewportRule {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the cascade precedence as according to
|
|
||||||
/// <http://dev.w3.org/csswg/css-cascade/#cascade-origin>
|
|
||||||
fn cascade_precendence(origin: Origin, important: bool) -> u8 {
|
|
||||||
match (origin, important) {
|
|
||||||
(Origin::UserAgent, true) => 1,
|
|
||||||
(Origin::User, true) => 2,
|
|
||||||
(Origin::Author, true) => 3,
|
|
||||||
(Origin::Author, false) => 4,
|
|
||||||
(Origin::User, false) => 5,
|
|
||||||
(Origin::UserAgent, false) => 6,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ViewportDescriptorDeclaration {
|
|
||||||
fn higher_or_equal_precendence(&self, other: &ViewportDescriptorDeclaration) -> bool {
|
|
||||||
let self_precedence = cascade_precendence(self.origin, self.important);
|
|
||||||
let other_precedence = cascade_precendence(other.origin, other.important);
|
|
||||||
|
|
||||||
self_precedence <= other_precedence
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub struct Cascade {
|
pub struct Cascade {
|
||||||
declarations: Vec<Option<(usize, ViewportDescriptorDeclaration)>>,
|
declarations: Vec<Option<(usize, ViewportDescriptorDeclaration)>>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue