[librsvg] CssStyles: Move two functions into methods here



commit e7e79cd9e892118befc216544c8a418acf422828
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Jan 10 17:38:23 2019 -0600

    CssStyles: Move two functions into methods here

 rsvg_internals/src/css.rs | 136 +++++++++++++++++++++++-----------------------
 rsvg_internals/src/xml.rs |  13 ++---
 2 files changed, 73 insertions(+), 76 deletions(-)
---
diff --git a/rsvg_internals/src/css.rs b/rsvg_internals/src/css.rs
index 1b17b5f9..8cb0129f 100644
--- a/rsvg_internals/src/css.rs
+++ b/rsvg_internals/src/css.rs
@@ -36,6 +36,72 @@ impl CssStyles {
         }
     }
 
+    pub fn parse(&mut self, base_url: Option<Url>, buf: &str) {
+        if buf.len() == 0 {
+            return; // libcroco doesn't like empty strings :(
+        }
+
+        unsafe {
+            let mut handler_data = DocHandlerData {
+                base_url,
+                css_styles: self,
+                selector: ptr::null_mut(),
+            };
+
+            let doc_handler = cr_doc_handler_new();
+            init_cr_doc_handler(&mut *doc_handler);
+
+            (*doc_handler).app_data = &mut handler_data as *mut _ as gpointer;
+
+            let buf_ptr = buf.as_ptr() as *mut _;
+            let buf_len = buf.len() as libc::c_ulong;
+
+            let parser = cr_parser_new_from_buf(buf_ptr, buf_len, CR_UTF_8, false.to_glib());
+            if parser.is_null() {
+                cr_doc_handler_unref(doc_handler);
+                return;
+            }
+
+            cr_parser_set_sac_handler(parser, doc_handler);
+            cr_doc_handler_unref(doc_handler);
+
+            cr_parser_set_use_core_grammar(parser, false.to_glib());
+            cr_parser_parse(parser);
+
+            cr_parser_destroy(parser);
+        }
+    }
+
+    pub fn load_css(&mut self, aurl: &AllowedUrl) -> Result<(), LoadingError> {
+        io::acquire_data(aurl, None)
+            .and_then(|data| {
+                let BinaryData {
+                    data: bytes,
+                    content_type,
+                } = data;
+
+                if content_type.as_ref().map(String::as_ref) == Some("text/css") {
+                    Ok(bytes)
+                } else {
+                    rsvg_log!("\"{}\" is not of type text/css; ignoring", aurl);
+                    Err(LoadingError::BadCss)
+                }
+            })
+            .and_then(|bytes| {
+                String::from_utf8(bytes).map_err(|_| {
+                    rsvg_log!(
+                        "\"{}\" does not contain valid UTF-8 CSS data; ignoring",
+                        aurl
+                    );
+                    LoadingError::BadCss
+                })
+            })
+            .and_then(|utf8| {
+                self.parse(Some(aurl.url().clone()), &utf8);
+                Ok(()) // FIXME: return CSS parsing errors
+            })
+    }
+
     fn define(&mut self, selector: &str, prop_name: &str, prop_value: &str, important: bool) {
         let decl_list = self
             .selectors_to_declarations
@@ -93,42 +159,6 @@ macro_rules! get_doc_handler_data {
     };
 }
 
