def _ready(self): if self.value: self["href"] = "mailto:" + self.value self.append(self.value) Element._ready(self)
def _add_meta(self): # Content type and charset should always go first self.content_type_metatag = Element("meta") self.content_type_metatag["http-equiv"] = "Content-Type" self.content_type_metatag["content"] = "%s; charset=%s" % ( self.metadata.content_type or "text/html", self.metadata.charset or "utf-8") self.meta_container.append(self.content_type_metatag) # Document-wide default base URL for relative URLs if self.metadata.base_href: base = Element("base") base["href"] = self.metadata.base_href self.meta_container.append(base) # Other meta tags for key, value in self.metadata.meta.items(): meta = Element("meta") if key.lower() in HTTP_EQUIV_KEYS: attribute = "http-equiv" else: attribute = "name" meta[attribute] = key meta["content"] = value self.meta_container.append(meta)
def create_group_row(self, group): row = Element("tr") row.add_class("group") cell = Element( "td", colspan=len(self.head_row.children), children=[ Element("span", class_name="grouping_value", children=[ self.grouping.translate_grouping_value(group) or self.translate_value( self.data, self.grouping.member, group) ]), Element("span", class_name="grouping_member", children=[self._grouping_member_translation]), Element("a", href="?" + get_request_query(grouping="", page=0).escape(), class_name="remove_grouping", children=[self._remove_grouping_translation]) ]) row.append(cell) return row
def get_undefined(self): from cocktail.html.element import Element e = Element() # Getting an undefined meta attribute yields None self.assertTrue(e.get_meta("desc") is None)
def test_resource_set(self): from cocktail.html.element import Element from cocktail.html.resources import Resource, Script e = Element() r1 = Resource("foo.js") # Adding the same resource object twice only adds the resource once e.add_resource(r1) e.add_resource(r1) self.assertEqual(e.resources, [r1]) # Adding a different resource object with the same resource URI adds a # single resource e.add_resource(Resource("foo.js")) self.assertEqual(e.resources, [r1]) # A different resource object with the same URI counts as a different # resource r2 = Script("foo.js") e.add_resource(r2) self.assertEqual(e.resources, [r1, r2]) # Adding the same URI using a string is also ignored e.add_resource("foo.js") self.assertEqual(e.resources, [r1, r2])
def get_undefined(self): from cocktail.html.element import Element e = Element() # Getting an undefined style declaration yields None self.assertTrue(e.get_style("font-weight") is None)
def test_set_none(self): from cocktail.html.element import Element e = Element() e.set_client_param("foo", None) self.assertTrue(e.get_client_param("foo") is None) self.assertEqual(e.client_params, {"foo": None})
def test_single_elements_with_children(self): from cocktail.html.element import Element from cocktail.html.renderers import HTML4Renderer, RenderingError e = Element("img") e.append(Element()) self.assertRaises(RenderingError, e.render, HTML4Renderer())
def test_empty_compound_elements(self): from cocktail.html.element import Element for tag in ("div", "script", "table", "p", "b"): e = Element() e.tag = tag self.assertEqual(self.get_html(e), "<%s></%s>" % (tag, tag))
def test_empty_single_elements(self): from cocktail.html.element import Element for tag in ("img", "link", "hr", "br"): e = Element() e.tag = tag self.assertEqual(self.get_html(e), "<%s>" % tag)
def __init__(self, *args, **kwargs): CollectionDisplay.__init__(self) self.__column_display = {} self.__column_labels = {} self.__split_rows = {} self.__split_row_values = {} self.__split_row_iterators = {} Element.__init__(self, *args, **kwargs)
def create_entry(self, item): entry = Element() entry.add_class("entry") entry.control = self.create_control(item) entry.append(entry.control) entry.remove_button = self.create_remove_button(item) entry.append(entry.remove_button) return entry
def _ready(self): Element._ready(self) self._font_sizes = get_tag_cloud_font_sizes(self.tags, self.max_font_increment) for tag in self.sorted_tags(self.tags): self.append(self.create_tag_entry(tag, self.tags[tag])) self.append(" ")
def test_empty_single_elements_with_attributes(self): from cocktail.html.element import Element for tag in ("img", "link", "hr", "br"): e = Element() e.tag = tag e["title"] = "hello world" self.assertEqual(self.get_html(e), '<%s title="hello world">' % tag) for tag in ("img", "link", "hr", "br"): e = Element() e.tag = tag e["title"] = "hello world" e["id"] = "foo" html = self.get_html(e) self.assertTrue( html == '<%s title="hello world" id="foo">' % tag or html == '<%s id="foo" title="hello world">' % tag) for tag in ("img", "link", "hr", "br"): e = Element() e.tag = tag e["selected"] = True e["checked"] = False e["id"] = "foo" html = self.get_html(e) self.assertTrue(html == '<%s selected id="foo">' % tag or html == '<%s id="foo" selected>' % tag)
def test_empty_compound_elements_with_attributes(self): from cocktail.html.element import Element for tag in ("div", "script", "table", "p", "b"): e = Element() e.tag = tag e["title"] = "hello world" self.assertEqual(self.get_html(e), '<%s title="hello world"></%s>' % (tag, tag)) for tag in ("div", "script", "table", "p", "b"): e = Element() e.tag = tag e["selected"] = True e["checked"] = False e["id"] = "foo" html = self.get_html(e) self.assertTrue( html == '<%s selected="selected" id="foo"></%s>' % (tag, tag) or html == '<%s id="foo" selected="selected"></%s>' % (tag, tag)) for tag in ("div", "script", "table", "p", "b"): e = Element() e.tag = tag e["title"] = "hello world" e["id"] = "foo" html = self.get_html(e) self.assertTrue( html == '<%s title="hello world" id="foo"></%s>' % (tag, tag) or html == '<%s id="foo" title="hello world"></%s>' % (tag, tag))
def _ready(self): Element._ready(self) if self.grouped: for group, members in self.displayed_members_by_group: tbody = self.create_group(group, members) if group: setattr(self, group + "_group", tbody) self.append(tbody) else: self._create_rows(self.displayed_members, self)
def __test_add_uri(self, uri, expected_type): from cocktail.html.element import Element e = Element() e.add_resource(uri) self.assertEqual(len(e.resources), 1) resource = e.resources[0] self.assertEqual(resource.uri, uri) self.assertTrue(isinstance(resource, expected_type))
def _render(self, rendering): doctype = self.metadata.doctype if doctype is None: doctype = rendering.renderer.doctype if doctype: rendering.write(doctype.strip()) rendering.write("\n") Element._render(self, rendering)
def test_insert_content(self): from cocktail.html.element import Element, Content parent = Element() parent.insert(0, "hello world") self.assertEqual(len(parent.children), 1) child = parent.children[0] self.assertTrue(isinstance(child, Content)) self.assertEqual(child.value, "hello world") self.assertTrue(child.parent is parent)
def test_visible_rendered(self): from cocktail.html.element import Element e = Element() # Visible elements should be rendered e.visible = True self.assertTrue(e.rendered) # Invisible elements shouldn't be rendered e.visible = False self.assertFalse(e.rendered)
def embed(self, document, source): from cocktail.html.element import Element embed = Element("script") embed["type"] = self.mime_type embed.append("\n//<![CDATA[\n" + source + "\n//]]>\n") if self.ie_condition: from cocktail.html.ieconditionalcomment import IEConditionalComment embed = IEConditionalComment(self.ie_condition, children=[embed]) document.scripts_container.append(embed) return embed
def _fill_head(self): # Cache sorted columns if self.order: self._sorted_columns = sorted_columns = {} for criteria in self.order: sign = criteria.__class__ expr = criteria.operands[0] if isinstance(expr, TranslationExpression): member = expr.operands[0].name language = expr.operands[1].value else: member = expr.name language = None sorted_columns[(member, language)] = sign # Selection column if (self.selection_mode != NO_SELECTION and self.use_separate_selection_column): selection_header = Element("th") selection_header.add_class("selection") self.head_row.append(selection_header) # Column groups if not self.grouped or not self.column_groups_displayed: self.column_groups_row.visible = False # Regular columns if self.grouped: self.columns_by_group = list(self.displayed_members_by_group) else: self.columns_by_group = [(None, list(self.displayed_members))] for group, columns in self.columns_by_group: if self.grouped and self.column_groups_displayed: self.column_groups_row.append( self.create_column_group_header(group, columns)) for column in columns: if column.translated: for language in self.translations or (get_language(), ): header = self.create_header(column, language) self.head_row.append(header) else: header = self.create_header(column) self.head_row.append(header)
def test_release(self): from cocktail.html.element import Element # Releasing a top-level element is a no-op parent = Element() parent.release() # Releasing a child element removes it from its parent child = Element() parent.append(child) child.release() self.assertTrue(child.parent is None) self.assertEqual(parent.children, [])
def _ready(self): if self.javascript_api: self.add_class("scriptable_video_player") self.add_resource("cocktail://scripts/youtubeplayer.js") self.add_resource(("https" if self.https else "http") + "://youtube.com/player_api", mime_type="text/javascript") self["src"] = self.get_video_url() self["width"] = self.width self["height"] = self.height self["allowfullscreen"] = self.allow_fullscreen Element._ready(self)
def test_invisible_element_not_rendered(self): from cocktail.html.element import Element e = Element() e.visible = False self.assertEqual(self.get_html(e), "") e.visible = True e.collapsible = True self.assertEqual(self.get_html(e), "") child = Element() child.visible = False e.append(child) self.assertEqual(self.get_html(e), "")
def _ready(self): if self.javascript_api: self.add_class("scriptable_video_player") self.add_resource("%s://player.vimeo.com/api/player.js" % ("https" if self.https else "http")) self.add_resource("cocktail://scripts/vimeoplayer.js") self["src"] = self.get_video_url() self["width"] = self.width self["height"] = self.height self["allowfullscreen"] = self.allow_fullscreen self["mozallowfullscreen"] = self.allow_fullscreen self["webkitallowfullscreen"] = self.allow_fullscreen Element._ready(self)
def get_undefined(self): from cocktail.html.element import Element e = Element() # Getting an undefined attribute yields None self.assertTrue(e["id"] is None)
def test_elements_can_invalidate_cached_content(self): from cocktail.html.element import Element e = Element() e.cached = True e.cached_key = "foo" e.cache_tags.add("foo") e.render(cache=self.cache) e.append("foo") html = e.render(cache=self.cache) assert "foo" not in html self.cache.clear(scope=["foo"]) html = e.render(cache=self.cache) assert "foo" in html
def test_constructor(self): from cocktail.html.element import Element # Attributes can be set using the element's constructor e = Element(id="foo", title="hello world") self.assertEqual(e["id"], "foo") self.assertEqual(e["title"], "hello world")
def test_delete_undefined(self): from cocktail.html.element import Element e = Element() # Deleting an undefined attribute is a no-op del e["id"] self.assertFalse("id" in e.attributes)