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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// This file is part of html5ever_ext. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/html5ever_ext/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2017 The developers of html5ever_ext. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/html5ever_ext/master/COPYRIGHT.


/// Represents the structure of nodes unattached to a DOM.
/// Designed to make it easy to create an entire graph of nodes before adding it.
#[derive(Debug, Clone)]
pub struct UnattachedNode
{
	/// Local name of this node
	pub local_name: LocalName,
	
	/// Attributes of this node
	pub attributes: Vec<Attribute>,
	
	/// The children of this node
	pub children: Vec<Either<String, UnattachedNode>>,
}

impl<'a> From<&'a str> for UnattachedNode
{
	#[inline(always)]
	fn from(local_name: &'a str) -> Self
	{
		LocalName::from(local_name).empty_node()
	}
}

impl From<LocalName> for UnattachedNode
{
	#[inline(always)]
	fn from(local_name: LocalName) -> Self
	{
		local_name.empty_node()
	}
}

impl UnattachedNodeExt for UnattachedNode
{
	fn to_rc_dom(self) -> RcDom
	{
		let mut rc_dom = RcDom::default();
		self.attach_to_document_node(&mut rc_dom);
		rc_dom
	}
}

impl UnattachedNode
{
	/// Creates a HTML RcDom from this node as the 'html' root with a HTML5 DOCTYPE preceding it.
	/// Panics if the node is not called 'html'.
	#[inline(always)]
	pub fn to_html5_rc_dom(self) -> RcDom
	{
		let mut rc_dom = RcDom::default();
		rc_dom.create_html5_document(self);
		rc_dom
	}
	
	/// Creates a HTML document as a String from this node as the 'html' root with a HTML5 DOCTYPE preceding it.
	/// Panics if the node is not called 'html'.
	#[inline(always)]
	pub fn to_html5_document(self, html_head_and_body_tags_are_optional: bool) -> String
	{
		self.to_html5_rc_dom().minify_to_string(html_head_and_body_tags_are_optional)
	}
	
	/// Represents an empty element, such as <br>
	#[inline(always)]
	pub fn empty(local_name: LocalName) -> Self
	{
		Self::with_attributes(local_name, vec![])
	}
	
	/// Represents an empty element with just attributes, such as <img src="/path/to/source.png">
	#[inline(always)]
	pub fn with_attributes(local_name: LocalName, attributes: Vec<Attribute>) -> Self
	{
		Self
		{
			local_name,
			attributes,
			children: vec![],
		}
	}
	
	/// Represents an empty element with just attributes and text, such as <details open>hello</details>
	#[inline(always)]
	pub fn with_attributes_and_text<S: Into<String>>(local_name: LocalName, attributes: Vec<Attribute>, text: S) -> Self
	{
		Self
		{
			local_name,
			attributes,
			children: vec!
			[
				Left(text.into()),
			],
		}
	}
	
	/// Represents an empty element with just text, such as <code>hello</code>
	#[inline(always)]
	pub fn with_text<S: Into<String>>(local_name: LocalName, text: S) -> Self
	{
		Self
		{
			local_name,
			attributes: vec![],
			children: vec!
			[
				Left(text.into()),
			],
		}
	}
	
	/// Add an empty attribute.
	#[inline(always)]
	pub fn with_empty_attribute(self, name: &str) -> Self
	{
		self.with_attribute(LocalName::from(name).empty_attribute())
	}
	
	/// Add an id attribute.
	#[inline(always)]
	pub fn with_id_attribute(self, id: &str) -> Self
	{
		if id.is_empty()
		{
			return self;
		}
		self.with_attribute(local_name!("id").attribute(id))
	}
	
	/// Add a title attribute.
	#[inline(always)]
	pub fn with_title_attribute(self, title: &str) -> Self
	{
		if title.is_empty()
		{
			return self;
		}
		self.with_attribute(local_name!("title").attribute(title))
	}
	
