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

Unified Diff: tools/grit/grit/format/html_inline.py

Issue 2179033002: Strip comments and whitespace from javascript resources (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: One minor fix 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: tools/grit/grit/format/html_inline.py
diff --git a/tools/grit/grit/format/html_inline.py b/tools/grit/grit/format/html_inline.py
index 643052dd2e66063e4c2155831f32a3e8ac92c203..d767777d97ec81fdb572ee85ae6c6a6e7c3e2fd8 100755
--- a/tools/grit/grit/format/html_inline.py
+++ b/tools/grit/grit/format/html_inline.py
@@ -18,6 +18,7 @@ import mimetypes
from grit import lazy_re
from grit import util
+from grit.format import minifier
# There is a python bug that makes mimetypes crash if the Windows
# registry contains non-Latin keys ( http://bugs.python.org/issue9291
@@ -129,8 +130,8 @@ class InlinedData:
def DoInline(
input_filename, grd_node, allow_external_script=False,
- preprocess_only=False, names_only=False, rewrite_function=None,
- filename_expansion_function=None):
+ preprocess_only=False, names_only=False, strip_whitespace = False,
Dirk Pranke 2016/07/25 21:12:36 nit: no spaces between strip_whitespace=False.
aberent 2016/07/27 09:46:36 Done.
+ rewrite_function=None, filename_expansion_function=None):
"""Helper function that inlines the resources in a specified file.
Reads input_filename, finds all the src attributes and attempts to
@@ -142,6 +143,7 @@ def DoInline(
grd_node: html node from the grd file for this include tag
preprocess_only: Skip all HTML processing, only handle <if> and <include>.
names_only: |nil| will be returned for the inlined contents (faster).
+ strip_whitespace: remove whitespace and comments in the input files.
rewrite_function: function(filepath, text, distribution) which will be
called to rewrite html content before inlining images.
filename_expansion_function: function(filename) which will be called to
@@ -220,7 +222,10 @@ def DoInline(
else:
str = leading + trailing
- def InlineFileContents(src_match, pattern, inlined_files=inlined_files):
+ def InlineFileContents(src_match,
+ pattern,
+ inlined_files=inlined_files,
+ strip_whitespace=True):
"""Helper function to inline external files of various types"""
filepath = GetFilepath(src_match)
if filepath is None:
@@ -243,14 +248,15 @@ def DoInline(
"""Helper function to directly inline generic external files (without
wrapping them with any kind of tags).
"""
- return InlineFileContents(src_match, '%s')
+ return InlineFileContents(src_match, '%s', strip_whitespace=False)
def InlineScript(match):
"""Helper function to inline external script files"""
attrs = (match.group('attrs1') + match.group('attrs2')).strip()
if attrs:
- attrs = ' ' + attrs
- return InlineFileContents(match, '<script' + attrs + '>%s</script>')
+ attrs = ' ' + attrs
+ return InlineFileContents(match, '<script' + attrs + '>%s</script>',
+ strip_whitespace=strip_whitespace)
def InlineCSSText(text, css_filepath):
"""Helper function that inlines external resources in CSS text"""
@@ -321,7 +327,13 @@ def DoInline(
# going to throw out anyway.
flat_text = CheckConditionalElements(flat_text)
+ flat_text = _INCLUDE_RE.sub(InlineIncludeFiles, flat_text)
+
if not preprocess_only:
+ if strip_whitespace:
+ flat_text = minifier.Minify(flat_text,
+ os.path.splitext(input_filename)[1])
+
if not allow_external_script:
# We need to inline css and js before we inline images so that image
# references gets inlined in the css and js
@@ -334,8 +346,6 @@ def DoInline(
lambda m: InlineCSSFile(m, '<style>%s</style>'),
flat_text)
- flat_text = _INCLUDE_RE.sub(InlineIncludeFiles, flat_text)
-
# Check conditional elements, second pass. This catches conditionals in any
# of the text we just inlined.
flat_text = CheckConditionalElements(flat_text)
@@ -357,8 +367,8 @@ def DoInline(
def InlineToString(input_filename, grd_node, preprocess_only = False,
- allow_external_script=False, rewrite_function=None,
- filename_expansion_function=None):
+ allow_external_script=False, strip_whitespace = True,
Dirk Pranke 2016/07/25 21:12:36 same nit.
aberent 2016/07/27 09:46:36 Done.
+ rewrite_function=None, filename_expansion_function=None):
"""Inlines the resources in a specified file and returns it as a string.
Args:
@@ -373,6 +383,7 @@ def InlineToString(input_filename, grd_node, preprocess_only = False,
grd_node,
preprocess_only=preprocess_only,
allow_external_script=allow_external_script,
+ strip_whitespace = strip_whitespace,
Dirk Pranke 2016/07/25 21:12:36 same nit.
aberent 2016/07/27 09:46:36 Done.
rewrite_function=rewrite_function,
filename_expansion_function=filename_expansion_function).inlined_data
except IOError, e:
@@ -411,6 +422,7 @@ def GetResourceFilenames(filename,
names_only=True,
preprocess_only=False,
allow_external_script=allow_external_script,
+ strip_whitespace = False,
Dirk Pranke 2016/07/25 21:12:36 same nit.
aberent 2016/07/27 09:46:36 Done.
rewrite_function=rewrite_function,
filename_expansion_function=filename_expansion_function).inlined_files
except IOError, e:

Powered by Google App Engine
This is Rietveld 408576698