def importStyle(self, uri, media, name, line=None, col=None): "Receive notification of a import statement in the style sheet." # defaultNamespaceURI??? super(EchoHandler, self).importStyle(uri, media, name, line, col) self._out.append(u'@import %s%s%s;\n' % (helper.string(uri), u'%s ' % media if media else u'', u'%s ' % name if name else u''))
def importStyle(self, uri, media, name, line=None, col=None): "Receive notification of a import statement in the style sheet." # defaultNamespaceURI??? super(EchoHandler, self).importStyle(uri, media, name, line, col) self._out.append(u'@import %s%s%s;\n' % (helper.string(uri), u'%s ' % media if media else u'', u'%s ' % name if name else u'') )
def searchItemset(itemset, itemsetTarget): itemsetComponents = itemset.split(".") for item in itemsetComponents: if Helper.string(itemsetTarget).findAdv(item) == -1: return False return True
def do_CSSMediaRule(self, rule): """ serializes CSSMediaRule + CSSComments """ # TODO: use Out()? # mediaquery if not rule.media.wellformed: return u'' # @media out = [self._atkeyword(rule)] if not len(self.prefs.spacer): # for now always with space as only webkit supports @mediaall? out.append(u' ') else: out.append(self.prefs.spacer) # might be empty out.append(self.do_stylesheets_medialist(rule.media)) # name, seq contains content after name only (Comments) if rule.name: out.append(self.prefs.spacer) nameout = Out(self) nameout.append(helper.string(rule.name)) for item in rule.seq: nameout.append(item.value, item.type) out.append(nameout.value()) # { out.append(self.prefs.paranthesisSpacer) out.append(u'{') out.append(self.prefs.lineSeparator) # rules rulesout = [] for r in rule.cssRules: rtext = r.cssText if rtext: # indent each line of cssText rulesout.append(self._indentblock(rtext, self._level + 1)) rulesout.append(self.prefs.lineSeparator) if not self.prefs.keepEmptyRules and not u''.join(rulesout).strip(): return u'' out.extend(rulesout) # } out.append(u'%s}' % ((self._level + int(self.prefs.indentClosingBrace)) * self.prefs.indent)) return u''.join(out)
def do_CSSCharsetRule(self, rule): """ serializes CSSCharsetRule encoding: string always @charset "encoding"; no comments or other things allowed! """ if rule.wellformed: return u'@charset %s;' % helper.string(rule.encoding) else: return u''
def do_css_CSSValue(self, cssvalue): """Serializes a CSSValue""" if not cssvalue: return u'' else: out = Out(self) for item in cssvalue.seq: type_, val = item.type, item.value if hasattr(val, 'cssText'): # RGBColor or CSSValue if a CSSValueList out.append(val.cssText, type_) else: if val and val[0] == val[-1] and val[0] in '\'"': val = helper.string(val[1:-1]) # S must be kept! in between values but no extra space out.append(val, type_) return out.value()
def do_css_PropertyValue(self, value, valuesOnly=False): """Serializes a PropertyValue""" if not value: return u'' else: out = Out(self) for item in value.seq: type_, val = item.type, item.value if valuesOnly and type_ == cssutils.css.CSSComment: continue elif hasattr(val, 'cssText'): # RGBColor or CSSValue if a CSSValueList out.append(val.cssText, type_) else: if val and val[0] == val[-1] and val[0] in '\'"': val = helper.string(val[1:-1]) # S must be kept! in between values but no extra space out.append(val, type_) return out.value()
def append(self, val, typ=None, space=True, keepS=False, indent=False, lineSeparator=False): """Appends val. Adds a single S after each token except as follows: - typ COMMENT uses cssText depending on self.ser.prefs.keepComments - typ "Property", cssutils.css.CSSRule.UNKNOWN_RULE uses cssText - typ STRING escapes helper.string - typ S ignored except ``keepS=True`` - typ URI calls helper.uri - val ``{`` adds LF after - val ``;`` removes S before and adds LF after - val ``, :`` removes S before - val ``+ > ~`` encloses in prefs.selectorCombinatorSpacer - some other vals add ``*spacer`` except ``space=False`` """ prefspace = self.ser.prefs.spacer if val or typ in ('STRING', 'URI'): # PRE if 'COMMENT' == typ: if self.ser.prefs.keepComments: val = val.cssText else: return elif hasattr(val, 'cssText'): val = val.cssText # elif typ in ('Property', cssutils.css.CSSRule.UNKNOWN_RULE): # val = val.cssText elif 'S' == typ and not keepS: return elif 'S' == typ and keepS: val = u' ' elif typ in ('NUMBER', 'DIMENSION', 'PERCENTAGE') and val == '0': # remove sign + or - if value is zero # TODO: only for lenghts! if self.out and self.out[-1] in u'+-': del self.out[-1] elif 'STRING' == typ: # may be empty but MUST not be None if val is None: return val = helper.string(val) if not prefspace: self._remove_last_if_S() elif 'URI' == typ: val = helper.uri(val) elif 'HASH' == typ: val = self.ser._hash(val) elif val in u'+>~,:{;)]/': self._remove_last_if_S() # APPEND if indent: self.out.append(self.ser._indentblock(val, self.ser._level+1)) else: self.out.append(val) # POST if lineSeparator: # Property , ... pass elif val in u'+>~': # enclose selector combinator self.out.insert(-1, self.ser.prefs.selectorCombinatorSpacer) self.out.append(self.ser.prefs.selectorCombinatorSpacer) elif u',' == val: # list self.out.append(self.ser.prefs.listItemSpacer) elif u':' == val: # prop self.out.append(self.ser.prefs.propertyNameSpacer) elif u'{' == val: # block start self.out.insert(-1, self.ser.prefs.paranthesisSpacer) self.out.append(self.ser.prefs.lineSeparator) elif u';' == val: # end or prop or block self.out.append(self.ser.prefs.lineSeparator) elif val not in u'}[]()/' and typ != 'FUNCTION' and space: self.out.append(self.ser.prefs.spacer) if typ != 'STRING' and not self.ser.prefs.spacer and \ self.out and not self.out[-1].endswith(u' '): self.out.append(u' ')
def append(self, val, type_=None, space=True, keepS=False, indent=False): """Appends val. Adds a single S after each token except as follows: - typ COMMENT uses cssText depending on self.ser.prefs.keepComments - typ "Property", cssutils.css.CSSRule.UNKNOWN_RULE uses cssText - typ STRING escapes helper.string - typ S ignored except ``keepS=True`` - typ URI calls helper.uri - val ``{`` adds LF after - val ``;``, typ 'styletext' removes S before and adds LF after - val ``, :`` removes S before - val ``+ > ~`` encloses in prefs.selectorCombinatorSpacer - some other vals add ``*spacer`` except ``space=False`` """ prefspace = self.ser.prefs.spacer if val or type_ in ('STRING', 'URI'): # PRE if 'COMMENT' == type_: if self.ser.prefs.keepComments: val = val.cssText else: return elif hasattr(val, 'cssText'): val = val.cssText # elif type_ in ('Property', cssutils.css.CSSRule.UNKNOWN_RULE): # val = val.cssText elif 'S' == type_ and not keepS: return elif 'S' == type_ and keepS: val = u' ' # elif type_ in ('NUMBER', 'DIMENSION', 'PERCENTAGE') and val == u'0': # # remove sign + or - if value is zero # # TODO: only for lenghts! # if self.out and self.out[-1] in u'+-': # del self.out[-1] elif 'STRING' == type_: # may be empty but MUST not be None if val is None: return val = helper.string(val) if not prefspace: self._remove_last_if_S() elif 'URI' == type_: val = helper.uri(val) elif 'HASH' == type_: val = self.ser._hash(val) elif val in u'+>~,:{;)]/=}': self._remove_last_if_S() # APPEND if indent or (val == u'}' and self.ser.prefs.indentClosingBrace): self.out.append(self.ser._indentblock(val, self.ser._level + 1)) else: if val.endswith(u' '): self._remove_last_if_S() self.out.append(val) # POST if val in u'+>~': # enclose selector combinator self.out.insert(-1, self.ser.prefs.selectorCombinatorSpacer) self.out.append(self.ser.prefs.selectorCombinatorSpacer) elif u')' == val and not keepS: # CHAR funcend # TODO: pref? self.out.append(u' ') elif u',' == val: # list self.out.append(self.ser.prefs.listItemSpacer) elif u':' == val: # prop self.out.append(self.ser.prefs.propertyNameSpacer) elif u'{' == val: # block start self.out.insert(-1, self.ser.prefs.paranthesisSpacer) self.out.append(self.ser.prefs.lineSeparator) elif u';' == val or 'styletext' == type_: # end or prop or block self.out.append(self.ser.prefs.lineSeparator) elif val not in u'}[]()/=' and space and type_ != 'FUNCTION': self.out.append(self.ser.prefs.spacer) if type_ != 'STRING' and not self.ser.prefs.spacer and \ self.out and not self.out[-1].endswith(u' '): self.out.append(u' ')
def namespaceDeclaration(self, prefix, uri, line=None, col=None): super(EchoHandler, self).namespaceDeclaration(prefix, uri, line, col) self._out.append(u'@namespace %s%s;\n' % (u'%s ' % prefix if prefix else u'', helper.string(uri)))