Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1271)

Unified Diff: third_party/WebKit/Source/build/scripts/minimize_css.py

Issue 2088123002: Minimize bundled user-agent CSS: html.css (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add unittest Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/build/scripts/minimize_css.py
diff --git a/third_party/WebKit/Source/build/scripts/minimize_css.py b/third_party/WebKit/Source/build/scripts/minimize_css.py
new file mode 100755
index 0000000000000000000000000000000000000000..b2cd54feef4fb4c69ec2d85d1decbe544e19cce8
--- /dev/null
+++ b/third_party/WebKit/Source/build/scripts/minimize_css.py
@@ -0,0 +1,128 @@
+#!/usr/bin/env python
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os.path
+import re
+import sys
+
+import in_generator
+
+
+class CSSMinimizer(object):
+
+ INITIAL = 0
+ MAYBE_COMMENT_START = 1
+ INSIDE_COMMENT = 2
+ MAYBE_COMMENT_END = 3
+ INSIDE_SINGLE_QUOTE = 4
+ INSIDE_SINGLE_QUOTE_ESCAPE = 5
+ INSIDE_DOUBLE_QUOTE = 6
+ INSIDE_DOUBLE_QUOTE_ESCAPE = 7
+
+ WHITESPACE_PATTERN = re.compile(r"\s+", re.MULTILINE)
Timothy Loh 2016/08/10 05:37:39 re.MULTILINE doesn't affect either of these regexe
kouhei (in TOK) 2016/11/01 10:49:16 Done.
+ OP_PADDING_PATTERN = re.compile(r";?\s*(?P<op>[\{\};])\s*", re.MULTILINE)
Timothy Loh 2016/08/10 05:37:39 I don't think you want the backslashes in [\{\};]
kouhei (in TOK) 2016/11/01 10:49:16 Done.
+
+ def __init__(self):
+ self._output = ''
+ self._codeblock = ''
+
+ def flush_codeblock(self):
+ stripped = re.sub(self.WHITESPACE_PATTERN, ' ', self._codeblock)
+ stripped = re.sub(self.OP_PADDING_PATTERN, r'\g<op>', stripped)
+ self._output += stripped
+ self._codeblock = ''
+
+ def parse(self, content):
+ state = self.INITIAL
+ for c in content:
+ if state == self.INITIAL:
+ if c == '/':
+ state = self.MAYBE_COMMENT_START
Timothy Loh 2016/08/10 05:37:38 "'"
kouhei (in TOK) 2016/11/01 10:49:16 Done.
+ elif c == '\'':
+ self.flush_codeblock()
+ self._output += c
+ state = self.INSIDE_SINGLE_QUOTE
+ elif c == '\"':
Timothy Loh 2016/08/10 05:37:38 '"'
kouhei (in TOK) 2016/11/01 10:49:16 Done.
+ self.flush_codeblock()
+ self._output += c
+ state = self.INSIDE_DOUBLE_QUOTE
+ else:
+ self._codeblock += c
+ elif state == self.MAYBE_COMMENT_START:
+ if c == '*':
+ self.flush_codeblock()
+ state = self.INSIDE_COMMENT
+ else:
+ self._codeblock += '/' + c
+ state = self.INITIAL
+ elif state == self.INSIDE_COMMENT:
+ if c == '*':
+ state = self.MAYBE_COMMENT_END
+ else:
+ pass
+ elif state == self.MAYBE_COMMENT_END:
+ if c == '/':
+ state = self.INITIAL
+ else:
+ state = self.INSIDE_COMMENT
+ elif state == self.INSIDE_SINGLE_QUOTE:
+ if c == '\\':
+ self._output += c
+ state = self.INSIDE_SINGLE_QUOTE_ESCAPE
+ elif c == '\'':
Timothy Loh 2016/08/10 05:37:38 "'"
kouhei (in TOK) 2016/11/01 10:49:16 Done.
+ self._output += c
+ state = self.INITIAL
+ else:
+ self._output += c
+ elif state == self.INSIDE_SINGLE_QUOTE_ESCAPE:
+ self._output += c
+ state = self.INSIDE_SINGLE_QUOTE
+ elif state == self.INSIDE_DOUBLE_QUOTE:
+ if c == '\\':
+ self._output += c
+ state = self.INSIDE_DOUBLE_QUOTE_ESCAPE
+ elif c == '"':
+ self._output += c
+ state = self.INITIAL
+ else:
+ self._output += c
+ elif state == self.INSIDE_DOUBLE_QUOTE_ESCAPE:
+ self._output += c
+ state = self.INSIDE_DOUBLE_QUOTE
+
+ self.flush_codeblock()
+ self._output = self._output.strip()
+ return self._output
+
+ @property
+ def output(self):
+ return self._output
+
+ @classmethod
+ def minimize_css(cls, content):
+ minimizer = CSSMinimizer()
+ minimizer.parse(content)
+ return minimizer.output
+
+
+class CSSMinimizerWriter(in_generator.GenericWriter):
+
+ def __init__(self, in_file_paths):
+ super(CSSMinimizerWriter, self).__init__(in_file_paths)
+
+ self._outputs = {}
+ for in_file_path in in_file_paths:
+ out_path = os.path.basename(in_file_path)
+ self._outputs[out_path] = lambda fp=in_file_path: self.generate_implementation(fp)
Timothy Loh 2016/08/10 05:37:39 This line is a bit hard for me to read, I'd use pa
kouhei (in TOK) 2016/11/01 10:49:15 Done.
+
+ def generate_implementation(self, in_file_path):
+ content = ''
+ with open(os.path.abspath(in_file_path)) as in_file:
+ content = in_file.read()
+ return CSSMinimizer.minimize_css(content)
+
+
+if __name__ == '__main__':
+ in_generator.Maker(CSSMinimizerWriter).main(sys.argv)

Powered by Google App Engine
This is Rietveld 408576698