Mercurial > libervia-web
annotate src/browser/sat_browser/nativedom.py @ 467:97c72fe4a5f2
browser_side: import fixes:
- moved browser modules in a sat_browser packages, to avoid import conflicts with std lib (e.g. logging), and let pyjsbuild work normaly
- refactored bad import practices: classes are most of time not imported directly, module is imported instead.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 09 Jun 2014 22:15:26 +0200 |
parents | src/browser/nativedom.py@1a0cec9b0f1e |
children | 747eaa662353 |
rev | line source |
---|---|
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
4 # Libervia: a Salut à Toi frontend |
340 | 5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org> |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
10 # (at your option) any later version. |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
15 # GNU Affero General Public License for more details. |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 This class provide basic DOM parsing based on native javascript parser |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 __init__ code comes from Tim Down at http://stackoverflow.com/a/8412989 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 """ |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from __pyjamas__ import JS |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 class Node(): |
245
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
29 |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 def __init__(self, js_node): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 self._node = js_node |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 def _jsNodesList2List(self, js_nodes_list): |
451 | 34 ret = [] |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 for i in range(len(js_nodes_list)): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 #ret.append(Element(js_nodes_list.item(i))) |
451 | 37 ret.append(self.__class__(js_nodes_list.item(i))) # XXX: Ugly, but used to word around a Pyjamas's bug |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 return ret |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 @property |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 def nodeName(self): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 return self._node.nodeName |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 @property |
337 | 45 def wholeText(self): |
46 return self._node.wholeText | |
47 | |
48 @property | |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 def childNodes(self): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 return self._jsNodesList2List(self._node.childNodes) |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 def getAttribute(self, attr): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 return self._node.getAttribute(attr) |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
245
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
55 def setAttribute(self, attr, value): |
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
56 return self._node.setAttribute(attr, value) |
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
57 |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 def hasAttribute(self, attr): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 return self._node.hasAttribute(attr) |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
245
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
61 def toxml(self): |
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
62 return JS("""this._node.outerHTML || new XMLSerializer().serializeToString(this._node);""") |
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
63 |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 class Element(Node): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 def __init__(self, js_node): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 Node.__init__(self, js_node) |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 def getElementsByTagName(self, tagName): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 return self._jsNodesList2List(self._node.getElementsByTagName(tagName)) |
245
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
72 |
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
73 |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 class Document(Node): |
245
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
75 |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 def __init__(self, js_document): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 Node.__init__(self, js_document) |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 @property |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 def documentElement(self): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 return Element(self._node.documentElement) |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 |
245
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
83 |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 class NativeDOM: |
245
43881c3dda9d
browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
85 |
142
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 def __init__(self): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 JS(""" |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 if (typeof window.DOMParser != "undefined") { |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 this.parseXml = function(xmlStr) { |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml"); |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 }; |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 } else if (typeof window.ActiveXObject != "undefined" && |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 new window.ActiveXObject("Microsoft.XMLDOM")) { |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 this.parseXml = function(xmlStr) { |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM"); |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 xmlDoc.async = "false"; |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 xmlDoc.loadXML(xmlStr); |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 return xmlDoc; |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 }; |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 } else { |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 throw new Error("No XML parser found"); |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 } |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 """) |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 def parseString(self, xml): |
f6aeeb753c06
browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 return Document(self.parseXml(xml)) |