def insert(self, name): # Do we want page markup to permit course admins to edit hooks? show_admin_content = False if (self.prefs and self.prefs.show_hooks and Roles.is_course_admin(self.course.app_context)): show_admin_content = True if self.course.version == courses.CourseModel12.VERSION: show_admin_content = False # Look up desired content chunk in course.yaml dict/sub-dict. content = '' environ = self.course.app_context.get_environ() for part in name.split(':'): if part in environ: item = environ[part] if type(item) == str: content = item else: environ = item if show_admin_content and not self._has_visible_content(content): content += name # Add the content to the page in response to the hook call. hook_div = safe_dom.Element('div', className='gcb-html-hook', id=re.sub('[^a-zA-Z-]', '-', name)) hook_div.add_child(tags.html_to_safe_dom(content, self)) # Mark up content to enable edit controls if show_admin_content: hook_div.add_attribute(onclick='gcb_edit_hook_point("%s")' % name) hook_div.add_attribute(className='gcb-html-hook-edit') return jinja2.Markup(hook_div.sanitized)
def get_question_preview(self): template_values = {} template_values['gcb_course_base'] = self.get_base_href(self) template_values['question'] = tags.html_to_safe_dom( '<question quid="%s">' % self.request.get('quid'), self) self.response.write(self.get_template( 'question_preview.html', []).render(template_values))
def get_question_group_preview(self): template_values = {} template_values['gcb_course_base'] = self.get_base_href(self) template_values['question'] = tags.html_to_safe_dom( '<question-group qgid="{}">'.format(self.request.get('qgid')), self) self.response.write(self.get_template( 'question_preview.html').render(template_values))
def get_question_preview(self): template_values = {} template_values['gcb_course_base'] = self.get_base_href(self) template_values['question'] = tags.html_to_safe_dom( '<question quid="{}">'.format(self.request.get('quid')), self) self.response.write( self.get_template('question_preview.html').render(template_values))
def test_context_aware_tags(self): html = '<div><count></count><simple></simple><count></count></div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEqual( ( '<div>2</div><div><Count>1</Count><SimpleTag></SimpleTag>' '<Count>2</Count></div><div>foot</div>' ), str(safe_dom))
def insert(self, name): name = name.replace(self.BACKWARD_COMPATIBILITY_SEPARATOR, self.SEPARATOR) content = self.content.get(name, "") # Add the content to the page in response to the hook call. hook_div = safe_dom.Element("div", className="gcb-html-hook", id=re.sub("[^a-zA-Z-]", "-", name)) hook_div.add_child(tags.html_to_safe_dom(content, self)) # Mark up content to enable edit controls if self.show_admin_content: hook_div.add_attribute(onclick='gcb_edit_hook_point("%s")' % name) hook_div.add_attribute(className="gcb-html-hook-edit") return jinja2.Markup(hook_div.sanitized)
def insert(self, name): name = name.replace(self.BACKWARD_COMPATIBILITY_SEPARATOR, self.SEPARATOR) content = self.content.get(name, '') # Add the content to the page in response to the hook call. hook_div = safe_dom.Element('div', className='gcb-html-hook', id=re.sub('[^a-zA-Z-]', '-', name)) hook_div.add_child(tags.html_to_safe_dom(content, self)) # Mark up content to enable edit controls if self.show_admin_content: hook_div.add_attribute(onclick='gcb_edit_hook_point("%s")' % name) hook_div.add_attribute(className='gcb-html-hook-edit') return jinja2.Markup(hook_div.sanitized)
def get_question_preview(self): """ Provides a preview of quiz questions. Invoked from student_dashboard. The question is displayed in a modal window that is initialized in modal-window.js. This is an adaptation of the question_preview used by the dashboard module. It supports Quizly questions. """ self.template_value['navbar'] = {'teacher': True} self.template_value['resources_path'] = RESOURCES_PATH url = self.request.get('url') if url == '': self.template_value['question'] = tags.html_to_safe_dom( '<question quid="{}">'.format(self.request.get('quid')), self) else: self.template_value['url'] = url self.template_value['question'] = 'Quizly' self.render(QUESTION_PREVIEW_TEMPLATE)
def test_simple_tag_consumes_children(self): html = '<div><simple><p>child1</p></simple></div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals('<div><SimpleTag></SimpleTag></div>', str(safe_dom))
def test_svg_tag_is_accepted(self): html = '<svg></svg>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals(html, str(safe_dom))
def test_mix_of_plain_text_and_tags_is_passed(self): html = 'This is plain text<br/>on several<br/>lines' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals(html, str(safe_dom))
def test_simple_tag_is_replaced(self): html = '<div><simple></simple></div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals('<div><SimpleTag></SimpleTag></div>', str(safe_dom))
def test_context_aware_tags(self): html = '<div><count></count><simple></simple><count></count></div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEqual( ('<div>2</div><div><Count>1</Count><SimpleTag></SimpleTag>' '<Count>2</Count></div><div>foot</div>'), str(safe_dom))
def test_chains_of_tags(self): html = '<div><reroot><p><simple></p></reroot></div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals( '<div><Re><Root><p><SimpleTag></SimpleTag></p></Root></Re></div>', str(safe_dom))
def test_scripts_are_not_escaped(self): html = '<script>alert("2"); var a = (1 < 2 && 2 > 1);</script>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals(html, str(safe_dom))
def test_reroot_tag_puts_children_in_new_root(self): html = '<div><reroot><p>one</p><p>two</p></reroot></div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals( '<div><Re><Root><p>one</p><p>two</p></Root></Re></div>', str(safe_dom))
def test_complex_tag_preserves_its_own_children(self): html = '<div><complex/></div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals( '<div><Complex><Child>Text</Child></Complex></div>', str(safe_dom))
def test_simple_tag_consumes_children(self): html = '<div><simple><p>child1</p></simple></div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals( '<div><SimpleTag></SimpleTag></div>', str(safe_dom))
def test_replaced_tag_preserves_tail_text(self): html = '<div><simple></simple>Tail text</div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals( '<div><SimpleTag></SimpleTag>Tail text</div>', str(safe_dom))
def test_complex_tag_preserves_its_own_children(self): html = '<div><complex/></div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals('<div><Complex><Child>Text</Child></Complex></div>', str(safe_dom))
def test_replaced_tag_preserves_tail_text(self): html = '<div><simple></simple>Tail text</div>' safe_dom = tags.html_to_safe_dom(html, self.mock_handler) self.assertEquals('<div><SimpleTag></SimpleTag>Tail text</div>', str(safe_dom))
def test_none_is_treated_as_empty(self): safe_dom = tags.html_to_safe_dom(None, self.mock_handler) self.assertEquals('', str(safe_dom))
def test_empty_text_is_passed(self): safe_dom = tags.html_to_safe_dom(None, self.mock_handler) self.assertEquals('', str(safe_dom))
def test_plain_text_is_passed(self): safe_dom = tags.html_to_safe_dom('This is plain text.', self.mock_handler) self.assertEquals('This is plain text.', str(safe_dom))
def test_plain_text_is_passed(self): safe_dom = tags.html_to_safe_dom( 'This is plain text.', self.mock_handler) self.assertEquals('This is plain text.', str(safe_dom))