def test_id_class_style_attrib_order(self): kwargs = { 'id': "container", 'class': "some classes here", 'style': "position:absolute", 'alt': "This is after the others" } div = Tag('div', kwargs, content="Test") self.assertHtmlEqual( div.render(), '<div id="container" class="some classes here" style="position:absolute" alt="This is after the others">Test</div>' )
def render(self): def absolutify(el, html): html.style_string += el.overlap_styles return html def layoutify(el, html): html.class_string += ' hi%d span%d' % (el.layout.height, el.layout.width) return html def add_padding(el, html): if el.layout.has_padding(): html.style_string += "; padding: %dpx %dpx %dpx %dpx" % ( el.layout.t_padding, el.layout.r_padding, el.layout.b_padding, el.layout.l_padding) return html def add_text_align(el, html): if el.layout.alignment != 'left': # create wrapper if not already wrapper if html.is_wrapper: wrapper = html else: wrapper = Tag('div', {}, content=html) wrapper.style_string += "; text-align: %s" % el.layout.alignment return wrapper else: return html if self.has_overlapping_nodes: # set position absolute, with pixel dimensions, all in the style attribute htmls = [el.html() for el in self.uiels] htmls = [ add_padding( el, layoutify(el, absolutify(el, add_text_align(el, html)))) for el, html in zip(self.uiels, htmls) ] column_element = Tag('div', { 'class': self.class_string, 'style': self.style_string }, content=htmls) else: el = self.uiels[0] html = el.html() column_element = add_padding( el, layoutify(el, add_text_align(el, html))) column_element.class_string += ' ' + self.class_string return column_element.render()
def render(self): def absolutify(el, html): html.style_string += el.overlap_styles return html def layoutify(el, html): html.class_string += ' hi%d span%d' % (el.layout.height, el.layout.width) return html def add_padding(el, html): if el.layout.has_padding(): html.style_string += "; padding: %dpx %dpx %dpx %dpx" % ( el.layout.t_padding, el.layout.r_padding, el.layout.b_padding, el.layout.l_padding) return html def add_text_align(el, html): if el.layout.alignment != 'left': # create wrapper if not already wrapper if html.is_wrapper: wrapper = html else: wrapper = Tag('div', {}, content=html) wrapper.style_string += "; text-align: %s" % el.layout.alignment return wrapper else: return html if self.has_overlapping_nodes: # set position absolute, with pixel dimensions, all in the style attribute htmls = [ el.html() for el in self.uiels ] htmls = [ add_padding(el, layoutify(el, absolutify(el, add_text_align(el, html)))) for el, html in zip(self.uiels, htmls) ] column_element = Tag('div', { 'class': self.class_string, 'style': self.style_string }, content=htmls) else: el = self.uiels[0] html = el.html() column_element = add_padding(el, layoutify(el, add_text_align(el, html))) column_element.class_string += ' ' + self.class_string return column_element.render()
def add_text_align(el, html): if el.layout.alignment != 'left': # create wrapper if not already wrapper if html.is_wrapper: wrapper = html else: wrapper = Tag('div', {}, content=html) wrapper.style_string += "; text-align: %s" % el.layout.alignment return wrapper else: return html
def test_nest_list(self): ptag = Tag( 'body', {}, content=(Tag('p', {}, content="A"), Tag('span', {}, content="B"), Tag('a', {}, content=Tag('p', {}, content="C"))), ) self.assertHtmlEqual( ptag.render(), "<body><p>A</p><span>B</span><a><p>C</p></a></body>")
def htmls(field, edit_inst_code_fn=None): base_attribs = {} tagname = 'input' content=None # logic to set up the html data if field.displayType.endswith('-text'): base_attribs = {'type': 'text', 'placeholder': field.placeholder, 'name': field.backend_field_name } if field.displayType == 'password-text': base_attribs['type'] = 'password' if field.displayType == 'paragraph-text': del base_attribs['type'] tagname = 'textarea' if edit_inst_code_fn is not None: edit_id = "{{ %s.%s }}" % (edit_inst_code_fn(), field.backend_field_name) if field.displayType == 'paragraph-text': content = edit_id else: base_attribs['value'] = edit_id # don't use place holder if some edit value is going to be filled in anyway. del base_attribs['placeholder'] elif field.displayType.endswith('-uploader'): tagname = 'div' class_string = 'upload-img btn' if field.displayType == 'image-uploader' else 'upload-file btn' filepicker_button = Tag('div', {'class': class_string, 'data-name': field.backend_field_name}, content=field.placeholder) # this is needed for ajaxify to put in the filepicker value, and for the browser to submit the form real_input = Tag('input', {'type': 'hidden', 'name': field.backend_field_name }) if edit_inst_code_fn is not None: real_input.attribs['value'] = "{{ %s.%s }}" % (edit_inst_code_fn(), field.backend_field_name) content = [filepicker_button, real_input] elif field.displayType == 'dropdown': tagname = 'select' opt_fields = [] for item in field.options: # TODO For edit form, this should return template code with an if statement opt_field = Tag('option', {'value': item}, content=item) opt_fields.append(opt_field) content = opt_fields base_attribs['name'] = field.backend_field_name elif field.displayType == 'option-boxes': tagname = 'span' opt_fields = [] for item in field.options: # TODO For edit form, this should return template code with an if statement opt_field_id = field._page.id_namespace.new_identifier('opt-label-%s' % field.backend_field_name) opt_field_options = [] opt_field_options.append(Tag('input', {'id': opt_field_id, 'class': 'field-type', 'type': 'radio', 'name':field.backend_field_name, 'value': item})) opt_field_options.append(Tag('label', {'for': opt_field_id}, content=item)) opt_field = Tag('div', {'class': 'option'}, content = opt_field_options) opt_fields.append(opt_field) content = opt_fields base_attribs['name'] = field.backend_field_name base_attribs['class'] = 'option-boxes' elif field.displayType == 'date-picker': tagname = 'div' base_attribs['class'] = 'date-picker-wrapper' if edit_inst_code_fn is not None: edit_id = "{{ %s.%s|date:\"m/d/Y\" }}" % (edit_inst_code_fn(), field.backend_field_name) inp = Tag('input', {'class': "date-picker-input", 'type': "text", 'name': field.backend_field_name, 'placeholder': field.placeholder, 'value':edit_id}) else: inp = Tag('input', {'class': "date-picker-input", 'type': "text", 'name': field.backend_field_name, 'placeholder': field.placeholder}) img = Tag('img', { 'class': "date-picker-icon"}) content = [inp, img] elif field.displayType == 'button': base_attribs['type'] = 'submit' base_attribs['value'] = field.placeholder base_attribs['class'] = 'btn' # create the html field_html = Tag(tagname, base_attribs, content=content) htmls = [] # add a label if possible try: if field.label is not None: label = Tag('label', {}, content=field.label) htmls.append(label) except AttributeError: pass htmls.append(field_html) try: error_div = Tag('div', {'class': 'form-error field-name-%s' % field.backend_field_name}) htmls.append(error_div) except AttributeError: pass return htmls
def test_html_escape(self): atag = Tag('a', {'href': 'http:"><script'}, content="Google") wrapper = Tag('div', {}, content=atag) self.assertHtmlEqual( wrapper.render(), '<div><a href="http:"><script">Google</a></div>')
def test_some_text(self): ptag = Tag('p', {}, content="Hulloooo") self.assertHtmlEqual(ptag.render(), "<p>Hulloooo</p>")
def test_attribs(self): atag = Tag('a', {'href': 'http://google.com/'}, content="Google") self.assertHtmlEqual(atag.render(), '<a href="http://google.com/">Google</a>')
def test_void_tag(self): br = Tag('br', {}) self.assertHtmlEqual(br.render(), '<br>')
def test_no_content(self): tag = Tag('p', {}) self.assertHtmlEqual(tag.render(), '<p></p>')