Skip to content

Commit 84de1b2

Browse files
Implementing Element in web-sys
1 parent 9b6804a commit 84de1b2

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

crates/web-sys/tests/all/element.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use super::websys_project;
2+
3+
#[test]
4+
fn element() {
5+
websys_project()
6+
.file(
7+
"src/lib.rs",
8+
r#"
9+
#![feature(use_extern_macros, wasm_custom_section)]
10+
extern crate wasm_bindgen;
11+
use wasm_bindgen::prelude::*;
12+
extern crate web_sys;
13+
14+
#[wasm_bindgen]
15+
pub fn test_element(element: &web_sys::Element) {
16+
assert_eq!(element.local_name(), "div", "Shouldn't have a div local name");
17+
assert_eq!(element.tag_name(), "div", "Should be a div tag");
18+
assert!(!element.has_attribute("id"), "Shouldn't have an id");
19+
element.set_id("beep");
20+
assert_eq!(element.id(), "beep", "Should have an id of 'beep'");
21+
22+
// must_use is set on this result?
23+
assert_eq!(element.set_attribute("id", "beep").unwrap(), (), "Should set id");
24+
assert!(element.has_attribute("id"), "Should now have an id");
25+
assert_eq!(element.remove_attribute("id").unwrap(), (), "Should return nothing if removed");
26+
27+
assert_eq!(element.class_name(), "", "Shouldn't have a class name");
28+
element.set_class_name("test thing");
29+
assert_eq!(element.class_name(), "test thing", "Should have a class name");
30+
assert_eq!(element.remove_attribute("class").unwrap(), (), "Should return nothing if removed");
31+
32+
/*TODO should we enable toggle_attribute tests? (Firefox Nightly + Chrome canary only)
33+
// TODO toggle_attribute should permit a single argument when optional arguments are supported
34+
assert!(!element.has_attribute("disabled"), "Should not be disabled");
35+
assert!(element.toggle_attribute("disabled", true).unwrap(), "Should return true when attribute is set");
36+
assert!(element.has_attribute("disabled"), "Should be disabled");
37+
assert!(!element.toggle_attribute("disabled", false).unwrap(), "Should return false when attribute is not set");
38+
assert!(!element.has_attribute("disabled"), "Should not be disabled");
39+
*/
40+
41+
assert!(!element.has_attribute("title"), "Should not have a title");
42+
assert_eq!(element.set_attribute("title", "boop").unwrap(), (), "Should return nothing if set correctly");
43+
assert!(element.has_attribute("title"), "Should have a title");
44+
// TODO check get_attribute here when supported
45+
assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed");
46+
assert!(!element.has_attribute("title"), "Should not have a title");
47+
48+
assert!(!element.has_attributes(), "Should not have any attributes");
49+
assert_eq!(element.set_attribute("title", "boop").unwrap(), (), "Should return nothing if set correctly");
50+
assert!(element.has_attributes(), "Should have attributes");
51+
assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed");
52+
53+
assert_eq!(element.matches(".this-is-a-thing").unwrap(), false, "Should not match selector");
54+
assert_eq!(element.webkit_matches_selector(".this-is-a-thing").unwrap(), false, "Should not match selector");
55+
element.set_class_name("this-is-a-thing");
56+
assert_eq!(element.matches(".this-is-a-thing").unwrap(), true, "Should match selector");
57+
assert_eq!(element.webkit_matches_selector(".this-is-a-thing").unwrap(), true, "Should match selector");
58+
assert_eq!(element.remove_attribute("class").unwrap(), (), "Should return nothing if removed");
59+
60+
61+
// TODO non standard moz_matches_selector should we even support?
62+
/* Tests needed for:
63+
insert_adjacent_text
64+
set_pointer_capture
65+
release_pointer_capture
66+
has_pointer_capture
67+
set_capture
68+
release_capture
69+
*/
70+
}
71+
"#,
72+
)
73+
.file(
74+
"test.js",
75+
r#"
76+
import * as assert from "assert";
77+
import * as wasm from "./out";
78+
79+
export function test() {
80+
let document = new Document();
81+
let el = document.createElement("div");
82+
wasm.test_element(el);
83+
}
84+
"#,
85+
)
86+
.test();
87+
}

crates/web-sys/tests/all/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use project_builder::{project, Project};
44
mod event;
55
mod headers;
66
mod response;
7+
mod element;
78

89
fn websys_project() -> Project {
910
project()

crates/web-sys/webidls/available/Element.webidl renamed to crates/web-sys/webidls/enabled/Element.webidl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ interface Element : Node {
6565
[Throws, Pure, BinaryName="matches"]
6666
boolean webkitMatchesSelector(DOMString selector);
6767

68+
/*TODO
6869
[Pure]
6970
HTMLCollection getElementsByTagName(DOMString localName);
7071
[Throws, Pure]
7172
HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
7273
[Pure]
7374
HTMLCollection getElementsByClassName(DOMString classNames);
75+
*/
7476
[ChromeOnly, Pure]
7577
sequence<Element> getElementsWithGrid();
7678

@@ -101,8 +103,10 @@ interface Element : Node {
101103
*
102104
* See <http://dev.w3.org/2006/webapi/selectors-api2/#matchesselector>
103105
*/
106+
/* Non standard
104107
[Throws, Pure, BinaryName="matches"]
105108
boolean mozMatchesSelector(DOMString selector);
109+
*/
106110

107111
// Pointer events methods.
108112
[Throws, Pref="dom.w3c_pointer_events.enabled"]
@@ -244,8 +248,10 @@ partial interface Element {
244248
partial interface Element {
245249
[Throws, Pure]
246250
Element? querySelector(DOMString selectors);
251+
/*TODO
247252
[Throws, Pure]
248253
NodeList querySelectorAll(DOMString selectors);
254+
*/
249255
};
250256

251257
// https://dom.spec.whatwg.org/#dictdef-shadowrootinit
@@ -280,8 +286,10 @@ Element implements GeometryUtils;
280286
partial interface Element {
281287
[Throws, Func="nsDocument::IsUnprefixedFullscreenEnabled", NeedsCallerType]
282288
void requestFullscreen();
289+
/*Non standard
283290
[Throws, BinaryName="requestFullscreen", NeedsCallerType]
284291
void mozRequestFullScreen();
292+
*/
285293
};
286294

287295
// https://w3c.github.io/pointerlock/#extensions-to-the-element-interface

crates/webidl/src/util.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ impl<'a> FirstPassRecord<'a> {
176176
webidl::ast::TypeKind::DOMString if pos == TypePosition::Argument => {
177177
shared_ref(ident_ty(raw_ident("str")))
178178
}
179-
// `DOMString` is not supported yet in other positions.
180-
webidl::ast::TypeKind::DOMString => return None,
179+
webidl::ast::TypeKind::DOMString => {
180+
ident_ty(raw_ident("String"))
181+
},
181182

182183
// `ByteString -> `&[u8]` for arguments
183184
webidl::ast::TypeKind::ByteString if pos == TypePosition::Argument => {

0 commit comments

Comments
 (0)