comparison browser_side/nativedom.py @ 142:f6aeeb753c06

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