namespace method
Binds a namespace prefix
to the provided uri
. The prefix
can be
omitted to declare a default namespace. Throws an ArgumentException if
the prefix
is invalid or conflicts with an existing declaration.
Implementation
void namespace(String? uri, [String? prefix]) {
if (prefix == xmlns || prefix == xml) {
throw ArgumentException('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 ArgumentException(
'The "$prefix" prefix conflicts with existing binding.');
}
final meta = NamespaceData(prefix, false);
_stack.last.attributes
.add(XmlAttribute(meta.name, uri!, XmlAttributeType.DOUBLE_QUOTE));
_stack.last.namespaces[uri] = meta;
}