def style(self, style: _AttrValueType) -> None: """Set style attribute of this node. If argument ``style`` is string, it will be parsed to ``CSSStyleDeclaration``. """ if isinstance(style, str): self.__style._parse_str(style) elif style is None: self.__style._parse_str('') elif isinstance(style, CSSStyleDeclaration): self.__style._owner = None if style._owner is not None: new_style = CSSStyleDeclaration(owner=self) new_style.update(style) self.__style = new_style else: # always making new decl may be better style._owner = self self.__style = style else: raise TypeError('Invalid type for style: {}'.format(type(style)))
def test_overwrite_style(self): self.rule.style = CSSStyleDeclaration() self.rule.style.color = 'red' self.assertEqual(self.rule.cssText, ' {color: red;}') self.rule.selectorText = 'h1' self.assertEqual(self.rule.cssText, 'h1 {color: red;}') self.rule.selectorText = 'h1,h2' self.assertEqual(self.rule.cssText, 'h1,h2 {color: red;}') self.rule.style.removeProperty('color') self.assertEqual(self.rule.cssText, '')
def test_style_new(self): st = CSSStyleDeclaration() self.assertEqual(self.elm.style.cssText, '') self.assertEqual(st.cssText, '') st.setProperty('color', 'red') self.assertEqual(st.cssText, 'color: red;') self.assertEqual(self.elm.style.cssText, '') self.elm.style = st self.assertEqual(self.elm.style.cssText, 'color: red;') st.setProperty('color', 'blue') self.assertEqual(st.cssText, 'color: blue;') # shouldn't do this??? self.assertEqual(self.elm.style.cssText, 'color: blue;')
def setUp(self): self.css = CSSStyleDeclaration()
class TestCSSStyleDeclaration(TestCase): def setUp(self): self.css = CSSStyleDeclaration() def test_init(self): self.assertEqual(self.css.cssText, '') self.assertEqual(self.css.length, 0) def test_set_get_remove(self): self.assertEqual(self.css.getPropertyValue('color'), '') self.css.setProperty('color', 'red') self.assertEqual(self.css.getPropertyValue('color'), 'red') self.assertEqual(self.css['color'], 'red') self.assertEqual(self.css.cssText, 'color: red;') self.assertEqual(self.css.length, 1) self.css.setProperty('font-color', '#000') self.assertEqual(self.css.getPropertyValue('color'), 'red') self.assertEqual(self.css['color'], 'red') self.assertEqual(self.css.getPropertyValue('font-color'), '#000') self.assertEqual(self.css['font-color'], '#000') self.assertIn('font-color: #000;', self.css.cssText) self.assertIn('color: red;', self.css.cssText) self.assertEqual(self.css.length, 2) # removedProperty returns value of removed property self.assertEqual(self.css.removeProperty('color'), 'red') self.assertEqual(self.css.getPropertyValue('color'), '') self.assertEqual(self.css['color'], '') self.assertEqual(self.css.getPropertyValue('font-color'), '#000') self.assertEqual(self.css['font-color'], '#000') self.assertEqual(self.css.cssText, 'font-color: #000;') self.assertNotIn('color: red;', self.css.cssText) self.assertEqual(self.css.length, 1) def test_property_access(self): self.assertEqual(self.css.color, '') self.css.color = 'red' self.assertEqual(self.css.color, 'red') self.assertEqual(self.css.getPropertyValue('color'), 'red') self.assertEqual(self.css['color'], 'red') self.assertEqual(self.css.cssText, 'color: red;') self.assertEqual(self.css.length, 1) del self.css.color self.assertEqual(self.css.color, '') self.assertEqual(self.css.getPropertyValue('color'), '') self.assertEqual(self.css.cssText, '') self.assertEqual(self.css.length, 0) def test_property_access_dash(self): self.assertEqual(self.css.zIndex, '') self.assertEqual(self.css.getPropertyValue('z-index'), '') self.assertEqual(self.css.getPropertyValue('zIndex'), '') self.css.zIndex = 1 self.assertEqual(self.css.zIndex, 1) self.assertEqual(self.css.getPropertyValue('z-index'), 1) self.assertEqual(self.css.getPropertyValue('zIndex'), '') self.assertEqual(self.css.cssText, 'z-index: 1;') self.assertEqual(self.css.length, 1) self.css.setProperty('z-index', 2) self.assertEqual(self.css.zIndex, 2) self.assertEqual(self.css.getPropertyValue('z-index'), 2) self.assertEqual(self.css.getPropertyValue('zIndex'), '') self.assertEqual(self.css.cssText, 'z-index: 2;') self.assertEqual(self.css.length, 1) del self.css.zIndex self.assertEqual(self.css.zIndex, '') self.assertEqual(self.css.getPropertyValue('z-index'), '') self.assertEqual(self.css.getPropertyValue('zIndex'), '') self.assertEqual(self.css.cssText, '') self.assertEqual(self.css.length, 0) def test_property_access_dash_two(self): self.assertEqual(self.css.listStyleType, '') self.assertEqual(self.css.getPropertyValue('list-style-type'), '') self.assertEqual(self.css.getPropertyValue('listStyleType'), '') self.css.listStyleType = 'disc' self.assertEqual(self.css.listStyleType, 'disc') self.assertEqual(self.css.getPropertyValue('list-style-type'), 'disc') self.assertEqual(self.css.getPropertyValue('listStyleType'), '') self.assertEqual(self.css.cssText, 'list-style-type: disc;') self.assertEqual(self.css.length, 1) del self.css.listStyleType self.assertEqual(self.css.listStyleType, '') self.assertEqual(self.css.getPropertyValue('list-style-type'), '') self.assertEqual(self.css.getPropertyValue('listStyleType'), '') self.assertEqual(self.css.cssText, '') self.assertEqual(self.css.length, 0) def test_float(self): self.assertEqual(self.css.cssFloat, '') self.assertEqual(self.css.getPropertyValue('float'), '') self.assertEqual(self.css.getPropertyValue('cssFloat'), '') self.assertEqual(self.css.getPropertyValue('css-float'), '') self.css.cssFloat = 1 self.assertEqual(self.css.getPropertyValue('cssFloat'), '') self.assertEqual(self.css.getPropertyValue('float'), 1) self.assertEqual(self.css.getPropertyValue('css-float'), '')
def test_append2(self): self.list.append(self.rule) rule2 = CSSStyleRule('h2', CSSStyleDeclaration('background: black;')) self.list.append(rule2) css = self.list.cssText self.assertIn('h1 {color: red;}\nh2 {background: black;}', css)
def setUp(self): self.list = CSSRuleList() self.style = CSSStyleDeclaration() self.style.color = 'red' self.rule = CSSStyleRule('h1', self.style)
def test_init(self): style = CSSStyleDeclaration() style.color = 'red' rule = CSSStyleRule('h1', style) self.assertEqual(rule.cssText, 'h1 {color: red;}')
def __init__(self, *args, style: str=None, **kwargs): super().__init__(*args, **kwargs) self._style = CSSStyleDeclaration(style, owner=self)
class HTMLElement(Element): _special_attr_string = ['title', 'type'] _special_attr_boolean = ['draggable', 'hidden'] def __init__(self, *args, style: str=None, **kwargs): super().__init__(*args, **kwargs) self._style = CSSStyleDeclaration(style, owner=self) def _get_attrs_by_string(self) -> str: attrs = super()._get_attrs_by_string() style = self.getAttribute('style') if style: attrs += ' style="{}"'.format(style) return attrs.strip() def __copy__(self) -> 'HTMLElement': clone = super().__copy__() clone.style.update(self.style) return clone @property def end_tag(self) -> str: if self.tag in HTML_EMPTY: return '' else: return super().end_tag @property def style(self) -> CSSStyleDeclaration: return self._style @style.setter def style(self, style: str): if isinstance(style, str): self._style._parse_str(style) elif style is None: self._style._parse_str('') elif isinstance(style, CSSStyleDeclaration): self._style._owner = None style._owner = self self._style = style self._style.update() else: raise TypeError('Invalid type for style: {}'.format(type(style))) def getAttribute(self, attr: str) -> str: if attr == 'style': # if style is neither None nor empty, return None # otherwise, return style.cssText if self.style: return self.style.cssText else: return None else: return super().getAttribute(attr) def _set_attribute(self, attr: str, value: str): if attr == 'style': self.style = value else: super()._set_attribute(attr, value) def _remove_attribute(self, attr: str): if attr == 'style': self.style = None else: super()._remove_attribute(attr)
def __init__(self, *args: Any, style: str = None, **kwargs: Any) -> None: # noqa: D102 super().__init__(*args, **kwargs) self.__style = CSSStyleDeclaration(style, owner=self)
class HTMLElement(Element): """Base class for HTMLElement. This class extends `Element` class with some HTML specific features. """ _special_attr_string = ['title', 'type'] _special_attr_boolean = ['hidden'] _parser_class = HTMLElementParser # type: Type[ElementParser] def __init__(self, *args: Any, style: str = None, **kwargs: Any) -> None: # noqa: D102 super().__init__(*args, **kwargs) self.__style = CSSStyleDeclaration(style, owner=self) def _get_attrs_by_string(self) -> str: attrs = super()._get_attrs_by_string() style = self.getAttribute('style') if style: attrs += ' style="{}"'.format(style) return attrs.strip() def _clone_node(self) -> 'HTMLElement': clone = super()._clone_node() clone.style.update(self.style) return clone @property def end_tag(self) -> str: """Retrun html end tag. If tag is empty tag like <img> or <br>, return empty string. """ if self.tag in HTML_EMPTY: return '' return super().end_tag @property def style(self) -> CSSStyleDeclaration: """Return style attribute of this node.""" return self.__style @style.setter def style(self, style: _AttrValueType) -> None: """Set style attribute of this node. If argument ``style`` is string, it will be parsed to ``CSSStyleDeclaration``. """ if isinstance(style, str): self.__style._parse_str(style) elif style is None: self.__style._parse_str('') elif isinstance(style, CSSStyleDeclaration): self.__style._owner = None if style._owner is not None: new_style = CSSStyleDeclaration(owner=self) new_style.update(style) self.__style = new_style else: # always making new decl may be better style._owner = self self.__style = style else: raise TypeError('Invalid type for style: {}'.format(type(style))) @property def draggable(self) -> Union[bool, str]: """Get ``draggable`` property.""" if not self.hasAttribute('draggable'): return False return self.getAttribute('draggable') # type: ignore @draggable.setter def draggable(self, value: Union[bool, str]) -> None: """Set ``draggable`` property. ``value`` is boolean or string. """ if value is False: self.removeAttribute('draggable') else: self.setAttribute('draggable', value) def getAttribute(self, attr: str) -> _AttrValueType: # noqa: D102 if attr == 'style': # if style is neither None nor empty, return None # otherwise, return style.cssText if self.style: return self.style.cssText return None return super().getAttribute(attr) def _set_attribute(self, attr: str, value: _AttrValueType) -> None: if attr == 'style': self.style = value # type: ignore else: super()._set_attribute(attr, value) # type: ignore def _remove_attribute(self, attr: str) -> None: if attr == 'style': self.style = None # type: ignore else: super()._remove_attribute(attr) # type: ignore