mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Preserve Expat qualified names without namespace expansion (#9070)
Retain raw element and attribute prefixes when namespace_separator is None. Keep expanded names and parser configuration unchanged, with regression coverage in the pyexpat snippet. Assisted-by: OpenAI Codex:model identifier unavailable
This commit is contained in:
1 parent
1fa883b9b5
commit
2ab8af332f
2 files changed
+27
No files matched your search
@@ -1051,6 +1051,10 @@ mod _pyexpat {
|
||||
fn make_name(&self, name: &xml::name::OwnedName) -> String {
|
||||
match (&self.namespace_separator, &name.namespace) {
|
||||
(Some(sep), Some(ns)) => format!("{}{}{}", ns, sep, name.local_name),
|
||||
(None, _) => match &name.prefix {
|
||||
Some(prefix) => format!("{prefix}:{}", name.local_name),
|
||||
None => name.local_name.clone(),
|
||||
},
|
||||
_ => name.local_name.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
from xml.parsers import expat
|
||||
|
||||
# Without namespace expansion, element and attribute names retain prefixes.
|
||||
for separator, expected_name, expected_attribute in (
|
||||
(None, "p:x", "p:a"),
|
||||
("!", "urn:p!x", "urn:p!a"),
|
||||
):
|
||||
for ordered in (False, True):
|
||||
parser = expat.ParserCreate(namespace_separator=separator)
|
||||
parser.ordered_attributes = ordered
|
||||
starts = []
|
||||
ends = []
|
||||
parser.StartElementHandler = lambda name, attrs: starts.append((name, attrs))
|
||||
parser.EndElementHandler = ends.append
|
||||
parser.Parse('<r xmlns:p="urn:p"><p:x p:a="v" a="u"></p:x></r>', True)
|
||||
assert [name for name, attrs in starts] == ["r", expected_name]
|
||||
assert ends == [expected_name, "r"]
|
||||
expected_attributes = (
|
||||
[expected_attribute, "v", "a", "u"]
|
||||
if ordered
|
||||
else {expected_attribute: "v", "a": "u"}
|
||||
)
|
||||
assert starts[1][1] == expected_attributes
|
||||
Reference in new issue
Block a user