From 2c0dfd0ecd931973969ae9c8141523cfc9b3dcf1 Mon Sep 17 00:00:00 2001 From: fliiiix Date: Thu, 28 Dec 2023 15:33:43 +0100 Subject: [PATCH] Allow multiple XML-tags with the same name to be parsed as list --- feedparser/mixin.py | 15 ++++++++++++--- .../namespace/multiple-elements-with-same-key.xml | 10 ++++++++++ .../node_precedence/atom10_arbitrary_element.xml | 8 ++++---- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 tests/wellformed/namespace/multiple-elements-with-same-key.xml diff --git a/feedparser/mixin.py b/feedparser/mixin.py index 2a649f1b1..a45f1745e 100644 --- a/feedparser/mixin.py +++ b/feedparser/mixin.py @@ -635,9 +635,18 @@ def pop(self, element, strip_whitespace=1): old_value_depth = self.property_depth_map.setdefault( self.entries[-1], {} ).get(element) - if old_value_depth is None or self.depth <= old_value_depth: - self.property_depth_map[self.entries[-1]][element] = self.depth - self.entries[-1][element] = output + self.property_depth_map[self.entries[-1]][element] = self.depth + # Legacy elements are squashed to a singel item + legacy_elements = ["title", "summary", "author", "id"] + if element in self.entries[-1] and element not in legacy_elements: + previous_output = self.entries[-1][element] + if not isinstance(previous_output, list): + previous_output = [previous_output] + previous_output.append(output) + self.entries[-1][element] = previous_output + else: + if old_value_depth is None or self.depth <= old_value_depth: + self.entries[-1][element] = output if self.incontent: contentparams = copy.deepcopy(self.contentparams) contentparams["value"] = output diff --git a/tests/wellformed/namespace/multiple-elements-with-same-key.xml b/tests/wellformed/namespace/multiple-elements-with-same-key.xml new file mode 100644 index 000000000..c144342e8 --- /dev/null +++ b/tests/wellformed/namespace/multiple-elements-with-same-key.xml @@ -0,0 +1,10 @@ + + + + foo + bar + + diff --git a/tests/wellformed/node_precedence/atom10_arbitrary_element.xml b/tests/wellformed/node_precedence/atom10_arbitrary_element.xml index 58598dacc..a163424c8 100644 --- a/tests/wellformed/node_precedence/atom10_arbitrary_element.xml +++ b/tests/wellformed/node_precedence/atom10_arbitrary_element.xml @@ -1,18 +1,18 @@ Correct Value - Incorrect Value + Incorrect Value - Incorrect Value + Incorrect Value Correct Value - \ No newline at end of file +