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
// This file is part of css-purify. 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/css-purify/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 css-purify. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css-purify/master/COPYRIGHT.


/// Additional methods to work with QualName
pub trait QualNameExt
{
	/// Is this name effectively local?
	#[inline(always)]
	fn is_unprefixed_and_html_namespace_or_none(&self) -> bool;
	
	/// Is this qualified name this local-only name (no prefix, no namespace)
	#[inline(always)]
	fn is_only_local(&self, local_name: &LocalName) -> bool;
	
	/// Is this qualified name on these local-only names (no prefix, no namespace)
	#[inline(always)]
	fn is_only_local_of(&self, local_names: &[LocalName]) -> bool;
	
	/// Can this element have children?
	#[inline(always)]
	fn can_have_children(&self) -> bool;
	
	/// Should an immediate child text node have `<`, `>` and `&` characters escaped?
	/// In modern HTML 5, the only *common* nodes which don't need this are `<script>` and `<style>`.
	/// In this case, the immediate child text node's content should not contain, say, `</script>` as this will cause a parse error.
	#[inline(always)]
	fn text_content_should_be_escaped(&self) -> bool;
	
	/// Can this element's descendant text nodes have leading, trailing and interstitial whitespace collapsed?
	#[inline(always)]
	fn can_collapse_whitespace(&self) -> bool;
}

impl QualNameExt for QualName
{
	#[inline(always)]
	fn is_unprefixed_and_html_namespace_or_none(&self) -> bool
	{
		self.prefix.is_none() && match self.ns
		{
			ns!() | ns!(html) => true,
			_ => false,
		}
	}
	
	#[inline(always)]
	fn is_only_local(&self, local_name: &LocalName) -> bool
	{
		if self.is_unprefixed_and_html_namespace_or_none()
		{
			self.local == *local_name
		}
		else
		{
			false
		}
	}
	
	#[inline(always)]
	fn is_only_local_of(&self, local_names: &[LocalName]) -> bool
	{
		if self.is_unprefixed_and_html_namespace_or_none()
		{
			for local_name in local_names.iter()
			{
				if self.local == *local_name
				{
					return true;
				}
			}
			false
		}
		else
		{
			false
		}
	}
	
	//noinspection SpellCheckingInspection
	#[inline(always)]
	fn can_have_children(&self) -> bool
	{
		if !self.is_unprefixed_and_html_namespace_or_none()
		{
			return false;
		}
		
		match self.local
		{
			local_name!("area") | local_name!("base") | local_name!("basefont") | local_name!("bgsound") | local_name!("br") | local_name!("col") | local_name!("embed") | local_name!("frame") | local_name!("hr") | local_name!("img") | local_name!("input") | local_name!("keygen") | local_name!("link") | local_name!("meta") | local_name!("param") | local_name!("source") | local_name!("track") | local_name!("wbr") => false,
			
			_ => true,
		}
	}
	
	//noinspection SpellCheckingInspection
	#[inline(always)]
	fn text_content_should_be_escaped(&self) -> bool
	{
		if !self.is_unprefixed_and_html_namespace_or_none()
		{
			return false;
		}
		
		match self.local
		{
			local_name!("style") | local_name!("script") | local_name!("xmp") | local_name!("iframe") | local_name!("noembed") | local_name!("noframes") | local_name!("noscript") | local_name!("plaintext") => false,
			
			_ => true,
		}
	}
	
	#[inline(always)]
	fn can_collapse_whitespace(&self) -> bool
	{
		if !self.is_unprefixed_and_html_namespace_or_none()
		{
			return false;
		}
		
		match self.local
		{
			local_name!("pre") | local_name!("code") | local_name!("samp") | local_name!("kbd") => false,
			
			_ => true,
		}
	}
}

impl QualNameExt for RcDom
{
	#[inline(always)]
	fn is_unprefixed_and_html_namespace_or_none(&self) -> bool
	{
		self.document.is_unprefixed_and_html_namespace_or_none()
	}
	
	#[inline(always)]
	fn is_only_local(&self, local_name: &LocalName) -> bool
	{
		self.document.is_only_local(local_name)
	}
	
	#[inline(always)]
	fn is_only_local_of(&self, local_names: &[LocalName]) -> bool
	{
		self.document.is_only_local_of(local_names)
	}
	
	#[inline(always)]
	fn can_have_children(&self) -> bool
	{
		self.document.can_have_children()
	}
	
	#[inline(always)]
	fn text_content_should_be_escaped(&self) -> bool
	{
		self.document.text_content_should_be_escaped()
	}
	
	#[inline(always)]
	fn can_collapse_whitespace(&self) -> bool
	{
		self.document.can_collapse_whitespace()
	}
}

impl QualNameExt for Rc<Node>
{
	#[inline(always)]
	fn is_unprefixed_and_html_namespace_or_none(&self) -> bool
	{
		match self.data
		{
			NodeData::Element { ref name, .. } => name.is_unprefixed_and_html_namespace_or_none(),
			
			_ => false,
		}
	}
	
	#[inline(always)]
	fn is_only_local(&self, local_name: &LocalName) -> bool
	{
		match self.data
		{
			NodeData::Element { ref name, .. } => name.is_only_local(local_name),
			
			_ => false,
		}
	}
	
	#[inline(always)]
	fn is_only_local_of(&self, local_names: &[LocalName]) -> bool
	{
		match self.data
		{
			NodeData::Element { ref name, .. } => name.is_only_local_of(local_names),
			
			_ => false,
		}
	}
	
	#[inline(always)]
	fn can_have_children(&self) -> bool
	{
		match self.data
		{
			NodeData::Document => true,
			
			NodeData::Element { ref name, .. } => name.can_have_children(),
			
			_ => false,
		}
	}
	
	#[inline(always)]
	fn text_content_should_be_escaped(&self) -> bool
	{
		match self.data
		{
			NodeData::Element { ref name, .. } => name.text_content_should_be_escaped(),
			
			_ => false,
		}
	}
	
	#[inline(always)]
	fn can_collapse_whitespace(&self) -> bool
	{
		match self.data
		{
			NodeData::Element { ref name, .. } => name.can_collapse_whitespace(),
			
			_ => false,
		}
	}
}