mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Implement MediaList for Stylo.
This commit is contained in:
parent
2175785ca6
commit
52c9f4b527
4 changed files with 121 additions and 4 deletions
|
@ -137,8 +137,15 @@ impl ToCss for Expression {
|
|||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Expression {
|
||||
fn eq(&self, other: &Expression) -> bool {
|
||||
self.feature.mName == other.feature.mName &&
|
||||
self.value == other.value && self.range == other.range
|
||||
}
|
||||
}
|
||||
|
||||
/// A resolution.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(PartialEq, Debug, Clone)]
|
||||
pub enum Resolution {
|
||||
/// Dots per inch.
|
||||
Dpi(CSSFloat),
|
||||
|
@ -200,7 +207,7 @@ unsafe fn string_from_ns_string_buffer(buffer: *const nsStringBuffer) -> String
|
|||
}
|
||||
|
||||
/// A value found or expected in a media expression.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(PartialEq, Debug, Clone)]
|
||||
pub enum MediaExpressionValue {
|
||||
/// A length.
|
||||
Length(specified::Length),
|
||||
|
|
|
@ -1627,6 +1627,34 @@ extern "C" {
|
|||
pub fn Servo_DeclarationBlock_SetTextDecorationColorOverride(declarations:
|
||||
RawServoDeclarationBlockBorrowed);
|
||||
}
|
||||
extern "C" {
|
||||
pub fn Servo_MediaList_GetText(list: RawServoMediaListBorrowed,
|
||||
result: *mut nsAString_internal);
|
||||
}
|
||||
extern "C" {
|
||||
pub fn Servo_MediaList_SetText(list: RawServoMediaListBorrowed,
|
||||
text: *const nsACString_internal);
|
||||
}
|
||||
extern "C" {
|
||||
pub fn Servo_MediaList_GetLength(list: RawServoMediaListBorrowed) -> u32;
|
||||
}
|
||||
extern "C" {
|
||||
pub fn Servo_MediaList_GetMediumAt(list: RawServoMediaListBorrowed,
|
||||
index: u32,
|
||||
result: *mut nsAString_internal)
|
||||
-> bool;
|
||||
}
|
||||
extern "C" {
|
||||
pub fn Servo_MediaList_AppendMedium(list: RawServoMediaListBorrowed,
|
||||
new_medium:
|
||||
*const nsACString_internal);
|
||||
}
|
||||
extern "C" {
|
||||
pub fn Servo_MediaList_DeleteMedium(list: RawServoMediaListBorrowed,
|
||||
old_medium:
|
||||
*const nsACString_internal)
|
||||
-> bool;
|
||||
}
|
||||
extern "C" {
|
||||
pub fn Servo_CSSSupports2(name: *const nsACString_internal,
|
||||
value: *const nsACString_internal) -> bool;
|
||||
|
|
|
@ -66,7 +66,7 @@ impl ToCss for Qualifier {
|
|||
/// A [media query][mq].
|
||||
///
|
||||
/// [mq]: https://drafts.csswg.org/mediaqueries/
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(PartialEq, Clone, Debug)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub struct MediaQuery {
|
||||
/// The qualifier for this query.
|
||||
|
@ -299,4 +299,37 @@ impl MediaList {
|
|||
pub fn is_empty(&self) -> bool {
|
||||
self.media_queries.is_empty()
|
||||
}
|
||||
|
||||
/// Append a new media query item to the media list.
|
||||
/// https://drafts.csswg.org/cssom/#dom-medialist-appendmedium
|
||||
///
|
||||
/// Returns true if added, false if fail to parse the medium string.
|
||||
pub fn append_medium(&mut self, new_medium: &str) -> bool {
|
||||
let mut parser = Parser::new(new_medium);
|
||||
let new_query = match MediaQuery::parse(&mut parser) {
|
||||
Ok(query) => query,
|
||||
Err(_) => { return false; }
|
||||
};
|
||||
// This algorithm doesn't actually matches the current spec,
|
||||
// but it matches the behavior of Gecko and Edge.
|
||||
// See https://github.com/w3c/csswg-drafts/issues/697
|
||||
self.media_queries.retain(|query| query != &new_query);
|
||||
self.media_queries.push(new_query);
|
||||
true
|
||||
}
|
||||
|
||||
/// Delete a media query from the media list.
|
||||
/// https://drafts.csswg.org/cssom/#dom-medialist-deletemedium
|
||||
///
|
||||
/// Returns true if found and deleted, false otherwise.
|
||||
pub fn delete_medium(&mut self, old_medium: &str) -> bool {
|
||||
let mut parser = Parser::new(old_medium);
|
||||
let old_query = match MediaQuery::parse(&mut parser) {
|
||||
Ok(query) => query,
|
||||
Err(_) => { return false; }
|
||||
};
|
||||
let old_len = self.media_queries.len();
|
||||
self.media_queries.retain(|query| query != &old_query);
|
||||
old_len != self.media_queries.len()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue