diff --git a/common/lib/xmodule/xmodule/stringify.py b/common/lib/xmodule/xmodule/stringify.py index dad964140f..1e3fa91210 100644 --- a/common/lib/xmodule/xmodule/stringify.py +++ b/common/lib/xmodule/xmodule/stringify.py @@ -12,9 +12,17 @@ def stringify_children(node): fixed from http://stackoverflow.com/questions/4624062/get-all-text-inside-a-tag-in-lxml ''' - parts = ([node.text] + - list(chain(*([etree.tostring(c), c.tail] - for c in node.getchildren()) - ))) + # Useful things to know: + + # node.tostring() -- generates xml for the node, including start + # and end tags. We'll use this for the children. + # node.text -- the text after the end of a start tag to the start + # of the first child + # node.tail -- the text after the end this tag to the start of the + # next element. + parts = [node.text] + for c in node.getchildren(): + parts.append(etree.tostring(c, with_tail=True)) + # filter removes possible Nones in texts and tails return ''.join(filter(None, parts)) diff --git a/common/lib/xmodule/xmodule/tests/test_stringify.py b/common/lib/xmodule/xmodule/tests/test_stringify.py index 1c6ee855f3..29e99bef56 100644 --- a/common/lib/xmodule/xmodule/tests/test_stringify.py +++ b/common/lib/xmodule/xmodule/tests/test_stringify.py @@ -1,4 +1,4 @@ -from nose.tools import assert_equals +from nose.tools import assert_equals, assert_true, assert_false from lxml import etree from xmodule.stringify import stringify_children @@ -8,3 +8,32 @@ def test_stringify(): xml = etree.fromstring(html) out = stringify_children(xml) assert_equals(out, text) + +def test_stringify_again(): + html = """A voltage source is non-linear! +
+ \(V=V_C\)
+