	//noinspection SpellCheckingInspection
	/// Add an accesskey attribute.
	#[inline(always)]
	pub fn with_accesskey_attribute(self, accesskey: &str) -> Self
	{
		if accesskey.is_empty()
		{
			return self;
		}
		self.with_attribute(local_name!("accesskey").attribute(accesskey))
	}
	
	//noinspection SpellCheckingInspection
	/// Add a lang attribute.
	#[inline(always)]
	pub fn with_lang_attribute(self, lang: &str) -> Self
	{
		self.with_attribute(local_name!("lang").attribute(lang))
	}
	
	//noinspection SpellCheckingInspection
	/// Add a contenteditable attribute.
	#[inline(always)]
	pub fn with_contenteditable_attribute(self, editable: bool) -> Self
	{
		let value = if editable
		{
			""
		}
		else
		{
			"false"
		};
		
		self.with_attribute(local_name!("contenteditable").attribute(value))
	}
	
	//noinspection SpellCheckingInspection
	/// Add a spellcheck attribute.
	#[inline(always)]
	pub fn with_spellcheck_attribute(self, spellcheck: bool) -> Self
	{
		let value = if spellcheck
		{
			"true"
		}
		else
		{
			"false"
		};
		
		self.with_attribute(LocalName::from("spellcheck").attribute(value))
	}
	
	//noinspection SpellCheckingInspection
	/// Add a tabindex attribute.
	#[inline(always)]
	pub fn with_tabindex_attribute(self, tabindex: i32) -> Self
	{
		self.with_attribute(local_name!("tabindex").attribute(&format!("{}", tabindex)))
	}
	
	//noinspection SpellCheckingInspection
	/// Add a hidden attribute.
	#[inline(always)]
	pub fn with_hidden_attribute(self, hidden: bool) -> Self
	{
		if hidden
		{
			self.with_attribute(local_name!("hidden").empty_attribute())
		}
		else
		{
			self
		}
	}
	
	//noinspection SpellCheckingInspection
	/// Add a contextmenu attribute.
	#[inline(always)]
	pub fn with_contextmenu_attribute(self, id: &str) -> Self
	{
		if id.is_empty()
		{
			return self;
		}
		self.with_attribute(local_name!("contextmenu").attribute(id))
	}
	
	/// Add a data attribute.
	#[inline(always)]
	pub fn with_data_attribute(self, name: &str, value: &str) -> Self
	{
		const PREFIX: &'static str = "data-";
		let mut attribute_name = String::with_capacity(PREFIX.len() + name.len());
		attribute_name.push_str(PREFIX);
		attribute_name.push_str(name);
		let local_name = LocalName::from(attribute_name.as_str());
		
		self.with_attribute(local_name.attribute(value))
	}
	
	/// Add a draggable attribute.
	#[inline(always)]
	pub fn with_draggable_attribute(self, draggable: Draggable) -> Self
	{
		self.with_attribute(local_name!("draggable").attribute(draggable.to_str()))
	}
	
	/// Add a dir attribute.
	#[inline(always)]
	pub fn with_dir_attribute(self, dir: Dir) -> Self
	{
		self.with_attribute(local_name!("dir").attribute(dir.to_str()))
	}
	
	/// Add a href attribute.
	#[inline(always)]
	pub fn with_href_attribute(self, href: &str) -> Self
	{
		self.with_attribute(local_name!("href").attribute(href))
	}
	
	/// Add a class attribute.
	#[inline(always)]
	pub fn with_class_attribute(self, classes: &str) -> Self
	{
		self.with_attribute(local_name!("class").attribute(classes))
	}
	
	/// Add a class attribute.
	#[inline(always)]
	pub fn with_role_attribute(self, role: AriaRole) -> Self
	{
		self.with_attribute(local_name!("role").attribute(role.to_str()))
	}
	
	/// Add a class attribute from classes
	#[inline(always)]
	pub fn with_class_attribute_from_classes<S: Deref<Target=str>>(self, classes: &[S]) -> Self
	{
		let mut class_string = String::new();
		let mut after_first = false;
		for class in classes.iter()
		{
			let value = class.deref();
			if !value.is_empty()
			{
				if after_first
				{
					class_string.push(' ');
				}
				else
				{
					after_first = true;
				}
				class_string.push_str(value);
			}
		}
		self.with_class_attribute(&class_string)
	}
	
