summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/lang/XmlEmu.js
blob: 326effde5dec8a5770dc988b1d9a2c87ee90bc3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org

   License:
     LGPL 2.1: http://www.gnu.org/licenses/lgpl.html

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)

************************************************************************ */

/* ************************************************************************


************************************************************************ */

qx.OO.defineClass("qx.lang.XmlEmu");

/*
  Based on:
  IE7, version 0.9 (alpha) (2005-08-19)
  Copyright: 2004-2005, Dean Edwards (http://dean.edwards.name)
  License: http://creativecommons.org/licenses/LGPL/2.1/
  Modelled after: http://www.mozilla.org/xmlextras
*/

if (qx.sys.Client.getInstance().isMshtml())
{
  var DOMParser = function() {
    /* empty constructor */
  }

  DOMParser.prototype =
  {
    toString: function() {
      return "[object DOMParser]";
    },

    parseFromString: function(str, contentType)
    {
      /*
       According to information on the Microsoft XML Team's WebLog
       it is recommended to check for availability of MSXML versions 6.0 and 3.0.
       Other versions are included for completeness, 5.0 is excluded as it is
       "off-by-default" in IE7 (which could trigger a goldbar).

       http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
       http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/aabe29a2-bad2-4cea-8387-314174252a74.asp

       See similar code in qx.xml.Core, qx.io.remote.XmlHttpTransport
      */
      var vServers =
      [
        "MSXML2.DOMDocument.6.0",
        "MSXML2.DOMDocument.3.0",
        "MSXML2.DOMDocument.4.0",
        "MSXML2.DOMDocument",  // v3.0
        "MSXML.DOMDocument",   // v2.x
        "Microsoft.XMLDOM"     // v2.x
      ];

      var xmlDocument;

      for (var i=0, l=vServers.length; i<l; i++)
      {

        try
        {
          xmlDocument = new ActiveXObject(vServers[i]);
          break;
        }
        catch(ex)
        {
          xmlDocument = null;
        }
      }

      if(xmlDocument) {
        xmlDocument.loadXML(str);
      }

      return xmlDocument;
    },

    // not supported
    parseFromStream: new Function,
    baseURI: ""
  }

  var XMLSerializer = function() {
    /* empty constructor */
  }

  XMLSerializer.prototype =
  {
    toString: function() {
      return "[object XMLSerializer]";
    },

    serializeToString: function(root) {
      return root.xml || root.outerHTML;
    },

    // not supported
    serializeToStream: new Function
  }
}

// Implementation of selectNodes() and selectSingleNode()
// for Gecko/Mozilla browsers

if (window.XPathEvaluator && Element.prototype.__defineGetter__)
{
  qx.lang.XmlEmu._xpe = new XPathEvaluator();

  if (!Element.prototype.selectSingleNode)
  {
    Element.prototype.selectSingleNode = function (xpath) {
      return qx.lang.XmlEmu._xpe.evaluate(xpath, this, qx.lang.XmlEmu._xpe.createNSResolver(this), XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }
  }

  if (!Element.prototype.selectNodes)
  {
    Element.prototype.selectNodes = function (xpath) {
      var result = qx.lang.XmlEmu._xpe.evaluate(xpath, this, qx.lang.XmlEmu._xpe.createNSResolver(this), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      var nodes = [];

      for (var i=0; i<result.snapshotLength; i++) {
        nodes[i] = result.snapshotItem(i);
      }

      return nodes;
    }
  }

  if (!Document.prototype.selectSingleNode)
  {
    Document.prototype.selectSingleNode = function (xpath) {
      return qx.lang.XmlEmu._xpe.evaluate(xpath, this, qx.lang.XmlEmu._xpe.createNSResolver(this), XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }
  }

  if (!Document.prototype.selectNodes)
  {
    Document.prototype.selectNodes = function (xpath) {
      var result = qx.lang.XmlEmu._xpe.evaluate(xpath, this, qx.lang.XmlEmu._xpe.createNSResolver(this), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      var nodes = [];

      for (var i=0; i<result.snapshotLength; i++) {
        nodes[i] = result.snapshotItem(i);
      }

      return nodes;
    }
  }

  Element.prototype.__defineGetter__('text',
    function() {
      var text = "";
      for (var i=0; i<this.childNodes.length; i++) {
        text += this.childNodes[i].text != null ? this.childNodes[i].text : "";
      }
      return text;
    }
  );

  Element.prototype.__lookupGetter__('text');

  if (!window.Attr) {
    Attr = new Function();
  }
  Attr.prototype.__defineGetter__('text', function(){ return this.nodeValue; });
  Attr.prototype.__lookupGetter__('text');

  if (!window.Text) {
    Text = new Function();
  }
  Text.prototype.__defineGetter__('text', function(){ return this.nodeValue; });
  Text.prototype.__lookupGetter__('text');
}