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