@@ -102,8 +102,9 @@ pub mod tree_builder;
102102/// at the same time. However if we declare a namespace we could instead say:
103103///
104104/// ```text
105+ ///
105106/// // Furniture XML
106- /// <furn:table>
107+ /// <furn:table xmlns:furn="https://furniture.rs" >
107108/// <furn:name>African Coffee Table</furn:name>
108109/// <furn:width>80</furn:width>
109110/// <furn:length>120</furn:length>
@@ -119,21 +120,165 @@ pub mod tree_builder;
119120/// | |
120121/// | +- local name
121122/// |
122- /// prefix (when resolved gives namespace_url)
123+ /// prefix (when resolved gives namespace_url `https://furniture.rs` )
123124/// ```
124125#[ derive( PartialEq , Eq , PartialOrd , Ord , Hash , Debug , Clone ) ]
125126#[ cfg_attr( feature = "heap_size" , derive( HeapSizeOf ) ) ]
126127pub 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+ /// ```
128148 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+ /// ```
130186 pub ns : Namespace ,
131187 /// 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+ /// ```
132225 pub local : LocalName ,
133226}
134227
135228impl 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+ ///
137282 #[ inline]
138283 pub fn new ( prefix : Option < Prefix > , ns : Namespace , local : LocalName ) -> QualName {
139284 QualName {
@@ -144,6 +289,25 @@ impl QualName {
144289 }
145290
146291 /// 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+ ///
147311 #[ inline]
148312 pub fn expanded ( & self ) -> ExpandedName {
149313 ExpandedName {
0 commit comments