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
227
228
229
230
231
232
233
234
235
use ;
use cratedocument;
/// Javascript [`document.getElementById`](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById) method
///
/// It returns an [`Element`](https://docs.rs/web-sys/0.3.56/web_sys/struct.Element.html) object describing the DOM element object matching the specified ID, or `None` if no matching element was found in the document.
///
///
/// # Arguments
///
/// * `id` - The ID of the element to locate. The ID is case-sensitive string which is unique within the document; only one element may have any given ID.
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```
/// use webru::{body, create_element, get_element_by_id};
///
/// // `id` property for the new <p> element
/// const ID: &str = "RandomId#$4";
///
/// // Create a <p> element
/// let p = create_element("p");
///
/// // setting the id property
/// p.set_id(ID);
///
/// // Try to get the <p> element before inserting into the DOM
/// let fetched_p = get_element_by_id(ID);
///
/// // it will give None
/// assert_eq!(fetched_p, None);
///
/// // inserting the <p> into the DOM
/// body().append_child(&p).unwrap();
///
/// // Try to get the <p> element after inserting into the DOM
/// let fetched_p = get_element_by_id(ID);
///
/// // it will give Some(_)
/// assert_eq!(fetched_p, Some(p));
///
///
/// ```
/// Javascript [`document.getElementsByClassName`](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName) method
///
/// It returns An [`HTMLCollection`](https://docs.rs/web-sys/0.3.56/web_sys/struct.HtmlCollection.html) object. A collection of elements with the specified class name.
///
/// The elements are sorted as they appear in the document.
///
///
/// # Arguments
///
/// * `classname` - The class name of the elements.
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```
/// use webru::{body, create_element, get_elements_by_classname};
///
/// // `class` property for the new <p> element
/// const CLASS: &str = "RandomClass#$4";
///
/// // Create a <p> element
/// let p = create_element("p");
///
/// // setting the class property
/// p.set_class_name(CLASS);
///
/// // inserting the <p> into the DOM
/// body().append_child(&p).unwrap();
///
/// // Get the <p> element
/// let fetched_p = get_elements_by_classname(CLASS);
///
/// // because in this example I've pushed one element, so its length should be 1
/// assert_eq!(fetched_p.length(), 1);
///
/// // Getting the 1st element of the `fetched_p`
/// let fetched_p_0 = fetched_p.item(0).unwrap();
///
/// assert_eq!(fetched_p_0.tag_name(), "P");
/// assert_eq!(fetched_p_0.class_name(), CLASS);
///
/// ```
/// Javascript [`document.getElementsByClassName`](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName) method
///
/// This function does the same thing as the [`get_elements_by_classname`] function does.
///
/// But instead of returning [`HtmlCollection`] it returns [`Vec<Element>`]
///
/// Behind the scene it uses [`get_elements_by_classname`] and converts [`HtmlCollection`] into [`Vec<Element>`]
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// [`HtmlCollection`]: <https://docs.rs/web-sys/0.3.56/web_sys/struct.HtmlCollection.html>
/// [`Vec<Element>`]: <https://docs.rs/web-sys/0.3.56/web_sys/struct.Element.html>
///
/// Javascript [`document.querySelector`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) method
///
/// It returns an [`Element`](https://docs.rs/web-sys/0.3.56/web_sys/struct.Element.html) object representing the first element in the document that matches the specified set of [`CSS selectors`](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors), or `None` is returned if there are no matches.
///
///
/// # Arguments
///
/// * `selector` - A `&str` containing one or more selectors to match. This string must be a valid CSS selector string; if it isn't, it will be `panic`
///
///
/// # Panics
///
/// * This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// * This function will panic if the `selector` is not a valid CSS selector
///
///
/// # Example
///
/// ```
/// use webru::{body, create_element, query_selector};
///
/// // id of the <p>
/// const PARAGRAPH_ID: &str = "asdfsd";
///
/// // class of the <p>
/// const PARAGRAPH_CLASS: &str = "sadfsaa";
///
/// // Creating an html element <p>
/// let p = create_element("p");
///
/// // adding attributes
/// p.set_id(PARAGRAPH_ID);
/// p.set_class_name(PARAGRAPH_CLASS);
///
/// // inserting the element into the <body>
/// body().append_child(&p).unwrap();
///
/// // getting the right element with the `query_selector` function with its `id`
/// let p_right_from_id = query_selector(&format!("#{}", PARAGRAPH_ID));
///
/// // getting the right element with the `query_selector` function with its `class`
/// let p_right_from_class = query_selector(&format!(".{}", PARAGRAPH_CLASS));
///
/// // getting the wrong element with the `query_selector` function with its `id`
/// let p_wrong = query_selector(&format!("#{}", "ewsdfs"));
///
/// assert_eq!(p_right_from_id.clone().unwrap(), p);
/// assert_eq!(
/// p_right_from_id.clone().unwrap(),
/// p_right_from_class.clone().unwrap()
/// );
/// assert_ne!(p_right_from_class, p_wrong);
/// assert_ne!(p_right_from_class, None);
///
/// ```
/// Javascript [`document.querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) method
///
///
/// # Panics
///
/// * This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// * This function will panic if the `selector` is not a valid CSS selector
/// Javascript [`document.querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) method
///
/// This function does the same thing as the [`query_selector_all`] function does.
///
/// But instead of returning [`NodeList`] it returns [`Vec<Node>`]
///
/// Behind the scene it uses [`query_selector_all`] and converts [`NodeList`] into [`Vec<Node>`]
///
/// [`NodeList`]: <https://docs.rs/web-sys/0.3.56/web_sys/struct.NodeList.html>
/// [`Node`]: <https://docs.rs/web-sys/0.3.56/web_sys/struct.Node.html>
/// [`Vec<Node>`]: <https://docs.rs/web-sys/0.3.56/web_sys/struct.Node.html>
///
///
/// # Panics
///
/// * This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// * This function will panic if the `selector` is not a valid CSS selector
///