def process(self, path, metadata): ext = os.path.splitext(path)[1] if ext not in self.supported_extensions: return path, metadata basename = os.path.basename(path) src_path = os.path.join( copy_to_secure_location(path), basename) remove_file_dir(path) new_html, css = extract_css( open(src_path, 'rb').read().decode('utf-8'), basename, prettify_html=self.options['css_cleaner_prettify_html']) css, errors = cleanup_css( css, minified=self.options['css_cleaner_minified']) css_file = os.path.splitext(src_path)[0] + '.css' if css is not None: with open(css_file, 'wb') as fd: fd.write(css.encode('utf-8')) with open(src_path, 'wb') as fd: fd.write(new_html.encode('utf-8')) return src_path, metadata
def process(self, path, metadata): ext = os.path.splitext(path)[1] if ext not in self.supported_extensions: return path, metadata basename = os.path.basename(path) src_path = os.path.join(copy_to_secure_location(path), basename) remove_file_dir(path) new_html, css = extract_css( open(src_path, 'rb').read().decode('utf-8'), basename, prettify_html=self.options['css_cleaner_prettify_html']) css, errors = cleanup_css( css, minified=self.options['css_cleaner_minified']) css_file = os.path.splitext(src_path)[0] + '.css' if css is not None: with open(css_file, 'wb') as fd: fd.write(css.encode('utf-8')) with open(src_path, 'wb') as fd: fd.write(new_html.encode('utf-8')) return src_path, metadata
def test_cleanup_css_non_minified(self): css_input = 'p { foo: baz ; bar: baz}' result, errors = cleanup_css(css_input, minified=False) assert result == 'p {\n foo: baz;\n bar: baz\n }'
def test_cleanup_css_errors(self): css_input = 'p { foo: baz ; font-family: ; bar: baz}' result, errors = cleanup_css(css_input) assert 'ERROR PropertyValue: Unknown syntax' in errors assert 'WARNING Property: Unknown Property name' in errors
def test_cleanup_css_complex(self): css_sample = os.path.join( os.path.dirname(__file__), 'input', 'sample1.css') css_input = open(css_sample, 'rb').read() result, errors = cleanup_css(css_input) assert 'font-family: ;' not in result
def test_cleanup_css_empty_prop_middle(self): css_input = 'p { foo: baz ; font-family: ; bar: baz}' result, errors = cleanup_css(css_input) assert result == 'p{foo:baz;bar:baz}'
def test_cleanup_css_empty_prop_no_colon(self): css_input = 'p {font-family: }' result, errors = cleanup_css(css_input) assert result == ''
def test_cleanup_css_empty_style(self): css_input = 'p {}' result, errors = cleanup_css(css_input) assert result == ''
def test_cleanup_css_whitespace(self): css_input = 'p {font-family: ; font-size: 12px }' result, errors = cleanup_css(css_input) assert result == 'p{font-size:12px}'