From 6302fe077eee7f23de74509cf8f4c9adc5f95f59 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 2 May 2022 11:42:36 +0100 Subject: [PATCH] svg size handling: Insist on having the SVG element first And restructure the parsing to look like that in bundles.rs. Signed-off-by: Ian Jackson --- src/utils.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index a37839b9..f0770f58 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -220,6 +220,7 @@ pub enum SVGSizeError { #[error("attribute {0} repeated")] AttributeRepeated(String), #[error("attribute {0} unparseable")] AttributeUnparseable(String), #[error("specifies only one of width and height")] OneOfWidthHeight, + #[error("first element is not ")] WrongFirstElement, #[error("encountered EOF before SVG element")] UnexpectedEOF, } @@ -230,17 +231,24 @@ pub fn svg_parse_size(xml: &str) -> Option> { .chain(iter::repeat_with(|| Err(SvSE::UnexpectedEOF))); use xmlparser::Token as Tk; - let mut in_svg_element = false; - let mut wh = [None; 2]; + for token in &mut tokens { match token? { Tk::ElementStart{ local, .. } => { - in_svg_element = local.eq_ignore_ascii_case("svg"); - }, + if local.eq_ignore_ascii_case("svg") { break } + else { throw!(SvSE::WrongFirstElement) } + } + _ => { }, + } + } + + let mut wh = [None; 2]; + for token in &mut tokens { + match token? { Tk::ElementEnd{..} => { - if in_svg_element { return None } + break }, - Tk::Attribute { local, value, .. } if in_svg_element => { + Tk::Attribute { local, value, .. } => { let i = if local.eq_ignore_ascii_case("width" ) { 0 } else if local.eq_ignore_ascii_case("height") { 1 } else { continue }; -- 2.30.2