envjs19 0.3.8.20101029121421

Sign up to get free protection for your applications and to get access to all the features.
Files changed (279) hide show
  1. data/.jslintrbrc +30 -0
  2. data/.project +17 -0
  3. data/CHANGELOG.rdoc +57 -0
  4. data/DTD/xhtml-lat1.ent +196 -0
  5. data/DTD/xhtml-special.ent +80 -0
  6. data/DTD/xhtml-symbol.ent +237 -0
  7. data/DTD/xhtml.soc +14 -0
  8. data/DTD/xhtml1-frameset.dtd +1235 -0
  9. data/DTD/xhtml1-strict.dtd +978 -0
  10. data/DTD/xhtml1-transitional.dtd +1201 -0
  11. data/DTD/xhtml1.dcl +192 -0
  12. data/Makefile +7 -0
  13. data/Manifest.txt +280 -0
  14. data/README.rdoc +65 -0
  15. data/Rakefile +196 -0
  16. data/Wakefile +11 -0
  17. data/bin/envjsrb +257 -0
  18. data/build.properties +9 -0
  19. data/build.xml +247 -0
  20. data/gm/jquery.js +6002 -0
  21. data/gm/mainx.js +2648 -0
  22. data/gm/sensx.js +135 -0
  23. data/gm/t.js +6 -0
  24. data/gm/x.html +76 -0
  25. data/htmlparser/BrowserTreeBuilder.java +456 -0
  26. data/htmlparser/README +34 -0
  27. data/htmlparser/build.sh +38 -0
  28. data/jsl/jsl +0 -0
  29. data/jsl/jsl.default.conf +129 -0
  30. data/jsl/jsl.exe +0 -0
  31. data/lib/envjs.rb +35 -0
  32. data/lib/envjs/event_loop.js +206 -0
  33. data/lib/envjs/net.rb +3 -0
  34. data/lib/envjs/net/cgi.rb +94 -0
  35. data/lib/envjs/net/file.rb +69 -0
  36. data/lib/envjs/options.rb +11 -0
  37. data/lib/envjs/runtime.rb +346 -0
  38. data/lib/envjs/tempfile.rb +24 -0
  39. data/licenses/GPL-LICENSE.txt +278 -0
  40. data/licenses/MIT-LICENSE.txt +20 -0
  41. data/src/base64.js +80 -0
  42. data/src/build.js +6 -0
  43. data/src/cruft/bad.html +24 -0
  44. data/src/cruft/dom.js +606 -0
  45. data/src/cruft/element.js +297 -0
  46. data/src/cruft/good.html +30 -0
  47. data/src/cruft/good.js +32 -0
  48. data/src/cruft/internal.js +81 -0
  49. data/src/cruft/parser.js +458 -0
  50. data/src/css/properties.js +293 -0
  51. data/src/css/rule.js +22 -0
  52. data/src/css/sizzle.js +717 -0
  53. data/src/css/stylesheet.js +52 -0
  54. data/src/dom/attr.js +55 -0
  55. data/src/dom/cdatasection.js +31 -0
  56. data/src/dom/characterdata.js +119 -0
  57. data/src/dom/comment.js +30 -0
  58. data/src/dom/doctype.js +9 -0
  59. data/src/dom/document.js +1023 -0
  60. data/src/dom/dom.js +134 -0
  61. data/src/dom/element.js +217 -0
  62. data/src/dom/entities.js +273 -0
  63. data/src/dom/exception.js +28 -0
  64. data/src/dom/fragment.js +37 -0
  65. data/src/dom/implementation.js +140 -0
  66. data/src/dom/instruction.js +51 -0
  67. data/src/dom/namednodemap.js +374 -0
  68. data/src/dom/namespace.js +50 -0
  69. data/src/dom/node.js +618 -0
  70. data/src/dom/nodelist.js +195 -0
  71. data/src/dom/parser.js +1207 -0
  72. data/src/dom/text.js +73 -0
  73. data/src/event/event.js +47 -0
  74. data/src/event/mouseevent.js +4 -0
  75. data/src/event/uievent.js +8 -0
  76. data/src/html/a.js +110 -0
  77. data/src/html/anchor.js +80 -0
  78. data/src/html/area.js +57 -0
  79. data/src/html/base.js +26 -0
  80. data/src/html/blockquote-q.js +19 -0
  81. data/src/html/body.js +19 -0
  82. data/src/html/button.js +21 -0
  83. data/src/html/canvas.js +14 -0
  84. data/src/html/col-colgroup.js +49 -0
  85. data/src/html/collection.js +72 -0
  86. data/src/html/cookie.js +153 -0
  87. data/src/html/del-ins.js +25 -0
  88. data/src/html/div.js +28 -0
  89. data/src/html/document.js +364 -0
  90. data/src/html/element.js +382 -0
  91. data/src/html/fieldset.js +19 -0
  92. data/src/html/form.js +484 -0
  93. data/src/html/frame.js +89 -0
  94. data/src/html/frameset.js +25 -0
  95. data/src/html/head.js +44 -0
  96. data/src/html/html.js +0 -0
  97. data/src/html/htmlparser.js +340 -0
  98. data/src/html/iframe.js +26 -0
  99. data/src/html/image.js +0 -0
  100. data/src/html/img.js +62 -0
  101. data/src/html/input-elements.js +307 -0
  102. data/src/html/input.js +65 -0
  103. data/src/html/label.js +26 -0
  104. data/src/html/legend.js +19 -0
  105. data/src/html/link.js +82 -0
  106. data/src/html/map.js +22 -0
  107. data/src/html/meta.js +37 -0
  108. data/src/html/object.js +89 -0
  109. data/src/html/optgroup.js +25 -0
  110. data/src/html/option.js +103 -0
  111. data/src/html/param.js +38 -0
  112. data/src/html/script.js +122 -0
  113. data/src/html/select.js +132 -0
  114. data/src/html/style.js +31 -0
  115. data/src/html/table.js +199 -0
  116. data/src/html/tbody-thead-tfoot.js +92 -0
  117. data/src/html/td-th.js +18 -0
  118. data/src/html/textarea.js +31 -0
  119. data/src/html/title.js +20 -0
  120. data/src/html/tr.js +114 -0
  121. data/src/intro.js +141 -0
  122. data/src/outro.js +70 -0
  123. data/src/parser/html5.detailed.js +10762 -0
  124. data/src/parser/html5.min.js +503 -0
  125. data/src/parser/html5.pretty.js +10815 -0
  126. data/src/parser/intro.js +42 -0
  127. data/src/parser/outro.js +9 -0
  128. data/src/platform/core.js +323 -0
  129. data/src/platform/johnson.js +484 -0
  130. data/src/platform/rhino.js +327 -0
  131. data/src/platform/static/intro.js +41 -0
  132. data/src/platform/static/outro.js +30 -0
  133. data/src/profile/aop.js +238 -0
  134. data/src/profile/profile.js +402 -0
  135. data/src/serializer/xml.js +21 -0
  136. data/src/svg/animatedstring.js +25 -0
  137. data/src/svg/document.js +25 -0
  138. data/src/svg/element.js +22 -0
  139. data/src/svg/locatable.js +17 -0
  140. data/src/svg/rect.js +18 -0
  141. data/src/svg/rectelement.js +24 -0
  142. data/src/svg/stylable.js +49 -0
  143. data/src/svg/svgelement.js +22 -0
  144. data/src/svg/transformable.js +15 -0
  145. data/src/window/css.js +15 -0
  146. data/src/window/dialog.js +16 -0
  147. data/src/window/document.js +28 -0
  148. data/src/window/event.js +265 -0
  149. data/src/window/history.js +62 -0
  150. data/src/window/location.js +156 -0
  151. data/src/window/navigator.js +48 -0
  152. data/src/window/screen.js +53 -0
  153. data/src/window/timer.js +21 -0
  154. data/src/window/window.js +284 -0
  155. data/src/window/xhr.js +135 -0
  156. data/src/xpath/expression.js +49 -0
  157. data/src/xpath/implementation.js +2505 -0
  158. data/src/xpath/result.js +67 -0
  159. data/src/xpath/util.js +551 -0
  160. data/src/xpath/xmltoken.js +149 -0
  161. data/src/xslt/COPYING +34 -0
  162. data/src/xslt/ajaxslt-0.8.1/AUTHORS +1 -0
  163. data/src/xslt/ajaxslt-0.8.1/ChangeLog +136 -0
  164. data/src/xslt/ajaxslt-0.8.1/Makefile +49 -0
  165. data/src/xslt/ajaxslt-0.8.1/README +102 -0
  166. data/src/xslt/ajaxslt-0.8.1/TODO +15 -0
  167. data/src/xslt/ajaxslt-0.8.1/dom.js +566 -0
  168. data/src/xslt/ajaxslt-0.8.1/dom_unittest.html +24 -0
  169. data/src/xslt/ajaxslt-0.8.1/dom_unittest.js +131 -0
  170. data/src/xslt/ajaxslt-0.8.1/simplelog.js +79 -0
  171. data/src/xslt/ajaxslt-0.8.1/test/xpath.html +18 -0
  172. data/src/xslt/ajaxslt-0.8.1/test/xpath_script.js +45 -0
  173. data/src/xslt/ajaxslt-0.8.1/test/xslt.html +58 -0
  174. data/src/xslt/ajaxslt-0.8.1/test/xslt_script.js +33 -0
  175. data/src/xslt/ajaxslt-0.8.1/unittestsuite.html +26 -0
  176. data/src/xslt/ajaxslt-0.8.1/xmltoken.js +149 -0
  177. data/src/xslt/ajaxslt-0.8.1/xmltoken_unittest.html +18 -0
  178. data/src/xslt/ajaxslt-0.8.1/xmltoken_unittest.js +811 -0
  179. data/src/xslt/ajaxslt-0.8.1/xpath_unittest.html +39 -0
  180. data/src/xslt/ajaxslt-0.8.1/xpath_unittest.js +557 -0
  181. data/src/xslt/ajaxslt-0.8.1/xpathdebug.js +234 -0
  182. data/src/xslt/ajaxslt-0.8.1/xslt_unittest.html +138 -0
  183. data/src/xslt/ajaxslt-0.8.1/xslt_unittest.js +68 -0
  184. data/src/xslt/implementation.js +625 -0
  185. data/src/xslt/processor.js +37 -0
  186. data/src/xslt/util.js +449 -0
  187. data/test/base64.js +80 -0
  188. data/test/call-load-test.js +15 -0
  189. data/test/data.js +45 -0
  190. data/test/debug.js +53 -0
  191. data/test/firebug/errorIcon.png +0 -0
  192. data/test/firebug/firebug.css +209 -0
  193. data/test/firebug/firebug.html +23 -0
  194. data/test/firebug/firebug.js +672 -0
  195. data/test/firebug/firebugx.js +10 -0
  196. data/test/firebug/infoIcon.png +0 -0
  197. data/test/firebug/warningIcon.png +0 -0
  198. data/test/fixtures/html/events.html +171 -0
  199. data/test/fixtures/html/iframe1.html +46 -0
  200. data/test/fixtures/html/iframe1a.html +46 -0
  201. data/test/fixtures/html/iframe2.html +45 -0
  202. data/test/fixtures/html/iframe3.html +28 -0
  203. data/test/fixtures/html/iframeN.html +57 -0
  204. data/test/fixtures/html/malformed.html +181 -0
  205. data/test/fixtures/html/scope.html +81 -0
  206. data/test/fixtures/html/trivial.html +19 -0
  207. data/test/fixtures/html/with_js.html +26 -0
  208. data/test/fixtures/images/icon-blue.png +0 -0
  209. data/test/fixtures/js/external_script.js +1 -0
  210. data/test/fixtures/js/script.js +1 -0
  211. data/test/fixtures/js/script_error.js +2 -0
  212. data/test/foo.html +8 -0
  213. data/test/foo.js +40 -0
  214. data/test/html/events.html +171 -0
  215. data/test/html/iframe1.html +46 -0
  216. data/test/html/iframe1a.html +46 -0
  217. data/test/html/iframe2.html +45 -0
  218. data/test/html/iframe3.html +30 -0
  219. data/test/html/iframeN.html +57 -0
  220. data/test/html/malformed.html +181 -0
  221. data/test/html/scope.html +87 -0
  222. data/test/html/script.js +1 -0
  223. data/test/html/trivial.html +19 -0
  224. data/test/html/with_js.html +26 -0
  225. data/test/index.html +328 -0
  226. data/test/java-prototype.js +9 -0
  227. data/test/jquery.js +6002 -0
  228. data/test/primary-tests.js +26 -0
  229. data/test/prototype-test.js +13 -0
  230. data/test/qunit.js +61 -0
  231. data/test/qunit/qunit/qunit.css +17 -0
  232. data/test/qunit/qunit/qunit.js +997 -0
  233. data/test/scope.html +19 -0
  234. data/test/scope.rb +24 -0
  235. data/test/smp/dmathieu/index.html +8 -0
  236. data/test/specs/dist/env.spec.js +1534 -0
  237. data/test/specs/envjs.spec.css +46 -0
  238. data/test/specs/parser/html.js +31 -0
  239. data/test/specs/parser/spec.html +40 -0
  240. data/test/specs/parser/xml.js +31 -0
  241. data/test/specs/qunit.bdd.js +210 -0
  242. data/test/specs/qunit.css +17 -0
  243. data/test/specs/qunit.js +997 -0
  244. data/test/specs/template/spec-0.js +31 -0
  245. data/test/specs/template/spec-1.js +31 -0
  246. data/test/specs/template/spec.html +40 -0
  247. data/test/specs/window/css.js +23 -0
  248. data/test/specs/window/dialog.js +25 -0
  249. data/test/specs/window/document.js +23 -0
  250. data/test/specs/window/event.js +25 -0
  251. data/test/specs/window/history.js +34 -0
  252. data/test/specs/window/location.js +34 -0
  253. data/test/specs/window/navigator.js +71 -0
  254. data/test/specs/window/screen.js +42 -0
  255. data/test/specs/window/spec.html +48 -0
  256. data/test/specs/window/timer.js +26 -0
  257. data/test/specs/window/window.js +53 -0
  258. data/test/specs/xhr/spec.html +47 -0
  259. data/test/specs/xhr/xhr.js +31 -0
  260. data/test/test.js +10 -0
  261. data/test/unit/dom.js +44 -0
  262. data/test/unit/elementmembers.js +92 -0
  263. data/test/unit/events.js +195 -0
  264. data/test/unit/fixtures/external_script.js +1 -0
  265. data/test/unit/form.js +40 -0
  266. data/test/unit/iframe.js +234 -0
  267. data/test/unit/insertion.js +23 -0
  268. data/test/unit/multi-window.js +212 -0
  269. data/test/unit/nu.validator.js +34 -0
  270. data/test/unit/onload.js +90 -0
  271. data/test/unit/parser.js +122 -0
  272. data/test/unit/prototypecompat.js +22 -0
  273. data/test/unit/proxy.js +6 -0
  274. data/test/unit/scope.js +209 -0
  275. data/test/unit/timer.js +115 -0
  276. data/test/unit/window.js +57 -0
  277. data/test/x.js +1 -0
  278. data/test/y.js +1 -0
  279. metadata +367 -0
