[librsvg: 7/10] document: move Fragment type to document
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 7/10] document: move Fragment type to document
- Date: Tue, 15 Dec 2020 00:26:54 +0000 (UTC)
commit 8833bd4b83d9619571ac8a6b6a3f95ce9c19527a
Author: Paolo Borelli <pborelli gnome org>
Date: Sat Dec 12 16:31:16 2020 +0100
document: move Fragment type to document
Fragment has nothing to do with resolving URLs, it is a helper
typo to acquire nodes in the document.
src/document.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++--
src/drawing_ctx.rs | 3 +--
src/error.rs | 2 +-
src/filter.rs | 3 +--
src/filters/image.rs | 3 +--
src/gradient.rs | 3 +--
src/handle.rs | 4 ++--
src/iri.rs | 2 +-
src/marker.rs | 3 +--
src/paint_server.rs | 3 +--
src/pattern.rs | 3 +--
src/structure.rs | 3 +--
src/text.rs | 3 +--
src/url_resolver.rs | 60 +---------------------------------------------
14 files changed, 80 insertions(+), 83 deletions(-)
---
diff --git a/src/document.rs b/src/document.rs
index 55a0a312..0fe7725e 100644
--- a/src/document.rs
+++ b/src/document.rs
@@ -6,18 +6,19 @@ use once_cell::sync::Lazy;
use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
+use std::fmt;
use std::include_str;
use std::rc::Rc;
use crate::attributes::Attributes;
use crate::css::{self, Origin, Stylesheet};
-use crate::error::{AcquireError, AllowedUrlError, LoadingError};
+use crate::error::{AcquireError, AllowedUrlError, FragmentError, LoadingError};
use crate::handle::LoadOptions;
use crate::io::{self, BinaryData};
use crate::limits;
use crate::node::{Node, NodeBorrow, NodeData};
use crate::surface_utils::shared_surface::SharedImageSurface;
-use crate::url_resolver::{AllowedUrl, Fragment, UrlResolver};
+use crate::url_resolver::{AllowedUrl, UrlResolver};
use crate::xml::xml_load_from_possibly_compressed_stream;
static UA_STYLESHEETS: Lazy<Vec<Stylesheet>> = Lazy::new(|| {
@@ -251,6 +252,46 @@ fn image_loading_error_from_cairo(status: cairo::Status, aurl: &AllowedUrl) -> L
}
}
+/// Optional URI, mandatory fragment id
+#[derive(Debug, PartialEq, Clone)]
+pub struct Fragment(Option<String>, String);
+
+impl Fragment {
+ // Outside of testing, we don't want code creating Fragments by hand;
+ // they are obtained by parsing a href string.
+ #[cfg(test)]
+ pub fn new(uri: Option<String>, fragment: String) -> Fragment {
+ Fragment(uri, fragment)
+ }
+
+ pub fn parse(href: &str) -> Result<Fragment, FragmentError> {
+ let (uri, fragment) = match href.rfind('#') {
+ None => (Some(href), None),
+ Some(p) if p == 0 => (None, Some(&href[1..])),
+ Some(p) => (Some(&href[..p]), Some(&href[(p + 1)..])),
+ };
+
+ match (uri, fragment) {
+ (u, Some(f)) if !f.is_empty() => Ok(Fragment(u.map(String::from), String::from(f))),
+ _ => Err(FragmentError::FragmentRequired),
+ }
+ }
+
+ pub fn uri(&self) -> Option<&str> {
+ self.0.as_deref()
+ }
+
+ pub fn fragment(&self) -> &str {
+ &self.1
+ }
+}
+
+impl fmt::Display for Fragment {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}#{}", self.uri().unwrap_or(""), self.fragment())
+ }
+}
+
pub struct AcquiredNode {
stack: Option<Rc<RefCell<NodeStack>>>,
node: Node,
@@ -501,3 +542,26 @@ impl DocumentBuilder {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parses_fragment() {
+ assert_eq!(
+ Fragment::parse("#foo").unwrap(),
+ Fragment::new(None, "foo".to_string())
+ );
+
+ assert_eq!(
+ Fragment::parse("uri#foo").unwrap(),
+ Fragment::new(Some("uri".to_string()), "foo".to_string())
+ );
+
+ assert!(matches!(
+ Fragment::parse("uri"),
+ Err(FragmentError::FragmentRequired)
+ ));
+ }
+}
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 8dd4b264..58b35618 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -13,7 +13,7 @@ use crate::aspect_ratio::AspectRatio;
use crate::bbox::BoundingBox;
use crate::coord_units::CoordUnits;
use crate::dasharray::Dasharray;
-use crate::document::AcquiredNodes;
+use crate::document::{AcquiredNodes, Fragment};
use crate::dpi::Dpi;
use crate::element::Element;
use crate::error::{AcquireError, ImplementationLimit, RenderingError};
@@ -40,7 +40,6 @@ use crate::surface_utils::{
};
use crate::transform::Transform;
use crate::unit_interval::UnitInterval;
-use crate::url_resolver::Fragment;
use crate::viewbox::ViewBox;
/// Holds values that are required to normalize `Length` values to a current viewport.
diff --git a/src/error.rs b/src/error.rs
index 25f0e056..53688825 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -6,10 +6,10 @@ use std::fmt;
use cssparser::{BasicParseError, BasicParseErrorKind, ParseErrorKind, ToCss};
use markup5ever::QualName;
+use crate::document::Fragment;
use crate::io::IoError;
use crate::limits;
use crate::node::Node;
-use crate::url_resolver::Fragment;
/// A short-lived error.
///
diff --git a/src/filter.rs b/src/filter.rs
index dc589d33..5fa3cbb1 100644
--- a/src/filter.rs
+++ b/src/filter.rs
@@ -6,7 +6,7 @@ use std::slice::Iter;
use crate::attributes::Attributes;
use crate::coord_units::CoordUnits;
-use crate::document::AcquiredNodes;
+use crate::document::{AcquiredNodes, Fragment};
use crate::drawing_ctx::ViewParams;
use crate::element::{Draw, Element, ElementResult, SetAttributes};
use crate::error::ValueErrorKind;
@@ -16,7 +16,6 @@ use crate::node::{Node, NodeBorrow};
use crate::parsers::{Parse, ParseValue};
use crate::properties::ComputedValues;
use crate::rect::Rect;
-use crate::url_resolver::Fragment;
/// The <filter> node.
pub struct Filter {
diff --git a/src/filters/image.rs b/src/filters/image.rs
index e99362ba..40a46453 100644
--- a/src/filters/image.rs
+++ b/src/filters/image.rs
@@ -2,7 +2,7 @@ use markup5ever::{expanded_name, local_name, namespace_url, ns};
use crate::aspect_ratio::AspectRatio;
use crate::attributes::Attributes;
-use crate::document::AcquiredNodes;
+use crate::document::{AcquiredNodes, Fragment};
use crate::drawing_ctx::DrawingCtx;
use crate::element::{ElementResult, SetAttributes};
use crate::href::{is_href, set_href};
@@ -10,7 +10,6 @@ use crate::node::{CascadedValues, Node};
use crate::parsers::ParseValue;
use crate::rect::Rect;
use crate::surface_utils::shared_surface::SharedImageSurface;
-use crate::url_resolver::Fragment;
use crate::viewbox::ViewBox;
use super::context::{FilterContext, FilterOutput, FilterResult};
diff --git a/src/gradient.rs b/src/gradient.rs
index 854b94cd..a83c9e1e 100644
--- a/src/gradient.rs
+++ b/src/gradient.rs
@@ -9,7 +9,7 @@ use std::cell::RefCell;
use crate::attributes::Attributes;
use crate::bbox::BoundingBox;
use crate::coord_units::CoordUnits;
-use crate::document::{AcquiredNodes, NodeStack};
+use crate::document::{AcquiredNodes, Fragment, NodeStack};
use crate::drawing_ctx::DrawingCtx;
use crate::element::{Draw, Element, ElementResult, SetAttributes};
use crate::error::*;
@@ -21,7 +21,6 @@ use crate::properties::ComputedValues;
use crate::property_defs::StopColor;
use crate::transform::Transform;
use crate::unit_interval::UnitInterval;
-use crate::url_resolver::Fragment;
/// Contents of a <stop> element for gradient color stops
#[derive(Copy, Clone)]
diff --git a/src/handle.rs b/src/handle.rs
index e65333cb..a44e3960 100644
--- a/src/handle.rs
+++ b/src/handle.rs
@@ -4,14 +4,14 @@
use crate::bbox::BoundingBox;
use crate::css::{Origin, Stylesheet};
-use crate::document::{AcquiredNodes, Document};
+use crate::document::{AcquiredNodes, Document, Fragment};
use crate::dpi::Dpi;
use crate::drawing_ctx::{draw_tree, DrawingMode, ViewParams};
use crate::error::{DefsLookupErrorKind, LoadingError, RenderingError};
use crate::node::{CascadedValues, Node, NodeBorrow};
use crate::rect::Rect;
use crate::structure::IntrinsicDimensions;
-use crate::url_resolver::{AllowedUrl, Fragment, UrlResolver};
+use crate::url_resolver::{AllowedUrl, UrlResolver};
/// Loading options for SVG documents.
#[derive(Clone)]
diff --git a/src/iri.rs b/src/iri.rs
index d6de265e..2a88f931 100644
--- a/src/iri.rs
+++ b/src/iri.rs
@@ -2,9 +2,9 @@
use cssparser::Parser;
+use crate::document::Fragment;
use crate::error::*;
use crate::parsers::Parse;
-use crate::url_resolver::Fragment;
/// Used where style properties take a funciri or "none"
///
diff --git a/src/marker.rs b/src/marker.rs
index 45fb9e94..7f639594 100644
--- a/src/marker.rs
+++ b/src/marker.rs
@@ -10,7 +10,7 @@ use crate::angle::Angle;
use crate::aspect_ratio::*;
use crate::attributes::Attributes;
use crate::bbox::BoundingBox;
-use crate::document::AcquiredNodes;
+use crate::document::{AcquiredNodes, Fragment};
use crate::drawing_ctx::DrawingCtx;
use crate::element::{Draw, Element, ElementResult, SetAttributes};
use crate::error::*;
@@ -23,7 +23,6 @@ use crate::path_builder::{arc_segment, ArcParameterization, CubicBezierCurve, Pa
use crate::properties::ComputedValues;
use crate::rect::Rect;
use crate::transform::Transform;
-use crate::url_resolver::Fragment;
use crate::viewbox::*;
// markerUnits attribute: https://www.w3.org/TR/SVG/painting.html#MarkerElement
diff --git a/src/paint_server.rs b/src/paint_server.rs
index 6dba1bbe..6f88424e 100644
--- a/src/paint_server.rs
+++ b/src/paint_server.rs
@@ -3,7 +3,7 @@
use cssparser::Parser;
use crate::bbox::BoundingBox;
-use crate::document::AcquiredNodes;
+use crate::document::{AcquiredNodes, Fragment};
use crate::drawing_ctx::DrawingCtx;
use crate::element::Element;
use crate::error::{
@@ -14,7 +14,6 @@ use crate::node::NodeBorrow;
use crate::parsers::Parse;
use crate::pattern::{ResolvedPattern, UserSpacePattern};
use crate::properties::ComputedValues;
-use crate::url_resolver::Fragment;
#[derive(Debug, Clone, PartialEq)]
pub enum PaintServer {
diff --git a/src/pattern.rs b/src/pattern.rs
index 92680d08..2c818e48 100644
--- a/src/pattern.rs
+++ b/src/pattern.rs
@@ -7,7 +7,7 @@ use crate::aspect_ratio::*;
use crate::attributes::Attributes;
use crate::bbox::BoundingBox;
use crate::coord_units::CoordUnits;
-use crate::document::{AcquiredNodes, NodeStack};
+use crate::document::{AcquiredNodes, Fragment, NodeStack};
use crate::drawing_ctx::DrawingCtx;
use crate::element::{Draw, Element, ElementResult, SetAttributes};
use crate::error::*;
@@ -18,7 +18,6 @@ use crate::parsers::ParseValue;
use crate::properties::ComputedValues;
use crate::rect::Rect;
use crate::transform::Transform;
-use crate::url_resolver::Fragment;
use crate::viewbox::*;
coord_units!(PatternUnits, CoordUnits::ObjectBoundingBox);
diff --git a/src/structure.rs b/src/structure.rs
index 2e0dfa89..2d6a1761 100644
--- a/src/structure.rs
+++ b/src/structure.rs
@@ -6,7 +6,7 @@ use crate::aspect_ratio::*;
use crate::attributes::Attributes;
use crate::bbox::BoundingBox;
use crate::coord_units::CoordUnits;
-use crate::document::AcquiredNodes;
+use crate::document::{AcquiredNodes, Fragment};
use crate::drawing_ctx::{ClipMode, DrawingCtx, ViewParams};
use crate::element::{Draw, ElementResult, SetAttributes};
use crate::error::*;
@@ -16,7 +16,6 @@ use crate::node::{CascadedValues, Node, NodeBorrow, NodeDraw};
use crate::parsers::{Parse, ParseValue};
use crate::properties::ComputedValues;
use crate::rect::Rect;
-use crate::url_resolver::Fragment;
use crate::viewbox::*;
#[derive(Default)]
diff --git a/src/text.rs b/src/text.rs
index e94f2020..c1ca2d67 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -5,7 +5,7 @@ use std::cell::RefCell;
use crate::attributes::Attributes;
use crate::bbox::BoundingBox;
-use crate::document::AcquiredNodes;
+use crate::document::{AcquiredNodes, Fragment};
use crate::drawing_ctx::DrawingCtx;
use crate::element::{Draw, Element, ElementResult, SetAttributes};
use crate::error::*;
@@ -19,7 +19,6 @@ use crate::property_defs::{
XmlSpace,
};
use crate::space::{xml_space_normalize, NormalizeDefault, XmlSpaceNormalize};
-use crate::url_resolver::Fragment;
/// An absolutely-positioned array of `Span`s
///
diff --git a/src/url_resolver.rs b/src/url_resolver.rs
index 305037ef..9155072d 100644
--- a/src/url_resolver.rs
+++ b/src/url_resolver.rs
@@ -6,7 +6,7 @@ use std::ops::Deref;
use std::path::{Path, PathBuf};
use url::Url;
-use crate::error::{AllowedUrlError, FragmentError};
+use crate::error::AllowedUrlError;
/// Currently only contains the base URL.
///
@@ -127,46 +127,6 @@ fn canonicalize<P: AsRef<Path>>(path: P) -> Result<PathBuf, io::Error> {
Ok(path.as_ref().to_path_buf())
}
-/// Optional URI, mandatory fragment id
-#[derive(Debug, PartialEq, Clone)]
-pub struct Fragment(Option<String>, String);
-
-impl Fragment {
- // Outside of testing, we don't want code creating Fragments by hand;
- // they are obtained by parsing a href string.
- #[cfg(test)]
- pub fn new(uri: Option<String>, fragment: String) -> Fragment {
- Fragment(uri, fragment)
- }
-
- pub fn parse(href: &str) -> Result<Fragment, FragmentError> {
- let (uri, fragment) = match href.rfind('#') {
- None => (Some(href), None),
- Some(p) if p == 0 => (None, Some(&href[1..])),
- Some(p) => (Some(&href[..p]), Some(&href[(p + 1)..])),
- };
-
- match (uri, fragment) {
- (u, Some(f)) if !f.is_empty() => Ok(Fragment(u.map(String::from), String::from(f))),
- _ => Err(FragmentError::FragmentRequired),
- }
- }
-
- pub fn uri(&self) -> Option<&str> {
- self.0.as_deref()
- }
-
- pub fn fragment(&self) -> &str {
- &self.1
- }
-}
-
-impl fmt::Display for Fragment {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "{}#{}", self.uri().unwrap_or(""), self.fragment())
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;
@@ -264,22 +224,4 @@ mod tests {
Err(AllowedUrlError::NotSiblingOrChildOfBaseFile)
));
}
-
- #[test]
- fn parses_fragment() {
- assert_eq!(
- Fragment::parse("#foo").unwrap(),
- Fragment::new(None, "foo".to_string())
- );
-
- assert_eq!(
- Fragment::parse("uri#foo").unwrap(),
- Fragment::new(Some("uri".to_string()), "foo".to_string())
- );
-
- assert!(matches!(
- Fragment::parse("uri"),
- Err(FragmentError::FragmentRequired)
- ));
- }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]