From 6738a37ec1531446590f49c5c1fd5ee2d50579d7 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 27 Aug 2013 11:08:22 -0400 Subject: [PATCH 1/2] Keep comments in capa XML from causing failures Comments (and processing instructions!) are handled oddly in lxml. This change will keep them from causing failures. They will be omitted from the HTML generated, which is fine, since they aren't needed there. --- common/lib/capa/capa/capa_problem.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/common/lib/capa/capa/capa_problem.py b/common/lib/capa/capa/capa_problem.py index c2bdeadc21..08a223f609 100644 --- a/common/lib/capa/capa/capa_problem.py +++ b/common/lib/capa/capa/capa_problem.py @@ -555,6 +555,13 @@ class LoncapaProblem(object): Used by get_html. ''' + if not isinstance(problemtree.tag, basestring): + # Comment and ProcessingInstruction nodes are not Elements, + # and we're ok leaving those behind. + # BTW: etree gives us no good way to distinguish these things + # other than to examine .tag to see if it's a string. :( + return + if (problemtree.tag == 'script' and problemtree.get('type') and 'javascript' in problemtree.get('type')): # leave javascript intact. From e88e04d3a6704ccdabe48f7b0fb6fe0b09ae8078 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 27 Aug 2013 11:44:41 -0400 Subject: [PATCH 2/2] A test that our XML-comments fix works. --- .../lib/capa/capa/tests/test_html_render.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/common/lib/capa/capa/tests/test_html_render.py b/common/lib/capa/capa/tests/test_html_render.py index 9bc326d7b9..8e343ee1cf 100644 --- a/common/lib/capa/capa/tests/test_html_render.py +++ b/common/lib/capa/capa/tests/test_html_render.py @@ -226,6 +226,26 @@ class CapaHtmlRenderTest(unittest.TestCase): span_element = rendered_html.find('span') self.assertEqual(span_element.get('attr'), "TEST") + def test_xml_comments_and_other_odd_things(self): + # Comments and processing instructions should be skipped. + xml_str = textwrap.dedent("""\ + + + ]> + + + + + """) + + # Create the problem + problem = new_loncapa_problem(xml_str) + + # Render the HTML + the_html = problem.get_html() + self.assertRegexpMatches(the_html, r"
\s+
") + def _create_test_file(self, path, content_str): test_fp = self.system.filestore.open(path, "w") test_fp.write(content_str)