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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2014-2017 The html5ever Project Developers. See the
// COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Types for tag and attribute names, and tree-builder functionality.

use std::cell::Ref;
use std::fmt;
use tendril::StrTendril;

pub use self::tree_builder::{create_element, AppendNode, AppendText, ElementFlags, NodeOrText};
pub use self::tree_builder::{ElemName, NextParserState, Tracer, TreeSink};
pub use self::tree_builder::{LimitedQuirks, NoQuirks, Quirks, QuirksMode};
use super::{LocalName, Namespace, Prefix};

/// An [expanded name], containing the tag and the namespace.
///
/// [expanded name]: https://www.w3.org/TR/REC-xml-names/#dt-expname
#[derive(Copy, Clone, Eq, Hash, PartialEq)]
pub struct ExpandedName<'a> {
    pub ns: &'a Namespace,
    pub local: &'a LocalName,
}

impl<'a> ElemName for ExpandedName<'a> {
    #[inline(always)]
    fn ns(&self) -> &Namespace {
        self.ns
    }

    #[inline(always)]
    fn local_name(&self) -> &LocalName {
        self.local
    }
}

impl<'a> ElemName for Ref<'a, ExpandedName<'a>> {
    #[inline(always)]
    fn ns(&self) -> &Namespace {
        self.ns
    }

    #[inline(always)]
    fn local_name(&self) -> &LocalName {
        self.local
    }
}

impl<'a> fmt::Debug for ExpandedName<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.ns.is_empty() {
            write!(f, "{}", self.local)
        } else {
            write!(f, "{{{}}}:{}", self.ns, self.local)
        }
    }
}

/// Helper to quickly create an expanded name.
///
/// Can be used with no namespace as `expanded_name!("", "some_name")`
/// or with a namespace as `expanded_name!(ns "some_name")`.  In the
/// latter case, `ns` is one of the symbols which the [`ns!`][ns]
/// macro accepts; note the lack of a comma between the `ns` and
/// `"some_name"`.
///
/// [ns]: macro.ns.html
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate markup5ever;
///
/// # fn main() {
/// use markup5ever::ExpandedName;
///
/// assert_eq!(
///     expanded_name!("", "div"),
///     ExpandedName {
///         ns: &ns!(),
///         local: &local_name!("div")
///     }
/// );
///
/// assert_eq!(
///     expanded_name!(html "div"),
///     ExpandedName {
///         ns: &ns!(html),
///         local: &local_name!("div")
///     }
/// );
/// # }
#[macro_export]
macro_rules! expanded_name {
    ("", $local: tt) => {
        $crate::interface::ExpandedName {
            ns: &ns!(),
            local: &local_name!($local),
        }
    };
    ($ns: ident $local: tt) => {
        $crate::interface::ExpandedName {
            ns: &ns!($ns),
            local: &local_name!($local),
        }
    };
}

pub mod tree_builder;

