2019-12-21 20:52:34 +00:00
|
|
|
//! XML namespacing support
|
|
|
|
|
2021-11-05 22:32:43 +00:00
|
|
|
use crate::string::{AvmString, WStr, WString};
|
2021-09-30 23:13:06 +00:00
|
|
|
use gc_arena::{Collect, MutationContext};
|
2019-12-21 20:52:34 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
/// Represents a scoped name within XML.
|
|
|
|
///
|
|
|
|
/// All names in XML are optionally namespaced. Each namespace is represented
|
|
|
|
/// as a string; the document contains a mapping of namespaces to URIs.
|
|
|
|
///
|
2021-09-30 23:13:06 +00:00
|
|
|
/// Names without a namespace use the default namespace.
|
|
|
|
///
|
2019-12-21 20:52:34 +00:00
|
|
|
/// The special namespace `xmlns` is used to map namespace strings to URIs; it
|
|
|
|
/// should not be used for user-specified namespaces.
|
2021-09-30 23:13:06 +00:00
|
|
|
#[derive(Copy, Clone, Collect, PartialEq, Eq, PartialOrd, Ord)]
|
2019-12-21 20:52:34 +00:00
|
|
|
#[collect(no_drop)]
|
2021-09-30 23:13:06 +00:00
|
|
|
pub struct XmlName<'gc> {
|
|
|
|
/// The position of the namespace separator in the name, if the name is namespaced.
|
|
|
|
namespace_sep: Option<usize>,
|
|
|
|
name: AvmString<'gc>,
|
2019-12-21 20:52:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 23:13:06 +00:00
|
|
|
impl<'gc> XmlName<'gc> {
|
|
|
|
pub fn in_namespace(
|
|
|
|
gc_context: MutationContext<'gc, '_>,
|
2021-11-05 22:32:43 +00:00
|
|
|
namespace: &WStr,
|
|
|
|
name: &WStr,
|
2021-09-30 23:13:06 +00:00
|
|
|
) -> Self {
|
|
|
|
let mut full_name = WString::from(namespace);
|
|
|
|
full_name.push_byte(b':');
|
|
|
|
full_name.push_str(name);
|
|
|
|
Self {
|
|
|
|
namespace_sep: Some(namespace.len()),
|
2021-10-05 21:12:41 +00:00
|
|
|
name: AvmString::new(gc_context, full_name),
|
2019-12-25 05:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:13:06 +00:00
|
|
|
pub fn in_default_namespace(name: AvmString<'gc>) -> Self {
|
|
|
|
Self {
|
|
|
|
namespace_sep: None,
|
|
|
|
name,
|
|
|
|
}
|
2019-12-21 20:52:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 23:13:06 +00:00
|
|
|
pub fn from_str(full_name: impl Into<AvmString<'gc>>) -> Self {
|
|
|
|
let full_name = full_name.into();
|
|
|
|
Self {
|
|
|
|
namespace_sep: full_name.find(b':'),
|
|
|
|
name: full_name,
|
2019-12-21 20:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the local part of this name.
|
2021-11-05 22:32:43 +00:00
|
|
|
pub fn local_name(&self) -> &WStr {
|
2021-09-30 23:13:06 +00:00
|
|
|
match self.namespace_sep {
|
2021-11-05 22:32:43 +00:00
|
|
|
Some(sep) => &self.name[sep + 1..],
|
|
|
|
None => &self.name,
|
2021-09-30 23:13:06 +00:00
|
|
|
}
|
2019-12-21 20:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the prefix part of this name, if available.
|
2021-11-05 22:32:43 +00:00
|
|
|
pub fn prefix(&self) -> Option<&WStr> {
|
|
|
|
self.namespace_sep.map(|sep| &self.name[..sep])
|
2019-12-21 20:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the fully qualified part of the name.
|
|
|
|
///
|
|
|
|
/// This consists of the namespace, if present, plus a colon and local name.
|
2021-09-30 23:13:06 +00:00
|
|
|
pub fn node_name(&self) -> AvmString<'gc> {
|
|
|
|
self.name
|
2019-12-21 20:52:34 +00:00
|
|
|
}
|
2020-08-24 03:38:14 +00:00
|
|
|
|
2021-09-30 23:13:06 +00:00
|
|
|
/// Compares both names as case-insensitve (for use in HTML parsing).
|
2020-08-24 03:38:14 +00:00
|
|
|
/// TODO: We shouldn't need this when we have a proper HTML parser.
|
2021-09-30 23:13:06 +00:00
|
|
|
pub fn eq_ignore_case(&self, other: XmlName<'gc>) -> bool {
|
2021-11-05 22:32:43 +00:00
|
|
|
self.name.eq_ignore_case(&other.name)
|
2020-08-24 03:38:14 +00:00
|
|
|
}
|
2019-12-21 20:52:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 23:13:06 +00:00
|
|
|
impl<'gc> fmt::Debug for XmlName<'gc> {
|
2019-12-21 20:52:34 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-02-12 13:03:17 +00:00
|
|
|
f.debug_struct("XmlName")
|
2021-09-30 23:13:06 +00:00
|
|
|
.field("namespace", &self.prefix())
|
|
|
|
.field("name", &self.local_name())
|
2019-12-21 20:52:34 +00:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|