From: Ian Jackson Date: Mon, 2 May 2022 10:42:36 +0000 (+0100) Subject: svg size handling: Insist on having the SVG element first X-Git-Tag: otter-1.1.0~311 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=6302fe077eee7f23de74509cf8f4c9adc5f95f59;p=otter.git 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 --- 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 };