/// A fully qualified name (with a namespace), used to depict names of tags and attributes.
///
/// Namespaces can be used to differentiate between similar XML fragments. For example:
///
/// ```text
/// // HTML
/// <table>
///   <tr>
///     <td>Apples</td>
///     <td>Bananas</td>
///   </tr>
/// </table>
///
/// // Furniture XML
/// <table>
///   <name>African Coffee Table</name>
///   <width>80</width>
///   <length>120</length>
/// </table>
/// ```
///
/// Without XML namespaces, we can't use those two fragments in the same document
/// at the same time. However if we declare a namespace we could instead say:
///
/// ```text
///
/// // Furniture XML
/// <furn:table xmlns:furn="https://furniture.rs">
///   <furn:name>African Coffee Table</furn:name>
///   <furn:width>80</furn:width>
///   <furn:length>120</furn:length>
/// </furn:table>
/// ```
///
/// and bind the prefix `furn` to a different namespace.
///
/// For this reason we parse names that contain a colon in the following way:
///
/// ```text
/// <furn:table>
///    |    |
///    |    +- local name
///    |
///  prefix (when resolved gives namespace_url `https://furniture.rs`)
/// ```
///
/// NOTE: `Prefix`, `LocalName` and `Prefix` are all derivative of
/// `string_cache::atom::Atom` and `Atom` implements `Deref<str>`.
///
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone)]
#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))]
pub struct QualName {
    /// The prefix of qualified (e.g. `furn` in `<furn:table>` above).
    /// Optional (since some namespaces can be empty or inferred), and
    /// only useful for namespace resolution (since different prefix
    /// can still resolve to same namespace)
    ///
    /// ```
    ///
    /// # fn main() {
    /// use markup5ever::{QualName, Namespace, LocalName, Prefix};
    ///
    /// let qual = QualName::new(
    ///     Some(Prefix::from("furn")),
    ///     Namespace::from("https://furniture.rs"),
    ///     LocalName::from("table"),
    /// );
    ///
    /// assert_eq!("furn", &qual.prefix.unwrap());
    ///
    /// # }
    /// ```
    pub prefix: Option<Prefix>,
    /// The namespace after resolution (e.g. `https://furniture.rs` in example above).
    ///
    /// ```
    /// # use markup5ever::{QualName, Namespace, LocalName, Prefix};
    ///
    /// # fn main() {
    /// # let qual = QualName::new(
    /// #    Some(Prefix::from("furn")),
    /// #    Namespace::from("https://furniture.rs"),
    /// #    LocalName::from("table"),
    /// # );
    ///
    /// assert_eq!("https://furniture.rs", &qual.ns);
    /// # }
    /// ```
    ///
    /// When matching namespaces used by HTML we can use `ns!` macro.
    /// Although keep in mind that ns! macro only works with namespaces
    /// that are present in HTML spec (like `html`, `xmlns`, `svg`, etc.).
    ///
    /// ```
    /// #[macro_use] extern crate markup5ever;
    ///
    /// # use markup5ever::{QualName, Namespace, LocalName, Prefix};
    ///
    /// let html_table = QualName::new(
    ///    None,
    ///    ns!(html),
    ///    LocalName::from("table"),
    /// );
    ///
    /// assert!(
    ///   match html_table.ns {
    ///     ns!(html) => true,
    ///     _ => false,
    ///   }
    /// );
    ///
    /// ```
    pub ns: Namespace,
    /// The local name (e.g. `table` in `<furn:table>` above).
    ///
    /// ```
    /// # use markup5ever::{QualName, Namespace, LocalName, Prefix};
    ///
    /// # fn main() {
    /// # let qual = QualName::new(
    /// #    Some(Prefix::from("furn")),
    /// #    Namespace::from("https://furniture.rs"),
    /// #    LocalName::from("table"),
    /// # );
    ///
    /// assert_eq!("table", &qual.local);
    /// # }
    /// ```
    /// When matching local name we can also use the `local_name!` macro:
    ///
    /// ```
    /// #[macro_use] extern crate markup5ever;
    ///
    /// # use markup5ever::{QualName, Namespace, LocalName, Prefix};
    ///
    /// # let qual = QualName::new(
    /// #    Some(Prefix::from("furn")),
    /// #    Namespace::from("https://furniture.rs"),
    /// #    LocalName::from("table"),
    /// # );
    ///
    /// // Initialize qual to furniture example
    ///
    /// assert!(
    ///   match qual.local {
    ///     local_name!("table") => true,
    ///     _ => false,
    ///   }
    /// );
    ///
    /// ```
    pub local: LocalName,
}

impl<'a> ElemName for Ref<'a, QualName> {
    #[inline(always)]
    fn ns(&self) -> &Namespace {
        &self.ns
    }

    #[inline(always)]
    fn local_name(&self) -> &LocalName {
        &self.local
    }
}

impl<'a> ElemName for &'a QualName {
    #[inline(always)]
    fn ns(&self) -> &Namespace {
        &self.ns
    }

    #[inline(always)]
    fn local_name(&self) -> &LocalName {
        &self.local
    }
}