-pub fn parse_into_css_styles(css_styles: &mut CssStyles, base_url: Option<Url>, buf: &str) {
-    if buf.len() == 0 {
-        return; // libcroco doesn't like empty strings :(
-    }
-
-    unsafe {
-        let mut handler_data = DocHandlerData {
-            base_url,
-            css_styles,
-            selector: ptr::null_mut(),
-        };
-
-        let doc_handler = cr_doc_handler_new();
-        init_cr_doc_handler(&mut *doc_handler);
-
-        (*doc_handler).app_data = &mut handler_data as *mut _ as gpointer;
-
-        let buf_ptr = buf.as_ptr() as *mut _;
-        let buf_len = buf.len() as libc::c_ulong;
-
-        let parser = cr_parser_new_from_buf(buf_ptr, buf_len, CR_UTF_8, false.to_glib());
-        if parser.is_null() {
-            cr_doc_handler_unref(doc_handler);
-            return;
-        }
-
-        cr_parser_set_sac_handler(parser, doc_handler);
-        cr_doc_handler_unref(doc_handler);
-
-        cr_parser_set_use_core_grammar(parser, false.to_glib());
-        cr_parser_parse(parser);
-
-        cr_parser_destroy(parser);
-    }
-}
-
 fn init_cr_doc_handler(handler: &mut CRDocHandler) {
     handler.import_style = Some(css_import_style);
     handler.start_selector = Some(css_start_selector);
@@ -138,38 +168,6 @@ fn init_cr_doc_handler(handler: &mut CRDocHandler) {
     handler.unrecoverable_error = Some(css_unrecoverable_error);
 }
 
-// This function just slurps CSS data from a possibly-relative href
-// and parses it.  We'll move it to a better place in the end.
-pub fn load_css(css_styles: &mut CssStyles, aurl: &AllowedUrl) -> Result<(), LoadingError> {
-    io::acquire_data(aurl, None)
-        .and_then(|data| {
-            let BinaryData {
-                data: bytes,
-                content_type,
-            } = data;
-
-            if content_type.as_ref().map(String::as_ref) == Some("text/css") {
-                Ok(bytes)
-            } else {
-                rsvg_log!("\"{}\" is not of type text/css; ignoring", aurl);
-                Err(LoadingError::BadCss)
-            }
-        })
-        .and_then(|bytes| {
-            String::from_utf8(bytes).map_err(|_| {
-                rsvg_log!(
-                    "\"{}\" does not contain valid UTF-8 CSS data; ignoring",
-                    aurl
-                );
-                LoadingError::BadCss
-            })
-        })
-        .and_then(|utf8| {
-            parse_into_css_styles(css_styles, Some(aurl.url().clone()), &utf8);
-            Ok(()) // FIXME: return CSS parsing errors
-        })
-}
-
 unsafe extern "C" fn css_import_style(
     a_this: *mut CRDocHandler,
     _a_media_list: *mut GList,
@@ -188,7 +186,7 @@ unsafe extern "C" fn css_import_style(
 
     if let Ok(aurl) = AllowedUrl::from_href(uri, handler_data.base_url.as_ref()) {
         // FIXME: handle CSS errors
-        let _ = load_css(handler_data.css_styles, &aurl);
+        let _ = handler_data.css_styles.load_css(&aurl);
     } else {
         rsvg_log!("disallowed URL \"{}\" for importing CSS", uri);
     }
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 86041bf4..f9d170c6 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -10,7 +10,7 @@ use xml_rs::{reader::XmlEvent, ParserConfig};
 use allowed_url::AllowedUrl;
 use attributes::Attribute;
 use create_node::create_node_and_register_id;
-use css::{self, CssStyles};
+use css::CssStyles;
 use defs::Defs;
 use error::LoadingError;
 use handle::{self, RsvgHandle};
@@ -225,7 +225,8 @@ impl XmlState {
                     handle::get_base_url(self.handle).as_ref(),
                 ) {
                     // FIXME: handle CSS errors
-                    let _ = css::load_css(self.css_styles.as_mut().unwrap(), &aurl);
+                    let css_styles = self.css_styles.as_mut().unwrap();
+                    let _ = css_styles.load_css(&aurl);
                 } else {
                     self.error("disallowed URL in xml-stylesheet");
                 }
@@ -287,11 +288,9 @@ impl XmlState {
         if node.get_type() == NodeType::Style {
             let css_data = node.with_impl(|style: &NodeStyle| style.get_css(&node));
 
-            css::parse_into_css_styles(
-                self.css_styles.as_mut().unwrap(),
-                handle::get_base_url(self.handle).clone(),
-                &css_data,
-            );
+            let css_styles = self.css_styles.as_mut().unwrap();
+
+            css_styles.parse(handle::get_base_url(self.handle).clone(), &css_data);
         }
 
         self.current_node = node.get_parent();


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