Skip to content

Commit bbe25b4

Browse files
committed
spec_object_parser: a yet another edge case when handling ATTRIBUTE-VALUE-XHTML
1 parent a57a53e commit bbe25b4

6 files changed

Lines changed: 53 additions & 7 deletions

File tree

reqif/helpers/lxml.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def my_escape_title(string: str) -> str:
4545
# FIXME: Would be great to find a better solution for this.
4646
def stringify_namespaced_children(node, namespace_tag=None) -> str:
4747
if namespace_tag is None:
48+
assert (
49+
len(node.nsmap) > 0
50+
), f"This method must be called on a namespaced tag:\n{dump_xml_node(node)}" # noqa: E501
4851
nskey = next(iter(node.nsmap.keys()))
4952
else:
5053
nskey = namespace_tag
@@ -79,7 +82,7 @@ def _stringify_reqif_ns_node(node):
7982
return string
8083

8184

82-
# https://stackoverflow.com/a/4624146/598057
85+
# https://stackoverflow.com/a/28173933/598057
8386
def stringify_children(node):
8487
return "".join(
8588
chunk
@@ -92,14 +95,17 @@ def stringify_children(node):
9295
child,
9396
encoding=str,
9497
with_tail=False,
95-
pretty_print=True,
98+
pretty_print=False,
9699
),
97100
child.tail,
98101
)
99102
for child in node.getchildren()
100103
)
101104
),
102-
(node.tail,),
105+
# The original snippet prints the node tail for some reason which is
106+
# surprisingly unnecessary.
107+
# (node.tail,), # noqa: ERA001
108+
(None,),
103109
)
104110
if chunk
105111
)

reqif/parsers/spec_object_parser.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from reqif.helpers.lxml import (
77
lxml_convert_children_from_reqif_ns_xhtml_string,
8+
stringify_children,
89
stringify_namespaced_children,
910
)
1011
from reqif.helpers.string.xhtml_indent import reqif_unindent_xhtml_string
@@ -205,10 +206,22 @@ def parse(spec_object_xml) -> ReqIFSpecObject:
205206
)
206207
elif attribute_xml.tag == "ATTRIBUTE-VALUE-XHTML":
207208
the_value = attribute_xml.find("THE-VALUE")
208-
attribute_value = stringify_namespaced_children(the_value)
209-
attribute_value_stripped_xhtml = reqif_unindent_xhtml_string(
210-
lxml_convert_children_from_reqif_ns_xhtml_string(the_value)
211-
)
209+
210+
# Edge: There are not <xhtml:...> or <reqif-xhtml...> tags.
211+
if len(the_value.nsmap) > 0:
212+
attribute_value = stringify_namespaced_children(the_value)
213+
attribute_value_stripped_xhtml = (
214+
reqif_unindent_xhtml_string(
215+
lxml_convert_children_from_reqif_ns_xhtml_string(
216+
the_value
217+
)
218+
)
219+
)
220+
else:
221+
attribute_value = stringify_children(the_value)
222+
attribute_value_stripped_xhtml = (
223+
reqif_unindent_xhtml_string(attribute_value)
224+
)
212225
attribute_definition_ref = (
213226
attribute_xml.find("DEFINITION")
214227
.find("ATTRIBUTE-DEFINITION-XHTML-REF")

tests/integration/reqif/SPEC-OBJECTS/02_XHTML_attribute/sample.reqif renamed to tests/integration/reqif/SPEC-OBJECTS/ATTRIBUTE-VALUE-XHTML/01_XHTML_attribute/sample.reqif

File renamed without changes.

tests/integration/reqif/SPEC-OBJECTS/02_XHTML_attribute/test.itest renamed to tests/integration/reqif/SPEC-OBJECTS/ATTRIBUTE-VALUE-XHTML/01_XHTML_attribute/test.itest

File renamed without changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd" xmlns:configuration="http://eclipse.org/rmf/pror/toolextensions/1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml">
3+
<CORE-CONTENT>
4+
<REQ-IF-CONTENT>
5+
<SPEC-OBJECTS>
6+
<SPEC-OBJECT IDENTIFIER="TEST_SPEC_OBJECT_IDENTIFIER" LAST-CHANGE="2021-10-15T11:34:36.007+02:00">
7+
<VALUES>
8+
<ATTRIBUTE-VALUE-XHTML>
9+
<DEFINITION>
10+
<ATTRIBUTE-DEFINITION-XHTML-REF>_gFhrXWojEeuExICsU7Acmg</ATTRIBUTE-DEFINITION-XHTML-REF>
11+
</DEFINITION>
12+
<THE-VALUE>
13+
Some text without any tags.
14+
</THE-VALUE>
15+
</ATTRIBUTE-VALUE-XHTML>
16+
</VALUES>
17+
<TYPE>
18+
<SPEC-OBJECT-TYPE-REF>TEST_SPEC_OBJECT_TYPE_IDENTIFIER_FUNCTIONAL</SPEC-OBJECT-TYPE-REF>
19+
</TYPE>
20+
</SPEC-OBJECT>
21+
</SPEC-OBJECTS>
22+
</REQ-IF-CONTENT>
23+
</CORE-CONTENT>
24+
</REQ-IF>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RUN: mkdir -p %S/output
2+
RUN: %reqif passthrough %S/sample.reqif %S/output/sample.reqif
3+
RUN: diff %S/sample.reqif %S/output/sample.reqif

0 commit comments

Comments
 (0)