Rename Reference to Href

That's really what it is.
parent 383a44d0
......@@ -33,15 +33,11 @@ impl Defs {
/// This may return a node within the same RSVG handle, or a node in a secondary RSVG
/// handle that is referenced by the current one. If the element's id is not found,
/// returns `None`.
pub fn lookup(
&mut self,
handle: *const RsvgHandle,
reference: &Reference,
) -> Option<&Rc<Node>> {
pub fn lookup(&mut self, handle: *const RsvgHandle, reference: &Href) -> Option<&Rc<Node>> {
match reference {
Reference::PlainUri(_) => None,
Reference::FragmentId(ref fragment) => self.nodes.get(fragment),
Reference::UriWithFragmentId(ref href, ref fragment) => {
Href::PlainUri(_) => None,
Href::FragmentId(ref fragment) => self.nodes.get(fragment),
Href::UriWithFragmentId(ref href, ref fragment) => {
match self.get_extern_handle(handle, href) {
Ok(extern_handle) => handle::get_defs(extern_handle).nodes.get(fragment),
Err(()) => None,
......@@ -76,15 +72,15 @@ impl Defs {
/// like `href="#foo"` as a reference to an SVG element in the same file as the one being
/// processes. This enum makes that distinction.
#[derive(Debug, PartialEq)]
pub enum Reference {
pub enum Href {
PlainUri(String),
FragmentId(String),
UriWithFragmentId(String, String),
}
/// Errors returned when creating a `Reference` out of an href
/// Errors returned when creating an `Href` out of a string
#[derive(Debug, PartialEq)]
pub enum ReferenceError {
pub enum HrefError {
/// The href is an invalid URI or has empty components.
ParseError,
......@@ -100,14 +96,14 @@ pub enum ReferenceError {
FragmentRequired,
}
impl Reference {
/// Parses an href into a Reference, or returns an error
impl Href {
/// Parses a string into an Href, or returns an error
///
/// An href can come from an `xlink:href` attribute in an SVG
/// element. This function determines if the provided href is a
/// plain absolute or relative URL ("`foo.png`"), or one with a
/// fragment identifier ("`foo.svg#bar`").
pub fn parse(href: &str) -> Result<Reference, ReferenceError> {
pub fn parse(href: &str) -> Result<Href, HrefError> {
let (uri, fragment) = match href.rfind('#') {
None => (Some(href), None),
Some(p) if p == 0 => (None, Some(&href[1..])),
......@@ -115,30 +111,30 @@ impl Reference {
};
match (uri, fragment) {
(None, Some(f)) if f.len() == 0 => Err(ReferenceError::ParseError),
(None, Some(f)) => Ok(Reference::FragmentId(f.to_string())),
(Some(u), _) if u.len() == 0 => Err(ReferenceError::ParseError),
(Some(u), None) => Ok(Reference::PlainUri(u.to_string())),
(Some(_u), Some(f)) if f.len() == 0 => Err(ReferenceError::ParseError),
(Some(u), Some(f)) => Ok(Reference::UriWithFragmentId(u.to_string(), f.to_string())),
(_, _) => Err(ReferenceError::ParseError),
(None, Some(f)) if f.len() == 0 => Err(HrefError::ParseError),
(None, Some(f)) => Ok(Href::FragmentId(f.to_string())),
(Some(u), _) if u.len() == 0 => Err(HrefError::ParseError),
(Some(u), None) => Ok(Href::PlainUri(u.to_string())),
(Some(_u), Some(f)) if f.len() == 0 => Err(HrefError::ParseError),
(Some(u), Some(f)) => Ok(Href::UriWithFragmentId(u.to_string(), f.to_string())),
(_, _) => Err(HrefError::ParseError),
}
}
pub fn without_fragment(href: &str) -> Result<Reference, ReferenceError> {
use self::Reference::*;
pub fn without_fragment(href: &str) -> Result<Href, HrefError> {
use self::Href::*;
match Reference::parse(href)? {
match Href::parse(href)? {
r @ PlainUri(_) => Ok(r),
FragmentId(_) | UriWithFragmentId(_, _) => Err(ReferenceError::FragmentForbidden),
FragmentId(_) | UriWithFragmentId(_, _) => Err(HrefError::FragmentForbidden),
}
}
pub fn with_fragment(href: &str) -> Result<Reference, ReferenceError> {
use self::Reference::*;
pub fn with_fragment(href: &str) -> Result<Href, HrefError> {
use self::Href::*;
match Reference::parse(href)? {
PlainUri(_) => Err(ReferenceError::FragmentRequired),
match Href::parse(href)? {
PlainUri(_) => Err(HrefError::FragmentRequired),
r @ FragmentId(_) => Ok(r),
r @ UriWithFragmentId(_, _) => Ok(r),
}
......@@ -167,7 +163,7 @@ pub extern "C" fn rsvg_defs_lookup(
let defs = unsafe { &mut *(defs as *mut Defs) };
let name = unsafe { utf8_cstr(name) };
let r = Reference::parse(name);
let r = Href::parse(name);
if r.is_err() {
return ptr::null();
}
......@@ -185,59 +181,56 @@ mod tests {
#[test]
fn parse() {
assert_eq!(
Reference::parse("uri").unwrap(),
Reference::PlainUri("uri".to_string())
Href::parse("uri").unwrap(),
Href::PlainUri("uri".to_string())
);
assert_eq!(
Reference::parse("#fragment").unwrap(),
Reference::FragmentId("fragment".to_string())
Href::parse("#fragment").unwrap(),
Href::FragmentId("fragment".to_string())
);
assert_eq!(
Reference::parse("uri#fragment").unwrap(),
Reference::UriWithFragmentId("uri".to_string(), "fragment".to_string())
Href::parse("uri#fragment").unwrap(),
Href::UriWithFragmentId("uri".to_string(), "fragment".to_string())
);
}
#[test]
fn parse_errors() {
assert_eq!(Reference::parse(""), Err(ReferenceError::ParseError));
assert_eq!(Reference::parse("#"), Err(ReferenceError::ParseError));
assert_eq!(Reference::parse("uri#"), Err(ReferenceError::ParseError));
assert_eq!(Href::parse(""), Err(HrefError::ParseError));
assert_eq!(Href::parse("#"), Err(HrefError::ParseError));
assert_eq!(Href::parse("uri#"), Err(HrefError::ParseError));
}
#[test]
fn without_fragment() {
assert_eq!(
Reference::without_fragment("uri").unwrap(),
Reference::PlainUri("uri".to_string())
Href::without_fragment("uri").unwrap(),
Href::PlainUri("uri".to_string())
);
assert_eq!(
Reference::without_fragment("#foo"),
Err(ReferenceError::FragmentForbidden)
Href::without_fragment("#foo"),
Err(HrefError::FragmentForbidden)
);
assert_eq!(
Reference::without_fragment("uri#foo"),
Err(ReferenceError::FragmentForbidden)
Href::without_fragment("uri#foo"),
Err(HrefError::FragmentForbidden)
);
}
#[test]
fn with_fragment() {
assert_eq!(
Reference::with_fragment("#foo").unwrap(),
Reference::FragmentId("foo".to_string())
Href::with_fragment("#foo").unwrap(),
Href::FragmentId("foo".to_string())
);
assert_eq!(
Reference::with_fragment("uri#foo").unwrap(),
Reference::UriWithFragmentId("uri".to_string(), "foo".to_string())
Href::with_fragment("uri#foo").unwrap(),
Href::UriWithFragmentId("uri".to_string(), "foo".to_string())
);
assert_eq!(
Reference::with_fragment("uri"),
Err(ReferenceError::FragmentRequired)
);
assert_eq!(Href::with_fragment("uri"), Err(HrefError::FragmentRequired));
}
}
......@@ -13,7 +13,7 @@ use std::rc::{Rc, Weak};
use bbox::BoundingBox;
use clip_path::{ClipPathUnits, NodeClipPath};
use coord_units::CoordUnits;
use defs::{Defs, Reference, RsvgDefs};
use defs::{Defs, Href, RsvgDefs};
use error::RenderingError;
use filters;
use float_eq_cairo::ApproxEqCairo;
......@@ -292,9 +292,9 @@ impl<'a> DrawingCtx<'a> {
// acquire it again. If you acquire a node "#foo" and don't release it before
// trying to acquire "foo" again, you will obtain a %NULL the second time.
pub fn get_acquired_node(&mut self, url: &str) -> Option<AcquiredNode> {
let reference = Reference::parse(url).ok()?;
let href = Href::parse(url).ok()?;
if let Some(node) = self.defs.borrow_mut().lookup(self.handle, &reference) {
if let Some(node) = self.defs.borrow_mut().lookup(self.handle, &href) {
if !self.acquired_nodes_contains(node) {
self.acquired_nodes.borrow_mut().push(node.clone());
let acq = AcquiredNode(self.acquired_nodes.clone(), node.clone());
......
......@@ -5,7 +5,7 @@ use cairo::{self, ImageSurface, MatrixTrait, PatternTrait};
use aspect_ratio::AspectRatio;
use attributes::Attribute;
use defs::Reference;
use defs::Href;
use drawing_ctx::DrawingCtx;
use error::RenderingError;
use handle::{self, RsvgHandle};
......@@ -210,10 +210,8 @@ impl Filter for Image {
let bounds_builder = self.base.get_bounds(ctx);
let bounds = bounds_builder.into_irect(draw_ctx);
let output_surface = match Reference::parse(href).map_err(|_| FilterError::InvalidInput)? {
Reference::PlainUri(_) => {
self.render_external_image(ctx, draw_ctx, bounds_builder, href)?
}
let output_surface = match Href::parse(href).map_err(|_| FilterError::InvalidInput)? {
Href::PlainUri(_) => self.render_external_image(ctx, draw_ctx, bounds_builder, href)?,
_ => self.render_node(ctx, draw_ctx, bounds, href)?,
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment