# Copyright (c) 2013 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 unittest
-import parse_html_deps
import re
+import unittest
+
+from tvcm import parse_html_deps
+from tvcm import module as module_module
+from tvcm import html_generation_controller
class ParseTests(unittest.TestCase):
parser = parse_html_deps.HTMLModuleParser()
module = parser.Parse("")
self.assertEquals([], module.scripts_external)
- self.assertEquals([], module.scripts_inline)
+ self.assertEquals([], module.inline_scripts)
self.assertEquals([], module.stylesheets)
self.assertEquals([], module.imports)
parser = parse_html_deps.HTMLModuleParser()
module = parser.Parse(None)
self.assertEquals([], module.scripts_external)
- self.assertEquals([], module.scripts_inline)
+ self.assertEquals([], module.inline_scripts)
self.assertEquals([], module.stylesheets)
self.assertEquals([], module.imports)
parser = parse_html_deps.HTMLModuleParser()
module = parser.Parse(html)
self.assertEquals(['polymer.min.js', 'foo.js'], module.scripts_external);
- self.assertEquals([], module.scripts_inline)
+ self.assertEquals([], module.inline_scripts)
self.assertEquals([], module.stylesheets)
self.assertEquals([], module.imports)
+ self.assertTrue(module.has_decl)
+ self.assertTrue('DOCTYPE html' not in module.html_contents_without_links_and_script)
+
+ class Ctl(html_generation_controller.HTMLGenerationController):
+ def GetHTMLForScriptHRef(self, href):
+ if href == "polymer.min.js":
+ return "<script>POLYMER</script>"
+ elif href == "foo.js":
+ return "<script>FOO</script>"
+ return None
+
+ def GetHTMLForStylesheetHRef(self, href):
+ return None
+
+ gen_html = module.GenerateHTML(Ctl())
+ ghtm = """
+ <html>
+ <head>
+ <script>POLYMER</script>
+ <script>FOO</script>
+ </head>
+ <body>
+ </body>
+ </html>"""
+ self.assertEquals(ghtm, gen_html)
def test_parse_link_rel_import(self):
html = """<!DOCTYPE html>
parser = parse_html_deps.HTMLModuleParser()
module = parser.Parse(html)
self.assertEquals([], module.scripts_external);
- self.assertEquals([], module.scripts_inline)
+ self.assertEquals([], module.inline_scripts)
self.assertEquals([], module.stylesheets)
self.assertEquals(['x-foo.html'], module.imports)
+ self.assertTrue(module.has_decl)
+
def test_parse_script_inline(self):
html = """<polymer-element name="tk-element-proto">
parser = parse_html_deps.HTMLModuleParser()
module = parser.Parse(html)
self.assertEquals([], module.scripts_external);
- self.assertEquals(1, len(module.scripts_inline))
+ self.assertEquals(1, len(module.inline_scripts))
self.assertEquals([], module.stylesheets)
self.assertEquals([], module.imports)
+ self.assertFalse(module.has_decl)
- val = module.scripts_inline[0]
- val = re.sub(r"\s+", '', val)
+ script0 = module.inline_scripts[0]
+ val = re.sub(r"\s+", '', script0.contents)
inner_script = """tvcm.require("foo");tvcm.require('bar');"""
self.assertEquals(inner_script, val)
+ self.assertEquals(1, len(script0.open_tags))
+ self.assertEquals('polymer-element', script0.open_tags[0].tag)
+
+ assert 'tvcm.require("foo");' not in module.html_contents_without_links_and_script
+
+ def test_parse_script_src_sripping(self):
+ html = """
+<script src="blah.js"></script>
+"""
+ module = parse_html_deps.HTMLModuleParser().Parse(html)
+ self.assertEquals('\n\n', module.html_contents_without_links_and_script)
+
+
def test_parse_link_rel_stylesheet(self):
html = """<polymer-element name="hi">
<template>
parser = parse_html_deps.HTMLModuleParser()
module = parser.Parse(html)
self.assertEquals([], module.scripts_external);
- self.assertEquals([], module.scripts_inline)
+ self.assertEquals([], module.inline_scripts)
self.assertEquals(['frameworkstyles.css'], module.stylesheets)
self.assertEquals([], module.imports)
+ self.assertFalse(module.has_decl)
+
+ class Ctl(html_generation_controller.HTMLGenerationController):
+ def GetHTMLForScriptHRef(self, href):
+ return None
+
+ def GetHTMLForStylesheetHRef(self, href):
+ if href == "frameworkstyles.css":
+ return "<style>FRAMEWORK</style>"
+ return None
+
+ gen_html = module.GenerateHTML(Ctl())
+ ghtm = """<polymer-element name="hi">
+ <template>
+ <style>FRAMEWORK</style>
+ </template>
+ </polymer-element>"""
+ self.assertEquals(ghtm, gen_html)
+
+
+ def test_parse_inline_style(self):
+ html = """
+<style>
+ hello
+</style>"""
+ module = parse_html_deps.HTMLModuleParser().Parse(html)
+ self.assertEquals(html, module.html_contents_without_links_and_script)
+
+ class Ctl(html_generation_controller.HTMLGenerationController):
+ def GetHTMLForInlineStylesheet(self, contents):
+ if contents == '\n hello\n':
+ return '\n HELLO\n'
+ return None
+
+ gen_html = module.GenerateHTML(Ctl())
+ ghtm = """
+<style>
+ HELLO
+</style>"""
+ self.assertEquals(ghtm, gen_html)
def test_parse_style_import(self):
html = """<polymer-element name="x-blink">
</template>
</polymer-element>"""
parser = parse_html_deps.HTMLModuleParser()
+ self.assertRaises(lambda: parser.Parse(html))
+
+ def test_html_contents_basic(self):
+ html = """<a b="c">d</a>"""
+ parser = parse_html_deps.HTMLModuleParser()
module = parser.Parse(html)
- self.assertEquals([], module.scripts_external);
- self.assertEquals([], module.scripts_inline)
- self.assertEquals(['awesome.css'], module.stylesheets)
- self.assertEquals([], module.imports)
+ self.assertEquals(html, module.html_contents_without_links_and_script)
+ def test_html_contents_with_entity(self):
+ html = """<a>→</a>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+ self.assertEquals(html, module.html_contents_without_links_and_script)
+
+ def test_html_content_with_charref(self):
+ html = """<a>></a>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+ self.assertEquals(html, module.html_contents_without_links_and_script)
+
+ def test_html_content_start_end_br(self):
+ html = """<a><br /></a>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+ self.assertEquals(html, module.html_contents_without_links_and_script)
+
+ def test_html_content_start_end_img(self):
+ html = """<a><img src="foo.png" id="bar" /></a>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+ self.assertEquals(html, module.html_contents_without_links_and_script)
+
+ def test_html_contents_with_link_stripping(self):
+ html = """<a b="c">d</a>
+ <link rel="import" href="x-foo.html">"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+ self.assertEquals("""<a b="c">d</a>""",
+ module.html_contents_without_links_and_script.strip())
+
+ def test_html_contents_with_style_link_stripping(self):
+ html = """<a b="c">d</a>
+ <link rel="stylesheet" href="frameworkstyles.css">"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+ self.assertEquals("""<a b="c">d</a>""",
+ module.html_contents_without_links_and_script.strip())
+
+ def test_malformed_script_raises(self):
+ html = """<script src="x"/>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ def DoIt():
+ module = parser.Parse(html)
+ self.assertRaises(Exception, DoIt)
+
+ def test_malformed_br_raises(self):
+ html = """<br>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ def DoIt():
+ module = parser.Parse(html)
+ self.assertRaises(Exception, DoIt)
+
+ def test_br_does_not_raise(self):
+ html = """<div><br/></div>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+
+ def test_p_does_not_raises(self):
+ html = """<div></p></div>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+
+ def test_link_endlink_does_not_raise(self):
+ html = """<link rel="stylesheet" href="foo.css"></link>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+
+ def test_link_script_does_not_raise(self):
+ html = """<link rel="stylesheet" href="foo.css">
+ <script>
+ </script>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+
+ def test_malformed_script_raises(self):
+ html = """<script src="/jszip-inflate.js"</script>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ def DoIt():
+ module = parser.Parse(html)
+ self.assertRaises(Exception, DoIt)
+
+ def test_script_with_script_inside_as_js(self):
+ html = """<script>
+ var html_lines = [
+ '<script>',
+ '<\/script>',
+ ];
+ </script>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ module = parser.Parse(html)
+
+ def test_invalid_script_escaping_raises(self):
+ html = """<script>
+ var html_lines = [
+ '<script>',
+ '< /script>',
+ ];
+ </script>"""
+ parser = parse_html_deps.HTMLModuleParser()
+ def DoIt():
+ module = parser.Parse(html)
+ self.assertRaises(Exception, DoIt)
if __name__ == '__main__':
unittest.main()