element.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. use wasm_bindgen::prelude::*;
  2. use wasm_bindgen_test::*;
  3. use web_sys::Element;
  4. #[wasm_bindgen(module = "/tests/wasm/element.js")]
  5. extern "C" {
  6. fn new_div() -> Element;
  7. }
  8. #[wasm_bindgen_test]
  9. fn element() {
  10. /* Tests needed for:
  11. namespace_uri
  12. */
  13. let element = new_div();
  14. assert_eq!(element, element);
  15. assert_eq!(element.prefix(), None, "Shouldn't have a prefix");
  16. assert_eq!(element.local_name(), "div", "Should 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. // must_use is set on this result?
  22. assert_eq!(
  23. element.set_attribute("id", "beep").unwrap(),
  24. (),
  25. "Should set id"
  26. );
  27. assert!(element.has_attribute("id"), "Should now have an id");
  28. assert_eq!(
  29. element.remove_attribute("id").unwrap(),
  30. (),
  31. "Should return nothing if removed"
  32. );
  33. assert_eq!(element.class_name(), "", "Shouldn't have a class name");
  34. element.set_class_name("test thing");
  35. assert_eq!(
  36. element.class_name(),
  37. "test thing",
  38. "Should have a class name"
  39. );
  40. assert_eq!(
  41. element.get_attribute("class").unwrap(),
  42. "test thing",
  43. "Should have a class name"
  44. );
  45. assert_eq!(
  46. element.remove_attribute("class").unwrap(),
  47. (),
  48. "Should return nothing if removed"
  49. );
  50. /* Tests needed for:
  51. get_attribute_ns
  52. */
  53. /*TODO should we enable toggle_attribute tests? (Firefox Nightly + Chrome canary only)
  54. // TODO toggle_attribute should permit a single argument when optional arguments are supported
  55. assert!(!element.has_attribute("disabled"), "Should not be disabled");
  56. assert!(element.toggle_attribute("disabled", true).unwrap(), "Should return true when attribute is set");
  57. assert!(element.has_attribute("disabled"), "Should be disabled");
  58. assert!(!element.toggle_attribute("disabled", false).unwrap(), "Should return false when attribute is not set");
  59. assert!(!element.has_attribute("disabled"), "Should not be disabled");
  60. */
  61. assert!(!element.has_attribute("title"), "Should not have a title");
  62. assert_eq!(
  63. element.set_attribute("title", "boop").unwrap(),
  64. (),
  65. "Should return nothing if set correctly"
  66. );
  67. assert!(element.has_attribute("title"), "Should have a title");
  68. // TODO check get_attribute here when supported
  69. assert_eq!(
  70. element.remove_attribute("title").unwrap(),
  71. (),
  72. "Should return nothing if removed"
  73. );
  74. assert!(!element.has_attribute("title"), "Should not have a title");
  75. /* Tests needed for:
  76. set_attribute_ns
  77. */
  78. assert!(!element.has_attributes(), "Should not have any attributes");
  79. assert_eq!(
  80. element.set_attribute("title", "boop").unwrap(),
  81. (),
  82. "Should return nothing if set correctly"
  83. );
  84. assert!(element.has_attributes(), "Should have attributes");
  85. assert_eq!(
  86. element.remove_attribute("title").unwrap(),
  87. (),
  88. "Should return nothing if removed"
  89. );
  90. /* Tests needed for:
  91. remove_attribute_ns
  92. has_attribure_ns
  93. closest
  94. */
  95. assert_eq!(
  96. element.matches(".this-is-a-thing").unwrap(),
  97. false,
  98. "Should not match selector"
  99. );
  100. assert_eq!(
  101. element.webkit_matches_selector(".this-is-a-thing").unwrap(),
  102. false,
  103. "Should not match selector"
  104. );
  105. element.set_class_name("this-is-a-thing");
  106. assert_eq!(
  107. element.matches(".this-is-a-thing").unwrap(),
  108. true,
  109. "Should match selector"
  110. );
  111. assert_eq!(
  112. element.webkit_matches_selector(".this-is-a-thing").unwrap(),
  113. true,
  114. "Should match selector"
  115. );
  116. assert_eq!(
  117. element.remove_attribute("class").unwrap(),
  118. (),
  119. "Should return nothing if removed"
  120. );
  121. // TODO non standard moz_matches_selector should we even support?
  122. /* Tests needed for:
  123. insert_adjacent_element
  124. insert_adjacent_text
  125. set_pointer_capture
  126. release_pointer_capture
  127. has_pointer_capture
  128. set_capture
  129. release_capture
  130. scroll_top
  131. set_scroll_top
  132. scroll_left
  133. set_scroll_left
  134. scroll_width
  135. scroll_height
  136. scroll,
  137. scroll_to
  138. scroll_by
  139. client_top
  140. client_left
  141. client_width
  142. client_height
  143. scroll_top_max
  144. scroll_left_max
  145. */
  146. assert_eq!(element.inner_html(), "", "Should return no content");
  147. element.set_inner_html("<strong>Hey!</strong><em>Web!</em>");
  148. assert_eq!(
  149. element.inner_html(),
  150. "<strong>Hey!</strong><em>Web!</em>",
  151. "Should return HTML conent"
  152. );
  153. assert_eq!(
  154. element.query_selector_all("strong").unwrap().length(),
  155. 1,
  156. "Should return one element"
  157. );
  158. assert!(
  159. element.query_selector("strong").unwrap().is_some(),
  160. "Should return an element"
  161. );
  162. element.set_inner_html("");
  163. assert_eq!(element.inner_html(), "", "Should return no content");
  164. /* Tests needed for:
  165. outer_html
  166. set_outer_html
  167. insert_adjacent_html
  168. */
  169. assert!(
  170. element.query_selector(".none-existant").unwrap().is_none(),
  171. "Should return no results"
  172. );
  173. assert_eq!(
  174. element
  175. .query_selector_all(".none-existant")
  176. .unwrap()
  177. .length(),
  178. 0,
  179. "Should return no results"
  180. );
  181. /* Tests needed for:
  182. slot
  183. set_slot
  184. request_fullscreen
  185. request_pointer_lock
  186. */
  187. let child = new_div();
  188. assert_eq!(
  189. element.get_elements_by_tag_name("div").length(),
  190. 0,
  191. "Element should not contain any div child"
  192. );
  193. element.append_child(&child).unwrap();
  194. assert_eq!(
  195. element.get_elements_by_tag_name("div").length(),
  196. 1,
  197. "Element should contain one div child"
  198. );
  199. assert_eq!(
  200. element.get_elements_by_class_name("foo").length(),
  201. 0,
  202. "Element should not have childs with class foo"
  203. );
  204. child.class_list().add_1("foo").unwrap();
  205. assert_eq!(
  206. element.get_elements_by_class_name("foo").length(),
  207. 1,
  208. "Element should have one child with class foo"
  209. );
  210. element.remove_child(&child).unwrap();
  211. }