1 #!/usr/bin/env python 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 import re 7 import unittest 8 9 from py_vulcanize import parse_html_deps 10 from py_vulcanize import html_generation_controller 11 12 13 class ParseTests(unittest.TestCase): 14 15 def test_parse_empty(self): 16 parser = parse_html_deps.HTMLModuleParser() 17 module = parser.Parse('') 18 self.assertEquals([], module.scripts_external) 19 self.assertEquals([], module.inline_scripts) 20 self.assertEquals([], module.stylesheets) 21 self.assertEquals([], module.imports) 22 23 def test_parse_none(self): 24 parser = parse_html_deps.HTMLModuleParser() 25 module = parser.Parse(None) 26 self.assertEquals([], module.scripts_external) 27 self.assertEquals([], module.inline_scripts) 28 self.assertEquals([], module.stylesheets) 29 self.assertEquals([], module.imports) 30 31 def test_parse_script_src_basic(self): 32 html = """<!DOCTYPE html> 33 <html> 34 <head> 35 <script src="polymer.min.js"></script> 36 <script src="foo.js"></script> 37 </head> 38 <body> 39 </body> 40 </html>""" 41 parser = parse_html_deps.HTMLModuleParser() 42 module = parser.Parse(html) 43 self.assertEquals(['polymer.min.js', 'foo.js'], module.scripts_external) 44 self.assertEquals([], module.inline_scripts) 45 self.assertEquals([], module.stylesheets) 46 self.assertEquals([], module.imports) 47 self.assertNotIn( 48 'DOCTYPE html', 49 module.html_contents_without_links_and_script) 50 51 def test_parse_link_rel_import(self): 52 html = """<!DOCTYPE html> 53 <html> 54 <head> 55 <link rel="import" href="x-foo.html"> 56 </head> 57 <body> 58 </body> 59 </html>""" 60 parser = parse_html_deps.HTMLModuleParser() 61 module = parser.Parse(html) 62 self.assertEquals([], module.scripts_external) 63 self.assertEquals([], module.inline_scripts) 64 self.assertEquals([], module.stylesheets) 65 self.assertEquals(['x-foo.html'], module.imports) 66 67 def test_parse_script_inline(self): 68 html = """<polymer-element name="tk-element-proto"> 69 <template> 70 </template> 71 <script> 72 py_vulcanize.require("foo"); 73 py_vulcanize.require('bar'); 74 </script> 75 </polymer-element>""" 76 77 parser = parse_html_deps.HTMLModuleParser() 78 module = parser.Parse(html) 79 self.assertEquals([], module.scripts_external) 80 self.assertEquals(1, len(module.inline_scripts)) 81 self.assertEquals([], module.stylesheets) 82 self.assertEquals([], module.imports) 83 84 script0 = module.inline_scripts[0] 85 val = re.sub(r'\s+', '', script0.contents) 86 inner_script = """py_vulcanize.require("foo");py_vulcanize.require('bar');""" 87 self.assertEquals(inner_script, val) 88 89 self.assertEquals(3, len(script0.open_tags)) 90 self.assertEquals('polymer-element', script0.open_tags[2].tag) 91 92 self.assertNotIn( 93 'py_vulcanize.require("foo");', 94 module.html_contents_without_links_and_script) 95 96 def test_parse_script_inline_and_external(self): 97 html = """<polymer-element name="tk-element-proto"> 98 <template> 99 </template> 100 <script>window = {}</script> 101 <script src="foo.js"></script> 102 <script>window = undefined</script> 103 </polymer-element>""" 104 105 parser = parse_html_deps.HTMLModuleParser() 106 module = parser.Parse(html) 107 self.assertEquals(3, len(module.scripts)) 108 self.assertEquals('window = {}', module.scripts[0].contents) 109 self.assertEquals("foo.js",module.scripts[1].src) 110 self.assertTrue(module.scripts[1].is_external) 111 self.assertEquals('window = undefined', module.scripts[2].contents) 112 self.assertEquals([], module.imports) 113 114 def test_parse_script_src_sripping(self): 115 html = """ 116 <script src="blah.js"></script> 117 """ 118 module = parse_html_deps.HTMLModuleParser().Parse(html) 119 self.assertEquals('', 120 module.html_contents_without_links_and_script) 121 122 def test_parse_link_rel_stylesheet(self): 123 html = """<polymer-element name="hi"> 124 <template> 125 <link rel="stylesheet" href="frameworkstyles.css"> 126 </template> 127 </polymer-element>""" 128 parser = parse_html_deps.HTMLModuleParser() 129 module = parser.Parse(html) 130 self.assertEquals([], module.scripts_external) 131 self.assertEquals([], module.inline_scripts) 132 self.assertEquals(['frameworkstyles.css'], module.stylesheets) 133 self.assertEquals([], module.imports) 134 135 class Ctl(html_generation_controller.HTMLGenerationController): 136 137 def GetHTMLForStylesheetHRef(self, href): 138 if href == 'frameworkstyles.css': 139 return '<style>FRAMEWORK</style>' 140 return None 141 142 gen_html = module.GenerateHTML(Ctl()) 143 ghtm = """<polymer-element name="hi"> 144 <template> 145 <style>FRAMEWORK</style> 146 </template> 147 </polymer-element>""" 148 self.assertEquals(ghtm, gen_html) 149 150 def test_parse_inline_style(self): 151 html = """<style> 152 hello 153 </style>""" 154 module = parse_html_deps.HTMLModuleParser().Parse(html) 155 self.assertEquals(html, module.html_contents_without_links_and_script) 156 157 class Ctl(html_generation_controller.HTMLGenerationController): 158 159 def GetHTMLForInlineStylesheet(self, contents): 160 if contents == '\n hello\n': 161 return '\n HELLO\n' 162 return None 163 164 gen_html = module.GenerateHTML(Ctl()) 165 ghtm = """<style> 166 HELLO 167 </style>""" 168 self.assertEquals(ghtm, gen_html) 169 170 def test_parse_style_import(self): 171 html = """<polymer-element name="x-blink"> 172 <template> 173 <style> 174 @import url(awesome.css); 175 </style> 176 </template> 177 </polymer-element>""" 178 parser = parse_html_deps.HTMLModuleParser() 179 self.assertRaises(lambda: parser.Parse(html)) 180 181 def test_nested_templates(self): 182 orig_html = """<template> 183 <template> 184 <div id="foo"></div> 185 </template> 186 </template>""" 187 parser = parse_html_deps.HTMLModuleParser() 188 res = parser.Parse(orig_html) 189 html = res.html_contents_without_links_and_script 190 self.assertEquals(html, orig_html) 191 192 def test_html_contents_basic(self): 193 html = """<a b="c">d</a>""" 194 parser = parse_html_deps.HTMLModuleParser() 195 module = parser.Parse(html) 196 self.assertEquals(html, module.html_contents_without_links_and_script) 197 198 def test_html_contents_with_entity(self): 199 html = """<a>→</a>""" 200 parser = parse_html_deps.HTMLModuleParser() 201 module = parser.Parse(html) 202 self.assertEquals(u'<a>\u2192</a>', 203 module.html_contents_without_links_and_script) 204 205 def test_html_content_with_charref(self): 206 html = """<a>></a>""" 207 parser = parse_html_deps.HTMLModuleParser() 208 module = parser.Parse(html) 209 self.assertEquals('<a>></a>', 210 module.html_contents_without_links_and_script) 211 212 def test_html_content_start_end_br(self): 213 html = """<a><br /></a>""" 214 parser = parse_html_deps.HTMLModuleParser() 215 module = parser.Parse(html) 216 self.assertEquals('<a><br/></a>', 217 module.html_contents_without_links_and_script) 218 219 def test_html_content_start_end_img(self): 220 html = """<a><img src="foo.png" id="bar" /></a>""" 221 parser = parse_html_deps.HTMLModuleParser() 222 module = parser.Parse(html) 223 self.assertEquals('<a><img id="bar" src="foo.png"/></a>', 224 module.html_contents_without_links_and_script) 225 226 def test_html_contents_with_link_stripping(self): 227 html = """<a b="c">d</a> 228 <link rel="import" href="x-foo.html">""" 229 parser = parse_html_deps.HTMLModuleParser() 230 module = parser.Parse(html) 231 self.assertEquals("""<a b="c">d</a>""", 232 module.html_contents_without_links_and_script.strip()) 233 234 def test_html_contents_with_style_link_stripping(self): 235 html = """<a b="c">d</a> 236 <link rel="stylesheet" href="frameworkstyles.css">""" 237 parser = parse_html_deps.HTMLModuleParser() 238 module = parser.Parse(html) 239 self.assertEquals("""<a b="c">d</a>""", 240 module.html_contents_without_links_and_script.strip()) 241 242 def test_br_does_not_raise(self): 243 html = """<div><br/></div>""" 244 parser = parse_html_deps.HTMLModuleParser() 245 parser.Parse(html) 246 247 def test_p_does_not_raises(self): 248 html = """<div></p></div>""" 249 parser = parse_html_deps.HTMLModuleParser() 250 parser.Parse(html) 251 252 def test_link_endlink_does_not_raise(self): 253 html = """<link rel="stylesheet" href="foo.css"></link>""" 254 parser = parse_html_deps.HTMLModuleParser() 255 parser.Parse(html) 256 257 def test_link_script_does_not_raise(self): 258 html = """<link rel="stylesheet" href="foo.css"> 259 <script> 260 </script>""" 261 parser = parse_html_deps.HTMLModuleParser() 262 parser.Parse(html) 263 264 def test_script_with_script_inside_as_js(self): 265 html = """<script> 266 var html_lines = [ 267 '<script>', 268 '<\/script>', 269 ]; 270 </script>""" 271 parser = parse_html_deps.HTMLModuleParser() 272 parser.Parse(html) 273 274 def test_invalid_script_escaping_raises(self): 275 html = """<script> 276 var html_lines = [ 277 '<script>', 278 '< /script>', 279 ]; 280 </script>""" 281 parser = parse_html_deps.HTMLModuleParser() 282 283 def DoIt(): 284 parser.Parse(html) 285 self.assertRaises(Exception, DoIt) 286 287 def test_script_with_cdata(self): 288 html = """<script></h2></script>""" 289 parser = parse_html_deps.HTMLModuleParser() 290 module = parser.Parse(html) 291 self.assertEquals(1, len(module.inline_scripts)) 292 self.assertEquals('</h2>', module.inline_scripts[0].contents) 293