[librsvg] Parse <style> contents directly DocumentBuilder at the time of append_characters
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] Parse <style> contents directly DocumentBuilder at the time of append_characters
- Date: Fri, 1 Nov 2019 19:37:52 +0000 (UTC)
commit e219c3a209c6a55d6f8a765d5685e9c431de6ce0
Author: Federico Mena Quintero <federico gnome org>
Date: Fri Nov 1 13:35:31 2019 -0600
Parse <style> contents directly DocumentBuilder at the time of append_characters
That is, don't create a Chars node with the contents of the
stylesheet; just parse it directly.
rsvg_internals/src/document.rs | 13 ++++++++++++-
rsvg_internals/src/style.rs | 21 +++------------------
rsvg_internals/src/text.rs | 4 ----
rsvg_internals/src/xml.rs | 14 +-------------
4 files changed, 16 insertions(+), 36 deletions(-)
---
diff --git a/rsvg_internals/src/document.rs b/rsvg_internals/src/document.rs
index d4017565..f65e2abc 100644
--- a/rsvg_internals/src/document.rs
+++ b/rsvg_internals/src/document.rs
@@ -16,6 +16,7 @@ use crate::node::{NodeCascade, NodeData, NodeType, RsvgNode};
use crate::properties::ComputedValues;
use crate::property_bag::PropertyBag;
use crate::structure::{IntrinsicDimensions, Svg};
+use crate::style::Style;
use crate::surface_utils::shared_surface::SharedImageSurface;
use crate::text::NodeChars;
use crate::xml::xml_load_from_possibly_compressed_stream;
@@ -279,6 +280,16 @@ impl DocumentBuilder {
return;
}
+ if parent.borrow().get_type() == NodeType::Style {
+ if parent.borrow().get_impl::<Style>().is_text_css() {
+ self.parse_css(text);
+ }
+ } else {
+ self.append_chars_to_parent(text, parent);
+ }
+ }
+
+ fn append_chars_to_parent(&mut self, text: &str, parent: &mut RsvgNode) {
// When the last child is a Chars node we can coalesce
// the text and avoid screwing up the Pango layouts
let chars_node = if let Some(child) = parent
@@ -312,7 +323,7 @@ impl DocumentBuilder {
.map_err(|_| LoadingError::BadUrl)
}
- pub fn parse_css(&mut self, css_data: &str) {
+ fn parse_css(&mut self, css_data: &str) {
self.css_rules
.parse(self.load_options.base_url.as_ref(), css_data);
}
diff --git a/rsvg_internals/src/style.rs b/rsvg_internals/src/style.rs
index 07d4cb53..59247a83 100644
--- a/rsvg_internals/src/style.rs
+++ b/rsvg_internals/src/style.rs
@@ -1,8 +1,7 @@
use markup5ever::{expanded_name, local_name, namespace_url, ns};
-use crate::node::{NodeResult, NodeTrait, NodeType, RsvgNode};
+use crate::node::{NodeResult, NodeTrait, RsvgNode};
use crate::property_bag::PropertyBag;
-use crate::text::NodeChars;
/// Represents a <style> node.
///
@@ -14,7 +13,7 @@ pub struct Style {
}
impl Style {
- pub fn get_css(&self, node: &RsvgNode) -> String {
+ pub fn is_text_css(&self) -> bool {
// FIXME: See these:
//
// https://www.w3.org/TR/SVG11/styling.html#StyleElementTypeAttribute
@@ -24,21 +23,7 @@ impl Style {
// "contentStyleType" attribute of the svg element, which in turn
// defaults to "text/css".
- let have_css = self.type_.as_ref().map(|t| t == "text/css").unwrap_or(true);
-
- if have_css {
- node.children()
- .filter_map(|child| {
- if child.borrow().get_type() == NodeType::Chars {
- Some(child.borrow().get_impl::<NodeChars>().get_string())
- } else {
- None
- }
- })
- .collect::<String>()
- } else {
- "".to_string()
- }
+ self.type_.as_ref().map(|t| t == "text/css").unwrap_or(true)
}
}
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index 47657ce0..e21aad91 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -499,10 +499,6 @@ impl NodeChars {
}
}
- pub fn get_string(&self) -> String {
- self.string.borrow().clone()
- }
-
pub fn append(&self, s: &str) {
self.string.borrow_mut().push_str(s);
*self.space_normalized.borrow_mut() = None;
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 39d13fa2..fbff54e2 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -13,9 +13,8 @@ use crate::document::{Document, DocumentBuilder};
use crate::error::LoadingError;
use crate::io::{self, get_input_stream_for_loading};
use crate::limits::MAX_LOADED_ELEMENTS;
-use crate::node::{NodeType, RsvgNode};
+use crate::node::RsvgNode;
use crate::property_bag::PropertyBag;
-use crate::style::Style;
use crate::xml2_load::{ParseFromStreamError, Xml2Parser};
#[derive(Clone)]
@@ -286,18 +285,7 @@ impl XmlState {
fn element_creation_end_element(&self) {
let mut inner = self.inner.borrow_mut();
-
let node = inner.current_node.take().unwrap();
-
- if node.borrow().get_type() == NodeType::Style {
- let css_data = node.borrow().get_impl::<Style>().get_css(&node);
- inner
- .document_builder
- .as_mut()
- .unwrap()
- .parse_css(&css_data);
- }
-
inner.current_node = node.parent();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]