From c7c8a4e500e4c1ca74327b5d95a1b2904c2313b2 Mon Sep 17 00:00:00 2001 From: cahrens Date: Tue, 13 Sep 2016 16:53:03 -0400 Subject: [PATCH] Don't delete question p-tag if children. TNL-5510 --- common/lib/capa/capa/capa_problem.py | 8 ++++- .../lib/capa/capa/tests/test_capa_problem.py | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/common/lib/capa/capa/capa_problem.py b/common/lib/capa/capa/capa_problem.py index 6dd58fd2c5..687bdd7e8e 100644 --- a/common/lib/capa/capa/capa_problem.py +++ b/common/lib/capa/capa/capa_problem.py @@ -951,7 +951,13 @@ class LoncapaProblem(object): if p_tag and p_tag[0].text == inputfields[0].attrib['label']: label = p_tag[0].text - element_to_be_deleted = p_tag[0] + + p_tag_children = list(p_tag[0]) + if len(p_tag_children) == 0: + element_to_be_deleted = p_tag[0] + else: + # Delete the text from the p-tag, but leave the children. + p_tag[0].text = '' else: # In this case the problems don't have tag or label attribute inside the responsetype # so we will get the first preceding label tag w.r.t to this responsetype. diff --git a/common/lib/capa/capa/tests/test_capa_problem.py b/common/lib/capa/capa/tests/test_capa_problem.py index bdfacca2e5..edd02cd3c7 100644 --- a/common/lib/capa/capa/tests/test_capa_problem.py +++ b/common/lib/capa/capa/tests/test_capa_problem.py @@ -424,6 +424,39 @@ class CAPAProblemTest(unittest.TestCase): self.assert_question_tag(question1, question2, tag='label', label_attr=False) self.assert_question_tag(question1, question2, tag='p', label_attr=True) + def test_question_tag_child_left(self): + """ + If the "old" question tag has children, don't delete the children when + transforming to the new label tag. + """ + xml = """ + +

Question

+ + + choice1 + choice2 + + +
+ """ + + problem = new_loncapa_problem(xml) + self.assertEqual( + problem.problem_data, + { + '1_2_1': + { + 'label': "Question", + 'descriptions': {} + } + } + ) + # img tag is still present within the paragraph, but p text has been deleted + self.assertEqual(len(problem.tree.xpath('//p')), 1) + self.assertEqual(problem.tree.xpath('//p')[0].text, '') + self.assertEqual(len(problem.tree.xpath('//p/img')), 1) + @ddt.ddt class CAPAMultiInputProblemTest(unittest.TestCase):