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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::Document;
use crate::Selection;
use html5ever::QualName;
use html5ever::{
    tree_builder::{NoQuirks, TreeBuilderOpts},
    ParseOpts,
};
use markup5ever::local_name;
use markup5ever::{namespace_url, ns};
use tendril::StrTendril;
use tendril::TendrilSink;

macro_rules! parse_html {
    ($html: expr) => {
        html5ever::parse_fragment(
            Document::default(),
            ParseOpts {
                tokenizer: Default::default(),
                tree_builder: TreeBuilderOpts {
                    exact_errors: false,
                    scripting_enabled: true,
                    iframe_srcdoc: false,
                    drop_doctype: true,
                    ignore_missing_rules: false,
                    quirks_mode: NoQuirks,
                },
            },
            QualName::new(None, ns!(html), local_name!("")),
            Vec::new(),
        )
        .one($html)
    };
}

impl<'a> Selection<'a> {
    /// Removes the set of matched elements from the document.
    pub fn remove(&mut self) {
        for node in &self.nodes {
            node.remove_from_parent()
        }
    }

    /// Set the html contents of each element in the selection to specified parsed HTML.
    pub fn set_html<T>(&mut self, html: T)
    where
        T: Into<StrTendril>,
    {
        for node in self.nodes() {
            node.remove_children();
        }

        self.append_html(html)
    }

    /// Replaces each element in the set of matched elements with
    /// the parsed HTML.
    /// It returns the removed elements.
    ///
    /// This follows the same rules as `append`.
    pub fn replace_with_html<T>(&mut self, html: T)
    where
        T: Into<StrTendril>,
    {
        let dom = parse_html!(html);
        let mut i = 0;

        for node in self.nodes() {
            if i + 1 == self.size() {
                node.append_prev_siblings_from_another_tree(dom.tree);
                break;
            } else {
                node.append_prev_siblings_from_another_tree(dom.tree.clone());
            }
            i += 1;
        }

        self.remove()
    }

    /// Replaces each element in the set of matched element with
    /// the nodes from the given selection.
    ///
    /// This follows the same rules as `append`.
    pub fn replace_with_selection(&mut self, sel: &Selection) {
        for node in self.nodes() {
            for prev_sibling in sel.nodes() {
                node.append_prev_sibling(&prev_sibling.id);
            }
        }

        self.remove()
    }

    /// Parses the html and appends it to the set of matched elements.
    pub fn append_html<T>(&mut self, html: T)
    where
        T: Into<StrTendril>,
    {
        let dom = parse_html!(html);
        let mut i = 0;

        for node in self.nodes() {
            if i + 1 == self.size() {
                node.append_children_from_another_tree(dom.tree);
                break;
            } else {
                node.append_children_from_another_tree(dom.tree.clone());
            }
            i += 1;
        }
    }

    /// Appends the elements in the selection to the end of each element
    /// in the set of matched elements.
    pub fn append_selection(&mut self, sel: &Selection) {
        for node in self.nodes() {
            for child in sel.nodes() {
                node.append_child(&child.id);
            }
        }
    }
}