OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Converts gwt-compiled javascript to standalone javascript | 5 """Converts gwt-compiled javascript to standalone javascript |
6 | 6 |
7 gwt-compiled javascript is in the form of an html file that is expected to be | 7 gwt-compiled javascript is in the form of an js file that is expected to be |
8 loaded into its own iframe (with some extra work done in the embedding page). | 8 loaded into its own script tag. This reads such a compiled file and converts it |
9 This reads such a compiled file and converts it to standalone javascript that | 9 to standalone javascript that can be loaded as Chrome does. |
10 can be loaded in the main frame of a page. | |
11 """ | 10 """ |
12 | 11 |
| 12 # TODO(cjhopman): The proper way to do this is to write a gwt Linker |
| 13 # (gwt.core.ext.Linker) and use that for compilation. See |
| 14 # http://crbug.com/437113 |
| 15 |
13 import glob | 16 import glob |
14 import optparse | 17 import optparse |
15 import os | 18 import os |
16 import re | 19 import re |
17 import sys | 20 import sys |
18 | 21 |
19 def ExtractJavascript(content): | 22 def ExtractJavascript(content): |
20 """ Extracts javascript from within <script> tags in content. """ | 23 """ Extracts javascript from within <script> tags in content. """ |
21 scriptre = re.compile('<script>(<!--)?(?P<inner>.*?)(-->)?</script>', | 24 lines = content.split('\n'); |
22 re.MULTILINE | re.DOTALL) | 25 # The generated javascript looks something like: |
23 result = '' | 26 # |
24 for match in scriptre.finditer(content): | 27 # function domdistiller() { |
25 result += match.group('inner') | 28 # ... |
26 return result | 29 # } |
27 | 30 # domdistiller();<useful code here> |
28 def FindInputPath(indir): | 31 # <more useful code> |
29 """ Finds the path to a file of the form | 32 # <last useful code>;if (domdistiller) domdistiller.onScriptLoad(gwtOnLoad); |
30 in/dir/DC2C3039DDCBB4AD9B63A9D3E25A0BDF.cache.html | 33 # |
31 | 34 # And so we extract the useful parts and append the correct gwtOnLoad call. |
32 There should only be one such file in indir. | 35 marker = 'domdistiller();' |
33 """ | 36 for i, l in enumerate(lines): |
34 files = glob.glob(os.path.join(indir, '*.cache.html')) | 37 if l.startswith(marker): |
35 if len(files) != 1: | 38 return '\n'.join( |
36 print 'Unable to find input path: ', files | 39 [l[len(marker):]] + |
37 return None | 40 lines[i + 1:-1] + |
38 return files[0] | 41 [lines[-1].replace( |
| 42 'if (domdistiller) domdistiller.onScriptLoad(gwtOnLoad);', |
| 43 'gwtOnLoad(undefined, \'domdistiller\', \'\', 0);') |
| 44 ]) |
| 45 raise Exception('Failed to find marker line') |
39 | 46 |
40 def main(argv): | 47 def main(argv): |
41 parser = optparse.OptionParser() | 48 parser = optparse.OptionParser() |
42 parser.add_option('-i', '--indir') | 49 parser.add_option('-i', '--infile') |
43 parser.add_option('-o', '--outfile') | 50 parser.add_option('-o', '--outfile') |
44 options, _ = parser.parse_args(argv) | 51 options, _ = parser.parse_args(argv) |
45 | 52 |
46 if options.indir: | 53 if options.infile: |
47 inpath = FindInputPath(options.indir) | 54 infile = open(options.infile, 'r') |
48 if not inpath: | |
49 return 1 | |
50 infile = open(inpath, 'r') | |
51 else: | 55 else: |
52 print 'Reading input from stdin' | 56 print 'Reading input from stdin' |
53 infile = sys.stdin | 57 infile = sys.stdin |
54 | 58 |
55 if options.outfile: | 59 if options.outfile: |
56 outfile = open(options.outfile, 'w') | 60 outfile = open(options.outfile, 'w') |
57 else: | 61 else: |
58 outfile = sys.stdout | 62 outfile = sys.stdout |
59 | 63 |
60 compiledJs = infile.read() | 64 compiledJs = infile.read() |
61 # The compiled js expects to be running in its own iframe. This won't be the | 65 # The compiled js expects to be running in its own iframe. This won't be the |
62 # case for the standalone js. | 66 # case for the standalone js. |
63 compiledJs = compiledJs.replace('var $wnd = parent', 'var $wnd = window') | 67 compiledJs = compiledJs.replace('var $wnd = parent', 'var $wnd = window') |
64 outfile.write(ExtractJavascript(compiledJs)) | 68 outfile.write(ExtractJavascript(compiledJs)) |
65 outfile.write('gwtOnLoad(undefined,"domdistiller","",0);\n') | |
66 | 69 |
67 return 0 | 70 return 0 |
68 | 71 |
69 | 72 |
70 if __name__ == '__main__': | 73 if __name__ == '__main__': |
71 sys.exit(main(sys.argv)) | 74 sys.exit(main(sys.argv)) |
OLD | NEW |