summaryrefslogtreecommitdiffstats
path: root/vendor/web-sys/tests/wasm/element.rs
blob: a408a9c7cdfa99c69aed73219c5b225b39613c97 (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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use web_sys::Element;

#[wasm_bindgen(module = "/tests/wasm/element.js")]
extern "C" {
    fn new_div() -> Element;
}

#[wasm_bindgen_test]
fn element() {
    /* Tests needed for:
      namespace_uri
    */
    let element = new_div();

    assert_eq!(element, element);

    assert_eq!(element.prefix(), None, "Shouldn't have a prefix");
    assert_eq!(element.local_name(), "div", "Should have a div local name");
    assert_eq!(element.tag_name(), "DIV", "Should be a div tag");
    assert!(!element.has_attribute("id"), "Shouldn't have an id");
    element.set_id("beep");
    assert_eq!(element.id(), "beep", "Should have an id of 'beep'");

    // must_use is set on this result?
    assert_eq!(
        element.set_attribute("id", "beep").unwrap(),
        (),
        "Should set id"
    );
    assert!(element.has_attribute("id"), "Should now have an id");
    assert_eq!(
        element.remove_attribute("id").unwrap(),
        (),
        "Should return nothing if removed"
    );

    assert_eq!(element.class_name(), "", "Shouldn't have a class name");
    element.set_class_name("test thing");
    assert_eq!(
        element.class_name(),
        "test thing",
        "Should have a class name"
    );
    assert_eq!(
        element.get_attribute("class").unwrap(),
        "test thing",
        "Should have a class name"
    );
    assert_eq!(
        element.remove_attribute("class").unwrap(),
        (),
        "Should return nothing if removed"
    );
    /* Tests needed for:
    get_attribute_ns
    */

    /*TODO should we enable toggle_attribute tests? (Firefox Nightly + Chrome canary only)
        // TODO toggle_attribute should permit a single argument when optional arguments are supported
        assert!(!element.has_attribute("disabled"), "Should not be disabled");
        assert!(element.toggle_attribute("disabled", true).unwrap(), "Should return true when attribute is set");
        assert!(element.has_attribute("disabled"), "Should be disabled");
        assert!(!element.toggle_attribute("disabled", false).unwrap(), "Should return false when attribute is not set");
        assert!(!element.has_attribute("disabled"), "Should not be disabled");
    */

    assert!(!element.has_attribute("title"), "Should not have a title");
    assert_eq!(
        element.set_attribute("title", "boop").unwrap(),
        (),
        "Should return nothing if set correctly"
    );
    assert!(element.has_attribute("title"), "Should have a title");
    // TODO check get_attribute here when supported
    assert_eq!(
        element.remove_attribute("title").unwrap(),
        (),
        "Should return nothing if removed"
    );
    assert!(!element.has_attribute("title"), "Should not have a title");
    /* Tests needed for:
    set_attribute_ns
    */

    assert!(!element.has_attributes(), "Should not have any attributes");
    assert_eq!(
        element.set_attribute("title", "boop").unwrap(),
        (),
        "Should return nothing if set correctly"
    );
    assert!(element.has_attributes(), "Should have attributes");
    assert_eq!(
        element.remove_attribute("title").unwrap(),
        (),
        "Should return nothing if removed"
    );
    /* Tests needed for:
    remove_attribute_ns
    has_attribure_ns
    closest
    */

    assert_eq!(
        element.matches(".this-is-a-thing").unwrap(),
        false,
        "Should not match selector"
    );
    assert_eq!(
        element.webkit_matches_selector(".this-is-a-thing").unwrap(),
        false,
        "Should not match selector"
    );
    element.set_class_name("this-is-a-thing");
    assert_eq!(
        element.matches(".this-is-a-thing").unwrap(),
        true,
        "Should match selector"
    );
    assert_eq!(
        element.webkit_matches_selector(".this-is-a-thing").unwrap(),
        true,
        "Should match selector"
    );
    assert_eq!(
        element.remove_attribute("class").unwrap(),
        (),
        "Should return nothing if removed"
    );

    // TODO non standard moz_matches_selector should we even support?

    /* Tests needed for:
    insert_adjacent_element
    insert_adjacent_text
    set_pointer_capture
    release_pointer_capture
    has_pointer_capture
    set_capture
    release_capture
    scroll_top
    set_scroll_top
    scroll_left
    set_scroll_left
    scroll_width
    scroll_height
    scroll,
    scroll_to
    scroll_by
    client_top
    client_left
    client_width
    client_height
    scroll_top_max
    scroll_left_max
    */
    assert_eq!(element.inner_html(), "", "Should return no content");
    element.set_inner_html("<strong>Hey!</strong><em>Web!</em>");
    assert_eq!(
        element.inner_html(),
        "<strong>Hey!</strong><em>Web!</em>",
        "Should return HTML conent"
    );
    assert_eq!(
        element.query_selector_all("strong").unwrap().length(),
        1,
        "Should return one element"
    );
    assert!(
        element.query_selector("strong").unwrap().is_some(),
        "Should return an element"
    );
    element.set_inner_html("");
    assert_eq!(element.inner_html(), "", "Should return no content");

    /* Tests needed for:
    outer_html
    set_outer_html
    insert_adjacent_html
    */

    assert!(
        element.query_selector(".none-existant").unwrap().is_none(),
        "Should return no results"
    );
    assert_eq!(
        element
            .query_selector_all(".none-existant")
            .unwrap()
            .length(),
        0,
        "Should return no results"
    );
    /* Tests needed for:
      slot
      set_slot
      request_fullscreen
      request_pointer_lock
    */

    let child = new_div();
    assert_eq!(
        element.get_elements_by_tag_name("div").length(),
        0,
        "Element should not contain any div child"
    );
    element.append_child(&child).unwrap();
    assert_eq!(
        element.get_elements_by_tag_name("div").length(),
        1,
        "Element should contain one div child"
    );
    assert_eq!(
        element.get_elements_by_class_name("foo").length(),
        0,
        "Element should not have childs with class foo"
    );
    child.class_list().add_1("foo").unwrap();
    assert_eq!(
        element.get_elements_by_class_name("foo").length(),
        1,
        "Element should have one child with class foo"
    );
    element.remove_child(&child).unwrap();
}