Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
items = sorted(stylizer.page_rule.items())
css = ';\n'.join("%s: %s" % (key, val) for key, val in items)
css = ('@page {\n%s\n}\n'%css) if items else ''
rules = [r.cssText for r in stylizer.font_face_rules +
self.embed_font_rules]
raw = '\n\n'.join(rules)
css += '\n\n' + raw
global_css[css].append(item)
gc_map = {}
manifest = self.oeb.manifest
for css in global_css:
href = None
if css.strip():
id_, href = manifest.generate('page_css', 'page_styles.css')
manifest.add(id_, href, CSS_MIME, data=cssutils.parseString(css,
validate=False))
gc_map[css] = href
ans = {}
for css, items in global_css.iteritems():
for item in items:
ans[item] = gc_map[css]
return ans
def loads(css, verbose=VERBOSE):
sheet = cssutils.parseString(css, validate=verbose)
return Element(sheet, verbose=verbose)
"""
This function will cache the result from cssutils
It is a big gain when number of rules is big
Maximum cache entries are 1000. This is mainly for
protecting memory leak in case something gone wild.
Be aware that you can turn the cache off in Premailer
Args:
css_body(str): css rules in string format
validate(bool): if cssutils should validate
Returns:
cssutils.css.cssstylesheet.CSSStyleSheet
"""
return cssutils.parseString(css_body, validate=validate)
def adddata(self):
"""Add more data to the svgimgutils"""
self.style_element = self.root.find('./' + self.root[0].tag + '/' + self.root[0][0].tag)
self.number_of_classes = cssutils.parseString(self.style_element.text).cssRules.length
def get_css_style(csstext, style):
cssstyle = ""
if csstext:
try:
import cssutils
from cssutils import parseString
cssutils.log.setLevel(logging.CRITICAL)
except ImportError:
return ""
cssnode = parseString(csstext)
stylesheet = cssnode.cssRules
for rule in stylesheet:
if rule.selectorText.replace(".", "") == style:
cssstyle = str(rule.style.cssText)
return cssstyle
def getMaskedClasses(self, soup):
masked_classes = []
mask_style = soup.find_all("style", text=re.compile(r"\.chp\-release\..*?display:none"))
for style in mask_style:
parsed_style = cssutils.parseString(style.get_text())
for rule in parsed_style:
if rule.type == rule.STYLE_RULE:
disp = rule.style.getProperty('display')
if disp and disp.cssValue.cssText.lower() == "none":
for selector in rule.selectorList:
if len(selector.seq) == 2:
root, key = selector.seq
if root.value == ".chp-release" and root.type == 'class':
masked_classes.append(key.value[1:])
print("Masked classes:")
print(masked_classes)
return masked_classes
def format_body(self, body):
sheet = cssutils.parseString(body)
return sheet.cssText
def _minified():
href = self.base_url if self.base_url != '' else None
sheet = cssutils.parseString(css, href=href)
sheet = cssutils.resolveImports(sheet)
cssutils.ser.prefs.useMinified()
return sheet.cssText
return self.cached(css.encode('utf-8'), _minified)
def replace_css(self, css):
manifest = self.oeb.manifest
for item in manifest.values():
if item.media_type in OEB_STYLES:
manifest.remove(item)
id, href = manifest.generate('css', 'stylesheet.css')
item = manifest.add(id, href, CSS_MIME, data=cssutils.parseString(css,
validate=False))
self.oeb.manifest.main_stylesheet = item
return href
"""
This function will cache the result from cssutils
It is a big gain when number of rules is big
Maximum cache entries are 1000. This is mainly for
protecting memory leak in case something gone wild.
Be aware that you can turn the cache off in Premailer
Args:
css_body(str): css rules in string format
validate(bool): if cssutils should validate
Returns:
cssutils.css.cssstylesheet.CSSStyleSheet
"""
return cssutils.parseString(css_body, validate=validate)