[librsvg] (#452): Implement SvgHandle::has_element_with_id()



commit e116c89bf92f140ebe146f70bc83e78cb5b4ef1c
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Jun 18 12:46:59 2019 -0500

    (#452): Implement SvgHandle::has_element_with_id()
    
    This is analogous to rsvg_handle_has_sub().
    
    Fixes https://gitlab.gnome.org/GNOME/librsvg/issues/452

 Makefile.am                 |  1 +
 librsvg_crate/src/lib.rs    | 13 +++++++++++++
 librsvg_crate/tests/api.rs  | 39 +++++++++++++++++++++++++++++++++++++++
 rsvg_internals/src/error.rs |  4 ++--
 4 files changed, 55 insertions(+), 2 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 690830a3..3d69ea6a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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                       \
diff --git a/librsvg_crate/src/lib.rs b/librsvg_crate/src/lib.rs
index ee94f610..9b3c66c6 100644
--- a/librsvg_crate/src/lib.rs
+++ b/librsvg_crate/src/lib.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,
diff --git a/librsvg_crate/tests/api.rs b/librsvg_crate/tests/api.rs
new file mode 100644
index 00000000..2da279be
--- /dev/null
+++ b/librsvg_crate/tests/api.rs
@@ -0,0 +1,39 @@
+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
+        ))
+    );
+}
diff --git a/rsvg_internals/src/error.rs b/rsvg_internals/src/error.rs
index 8f9f25de..d8ad81e5 100644
--- a/rsvg_internals/src/error.rs
+++ b/rsvg_internals/src/error.rs
@@ -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,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]