impl QualName {
    /// Basic constructor function.
    ///
    /// First let's try it for the following example where `QualName`
    /// is defined as:
    /// ```text
    /// <furn:table> <!-- namespace url is https://furniture.rs -->
    /// ```
    ///
    /// Given this definition, we can define `QualName` using strings.
    ///
    /// ```
    /// use markup5ever::{QualName, Namespace, LocalName, Prefix};
    ///
    /// # fn main() {
    /// let qual_name = QualName::new(
    ///     Some(Prefix::from("furn")),
    ///     Namespace::from("https://furniture.rs"),
    ///     LocalName::from("table"),
    /// );
    /// # }
    /// ```
    ///
    /// If we were instead to construct this element instead:
    ///
    /// ```text
    ///
    /// <table>
    ///  ^^^^^---- no prefix and thus default html namespace
    ///
    /// ```
    ///
    /// Or could define it using macros, like so:
    ///
    /// ```
    /// #[macro_use] extern crate markup5ever;
    /// use markup5ever::{QualName, Namespace, LocalName, Prefix};
    ///
    /// # fn main() {
    /// let qual_name = QualName::new(
    ///     None,
    ///     ns!(html),
    ///     local_name!("table")
    /// );
    /// # }
    /// ```
    ///
    /// Let's analyse the above example.
    /// Since we have no prefix its value is None. Second we have html namespace.
    /// In html5ever html namespaces are supported out of the box,
    /// we can write `ns!(html)` instead of typing `Namespace::from("http://www.w3.org/1999/xhtml")`.
    /// Local name is also one of the HTML elements local names, so can
    /// use `local_name!("table")` macro.
    ///
    #[inline]
    pub fn new(prefix: Option<Prefix>, ns: Namespace, local: LocalName) -> QualName {
        QualName { prefix, ns, local }
    }

    /// Take a reference of `self` as an `ExpandedName`, dropping the unresolved prefix.
    ///
    /// In XML and HTML prefixes are only used to extract the relevant namespace URI.
    /// Expanded name only contains resolved namespace and tag name, which are only
    /// relevant parts of an XML or HTML tag and attribute name respectively.
    ///
    /// In lieu of our XML Namespace example
    ///
    /// ```text
    /// <furn:table> <!-- namespace url is https://furniture.rs -->
    /// ```
    /// For it the expanded name would become roughly equivalent to:
    ///
    /// ```text
    /// ExpandedName {
    ///    ns: "https://furniture.rs",
    ///    local: "table",
    /// }
    /// ```
    ///
    #[inline]
    pub fn expanded(&self) -> ExpandedName {
        ExpandedName {
            ns: &self.ns,
            local: &self.local,
        }
    }
}

/// A tag attribute, e.g. `class="test"` in `<div class="test" ...>`.
///
/// The namespace on the attribute name is almost always ns!("").
/// The tokenizer creates all attributes this way, but the tree
/// builder will adjust certain attribute names inside foreign
/// content (MathML, SVG).
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
pub struct Attribute {
    /// The name of the attribute (e.g. the `class` in `<div class="test">`)
    pub name: QualName,
    /// The value of the attribute (e.g. the `"test"` in `<div class="test">`)
    pub value: StrTendril,
}

#[cfg(test)]
mod tests {
    use super::Namespace;

    #[test]
    fn ns_macro() {
        assert_eq!(ns!(), Namespace::from(""));

        assert_eq!(ns!(html), Namespace::from("http://www.w3.org/1999/xhtml"));
        assert_eq!(
            ns!(xml),
            Namespace::from("http://www.w3.org/XML/1998/namespace")
        );
        assert_eq!(ns!(xmlns), Namespace::from("http://www.w3.org/2000/xmlns/"));
        assert_eq!(ns!(xlink), Namespace::from("http://www.w3.org/1999/xlink"));
        assert_eq!(ns!(svg), Namespace::from("http://www.w3.org/2000/svg"));
        assert_eq!(
            ns!(mathml),
            Namespace::from("http://www.w3.org/1998/Math/MathML")
        );
    }
}