	/// Add classes irrespective of whether a class attribute exists or not.
	#[inline(always)]
	pub fn with_classes<S: Deref<Target=str>>(mut self, classes: &[S]) -> Self
	{
		for class in classes.iter()
		{
			let value = class.deref();
			if !value.is_empty()
			{
				self = self.with_class(value)
			}
		}
		self
	}
	
	/// Add a class attribute if it does not exist, or appends to an existing class attribute if it does
	#[inline(always)]
	pub fn with_class(mut self, value: &str) -> Self
	{
		if value.is_empty()
		{
			return self;
		}
		let class_local_name = local_name!("class");
		let mut found = false;
		for attribute in self.attributes.iter_mut()
		{
			if attribute.name.is_only_local(&class_local_name)
			{
				if attribute.value.len32() != 0
				{
					attribute.value.push_char(' ');
				}
				attribute.value.push_slice(value);
				found = true;
				break;
			}
		}
		if found
		{
			self
		}
		else
		{
			self.with_class_attribute(value)
		}
	}
	
	/// Add an attribute.
	#[inline(always)]
	pub fn with_attribute(mut self, attribute: Attribute) -> Self
	{
		self.attributes.push(attribute);
		self
	}
	
	/// Add a child element.
	#[inline(always)]
	pub fn with_child_element(mut self, child_element: UnattachedNode) -> Self
	{
		self.children.push(Right(child_element));
		self
	}
	
	/// Add a child text. If there is an existing child text element, appends its text to it.
	#[inline(always)]
	pub fn with_child_text<S: Into<String>>(mut self, child_text: S) -> Self
	{
		let child_text = child_text.into();
		if self.children.is_empty()
		{
			self.children.push(Left(child_text));
		}
		else
		{
			// Odd design to avoid double-mutable borrow of self.children
			let unmatched = match self.children.last_mut().unwrap().as_mut().left()
			{
				Some(existing_text) =>
				{
					existing_text.push_str(&child_text);
					true
				}
				None => false,
			};
			if unmatched
			{
				self.children.push(Left(child_text));
			}
		}
		self
	}
	
	/// Attach this node as a child of the document_node (ie as a root node)
	#[inline(always)]
	pub fn attach_to_document_node(self, rc_dom: &mut RcDom) -> Rc<Node>
	{
		let node = rc_dom.append_new_element_to_document_node(self.local_name.qual_name(), self.attributes);
		Self::process_children(rc_dom, self.children, node)
	}
	
	/// Attach this node as a child of parent_node
	#[inline(always)]
	pub fn attach_to_parent_node(self, rc_dom: &mut RcDom, parent_node: &Rc<Node>) -> Rc<Node>
	{
		let node = rc_dom.append_new_element_to_parent_node(parent_node, self.local_name.qual_name(), self.attributes);
		Self::process_children(rc_dom, self.children, node)
	}
	
	/// Attach this node before the sibling_node
	#[inline(always)]
	pub fn attach_to_before_sibling_node(self, rc_dom: &mut RcDom, sibling_node: &Rc<Node>) -> Rc<Node>
	{
		let node = rc_dom.append_new_element_before_sibling_node(sibling_node, self.local_name.qual_name(), self.attributes);
		Self::process_children(rc_dom, self.children, node)
	}
	
	#[inline(always)]
	fn process_children(rc_dom: &mut RcDom, mut children: Vec<Either<String, UnattachedNode>>, parent_node: Rc<Node>) -> Rc<Node>
	{
		for child in children.drain(..)
		{
			match child
			{
				Left(text) => parent_node.append_text(rc_dom, &text),
				Right(child) =>
				{
					child.attach_to_parent_node(rc_dom, &parent_node);
				}
			}
		}
		parent_node
	}
}