1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! The `style` element.
use markup5ever::{expanded_name, local_name, ns};
use crate::element::{ElementTrait, set_attribute};
use crate::error::*;
use crate::session::Session;
use crate::xml::Attributes;
/// Represents the syntax used in the `<style>` node.
///
/// Currently only "text/css" is supported.
///
/// <https://www.w3.org/TR/SVG11/styling.html#StyleElementTypeAttribute>
/// <https://www.w3.org/TR/SVG11/styling.html#ContentStyleTypeAttribute>
#[derive(Copy, Clone, Default, PartialEq, Debug)]
pub enum StyleType {
#[default]
TextCss,
}
impl StyleType {
fn parse(value: &str) -> Result<StyleType, ValueErrorKind> {
// https://html.spec.whatwg.org/multipage/semantics.html#the-style-element
//
// 4. If element's type attribute is present and its value is
// neither the empty string nor an ASCII case-insensitive
// match for "text/css", then return.
if value.eq_ignore_ascii_case("text/css") {
Ok(StyleType::TextCss)
} else {
Err(ValueErrorKind::parse_error(
"invalid value for type attribute in style element",
))
}
}
}
/// Represents a `<style>` node.
///
/// It does not render itself, and just holds CSS stylesheet information for the rest of
/// the code to use.
#[derive(Default)]
pub struct Style {
type_: StyleType,
}
impl Style {
pub fn style_type(&self) -> StyleType {
self.type_
}
}
impl ElementTrait for Style {
fn set_attributes(&mut self, attrs: &Attributes, session: &Session) {
for (attr, value) in attrs.iter() {
if attr.expanded() == expanded_name!("", "type") {
set_attribute(
&mut self.type_,
StyleType::parse(value).attribute(attr),
session,
);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_style_type() {
assert_eq!(StyleType::parse("text/css").unwrap(), StyleType::TextCss);
}
#[test]
fn invalid_style_type_yields_error() {
assert!(StyleType::parse("").is_err());
assert!(StyleType::parse("some-other-stylesheet-language").is_err());
}
}