Commit d7e0def6 authored by Rahix's avatar Rahix 🦀
Browse files

Merge 'Add SVG size and viewBox verification'

Closes #13

See merge request !25
parents 0d9d6be5 71de06cf
......@@ -7,6 +7,7 @@ use web_sys;
use crate::colliders;
use crate::components;
use crate::utils;
// rustfmt skip because of a bug where rustfmt removes the `async` keyword ...
#[rustfmt::skip]
......@@ -53,6 +54,19 @@ impl SvgLoader {
.query_selector_all(query)
.unwrap()
}
pub fn verify_viewbox(&self) -> bool {
let svg = utils::get_element_by_id::<web_sys::Element>(&self.loading_id)
.unwrap()
.query_selector("svg")
.unwrap()
.unwrap();
let svg_width = svg.get_attribute("width").unwrap();
let svg_height = svg.get_attribute("height").unwrap();
let viewbox = svg.get_attribute("viewBox").unwrap();
let expected_viewbox = format!("0 0 {} {}", svg_width, svg_height);
viewbox == expected_viewbox
}
}
impl Drop for SvgLoader {
......@@ -155,6 +169,11 @@ pub struct SvgLevel {
impl SvgLevel {
pub async fn load_from_svg_file(url: &str) -> Result<Self, JsValue> {
let svg_loader = SvgLoader::load_svg(url).await;
if svg_loader.verify_viewbox() == false {
return Err(JsValue::from_str("SVG Viewbox and SVG size don't match"));
}
svg_loader.hide_canvas_node_by_query("g[inkscape\\:groupmode=\"layer\"]");
svg_loader.show_canvas_node_by_query(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment