Skip to content

Commit 1b4e978

Browse files
author
bors-servo
authored
Auto merge of #370 - Ygg01:docs_qual_name, r=jdm
Extended examples in API docs for QualName This PR adds documentation for `QualName` as well as some doc tests for it relating to #367 . @kornelski is this what you had in mind? This PR is a minor change (since no API was changed other than the docs). Comments and suggestions welcome.
2 parents 1f0442a + 5bc4c7e commit 1b4e978

File tree

2 files changed

+176
-6
lines changed

2 files changed

+176
-6
lines changed

markup5ever/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "markup5ever"
3-
version = "0.7.6"
3+
version = "0.7.7"
44
authors = [ "The html5ever Project Developers" ]
55
license = "MIT / Apache-2.0"
66
repository = "https://github.com/servo/html5ever"

markup5ever/interface/mod.rs

Lines changed: 175 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,171 @@ 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
/// ```
125+
///
126+
/// NOTE: `Prefix`, `LocalName` and `Prefix` are all derivative of
127+
/// `string_cache::atom::Atom` and `Atom` implements `Deref<str>`.
128+
///
124129
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone)]
125130
#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))]
126131
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+
/// ```
128152
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+
/// ```
130192
pub ns: Namespace,
131193
/// 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+
/// ```
132231
pub local: LocalName,
133232
}
134233

135234
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+
///
137288
#[inline]
138289
pub fn new(prefix: Option<Prefix>, ns: Namespace, local: LocalName) -> QualName {
139290
QualName {
@@ -144,6 +295,25 @@ impl QualName {
144295
}
145296

146297
/// 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+
///
147317
#[inline]
148318
pub fn expanded(&self) -> ExpandedName {
149319
ExpandedName {

0 commit comments

Comments
 (0)