Mercurial > libervia-web
comparison 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 |
comparison
equal
deleted
inserted
replaced
466:01880aa8ea2d | 467:97c72fe4a5f2 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Libervia: a Salut à Toi frontend | |
5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org> | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """ | |
21 This class provide basic DOM parsing based on native javascript parser | |
22 __init__ code comes from Tim Down at http://stackoverflow.com/a/8412989 | |
23 """ | |
24 | |
25 from __pyjamas__ import JS | |
26 | |
27 | |
28 class Node(): | |
29 | |
30 def __init__(self, js_node): | |
31 self._node = js_node | |
32 | |
33 def _jsNodesList2List(self, js_nodes_list): | |
34 ret = [] | |
35 for i in range(len(js_nodes_list)): | |
36 #ret.append(Element(js_nodes_list.item(i))) | |
37 ret.append(self.__class__(js_nodes_list.item(i))) # XXX: Ugly, but used to word around a Pyjamas's bug | |
38 return ret | |
39 | |
40 @property | |
41 def nodeName(self): | |
42 return self._node.nodeName | |
43 | |
44 @property | |
45 def wholeText(self): | |
46 return self._node.wholeText | |
47 | |
48 @property | |
49 def childNodes(self): | |
50 return self._jsNodesList2List(self._node.childNodes) | |
51 | |
52 def getAttribute(self, attr): | |
53 return self._node.getAttribute(attr) | |
54 | |
55 def setAttribute(self, attr, value): | |
56 return self._node.setAttribute(attr, value) | |
57 | |
58 def hasAttribute(self, attr): | |
59 return self._node.hasAttribute(attr) | |
60 | |
61 def toxml(self): | |
62 return JS("""this._node.outerHTML || new XMLSerializer().serializeToString(this._node);""") | |
63 | |
64 | |
65 class Element(Node): | |
66 | |
67 def __init__(self, js_node): | |
68 Node.__init__(self, js_node) | |
69 | |
70 def getElementsByTagName(self, tagName): | |
71 return self._jsNodesList2List(self._node.getElementsByTagName(tagName)) | |
72 | |
73 | |
74 class Document(Node): | |
75 | |
76 def __init__(self, js_document): | |
77 Node.__init__(self, js_document) | |
78 | |
79 @property | |
80 def documentElement(self): | |
81 return Element(self._node.documentElement) | |
82 | |
83 | |
84 class NativeDOM: | |
85 | |
86 def __init__(self): | |
87 JS(""" | |
88 | |
89 if (typeof window.DOMParser != "undefined") { | |
90 this.parseXml = function(xmlStr) { | |
91 return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml"); | |
92 }; | |
93 } else if (typeof window.ActiveXObject != "undefined" && | |
94 new window.ActiveXObject("Microsoft.XMLDOM")) { | |
95 this.parseXml = function(xmlStr) { | |
96 var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM"); | |
97 xmlDoc.async = "false"; | |
98 xmlDoc.loadXML(xmlStr); | |
99 return xmlDoc; | |
100 }; | |
101 } else { | |
102 throw new Error("No XML parser found"); | |
103 } | |
104 """) | |
105 | |
106 def parseString(self, xml): | |
107 return Document(self.parseXml(xml)) |