summaryrefslogtreecommitdiff
path: root/usvg/src/preproc/fix_recursive_links.rs
blob: c78319b0772ad00977c2a76e584a54100717e75f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use super::prelude::*;


pub fn fix_recursive_links(doc: &Document) {
    fix_pattern(doc);
    fix_func_iri(doc, EId::ClipPath, AId::ClipPath);
    fix_func_iri(doc, EId::Mask, AId::Mask);
    fix_func_iri(doc, EId::Filter, AId::Filter);
}

fn fix_pattern(doc: &Document) {
    for pattern_node in doc.root().descendants().filter(|n| n.is_tag_name(EId::Pattern)) {
        for mut node in pattern_node.descendants() {
            let mut check_attr = |aid: AId| {
                let av = node.attributes().get_value(aid).cloned();
                if let Some(AValue::Paint(link, _)) = av {
                    if link == pattern_node {
                        // If a pattern child has a link to the pattern itself
                        // then we have to replace it with `none`.
                        // Otherwise we will get endless loop/recursion and stack overflow.
                        node.set_attribute((aid, AValue::None));
                    } else {
                        // Check that linked node children doesn't link this pattern.
                        for node2 in link.descendants() {
                            let av2 = node2.attributes().get_value(aid).cloned();
                            if let Some(AValue::Paint(link2, _)) = av2 {
                                if link2 == pattern_node {
                                    node.set_attribute((aid, AValue::None));
                                }
                            }
                        }
                    }
                }
            };

            check_attr(AId::Fill);
            check_attr(AId::Stroke);
        }
    }
}

fn fix_func_iri(doc: &Document, eid: EId, aid: AId) {
    for node in doc.root().descendants().filter(|n| n.is_tag_name(eid)) {
        for mut child in node.descendants() {
            let av = child.attributes().get_value(aid).cloned();
            if let Some(AValue::FuncLink(link)) = av {
                if link == node {
                    // If a mask child has a link to the mask itself
                    // then we have to replace it with `none`.
                    // Otherwise we will get endless loop/recursion and stack overflow.
                    child.remove_attribute(aid);
                } else {
                    // Check that linked node children doesn't link this mask.
                    for mut node2 in link.descendants() {
                        let av2 = node2.attributes().get_value(aid).cloned();
                        if let Some(AValue::FuncLink(link2)) = av2 {
                            if link2 == node {
                                node2.remove_attribute(aid);
                            }
                        }
                    }
                }
            }
        }
    }
}