diff options
author | Federico Mena Quintero <federico@gnome.org> | 2022-08-29 14:05:20 -0500 |
---|---|---|
committer | Federico Mena Quintero <federico@gnome.org> | 2022-08-29 14:05:20 -0500 |
commit | b1d6ddccebb36359ae01461a6bb9af464f9b6069 (patch) | |
tree | 563e717bf491c5261c3b1bbf96aaa8a59b67e4b5 | |
parent | c8a23dfb6d63ca7d4278efdd3dc10ab96532bb99 (diff) | |
download | librsvg-b1d6ddccebb36359ae01461a6bb9af464f9b6069.tar.gz |
Stylesheet::from_href - take an AllowedUrl instead of validating it here
Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/738>
-rw-r--r-- | src/css.rs | 28 | ||||
-rw-r--r-- | src/document.rs | 14 |
2 files changed, 23 insertions, 19 deletions
@@ -750,7 +750,7 @@ pub enum Origin { Author, } -/// A parsed CSS stylesheet +/// A parsed CSS stylesheet. pub struct Stylesheet { origin: Origin, qualified_rules: Vec<QualifiedRule>, @@ -802,6 +802,10 @@ impl Stylesheet { } } + /// Parses a new stylesheet from CSS data in a string. + /// + /// The `url_resolver_url` is required for `@import` rules, so that librsvg can determine if + /// the requested path is allowed. pub fn from_data( buf: &str, url_resolver: &UrlResolver, @@ -809,29 +813,29 @@ impl Stylesheet { session: Session, ) -> Result<Self, LoadingError> { let mut stylesheet = Stylesheet::empty(origin); - stylesheet.parse(buf, url_resolver, session)?; + stylesheet.add_rules_from_string(buf, url_resolver, session)?; Ok(stylesheet) } + /// Parses a new stylesheet by loading CSS data from a URL. pub fn from_href( - href: &str, - url_resolver: &UrlResolver, + aurl: &AllowedUrl, origin: Origin, session: Session, ) -> Result<Self, LoadingError> { let mut stylesheet = Stylesheet::empty(origin); - let aurl = url_resolver - .resolve_href(href) - .map_err(|_| LoadingError::BadUrl)?; stylesheet.load(&aurl, session)?; Ok(stylesheet) } - /// Parses a CSS stylesheet from a string + /// Parses the CSS rules in `buf` and appends them to the stylesheet. + /// + /// The `url_resolver_url` is required for `@import` rules, so that librsvg can determine if + /// the requested path is allowed. /// - /// The `base_url` is required for `@import` rules, so that librsvg - /// can determine if the requested path is allowed. - fn parse( + /// If there is an `@import` rule, its rules will be recursively added into the + /// stylesheet, in the order in which they appear. + fn add_rules_from_string( &mut self, buf: &str, url_resolver: &UrlResolver, @@ -898,7 +902,7 @@ impl Stylesheet { }) .and_then(|utf8| { let url = (**aurl).clone(); - self.parse(&utf8, &UrlResolver::new(Some(url)), session) + self.add_rules_from_string(&utf8, &UrlResolver::new(Some(url)), session) }) } diff --git a/src/document.rs b/src/document.rs index a297477b..d392adef 100644 --- a/src/document.rs +++ b/src/document.rs @@ -549,13 +549,13 @@ impl DocumentBuilder { ))); } - // FIXME: handle CSS errors - if let Ok(stylesheet) = Stylesheet::from_href( - href, - &self.load_options.url_resolver, - Origin::Author, - self.session.clone(), - ) { + let aurl = self + .load_options + .url_resolver + .resolve_href(href) + .map_err(|_| LoadingError::BadUrl)?; + + if let Ok(stylesheet) = Stylesheet::from_href(&aurl, Origin::Author, self.session.clone()) { self.stylesheets.push(stylesheet); } |