Skip to content

Commit 49f023a

Browse files
authored
Merge pull request p12tic#66 from p12tic/optimize-cssless
Preprocess: Cache style parsing results
2 parents 824afb2 + 6015685 commit 49f023a

1 file changed

Lines changed: 35 additions & 12 deletions

File tree

commands/preprocess_cssless.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
from lxml import etree
2222
from io import StringIO
2323
from lxml.etree import strip_elements
24+
import copy
25+
import functools
26+
import io
2427
import logging
2528
import os
2629
import warnings
27-
import io
2830

2931
def preprocess_html_merge_cssless(src_path, dst_path):
3032
with open(src_path, 'r') as a_file:
@@ -86,28 +88,49 @@ def needs_td_wrapper(element):
8688
return False
8789
return True
8890

89-
def remove_css_property(element, property_name):
90-
atrib = cssutils.parseStyle(element.get('style'))
91-
atrib.removeProperty(property_name)
92-
element.set('style', atrib.getCssText(separator=''))
93-
if len(element.get('style')) == 0:
94-
element.attrib.pop('style')
91+
@functools.lru_cache(maxsize=None)
92+
def cssutils_parse_style_cached_nocopy(style):
93+
return cssutils.parseStyle(style)
9594

96-
def get_css_property_value(el, prop_name):
97-
atrib = cssutils.parseStyle(el.get('style'))
95+
def cssutils_parse_style_cached(style):
96+
return copy.deepcopy(cssutils_parse_style_cached_nocopy(style))
97+
98+
@functools.lru_cache(maxsize=None)
99+
def get_css_style_property_value(style, prop_name):
100+
atrib = cssutils_parse_style_cached_nocopy(style)
98101
value = atrib.getPropertyCSSValue(prop_name)
99102
if value:
100103
return value.cssText
101104
return None
102105

103-
def has_css_property_value(el, prop_name, prop_value):
104-
value = get_css_property_value(el, prop_name)
106+
@functools.lru_cache(maxsize=None)
107+
def has_css_style_property_value(style, prop_name, prop_value):
108+
value = get_css_style_property_value(style, prop_name)
105109
if value and value == prop_value:
106110
return True
107111
return False
108112

113+
@functools.lru_cache(maxsize=None)
114+
def remove_css_style_property(style, property_name):
115+
atrib = cssutils_parse_style_cached(style)
116+
atrib.removeProperty(property_name)
117+
return atrib.getCssText(separator='')
118+
119+
def remove_css_property(element, property_name):
120+
new_style = remove_css_style_property(element.get('style'), property_name)
121+
if len(new_style) > 0:
122+
element.set('style', new_style)
123+
elif 'style' in element.attrib:
124+
element.attrib.pop('style')
125+
126+
def get_css_property_value(el, prop_name):
127+
return get_css_style_property_value(el.get('style'), prop_name)
128+
129+
def has_css_property_value(el, prop_name, prop_value):
130+
return has_css_style_property_value(el.get('style'), prop_name, prop_value)
131+
109132
def set_css_property_value(el, prop_name, prop_value):
110-
atrib = cssutils.parseStyle(el.get('style'))
133+
atrib = cssutils_parse_style_cached(el.get('style'))
111134
atrib.setProperty(prop_name, prop_value)
112135
el.set('style', atrib.getCssText(separator=''))
113136

0 commit comments

Comments
 (0)