data/gm/sensx.js ADDED
@@ -0,0 +1,135 @@
1
+ window.google = window.google || {};
2
+ google.maps = google.maps || {};
3
+ (function () {
4
+
5
+ function getScript(src) {
6
+ document.write('<' + 'script src="' + src + '"' + ' type="text/javascript"><' + '/script>');
7
+ }
8
+
9
+ google.maps.Load = function (apiLoad) {
10
+ apiLoad([, [
11
+ [
12
+ ["http://mt0.google.com/vt/v=ap.119\u0026hl=en-US\u0026", "http://mt1.google.com/vt/v=ap.119\u0026hl=en-US\u0026"], , "ap.119"],
13
+ [
14
+ ["http://khm0.google.com/kh/v=57\u0026hl=en-US\u0026", "http://khm1.google.com/kh/v=57\u0026hl=en-US\u0026"], , "57", , ],
15
+ [
16
+ ["http://mt0.google.com/vt/v=apt.119\u0026hl=en-US\u0026", "http://mt1.google.com/vt/v=apt.119\u0026hl=en-US\u0026"], , "apt.119", "imgtp=png32\u0026"],
17
+ [
18
+ ["http://mt0.google.com/vt/v=app.119\u0026hl=en-US\u0026", "http://mt1.google.com/vt/v=app.119\u0026hl=en-US\u0026"], , "app.119"], "fzwq1GFIQiP5cNRPOUZ4M6mfdbQT9hD-lAP3jA", [
19
+ [, 0, 7, 7, [
20
+ [
21
+ [330000000, 1246050000],
22
+ [386200000, 1293600000]
23
+ ],
24
+ [
25
+ [366500000, 1297000000],
26
+ [386200000, 1320034790]
27
+ ]
28
+ ],
29
+ ["http://mt0.gmaptiles.co.kr/mt/v=kr1.12\u0026hl=en-US\u0026", "http://mt1.gmaptiles.co.kr/mt/v=kr1.12\u0026hl=en-US\u0026"], , , , "http://www.gmaptiles.co.kr/mapprint"],
30
+ [, 0, 8, 9, [
31
+ [
32
+ [330000000, 1246050000],
33
+ [386200000, 1279600000]
34
+ ],
35
+ [
36
+ [345000000, 1279600000],
37
+ [386200000, 1286700000]
38
+ ],
39
+ [
40
+ [348900000, 1286700000],
41
+ [386200000, 1293600000]
42
+ ],
43
+ [
44
+ [354690000, 1293600000],
45
+ [386200000, 1320034790]
46
+ ]
47
+ ],
48
+ ["http://mt0.gmaptiles.co.kr/mt/v=kr1.12\u0026hl=en-US\u0026", "http://mt1.gmaptiles.co.kr/mt/v=kr1.12\u0026hl=en-US\u0026"], , , , "http://www.gmaptiles.co.kr/mapprint"],
49
+ [, 0, 10, 19, [
50
+ [
51
+ [329890840, 1246055600],
52
+ [386930130, 1284960940]
53
+ ],
54
+ [
55
+ [344646740, 1284960940],
56
+ [386930130, 1288476560]
57
+ ],
58
+ [
59
+ [350277470, 1288476560],
60
+ [386930130, 1310531620]
61
+ ],
62
+ [
63
+ [370277730, 1310531620],
64
+ [386930130, 1320034790]
65
+ ]
66
+ ],
67
+ ["http://mt0.gmaptiles.co.kr/mt/v=kr1.12\u0026hl=en-US\u0026", "http://mt1.gmaptiles.co.kr/mt/v=kr1.12\u0026hl=en-US\u0026"], , , , "http://www.gmaptiles.co.kr/mapprint"],
68
+ [, 3, 7, 7, [
69
+ [
70
+ [330000000, 1246050000],
71
+ [386200000, 1293600000]
72
+ ],
73
+ [
74
+ [366500000, 1297000000],
75
+ [386200000, 1320034790]
76
+ ]
77
+ ],
78
+ ["http://mt0.gmaptiles.co.kr/mt/v=kr1p.12\u0026hl=en-US\u0026", "http://mt1.gmaptiles.co.kr/mt/v=kr1p.12\u0026hl=en-US\u0026"]
79
+ ],
80
+ [, 3, 8, 9, [
81
+ [
82
+ [330000000, 1246050000],
83
+ [386200000, 1279600000]
84
+ ],
85
+ [
86
+ [345000000, 1279600000],
87
+ [386200000, 1286700000]
88
+ ],
89
+ [
90
+ [348900000, 1286700000],
91
+ [386200000, 1293600000]
92
+ ],
93
+ [
94
+ [354690000, 1293600000],
95
+ [386200000, 1320034790]
96
+ ]
97
+ ],
98
+ ["http://mt0.gmaptiles.co.kr/mt/v=kr1p.12\u0026hl=en-US\u0026", "http://mt1.gmaptiles.co.kr/mt/v=kr1p.12\u0026hl=en-US\u0026"]
99
+ ],
100
+ [, 3, 10, , [
101
+ [
102
+ [329890840, 1246055600],
103
+ [386930130, 1284960940]
104
+ ],
105
+ [
106
+ [344646740, 1284960940],
107
+ [386930130, 1288476560]
108
+ ],
109
+ [
110
+ [350277470, 1288476560],
111
+ [386930130, 1310531620]
112
+ ],
113
+ [
114
+ [370277730, 1310531620],
115
+ [386930130, 1320034790]
116
+ ]
117
+ ],
118
+ ["http://mt0.gmaptiles.co.kr/mt/v=kr1p.12\u0026hl=en-US\u0026", "http://mt1.gmaptiles.co.kr/mt/v=kr1p.12\u0026hl=en-US\u0026"]
119
+ ]
120
+ ],
121
+ [
122
+ ["http://cbk0.google.com/cbk/", "http://cbk1.google.com/cbk/"], , ""],
123
+ [
124
+ ["http://khmdb0.google.com/kh/v=25\u0026hl=en-US\u0026", "http://khmdb1.google.com/kh/v=25\u0026hl=en-US\u0026"], , "25"],
125
+ [
126
+ ["http://mt0.google.com/mapslt/hl=en-US\u0026", "http://mt1.google.com/mapslt/hl=en-US\u0026"], , ""]
127
+ ],
128
+ ["en-US", "US", , , , "http://maps.google.com", "http://maps.gstatic.com/intl/en_us/mapfiles/", "http://gg.google.com"],
129
+ ["http://maps.gstatic.com/intl/en_us/mapfiles/api-3/0/32"],
130
+ [3396856183], 1], loadScriptTime);
131
+
132
+ };
133
+ var loadScriptTime = (new Date).getTime();
134
+ getScript("mainx.js");
135
+ })();
data/gm/t.js ADDED
@@ -0,0 +1,6 @@
1
+ window.location = "x.html";
2
+ load("jquery.js");
3
+ print("a");
4
+ $("#addr").click();
5
+ print("b");
6
+ print(document.xml);
data/gm/x.html ADDED
@@ -0,0 +1,76 @@
1
+ <html>
2
+ <head>
3
+ </head>
4
+ <body>
5
+
6
+ <script type="text/javascript" src="http://www.json.org/json2.js"></script>
7
+ <!-- <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> -->
8
+ <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
9
+ <!--<script type="text/javascript" src="sensx.js"></script>-->
10
+
11
+ <script type="text/javascript">
12
+ var geocoder;
13
+ var directionsService;
14
+
15
+ geocoder = new google.maps.Geocoder();
16
+ print("?",geocoder);
17
+ directionsService = new google.maps.DirectionsService();
18
+
19
+ function codeAddress() {
20
+ print("CA",geocoder,"?");
21
+ var address = document.getElementById("address").value;
22
+ if (geocoder) {
23
+ print("0",address,geocoder.geocode);
24
+ geocoder.geocode( { 'address': address}, function(results, status) {
25
+ print("CA ++++++++++++++++",JSON.stringify(results),status);
26
+ if (status == google.maps.GeocoderStatus.OK) {
27
+ document.getElementById("geocode").value = JSON.stringify(results, null, 2);
28
+ } else {
29
+ alert("Geocode was not successful for the following reason: " + status);
30
+ }
31
+ });
32
+ }
33
+ }
34
+
35
+ function calcRoute() {
36
+ print("CR");
37
+ var start = document.getElementById("start").value;
38
+ var end = document.getElementById("end").value;
39
+ var request = {
40
+ origin:start,
41
+ destination:end,
42
+ travelMode: google.maps.DirectionsTravelMode.DRIVING
43
+ };
44
+ directionsService.route(request, function(result, status) {
45
+ if (status == google.maps.DirectionsStatus.OK) {
46
+ document.getElementById("directions").value = JSON.stringify(result, null, 2);
47
+ } else {
48
+ alert("Routing was not successful for the following reason: " + status);
49
+ }
50
+ });
51
+ }
52
+ </script>
53
+
54
+
55
+
56
+
57
+
58
+ Hi
59
+ <div>
60
+ <input id="address" type="textbox" value="200 Fifth Ave New York, NY">
61
+ <input id="addr" type="button" value="Geocode" onclick="codeAddress()">
62
+ <br />
63
+ <textarea id="geocode" cols="100" rows="50"></textarea>
64
+ </div>
65
+
66
+ <br />
67
+ <div>
68
+ <input id="start" type="textbox" value="200 Fifth Ave New York, NY">
69
+ <input id="end" type="textbox" value="LGA">
70
+ <input id="route" type="button" value="Calc Route" onclick="calcRoute()">
71
+ <br />
72
+ <textarea id="directions" cols="100" rows="50"></textarea>
73
+ </div>
74
+
75
+ </body>
76
+ </html>
@@ -0,0 +1,456 @@
1
+ /*
2
+ * Copyright (c) 2007 Henri Sivonen
3
+ * Copyright (c) 2008-2009 Mozilla Foundation
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a
6
+ * copy of this software and associated documentation files (the "Software"),
7
+ * to deal in the Software without restriction, including without limitation
8
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
+ * and/or sell copies of the Software, and to permit persons to whom the
10
+ * Software is furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
+ * DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ package nu.validator.htmlparser.gwt;
25
+
26
+ import java.util.LinkedList;
27
+
28
+ import nu.validator.htmlparser.common.DocumentMode;
29
+ import nu.validator.htmlparser.impl.CoalescingTreeBuilder;
30
+ import nu.validator.htmlparser.impl.HtmlAttributes;
31
+
32
+ import org.xml.sax.SAXException;
33
+
34
+ import com.google.gwt.core.client.JavaScriptException;
35
+ import com.google.gwt.core.client.JavaScriptObject;
36
+
37
+ class BrowserTreeBuilder extends CoalescingTreeBuilder<JavaScriptObject> {
38
+
39
+ private JavaScriptObject document;
40
+
41
+ private JavaScriptObject script;
42
+
43
+ private JavaScriptObject placeholder;
44
+
45
+ private boolean readyToRun;
46
+
47
+ private final LinkedList<ScriptHolder> scriptStack = new LinkedList<ScriptHolder>();
48
+
49
+ private class ScriptHolder {
50
+ private final JavaScriptObject script;
51
+
52
+ private final JavaScriptObject placeholder;
53
+
54
+ /**
55
+ * @param script
56
+ * @param placeholder
57
+ */
58
+ public ScriptHolder(JavaScriptObject script,
59
+ JavaScriptObject placeholder) {
60
+ this.script = script;
61
+ this.placeholder = placeholder;
62
+ }
63
+
64
+ /**
65
+ * Returns the script.
66
+ *
67
+ * @return the script
68
+ */
69
+ public JavaScriptObject getScript() {
70
+ return script;
71
+ }
72
+
73
+ /**
74
+ * Returns the placeholder.
75
+ *
76
+ * @return the placeholder
77
+ */
78
+ public JavaScriptObject getPlaceholder() {
79
+ return placeholder;
80
+ }
81
+ }
82
+
83
+ protected BrowserTreeBuilder(JavaScriptObject document) {
84
+ super();
85
+ this.document = document;
86
+ installExplorerCreateElementNS(document);
87
+ }
88
+
89
+ private static native boolean installExplorerCreateElementNS(
90
+ JavaScriptObject doc) /*-{
91
+ if (!doc.createElementNS) {
92
+ doc.createElementNS = function (uri, local) {
93
+ if ("http://www.w3.org/1999/xhtml" == uri) {
94
+ return doc.createElement(local);
95
+ } else if ("http://www.w3.org/1998/Math/MathML" == uri) {
96
+ if (!doc.mathplayerinitialized) {
97
+ var obj = document.createElement("object");
98
+ obj.setAttribute("id", "mathplayer");
99
+ obj.setAttribute("classid", "clsid:32F66A20-7614-11D4-BD11-00104BD3F987");
100
+ document.getElementsByTagName("head")[0].appendChild(obj);
101
+ document.namespaces.add("m", "http://www.w3.org/1998/Math/MathML", "#mathplayer");
102
+ doc.mathplayerinitialized = true;
103
+ }
104
+ return doc.createElement("m:" + local);
105
+ } else if ("http://www.w3.org/2000/svg" == uri) {
106
+ if (!doc.renesisinitialized) {
107
+ var obj = document.createElement("object");
108
+ obj.setAttribute("id", "renesis");
109
+ obj.setAttribute("classid", "clsid:AC159093-1683-4BA2-9DCF-0C350141D7F2");
110
+ document.getElementsByTagName("head")[0].appendChild(obj);
111
+ document.namespaces.add("s", "http://www.w3.org/2000/svg", "#renesis");
112
+ doc.renesisinitialized = true;
113
+ }
114
+ return doc.createElement("s:" + local);
115
+ } else {
116
+ // throw
117
+ }
118
+ }
119
+ }
120
+ }-*/;
121
+
122
+ private static native boolean hasAttributeNS(JavaScriptObject element,
123
+ String uri, String localName) /*-{
124
+ return element.hasAttributeNS(uri, localName);
125
+ }-*/;
126
+
127
+ private static native void setAttributeNS(JavaScriptObject element,
128
+ String uri, String localName, String value) /*-{
129
+ element.setAttributeNS(uri, localName, value);
130
+ }-*/;
131
+
132
+ @Override protected void addAttributesToElement(JavaScriptObject element,
133
+ HtmlAttributes attributes) throws SAXException {
134
+ try {
135
+ for (int i = 0; i < attributes.getLength(); i++) {
136
+ String localName = attributes.getLocalName(i);
137
+ String uri = attributes.getURI(i);
138
+ if (!hasAttributeNS(element, uri, localName)) {
139
+ setAttributeNS(element, uri, localName,
140
+ attributes.getValue(i));
141
+ }
142
+ }
143
+ } catch (JavaScriptException e) {
144
+ fatal(e);
145
+ }
146
+ }
147
+
148
+ private static native void appendChild(JavaScriptObject parent,
149
+ JavaScriptObject child) /*-{
150
+ parent.appendChild(child);
151
+ }-*/;
152
+
153
+ private static native JavaScriptObject createTextNode(JavaScriptObject doc,
154
+ String text) /*-{
155
+ return doc.createTextNode(text);
156
+ }-*/;
157
+
158
+ @Override protected void appendCharacters(JavaScriptObject parent,
159
+ String text) throws SAXException {
160
+ try {
161
+ if (parent == placeholder) {
162
+ appendChild(script, createTextNode(document, text));
163
+
164
+ }
165
+ appendChild(parent, createTextNode(document, text));
166
+ } catch (JavaScriptException e) {
167
+ fatal(e);
168
+ }
169
+ }
170
+
171
+ private static native boolean hasChildNodes(JavaScriptObject element) /*-{
172
+ return element.hasChildNodes();
173
+ }-*/;
174
+
175
+ private static native JavaScriptObject getFirstChild(
176
+ JavaScriptObject element) /*-{
177
+ return element.firstChild;
178
+ }-*/;
179
+
180
+ @Override protected void appendChildrenToNewParent(
181
+ JavaScriptObject oldParent, JavaScriptObject newParent)
182
+ throws SAXException {
183
+ try {
184
+ while (hasChildNodes(oldParent)) {
185
+ appendChild(newParent, getFirstChild(oldParent));
186
+ }
187
+ } catch (JavaScriptException e) {
188
+ fatal(e);
189
+ }
190
+ }
191
+
192
+ private static native JavaScriptObject createComment(JavaScriptObject doc,
193
+ String text) /*-{
194
+ return doc.createComment(text);
195
+ }-*/;
196
+
197
+ @Override protected void appendComment(JavaScriptObject parent,
198
+ String comment) throws SAXException {
199
+ try {
200
+ if (parent == placeholder) {
201
+ appendChild(script, createComment(document, comment));
202
+ }
203
+ appendChild(parent, createComment(document, comment));
204
+ } catch (JavaScriptException e) {
205
+ fatal(e);
206
+ }
207
+ }
208
+
209
+ @Override protected void appendCommentToDocument(String comment)
210
+ throws SAXException {
211
+ try {
212
+ appendChild(document, createComment(document, comment));
213
+ } catch (JavaScriptException e) {
214
+ fatal(e);
215
+ }
216
+ }
217
+
218
+ private static native JavaScriptObject createElementNS(
219
+ JavaScriptObject doc, String ns, String local) /*-{
220
+ return doc.createElementNS(ns, local);
221
+ }-*/;
222
+
223
+ @Override protected JavaScriptObject createElement(String ns, String name,
224
+ HtmlAttributes attributes) throws SAXException {
225
+ try {
226
+ JavaScriptObject rv = createElementNS(document, ns, name);
227
+ for (int i = 0; i < attributes.getLength(); i++) {
228
+ setAttributeNS(rv, attributes.getURI(i),
229
+ attributes.getLocalName(i), attributes.getValue(i));
230
+ }
231
+
232
+ if ("script" == name) {
233
+ if (placeholder != null) {
234
+ scriptStack.addLast(new ScriptHolder(script, placeholder));
235
+ }
236
+ script = rv;
237
+ placeholder = createElementNS(document,
238
+ "http://n.validator.nu/placeholder/", "script");
239
+ rv = placeholder;
240
+ for (int i = 0; i < attributes.getLength(); i++) {
241
+ setAttributeNS(rv, attributes.getURI(i),
242
+ attributes.getLocalName(i), attributes.getValue(i));
243
+ }
244
+ }
245
+
246
+ return rv;
247
+ } catch (JavaScriptException e) {
248
+ fatal(e);
249
+ throw new RuntimeException("Unreachable");
250
+ }
251
+ }
252
+
253
+ @Override protected JavaScriptObject createHtmlElementSetAsRoot(
254
+ HtmlAttributes attributes) throws SAXException {
255
+ try {
256
+ JavaScriptObject rv = createElementNS(document,
257
+ "http://www.w3.org/1999/xhtml", "html");
258
+ for (int i = 0; i < attributes.getLength(); i++) {
259
+ setAttributeNS(rv, attributes.getURI(i),
260
+ attributes.getLocalName(i), attributes.getValue(i));
261
+ }
262
+ appendChild(document, rv);
263
+ return rv;
264
+ } catch (JavaScriptException e) {
265
+ fatal(e);
266
+ throw new RuntimeException("Unreachable");
267
+ }
268
+ }
269
+
270
+ private static native JavaScriptObject getParentNode(
271
+ JavaScriptObject element) /*-{
272
+ return element.parentNode;
273
+ }-*/;
274
+
275
+ @Override protected void appendElement(JavaScriptObject child,
276
+ JavaScriptObject newParent) throws SAXException {
277
+ try {
278
+ if (newParent == placeholder) {
279
+ appendChild(script, cloneNodeDeep(child));
280
+ }
281
+ appendChild(newParent, child);
282
+ } catch (JavaScriptException e) {
283
+ fatal(e);
284
+ }
285
+ }
286
+
287
+ @Override protected boolean hasChildren(JavaScriptObject element)
288
+ throws SAXException {
289
+ try {
290
+ return hasChildNodes(element);
291
+ } catch (JavaScriptException e) {
292
+ fatal(e);
293
+ throw new RuntimeException("Unreachable");
294
+ }
295
+ }
296
+
297
+ private static native void insertBeforeNative(JavaScriptObject parent,
298
+ JavaScriptObject child, JavaScriptObject sibling) /*-{
299
+ parent.insertBefore(child, sibling);
300
+ }-*/;
301
+
302
+ private static native int getNodeType(JavaScriptObject node) /*-{
303
+ return node.nodeType;
304
+ }-*/;
305
+
306
+ private static native JavaScriptObject cloneNodeDeep(JavaScriptObject node) /*-{
307
+ return node.cloneNode(true);
308
+ }-*/;
309
+
310
+ /**
311
+ * Returns the document.
312
+ *
313
+ * @return the document
314
+ */
315
+ JavaScriptObject getDocument() {
316
+ JavaScriptObject rv = document;
317
+ document = null;
318
+ return rv;
319
+ }
320
+
321
+ private static native JavaScriptObject createDocumentFragment(
322
+ JavaScriptObject doc) /*-{
323
+ return doc.createDocumentFragment();
324
+ }-*/;
325
+
326
+ JavaScriptObject getDocumentFragment() {
327
+ JavaScriptObject rv = createDocumentFragment(document);
328
+ JavaScriptObject rootElt = getFirstChild(document);
329
+ while (hasChildNodes(rootElt)) {
330
+ appendChild(rv, getFirstChild(rootElt));
331
+ }
332
+ document = null;
333
+ return rv;
334
+ }
335
+
336
+ /**
337
+ * @see nu.validator.htmlparser.impl.TreeBuilder#createJavaScriptObject(String,
338
+ * java.lang.String, org.xml.sax.Attributes, java.lang.Object)
339
+ */
340
+ @Override protected JavaScriptObject createElement(String ns, String name,
341
+ HtmlAttributes attributes, JavaScriptObject form)
342
+ throws SAXException {
343
+ try {
344
+ JavaScriptObject rv = createElement(ns, name, attributes);
345
+ // rv.setUserData("nu.validator.form-pointer", form, null);
346
+ return rv;
347
+ } catch (JavaScriptException e) {
348
+ fatal(e);
349
+ return null;
350
+ }
351
+ }
352
+
353
+ /**
354
+ * @see nu.validator.htmlparser.impl.TreeBuilder#start()
355
+ */
356
+ @Override protected void start(boolean fragment) throws SAXException {
357
+ script = null;
358
+ placeholder = null;
359
+ readyToRun = false;
360
+ }
361
+
362
+ protected void documentMode(DocumentMode mode, String publicIdentifier,
363
+ String systemIdentifier, boolean html4SpecificAdditionalErrorChecks)
364
+ throws SAXException {
365
+ // document.setUserData("nu.validator.document-mode", mode, null);
366
+ }
367
+
368
+ /**
369
+ * @see nu.validator.htmlparser.impl.TreeBuilder#elementPopped(java.lang.String,
370
+ * java.lang.String, java.lang.Object)
371
+ */
372
+ @Override protected void elementPopped(String ns, String name,
373
+ JavaScriptObject node) throws SAXException {
374
+ if (node == placeholder) {
375
+ readyToRun = true;
376
+ requestSuspension();
377
+ }
378
+ //This is called to allow us to hook into
379
+ //the 'sax end element event'
380
+ elementPoppedNative(ns, name, node);
381
+ }
382
+
383
+ private static native void elementPoppedNative(String ns, String name,
384
+ JavaScriptObject node) /*-{
385
+ __elementPopped__(ns, name, node);
386
+ }-*/;
387
+
388
+ private static native void replace(JavaScriptObject oldNode,
389
+ JavaScriptObject newNode) /*-{
390
+ oldNode.parentNode.replaceChild(newNode, oldNode);
391
+ __elementPopped__('', newNode.nodeName, newNode);
392
+ }-*/;
393
+
394
+ void maybeRunScript() {
395
+ if (readyToRun) {
396
+ readyToRun = false;
397
+ replace(placeholder, script);
398
+ if (scriptStack.isEmpty()) {
399
+ script = null;
400
+ placeholder = null;
401
+ } else {
402
+ ScriptHolder scriptHolder = scriptStack.removeLast();
403
+ script = scriptHolder.getScript();
404
+ placeholder = scriptHolder.getPlaceholder();
405
+ }
406
+ }
407
+ }
408
+
409
+ @Override protected void insertFosterParentedCharacters(String text,
410
+ JavaScriptObject table, JavaScriptObject stackParent)
411
+ throws SAXException {
412
+ try {
413
+ JavaScriptObject child = createTextNode(document, text);
414
+ JavaScriptObject parent = getParentNode(table);
415
+ if (parent != null && getNodeType(parent) == 1) {
416
+ insertBeforeNative(parent, child, table);
417
+ } else {
418
+ appendChild(stackParent, child);
419
+ }
420
+ } catch (JavaScriptException e) {
421
+ fatal(e);
422
+ }
423
+ }
424
+
425
+ @Override protected void insertFosterParentedChild(JavaScriptObject child,
426
+ JavaScriptObject table, JavaScriptObject stackParent)
427
+ throws SAXException {
428
+ JavaScriptObject parent = getParentNode(table);
429
+ try {
430
+ if (parent != null && getNodeType(parent) == 1) {
431
+ insertBeforeNative(parent, child, table);
432
+ } else {
433
+ appendChild(stackParent, child);
434
+ }
435
+ } catch (JavaScriptException e) {
436
+ fatal(e);
437
+ }
438
+ }
439
+
440
+ private static native void removeChild(JavaScriptObject parent,
441
+ JavaScriptObject child) /*-{
442
+ parent.removeChild(child);
443
+ }-*/;
444
+
445
+ @Override protected void detachFromParent(JavaScriptObject element)
446
+ throws SAXException {
447
+ try {
448
+ JavaScriptObject parent = getParentNode(element);
449
+ if (parent != null) {
450
+ removeChild(parent, element);
451
+ }
452
+ } catch (JavaScriptException e) {
453
+ fatal(e);
454
+ }
455
+ }
456
+ }