Skip to content

Preprocess: Cache style parsing results #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions commands/preprocess_cssless.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
from lxml import etree
from io import StringIO
from lxml.etree import strip_elements
import copy
import functools
import io
import logging
import os
import warnings
import io

def preprocess_html_merge_cssless(src_path, dst_path):
with open(src_path, 'r') as a_file:
Expand Down Expand Up @@ -86,28 +88,49 @@ def needs_td_wrapper(element):
return False
return True

def remove_css_property(element, property_name):
atrib = cssutils.parseStyle(element.get('style'))
atrib.removeProperty(property_name)
element.set('style', atrib.getCssText(separator=''))
if len(element.get('style')) == 0:
element.attrib.pop('style')
@functools.lru_cache(maxsize=None)
def cssutils_parse_style_cached_nocopy(style):
return cssutils.parseStyle(style)

def get_css_property_value(el, prop_name):
atrib = cssutils.parseStyle(el.get('style'))
def cssutils_parse_style_cached(style):
return copy.deepcopy(cssutils_parse_style_cached_nocopy(style))

@functools.lru_cache(maxsize=None)
def get_css_style_property_value(style, prop_name):
atrib = cssutils_parse_style_cached_nocopy(style)
value = atrib.getPropertyCSSValue(prop_name)
if value:
return value.cssText
return None

def has_css_property_value(el, prop_name, prop_value):
value = get_css_property_value(el, prop_name)
@functools.lru_cache(maxsize=None)
def has_css_style_property_value(style, prop_name, prop_value):
value = get_css_style_property_value(style, prop_name)
if value and value == prop_value:
return True
return False

@functools.lru_cache(maxsize=None)
def remove_css_style_property(style, property_name):
atrib = cssutils_parse_style_cached(style)
atrib.removeProperty(property_name)
return atrib.getCssText(separator='')

def remove_css_property(element, property_name):
new_style = remove_css_style_property(element.get('style'), property_name)
if len(new_style) > 0:
element.set('style', new_style)
elif 'style' in element.attrib:
element.attrib.pop('style')

def get_css_property_value(el, prop_name):
return get_css_style_property_value(el.get('style'), prop_name)

def has_css_property_value(el, prop_name, prop_value):
return has_css_style_property_value(el.get('style'), prop_name, prop_value)

def set_css_property_value(el, prop_name, prop_value):
atrib = cssutils.parseStyle(el.get('style'))
atrib = cssutils_parse_style_cached(el.get('style'))
atrib.setProperty(prop_name, prop_value)
el.set('style', atrib.getCssText(separator=''))

Expand Down