Move css serialization functions to css_parser mod

This commit is contained in:
Tyler Hallada 2020-05-09 22:33:33 -04:00
parent 519c4067b7
commit 1a28b26121
2 changed files with 42 additions and 42 deletions

View File

@ -26,7 +26,7 @@ use html5ever::serialize::{Serialize, Serializer, TraversalScope};
use html5ever::tendril::StrTendril; use html5ever::tendril::StrTendril;
use html5ever::{Attribute as HTML5everAttribute, ExpandedName, LocalName, QualName}; use html5ever::{Attribute as HTML5everAttribute, ExpandedName, LocalName, QualName};
use crate::css_parser::{CssDeclaration, CssRule}; use crate::css_parser::{serialize_css_declarations, serialize_css_rules, CssDeclaration, CssRule};
pub fn create_element<'arena>(arena: Arena<'arena>, name: &str) -> Ref<'arena> { pub fn create_element<'arena>(arena: Arena<'arena>, name: &str) -> Ref<'arena> {
arena.alloc(Node::new(NodeData::Element { arena.alloc(Node::new(NodeData::Element {
@ -519,47 +519,6 @@ impl<'arena> TreeSink for Sink<'arena> {
} }
} }
fn serialize_css_rules(rules: &[CssRule]) -> String {
let mut serialized_rules = String::new();
for rule in rules {
match rule {
CssRule::StyleRule(style_rule) => {
serialized_rules += &style_rule.selectors;
serialized_rules += "{";
for declaration in style_rule.declarations.iter() {
serialized_rules += &declaration.to_string();
}
serialized_rules += &serialize_css_declarations(&style_rule.declarations);
serialized_rules += " }";
}
CssRule::AtRule(at_rule) => {
serialized_rules += "@";
serialized_rules += &at_rule.name;
serialized_rules += &at_rule.prelude;
if let Some(block) = &at_rule.block {
serialized_rules += "{";
serialized_rules += &serialize_css_rules(&block);
serialized_rules += " }";
} else {
serialized_rules += "; ";
}
}
}
}
serialized_rules
}
fn serialize_css_declarations(declarations: &[CssDeclaration]) -> String {
let mut serialized_declarations = String::new();
for (index, declaration) in declarations.iter().enumerate() {
serialized_declarations += &declaration.to_string();
if index != declarations.len() - 1 {
serialized_declarations += " ";
}
}
serialized_declarations
}
// Implementation adapted from implementation for RcDom: // Implementation adapted from implementation for RcDom:
// https://github.com/servo/html5ever/blob/45b2fca5c6/markup5ever/rcdom.rs#L410 // https://github.com/servo/html5ever/blob/45b2fca5c6/markup5ever/rcdom.rs#L410
impl<'arena> Serialize for Node<'arena> { impl<'arena> Serialize for Node<'arena> {

View File

@ -259,3 +259,44 @@ impl Into<String> for CssDeclaration {
format!("{}:{};", self.property, self.value) format!("{}:{};", self.property, self.value)
} }
} }
pub fn serialize_css_rules(rules: &[CssRule]) -> String {
let mut serialized_rules = String::new();
for rule in rules {
match rule {
CssRule::StyleRule(style_rule) => {
serialized_rules += &style_rule.selectors;
serialized_rules += "{";
for declaration in style_rule.declarations.iter() {
serialized_rules += &declaration.to_string();
}
serialized_rules += &serialize_css_declarations(&style_rule.declarations);
serialized_rules += " }";
}
CssRule::AtRule(at_rule) => {
serialized_rules += "@";
serialized_rules += &at_rule.name;
serialized_rules += &at_rule.prelude;
if let Some(block) = &at_rule.block {
serialized_rules += "{";
serialized_rules += &serialize_css_rules(&block);
serialized_rules += " }";
} else {
serialized_rules += "; ";
}
}
}
}
serialized_rules
}
pub fn serialize_css_declarations(declarations: &[CssDeclaration]) -> String {
let mut serialized_declarations = String::new();
for (index, declaration) in declarations.iter().enumerate() {
serialized_declarations += &declaration.to_string();
if index != declarations.len() - 1 {
serialized_declarations += " ";
}
}
serialized_declarations
}