diff options
author | zelidrag@chromium.org <zelidrag@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-03 00:43:52 +0000 |
---|---|---|
committer | zelidrag@chromium.org <zelidrag@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-03 00:43:52 +0000 |
commit | 28dad08b7fafcec561576ad93092e092580fb40f (patch) | |
tree | 83f0f6f7397461191bd4d721431c27a8f084c15c /tools/grit | |
parent | 80e24af6c97103df6cf9e8f032d70cc356b68aa8 (diff) | |
download | chromium_src-28dad08b7fafcec561576ad93092e092580fb40f.zip chromium_src-28dad08b7fafcec561576ad93092e092580fb40f.tar.gz chromium_src-28dad08b7fafcec561576ad93092e092580fb40f.tar.bz2 |
Modified grit to support <if expr="..."> tags within included html files. Removed ChromeOS-specific sub-pages from options by using the newly added if tag. Changed <!--include file="..."--> tag to <include src="...">.
BUG=chromium-os:4450
TEST=make sure that chromeos specific pages don't show in chrome:options for other platforms
Review URL: http://codereview.chromium.org/2872028
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51578 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/grit')
-rwxr-xr-x | tools/grit/grit/format/html_inline.py | 24 | ||||
-rw-r--r-- | tools/grit/grit/format/rc.py | 4 | ||||
-rw-r--r-- | tools/grit/grit/format/rc_unittest.py | 39 |
3 files changed, 59 insertions, 8 deletions
diff --git a/tools/grit/grit/format/html_inline.py b/tools/grit/grit/format/html_inline.py index 128b267..6decccc 100755 --- a/tools/grit/grit/format/html_inline.py +++ b/tools/grit/grit/format/html_inline.py @@ -19,6 +19,8 @@ import sys import base64 import mimetypes +from grit.node import base + DIST_DEFAULT = 'chromium' DIST_ENV_VAR = 'CHROMIUM_BUILD' DIST_SUBSTR = '%DISTRIBUTION%' @@ -67,7 +69,7 @@ def SrcInlineAsDataURL(src_match, base_path, distribution): prefix = src_match.string[src_match.start():src_match.start('filename')-1] return "%s\"data:%s;base64,%s\"" % (prefix, mimetype, inline_data) -def InlineFile(input_filename, output_filename): +def InlineFile(input_filename, output_filename, grd_node): """Inlines the resources in a specified file. Reads input_filename, finds all the src attributes and attempts to @@ -77,6 +79,7 @@ def InlineFile(input_filename, output_filename): Args: input_filename: name of file to read in output_filename: name of file to be written to + grd_node: html node from the grd file for this include tag """ input_filepath = os.path.dirname(input_filename) @@ -100,6 +103,16 @@ def InlineFile(input_filename, output_filename): filename = filename.replace('%DISTRIBUTION%', distribution) return os.path.join(input_filepath, filename) + def IsConditionSatisfied(src_match): + expression = src_match.group('expression') + return grd_node is None or grd_node.EvaluateCondition(expression) + + def CheckConditionalElements(src_match): + """Helper function to conditionally inline inner elements""" + if not IsConditionSatisfied(src_match): + return '' + return src_match.group('content') + def InlineFileContents(src_match, pattern): """Helper function to inline external script and css files""" filepath = GetFilepath(src_match) @@ -144,7 +157,6 @@ def InlineFile(input_filename, output_filename): lambda m: SrcReplace(m, filepath), text) - # We need to inline css and js before we inline images so that image # references gets inlined in the css and js flat_text = re.sub('<script .*?src="(?P<filename>[^"\']*)".*?></script>', @@ -157,10 +169,16 @@ def InlineFile(input_filename, output_filename): flat_text) flat_text = re.sub( - '<!--\s*include\s+file="(?P<filename>[^"\']*)".*-->', + '<include\s+src="(?P<filename>[^"\']*)".*>', InlineIncludeFiles, flat_text) + # Check conditional elements, remove unsatisfied ones from the file. + flat_text = re.sub('<if .*?expr="(?P<expression>[^"]*)".*?>\s*' + + '(?P<content>([\s\S]+?))\s*</if>', + CheckConditionalElements, + flat_text) + # TODO(glen): Make this regex not match src="" text that is not inside a tag flat_text = re.sub('src="(?P<filename>[^"\']*)"', SrcReplace, diff --git a/tools/grit/grit/format/rc.py b/tools/grit/grit/format/rc.py index 5d60bd6..bb64db1 100644 --- a/tools/grit/grit/format/rc.py +++ b/tools/grit/grit/format/rc.py @@ -150,7 +150,7 @@ def GetLangDirectivePair(language) : if _LANGUAGE_DIRECTIVE_PAIR.has_key(language) : return _LANGUAGE_DIRECTIVE_PAIR[language] else : - print 'Warning:GetLangDirectivePair() found undefined language %s' %(language) + print 'Warning:GetLangDirectivePair() found undefined language %s' % (language) return 'unknown language: see tools/grit/format/rc.py' def GetLangIdHex(language) : @@ -427,7 +427,7 @@ class RcInclude(interface.ItemFormatter): if self.flatten_html: # Generate the flattened HTML file. flat_filename = os.path.join(output_dir, os.path.basename(filename)) - html_inline.InlineFile(filename, flat_filename) + html_inline.InlineFile(filename, flat_filename, item) # Include the flattened HTML file. filename = os.path.basename(filename) diff --git a/tools/grit/grit/format/rc_unittest.py b/tools/grit/grit/format/rc_unittest.py index bf4bc26..bf14dcb 100644 --- a/tools/grit/grit/format/rc_unittest.py +++ b/tools/grit/grit/format/rc_unittest.py @@ -21,15 +21,16 @@ from grit import util from grit.tool import build class DummyOutput(object): - def __init__(self, type, language): + def __init__(self, type, language, file = 'hello.gif'): self.type = type self.language = language + self.file = file def GetType(self): return self.type def GetLanguage(self): return self.language def GetOutputFilename(self): - return 'hello.gif' + return self.file class FormatRcUnittest(unittest.TestCase): def testMessages(self): @@ -78,7 +79,7 @@ END'''.strip()) build.RcBuilder.ProcessNode(root, DummyOutput('rc_all', 'en'), buf) output = buf.getvalue() self.failUnless(output.strip() == u''' -IDC_KLONKMENU MENU +IDC_KLONKMENU MENU BEGIN POPUP "&File" BEGIN @@ -181,6 +182,38 @@ END'''.strip()) output = re.sub('"[c-zC-Z]:', '"', output) self.failUnless(output.strip() == expected) + def testRcIncludeFlattenedHtmlFile(self): + input_file = util.PathFromRoot('grit/test/data/include_test.html') + output_file = '%s/include_test.html' % tempfile.gettempdir() + root = grd_reader.Parse(StringIO.StringIO(''' + <includes> + <include name="HTML_FILE1" flattenhtml="true" file="%s" type="BINDATA" /> + </includes>''' % input_file), flexible_root = True) + util.FixRootForUnittest(root, tempfile.gettempdir()) + + buf = StringIO.StringIO() + build.RcBuilder.ProcessNode(root, DummyOutput('rc_all', 'en', output_file), + buf) + output = buf.getvalue() + + expected = u'HTML_FILE1 BINDATA "include_test.html"' + # hackety hack to work on win32&lin + output = re.sub('"[c-zC-Z]:', '"', output) + self.failUnless(output.strip() == expected) + + fo = file(output_file) + file_contents = fo.read() + fo.close() + + # Check for the content added by the <include> tag. + self.failUnless(file_contents.find('Hello Include!') != -1) + # Check for the content that was removed by if tag. + self.failUnless(file_contents.find('This should not be here anymore') == -1) + # Check for the content that was kept in place by if. + self.failUnless(file_contents.find('should be kept') != -1) + self.failUnless(file_contents.find('in the middle...') != -1) + self.failUnless(file_contents.find('at the end...') != -1) + def testStructureNodeOutputfile(self): input_file = util.PathFromRoot('grit/test/data/simple.html') |