(#452): Implement SvgHandle::has_element_with_id()

This is analogous to rsvg_handle_has_sub().

Fixes #452
parent 6dfee76b
Pipeline #89193 passed with stages
in 35 minutes and 46 seconds
......@@ -117,6 +117,7 @@ LIBRSVG_CRATE_SOURCES = \
librsvg_crate/build.rs \
librsvg_crate/examples/proportional.rs \
librsvg_crate/src/lib.rs \
librsvg_crate/tests/api.rs \
librsvg_crate/tests/intrinsic_dimensions.rs \
librsvg_crate/tests/render_to_viewport.rs \
librsvg_crate/tests/primitives.rs \
......
......@@ -309,6 +309,19 @@ fn url_from_file(file: &gio::File) -> Result<Url, LoadingError> {
/// [`Loader`](#struct.Loader.html).
pub struct SvgHandle(Handle);
impl SvgHandle {
/// Checks if the SVG has an element with the specified `id`.
///
/// Note that the `id` must be a plain fragment identifier like `#foo`, with
/// a leading `#` character.
///
/// The purpose of the `Err()` case in the return value is to indicate an
/// incorrectly-formatted `id` argument.
pub fn has_element_with_id(&self, id: &str) -> Result<bool, RenderingError> {
self.0.has_sub(id)
}
}
/// Can render an `SvgHandle` to a Cairo context.
pub struct CairoRenderer<'a> {
handle: &'a SvgHandle,
......
use librsvg::{DefsLookupErrorKind, HrefError, RenderingError};
mod utils;
use self::utils::load_svg;
#[test]
fn has_element_with_id_works() {
let svg = load_svg(
br#"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50">
<rect id="foo" x="10" y="10" width="30" height="30"/>
</svg>
"#,
);
assert!(svg.has_element_with_id("#foo").unwrap());
assert!(!svg.has_element_with_id("#bar").unwrap());
assert_eq!(
svg.has_element_with_id(""),
Err(RenderingError::InvalidId(DefsLookupErrorKind::HrefError(
HrefError::ParseError
)))
);
assert_eq!(
svg.has_element_with_id("not a fragment"),
Err(RenderingError::InvalidId(
DefsLookupErrorKind::CannotLookupExternalReferences
))
);
assert_eq!(
svg.has_element_with_id("notfragment#fragment"),
Err(RenderingError::InvalidId(
DefsLookupErrorKind::CannotLookupExternalReferences
))
);
}
......@@ -96,14 +96,14 @@ impl<'a> From<BasicParseError<'a>> for ValueErrorKind {
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum DefsLookupErrorKind {
HrefError(HrefError),
CannotLookupExternalReferences,
NotFound,
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum RenderingError {
Cairo(cairo::Status),
CircularReference,
......
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