Skip to content

Commit

Permalink
Correctly expose xml namespaces imported via xacro:include within a m…
Browse files Browse the repository at this point in the history
…acro (#287)

Namespace declarations are lifted to the parent of the include node.
In case of a macro instantiation, this is a dummy node whose contents are kept
but which is removed itself.
Hence, before removing the dummy node, we need to lift any namespace definitions.
  • Loading branch information
rhaschke authored Oct 2, 2021
1 parent 7c87000 commit ce201d7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/xacro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,11 @@ def handle_macro_call(node, macros, symbols):

eval_all(body, macros, scoped)

# Replaces the macro node with the expansion
# Remove any comments directly before the macro call
remove_previous_comments(node)
# Lift all namespace attributes from the expanded body node to node's parent
import_xml_namespaces(node.parentNode, body.attributes)
# Replaces the macro node with the expansion
replace_node(node, by=body, content_only=True)

macrostack.pop()
Expand Down
1 change: 1 addition & 0 deletions test/namespace.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a xmlns:xacro="http://www.ros.org/xacro" xmlns:b="http://www.ros.org/b" />
11 changes: 11 additions & 0 deletions test/test_xacro.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,17 @@ def test_error_reporting(self):
'''.format(file="./raise.xacro" if self.in_order else "???")
self.assertEqual(output, expected)

def test_xml_namespace_lifting(self):
src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro" xmlns:a="http://www.ros.org/a">
<xacro:macro name="test">
<xacro:include filename="namespace.xml"></xacro:include>
</xacro:macro>
<xacro:test />
</a>'''
res = '''<a xmlns:a="http://www.ros.org/a" xmlns:b="http://www.ros.org/b" />'''
self.assert_matches(self.quick_xacro(src), res)


# test class for in-order processing
class TestXacroInorder(TestXacro):
def __init__(self, *args, **kwargs):
Expand Down

0 comments on commit ce201d7

Please sign in to comment.