"""MarginRule implements DOM Level 2 CSS MarginRule."""
__all__ = ['MarginRule']
import xml.dom
import cssutils
from cssutils.prodparser import Choice, PreDef, Prod, ProdParser, Sequence
from . import cssrule
from .cssstyledeclaration import CSSStyleDeclaration
class MarginRule(cssrule.CSSRule):
"""
A margin at-rule consists of an ATKEYWORD that identifies the margin box
(e.g. '@top-left') and a block of declarations (said to be in the margin
context).
Format::
margin :
margin_sym S* '{' declaration [ ';' S* declaration? ]* '}' S*
;
margin_sym :
TOPLEFTCORNER_SYM |
TOPLEFT_SYM |
TOPCENTER_SYM |
TOPRIGHT_SYM |
TOPRIGHTCORNER_SYM |
BOTTOMLEFTCORNER_SYM |
BOTTOMLEFT_SYM |
BOTTOMCENTER_SYM |
BOTTOMRIGHT_SYM |
BOTTOMRIGHTCORNER_SYM |
LEFTTOP_SYM |
LEFTMIDDLE_SYM |
LEFTBOTTOM_SYM |
RIGHTTOP_SYM |
RIGHTMIDDLE_SYM |
RIGHTBOTTOM_SYM
;
e.g.::
@top-left {
content: "123";
}
"""
margins = [
'@top-left-corner',
'@top-left',
'@top-center',
'@top-right',
'@top-right-corner',
'@bottom-left-corner',
'@bottom-left',
'@bottom-center',
'@bottom-right',
'@bottom-right-corner',
'@left-top',
'@left-middle',
'@left-bottom',
'@right-top',
'@right-middle',
'@right-bottom',
]
def __init__(
self,
margin=None,
style=None,
parentRule=None,
parentStyleSheet=None,
readonly=False,
):
"""
:param atkeyword:
The margin area, e.g. '@top-left' for this rule
:param style:
CSSStyleDeclaration for this MarginRule
"""
super().__init__(parentRule=parentRule, parentStyleSheet=parentStyleSheet)
self._atkeyword = self._keyword = None
if margin:
self.margin = margin
if style:
self.style = style
else:
self.style = CSSStyleDeclaration(parentRule=self)
self._readonly = readonly
def _setMargin(self, margin):
"""Check if new keyword fits the rule it is used for."""
n = self._normalize(margin)
if n not in MarginRule.margins:
self._log.error(
f'Invalid margin @keyword for this {self.margin} rule: {margin!r}',
error=xml.dom.InvalidModificationErr,
)
else:
self._atkeyword = n
self._keyword = margin
margin = property(
lambda self: self._atkeyword,
_setMargin,
doc="Margin area of parent CSSPageRule. "
"`margin` and `atkeyword` are both normalized "
"@keyword of the @rule.",
)
atkeyword = margin
def __repr__(self):
return f"cssutils.css.{self.__class__.__name__}(margin={self.margin!r}, style={self.style.cssText!r})"
def __str__(self):
return "