Commit 973a8651 authored by RazrFalcon's avatar RazrFalcon

Refactoring.

parent 3edb988f
a-baseline-shift-001.svg
a-baseline-shift-002.svg
a-baseline-shift-003.svg
a-baseline-shift-004.svg
a-baseline-shift-005.svg
a-baseline-shift-006.svg
a-baseline-shift-007.svg
a-baseline-shift-008.svg
a-baseline-shift-009.svg
a-baseline-shift-010.svg
a-baseline-shift-011.svg
a-baseline-shift-012.svg
a-baseline-shift-013.svg
a-baseline-shift-014.svg
a-baseline-shift-015.svg
a-baseline-shift-016.svg
a-baseline-shift-017.svg
a-baseline-shift-018.svg
a-baseline-shift-001.svg
a-baseline-shift-002.svg
a-baseline-shift-003.svg
a-baseline-shift-004.svg
a-baseline-shift-005.svg
a-baseline-shift-006.svg
a-baseline-shift-007.svg
a-baseline-shift-008.svg
a-baseline-shift-009.svg
a-baseline-shift-010.svg
a-baseline-shift-011.svg
a-baseline-shift-012.svg
a-baseline-shift-013.svg
a-baseline-shift-014.svg
a-baseline-shift-015.svg
a-baseline-shift-016.svg
a-baseline-shift-017.svg
a-baseline-shift-018.svg
......@@ -27,6 +27,7 @@ mod resolve_attrs_via_xlink;
mod resolve_children_via_xlink;
mod resolve_conditional;
mod resolve_curr_color;
mod resolve_display;
mod resolve_font_size;
mod resolve_font_weight;
mod resolve_inherit;
......@@ -37,7 +38,6 @@ mod resolve_use;
mod rm_invalid_font_size;
mod rm_invalid_gradients;
mod rm_invalid_ts;
mod rm_invisible_elems;
mod rm_non_svg_data;
mod rm_unused_defs;
mod ungroup_a;
......@@ -46,8 +46,8 @@ mod ungroup_groups;
use self::conv_units::*;
use self::fix_gradient_stops::*;
use self::fix_recursive_links::*;
use self::fix_links::*;
use self::fix_recursive_links::*;
use self::group_defs::*;
use self::prepare_clip_path::*;
use self::prepare_mask::*;
......@@ -59,6 +59,7 @@ use self::resolve_attrs_via_xlink::*;
use self::resolve_children_via_xlink::*;
use self::resolve_conditional::*;
use self::resolve_curr_color::*;
use self::resolve_display::*;
use self::resolve_font_size::*;
use self::resolve_font_weight::*;
use self::resolve_inherit::*;
......@@ -69,7 +70,6 @@ use self::resolve_use::*;
use self::rm_invalid_font_size::*;
use self::rm_invalid_gradients::*;
use self::rm_invalid_ts::*;
use self::rm_invisible_elems::*;
use self::rm_non_svg_data::*;
use self::rm_unused_defs::*;
use self::ungroup_a::*;
......@@ -188,7 +188,7 @@ pub fn prepare_doc(doc: &mut svgdom::Document, opt: &Options) {
resolve_conditional(doc, opt);
remove_invalid_transform(doc);
remove_invisible_elements(doc);
resolve_display(doc);
prepare_clip_path_children(doc);
......
......@@ -4,20 +4,7 @@
use super::prelude::*;
/// Removes well-defined, but invisible, elements.
pub fn remove_invisible_elements(doc: &mut Document) {
// TODO: process Options::keep_invisible_shapes
rm_display_none(doc);
// TODO: to a separate module.
// Since 'svgdom' automatically removes (Func)IRI attributes
// from linked elements, 'use' elements may became obsolete, because
// a 'use' element without 'xlink:href' is invalid.
rm_use(doc);
}
fn rm_display_none(doc: &mut Document) {
pub fn resolve_display(doc: &mut Document) {
let root = doc.root();
doc.drain(root, |n| {
if let Some(&AValue::None) = n.attributes().get_value(AId::Display) {
......@@ -35,35 +22,3 @@ fn rm_display_none(doc: &mut Document) {
false
});
}
fn rm_use(doc: &mut Document) {
fn _rm(doc: &mut Document) -> usize {
let root = doc.root();
doc.drain(root, |n| {
if n.is_tag_name(EId::Use) {
if !n.has_attribute(AId::Href) {
// remove 'use' element without the 'xlink:href' attribute
return true;
} else {
// remove 'use' element with invalid 'xlink:href' attribute value
let attrs = n.attributes();
if let Some(&AValue::Link(_)) = attrs.get_value(AId::Href) {
// nothing
} else {
// NOTE: actually, an attribute with 'String' type is valid
// if it contain a path to an external file, like '../img.svg#rect1',
// but we don't support external SVG, so we treat it like an invalid
return true;
}
}
}
false
})
}
// 'use' can be linked to another 'use' and if it was removed
// the first one will became invalid, so we need to check DOM again.
// Loop until there are no drained elements.
while _rm(doc) > 0 {}
}
......@@ -88,6 +88,8 @@ pub fn resolve_use(doc: &mut Document, opt: &Options) {
doc.remove_node(node.clone());
}
}
remove_invalid_use(doc);
}
fn _resolve_use(
......@@ -178,3 +180,35 @@ fn resolve_symbol(
use_node.prepend_transform(orig_ts);
}
}
fn remove_invalid_use(doc: &mut Document) {
fn _rm(doc: &mut Document) -> usize {
let root = doc.root();
doc.drain(root, |n| {
if n.is_tag_name(EId::Use) {
if !n.has_attribute(AId::Href) {
// Remove 'use' elements without an 'xlink:href' attribute.
return true;
} else {
// Remove 'use' elements with an invalid 'xlink:href' attribute.
let attrs = n.attributes();
if let Some(&AValue::Link(_)) = attrs.get_value(AId::Href) {
// Nothing.
} else {
// NOTE: actually, an attribute with 'String' type is valid
// if it contain a path to an external file, like '../img.svg#rect1',
// but we don't support external SVG, so we treat them as invalid.
return true;
}
}
}
false
})
}
// 'use' can be linked to another 'use' and if it was removed
// the first one will became invalid, so we need to check DOM again.
// Loop until there are no drained elements.
while _rm(doc) > 0 {}
}
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