namespace method
Binds a namespace prefix
to the provided uri
. The prefix
can be
omitted to declare a default namespace. Throws an ArgumentError if
the prefix
is invalid or conflicts with an existing declaration.
Implementation
void namespace(String uri, [String? prefix]) {
if (prefix == ns.xmlns || prefix == ns.xml) {
throw ArgumentError('The "$prefix" prefix cannot be bound.');
}
if (optimizeNamespaces &&
_stack.any(
(builder) => builder.namespaces.containsKey(uri) && builder.namespaces[uri]!.prefix == prefix)) {
// Namespace prefix already correctly specified in an ancestor.
return;
}
if (_stack.last.namespaces.values.any((meta) => meta.prefix == prefix)) {
throw ArgumentError('The "$prefix" prefix conflicts with existing binding.');
}
final meta = NamespaceData(prefix, false);
_stack.last.attributes.add(XmlAttributeSyntheticImpl(meta.name, uri, XmlAttributeType.DOUBLE_QUOTE));
_stack.last.namespaces[uri] = meta;
}