comparison browser/sat_browser/nativedom.py @ 1124:28e3eb3bb217

files reorganisation and installation rework: - files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory) - VERSION file is now used, as for other SàT projects - replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly - removed check for data_dir if it's empty - installation tested working in virtual env - libervia launching script is now in bin/libervia
author Goffi <goffi@goffi.org>
date Sat, 25 Aug 2018 17:59:48 +0200
parents src/browser/sat_browser/nativedom.py@f2170536ba23
children 2af117bfe6cc
comparison
equal deleted inserted replaced
1123:63a4b8fe9782 1124:28e3eb3bb217
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Libervia: a Salut à Toi frontend
5 # Copyright (C) 2011-2018 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(object):
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 work around a Pyjamas's bug
38 return ret
39
40 def __getattr__(self, name):
41 if name in ('TEXT_NODE', 'ELEMENT_NODE', 'ATTRIBUTE_NODE', 'COMMENT_NODE', 'nodeName', 'nodeType', 'wholeText'):
42 return getattr(self._node, name)
43 return object.__getattribute__(self, name)
44
45 @property # XXX: doesn't work in --strict mode in pyjs
46 def childNodes(self):
47 return self._jsNodesList2List(self._node.childNodes)
48
49 def getAttribute(self, attr):
50 return self._node.getAttribute(attr)
51
52 def setAttribute(self, attr, value):
53 return self._node.setAttribute(attr, value)
54
55 def hasAttribute(self, attr):
56 return self._node.hasAttribute(attr)
57
58 def toxml(self):
59 return JS("""this._node.outerHTML || new XMLSerializer().serializeToString(this._node);""")
60
61
62 class Element(Node):
63
64 def __init__(self, js_node):
65 Node.__init__(self, js_node)
66
67 def getElementsByTagName(self, tagName):
68 return self._jsNodesList2List(self._node.getElementsByTagName(tagName))
69
70
71 class Document(Node):
72
73 def __init__(self, js_document):
74 Node.__init__(self, js_document)
75
76 @property
77 def documentElement(self):
78 return Element(self._node.documentElement)
79
80
81 class NativeDOM:
82
83 def __init__(self):
84 JS("""
85
86 if (typeof window.DOMParser != "undefined") {
87 this.parseXml = function(xmlStr) {
88 return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
89 };
90 } else if (typeof window.ActiveXObject != "undefined" &&
91 new window.ActiveXObject("Microsoft.XMLDOM")) {
92 this.parseXml = function(xmlStr) {
93 var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
94 xmlDoc.async = "false";
95 xmlDoc.loadXML(xmlStr);
96 return xmlDoc;
97 };
98 } else {
99 throw new Error("No XML parser found");
100 }
101 """)
102
103 def parseString(self, xml):
104 return Document(self.parseXml(xml))