@@ -102,8 +102,9 @@ pub mod tree_builder;
102
102
/// at the same time. However if we declare a namespace we could instead say:
103
103
///
104
104
/// ```text
105
+ ///
105
106
/// // Furniture XML
106
- /// <furn:table>
107
+ /// <furn:table xmlns:furn="https://furniture.rs" >
107
108
/// <furn:name>African Coffee Table</furn:name>
108
109
/// <furn:width>80</furn:width>
109
110
/// <furn:length>120</furn:length>
@@ -119,21 +120,171 @@ pub mod tree_builder;
119
120
/// | |
120
121
/// | +- local name
121
122
/// |
122
- /// prefix (when resolved gives namespace_url)
123
+ /// prefix (when resolved gives namespace_url `https://furniture.rs` )
123
124
/// ```
125
+ ///
126
+ /// NOTE: `Prefix`, `LocalName` and `Prefix` are all derivative of
127
+ /// `string_cache::atom::Atom` and `Atom` implements `Deref<str>`.
128
+ ///
124
129
#[ derive( PartialEq , Eq , PartialOrd , Ord , Hash , Debug , Clone ) ]
125
130
#[ cfg_attr( feature = "heap_size" , derive( HeapSizeOf ) ) ]
126
131
pub struct QualName {
127
- /// The namespace before resolution (e.g. `furn` in `<furn:table>` above).
132
+ /// The prefix of qualified (e.g. `furn` in `<furn:table>` above).
133
+ /// Optional (since some namespaces can be empty or inferred), and
134
+ /// only useful for namespace resolution (since different prefix
135
+ /// can still resolve to same namespace)
136
+ ///
137
+ /// ```
138
+ ///
139
+ /// # fn main() {
140
+ /// use markup5ever::{QualName, Namespace, LocalName, Prefix};
141
+ ///
142
+ /// let qual = QualName::new(
143
+ /// Some(Prefix::from("furn")),
144
+ /// Namespace::from("https://furniture.rs"),
145
+ /// LocalName::from("table"),
146
+ /// );
147
+ ///
148
+ /// assert_eq!("furn", &qual.prefix.unwrap());
149
+ ///
150
+ /// # }
151
+ /// ```
128
152
pub prefix : Option < Prefix > ,
129
- /// The namespace after resolution.
153
+ /// The namespace after resolution (e.g. `https://furniture.rs` in example above).
154
+ ///
155
+ /// ```
156
+ /// # use markup5ever::{QualName, Namespace, LocalName, Prefix};
157
+ ///
158
+ /// # fn main() {
159
+ /// # let qual = QualName::new(
160
+ /// # Some(Prefix::from("furn")),
161
+ /// # Namespace::from("https://furniture.rs"),
162
+ /// # LocalName::from("table"),
163
+ /// # );
164
+ ///
165
+ /// assert_eq!("https://furniture.rs", &qual.ns);
166
+ /// # }
167
+ /// ```
168
+ ///
169
+ /// When matching namespaces used by HTML we can use `ns!` macro.
170
+ /// Although keep in mind that ns! macro only works with namespaces
171
+ /// that are present in HTML spec (like `html`, `xmlns`, `svg`, etc.).
172
+ ///
173
+ /// ```
174
+ /// #[macro_use] extern crate markup5ever;
175
+ ///
176
+ /// # use markup5ever::{QualName, Namespace, LocalName, Prefix};
177
+ ///
178
+ /// let html_table = QualName::new(
179
+ /// None,
180
+ /// ns!(html),
181
+ /// LocalName::from("table"),
182
+ /// );
183
+ ///
184
+ /// assert!(
185
+ /// match html_table.ns {
186
+ /// ns!(html) => true,
187
+ /// _ => false,
188
+ /// }
189
+ /// );
190
+ ///
191
+ /// ```
130
192
pub ns : Namespace ,
131
193
/// The local name (e.g. `table` in `<furn:table>` above).
194
+ ///
195
+ /// ```
196
+ /// # use markup5ever::{QualName, Namespace, LocalName, Prefix};
197
+ ///
198
+ /// # fn main() {
199
+ /// # let qual = QualName::new(
200
+ /// # Some(Prefix::from("furn")),
201
+ /// # Namespace::from("https://furniture.rs"),
202
+ /// # LocalName::from("table"),
203
+ /// # );
204
+ ///
205
+ /// assert_eq!("table", &qual.local);
206
+ /// # }
207
+ /// ```
208
+ /// When matching local name we can also use the `local_name!` macro:
209
+ ///
210
+ /// ```
211
+ /// #[macro_use] extern crate markup5ever;
212
+ ///
213
+ /// # use markup5ever::{QualName, Namespace, LocalName, Prefix};
214
+ ///
215
+ /// # let qual = QualName::new(
216
+ /// # Some(Prefix::from("furn")),
217
+ /// # Namespace::from("https://furniture.rs"),
218
+ /// # LocalName::from("table"),
219
+ /// # );
220
+ ///
221
+ /// // Initialize qual to furniture example
222
+ ///
223
+ /// assert!(
224
+ /// match qual.local {
225
+ /// local_name!("table") => true,
226
+ /// _ => false,
227
+ /// }
228
+ /// );
229
+ ///
230
+ /// ```
132
231
pub local : LocalName ,
133
232
}
134
233
135
234
impl QualName {
136
- /// Simple constructor function.
235
+ /// Basic constructor function.
236
+ ///
237
+ /// First let's try it for the following example where `QualName`
238
+ /// is defined as:
239
+ /// ```text
240
+ /// <furn:table> <!-- namespace url is https://furniture.rs -->
241
+ /// ```
242
+ ///
243
+ /// Given this definition, we can define `QualName` using strings.
244
+ ///
245
+ /// ```
246
+ /// use markup5ever::{QualName, Namespace, LocalName, Prefix};
247
+ ///
248
+ /// # fn main() {
249
+ /// let qual_name = QualName::new(
250
+ /// Some(Prefix::from("furn")),
251
+ /// Namespace::from("https://furniture.rs"),
252
+ /// LocalName::from("table"),
253
+ /// );
254
+ /// # }
255
+ /// ```
256
+ ///
257
+ /// If we were instead to construct this element instead:
258
+ ///
259
+ /// ```text
260
+ ///
261
+ /// <table>
262
+ /// ^^^^^---- no prefix and thus default html namespace
263
+ ///
264
+ /// ```
265
+ ///
266
+ /// Or could define it using macros, like so:
267
+ ///
268
+ /// ```
269
+ /// #[macro_use] extern crate markup5ever;
270
+ /// use markup5ever::{QualName, Namespace, LocalName, Prefix};
271
+ ///
272
+ /// # fn main() {
273
+ /// let qual_name = QualName::new(
274
+ /// None,
275
+ /// ns!(html),
276
+ /// local_name!("table")
277
+ /// );
278
+ /// # }
279
+ /// ```
280
+ ///
281
+ /// Let's analyse the above example.
282
+ /// Since we have no prefix its value is None. Second we have html namespace.
283
+ /// In html5ever html namespaces are supported out of the box,
284
+ /// we can write `ns!(html)` instead of typing `Namespace::from("http://www.w3.org/1999/xhtml")`.
285
+ /// Local name is also one of the HTML elements local names, so can
286
+ /// use `local_name!("table")` macro.
287
+ ///
137
288
#[ inline]
138
289
pub fn new ( prefix : Option < Prefix > , ns : Namespace , local : LocalName ) -> QualName {
139
290
QualName {
@@ -144,6 +295,25 @@ impl QualName {
144
295
}
145
296
146
297
/// Take a reference of `self` as an `ExpandedName`, dropping the unresolved prefix.
298
+ ///
299
+ /// In XML and HTML prefixes are only used to extract the relevant namespace URI.
300
+ /// Expanded name only contains resolved namespace and tag name, which are only
301
+ /// relevant parts of an XML or HTML tag and attribute name respectively.
302
+ ///
303
+ /// In lieu of our XML Namespace example
304
+ ///
305
+ /// ```text
306
+ /// <furn:table> <!-- namespace url is https://furniture.rs -->
307
+ /// ```
308
+ /// For it the expanded name would become roughly equivalent to:
309
+ ///
310
+ /// ```text
311
+ /// ExpandedName {
312
+ /// ns: "https://furniture.rs",
313
+ /// local: "table",
314
+ /// }
315
+ /// ```
316
+ ///
147
317
#[ inline]
148
318
pub fn expanded ( & self ) -> ExpandedName {
149
319
ExpandedName {
0 commit comments