summaryrefslogtreecommitdiff
path: root/tests/FunctionalTests/selenium/html-xpath/html-xpath.js
blob: acad8b9347a45643f4a543b031cb954792657a87 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
	html-xpath, an implementation of DOM Level 3 XPath for Internet Explorer 5+
	Copyright (C) 2004 Dimitri Glazkov

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software

*/

var isIe = /MSIE [56789]/.test(navigator.userAgent) && (navigator.platform == "Win32");

// Mozilla has support by default, we don't have an implementation for the rest
if (isIe)
{
	// release number
	document.DomL3XPathRelease = "0.0.3.0";
	
	// XPathException
	// An Error object will be thrown, this is just a handler to instantiate that object
	var XPathException = new _XPathExceptionHandler();
	function _XPathExceptionHandler()
	{
		this.INVALID_EXPRESSION_ERR = 51;
		this.TYPE_ERR = 52;
		this.NOT_IMPLEMENTED_ERR = -1;
		this.RUNTIME_ERR = -2;
		
		this.ThrowNotImplemented = function(message)
		{
			ThrowError(this.NOT_IMPLEMENTED_ERR, "This functionality is not implemented.", message);
		}
		
		this.ThrowInvalidExpression = function(message)
		{
			ThrowError(this.INVALID_EXPRESSION_ERR, "Invalid expression", message);
		}
		
		this.ThrowType = function(message)
		{
			ThrowError(this.TYPE_ERR, "Type error", message);
		}
		
		this.Throw = function(message)
		{
			ThrowError(this.RUNTIME_ERR, "Run-time error", message);
		}
		
		function ThrowError(code, description, message)
		{
			var error = new Error(code, "DOM-L3-XPath " + document.DomL3XPathRelease + ": " + description + (message ? ", \"" + message  + "\"": ""));
			error.code = code;
			error.name = "XPathException";
			throw error;
		}
	}
	
	// DOMException
	// An Error object will be thrown, this is just a handler to instantiate that object
	var DOMException = new _DOMExceptionHandler();
	function _DOMExceptionHandler()
	{
		this.ThrowInvalidState = function(message)
		{
			ThrowError(13, "The state of the object is no longer valid", message);
		}

		function ThrowError(code, description, message)
		{
			var error = new Error(code, "DOM : " + description + (message ? ", \"" + message  + "\"": ""));
			error.code = code;
			error.name = "DOMException";
			throw error;
		}
	}

	// XPathEvaluator 
	// implemented as document object methods
	
	// XPathExpression createExpression(String expression, XPathNSResolver resolver)
	document.createExpression = function
		(
		expression,		// String
		resolver		// XPathNSResolver
		)
	{
		// returns XPathExpression object
		return new XPathExpression(expression, resolver);
	}

	// XPathNSResolver createNSResolver(nodeResolver)
	document.createNSResolver = function
		(
		nodeResolver	// Node
		)
	{
		// returns XPathNSResolver
		return new XPathNSResolver(nodeResolver);
	}

	// XPathResult evaluate(String expresison, Node contextNode, XPathNSResolver resolver, Number type, XPathResult result)
	document.evaluate = function
		(
		expression,		// String
		contextNode,	// Node
		resolver,		// XPathNSResolver
		type,			// Number
		result			// XPathResult
		)
		// can raise XPathException, DOMException
	{
		// return XPathResult
		return document.createExpression(expression, resolver).evaluate(contextNode, type, result);
	}

	// XPathExpression
	function XPathExpression
	(
		expression, // String
		resolver // XPathNSResolver
	)
	{
		this.expressionString = expression;
		this.resolver = resolver;

		// XPathResult evaluate(Node contextNode, Number type, XPathResult result)
		this.evaluate = function
		(
			contextNode, // Node
			type, // Number
			result // XPathResult		
		)
			// raises XPathException, DOMException
		{
			// return XPathResult
			return (result && result.constructor == XPathResult ? result.initialize(this, contextNode, resolver, type) : new XPathResult(this, contextNode, resolver, type));
		}
		
		this.toString = function()
		{
			return "[XPathExpression]";
		}
	}

	// XPathNSResolver
	function XPathNSResolver(node)
	{
		this.node = node;
	
		// String lookupNamespaceURI(String prefix)
		this.lookupNamespaceURI = function
			(
			prefix			// String
			)
		{
			XPathException.ThrowNotImplemented();
			// return String
			return null;
		}

		this.toString = function()
		{
			return "[XPathNSResolver]";
		}
	}

	// XPathResult
	XPathResult.ANY_TYPE = 0;
	XPathResult.NUMBER_TYPE = 1;
	XPathResult.STRING_TYPE = 2;
	XPathResult.BOOLEAN_TYPE = 3;
	XPathResult.UNORDERED_NODE_ITERATOR_TYPE = 4;
	XPathResult.ORDERED_NODE_ITERATOR_TYPE = 5;
	XPathResult.UNORDERED_SNAPSHOT_TYPE = 6;
	XPathResult.ORDERED_SNAPSHOT_TYPE = 7;
	XPathResult.ANY_UNORDERED_NODE_TYPE = 8;
	XPathResult.FIRST_ORDERED_NODE_TYPE = 9;
	
	function XPathResult
			(
			expression,		// XPathExpression
			contextNode,	// Node
			resolver,		// XPathNSResolver
			type			// Number
			)
	{
		this.initialize = function(expression, contextNode, resolver, type)
		{
			this._domResult = null;
			this._expression = expression;
			this._contextNode = contextNode;
			this._resolver = resolver;
			if (type)
			{
				this.resultType = type;
				this._isIterator = (type == XPathResult.UNORDERED_NODE_ITERATOR_TYPE || 
					type == XPathResult.ORDERED_NODE_ITERATOR_TYPE || 
					type == XPathResult.ANY_TYPE);
				this._isSnapshot = (type == XPathResult.UNORDERED_SNAPSHOT_TYPE || type == XPathResult.ORDERED_SNAPSHOT_TYPE);
				this._isNodeSet = type > XPathResult.BOOLEAN_TYPE;
			}
			else
			{
				this.resultType = XPathResult.ANY_TYPE;
				this._isIterator = true;
				this._isSnapshot = false;
				this._isNodeSet = true;
			}
			return this;
		}
		
		this.initialize(expression, contextNode, resolver, type);
		
		this.getInvalidIteratorState = function()
		{
			return documentChangeDetected() || !this._isIterator;			
		}
		
		this.getSnapshotLength = function()
			// raises XPathException
		{
			if (!this._isSnapshot)
			{
				XPathException.ThrowType("Snapshot is not an expected result type");
			}
			activateResult(this);
			// return Number
			return this._domResult.length;
		}
		
		// Node iterateNext()
		this.iterateNext = function()
			// raises XPathException, DOMException
		{
			if (!this._isIterator)
			{
				XPathException.ThrowType("Iterator is not an expected result type");
			}
			activateResult(this);
			if (documentChangeDetected())
			{
				DOMException.ThrowInvalidState("iterateNext");
			}
			// return Node
			return getNextNode(this);
		}
		
		// Node snapshotItem(Number index)
		this.snapshotItem = function(index)
			// raises XPathException
		{
			if (!this._isSnapshot)
			{
				XPathException.ThrowType("Snapshot is not an expected result type");
			}
			// return Node
			return getItemNode(this, index); 
		}
		
		this.toString = function()
		{
			return "[XPathResult]";
		}
		
		// returns string value of the result, if result type is STRING_TYPE
		// otherwise throws an XPathException
		this.getStringValue = function()
		{
			if (this.resultType != XPathResult.STRING_TYPE)
			{
				XPathException.ThrowType("The expression can not be converted to return String");
			}
			return getNodeText(this);
		}
		
		// returns number value of the result, if the result is NUMBER_TYPE
		// otherwise throws an XPathException
		this.getNumberValue = function()
		{
			if (this.resultType != XPathResult.NUMBER_TYPE)
			{
				XPathException.ThrowType("The expression can not be converted to return Number");
			}
			var number = parseInt(getNodeText(this));
			if (isNaN(number))
			{
				XPathException.ThrowType("The result can not be converted to Number");
			}
			return number;
		}
		
		// returns boolean value of the result, if the result is BOOLEAN_TYPE
		// otherwise throws an XPathException
		this.getBooleanValue = function()
		{
			if (this.resultType != XPathResult.BOOLEAN_TYPE)
			{
				XPathException.ThrowType("The expression can not be converted to return Boolean");
			}
			
			var	
				text = getNodeText(this);
				bool = (text ? text.toLowerCase() : null);
			if (bool == "false" || bool == "true")
			{
				return bool;
			}
			XPathException.ThrowType("The result can not be converted to Boolean");
		}
		
		// returns single node, if the result is ANY_UNORDERED_NODE_TYPE or FIRST_ORDERED_NODE_TYPE
		// otherwise throws an XPathException
		this.getSingleNodeValue = function()
		{
			if (this.resultType != XPathResult.ANY_UNORDERED_NODE_TYPE && 
				this.resultType != XPathResult.FIRST_ORDERED_NODE_TYPE)
			{
				XPathException.ThrowType("The expression can not be converted to return single Node value");
			}
			return getSingleNode(this);
		}
		
		function documentChangeDetected()
		{
			return document._XPathMsxmlDocumentHelper.documentChangeDetected();
		}
		
		function getNodeText(result)
		{
			activateResult(result);
			return result._textResult;
//			return ((node = getSingleNode(result)) ? (node.nodeType == 1 ? node.innerText : node.nodeValue) : null);
		}
		
		function findNode(result, current)
		{
			switch(current.nodeType)
			{
				case 1: // NODE_ELEMENT
					var id = current.attributes.getNamedItem("id");
					if (id)
					{
						return document.getElementById(id.value);
					}
					XPathException.Throw("unable to locate element in XML tree");
				case 2: // NODE_ATTRIBUTE
					var id = current.selectSingleNode("..").attributes.getNamedItem("id");
					if (id)
					{
						var node = document.getElementById(id.text);
						if (node)
						{
							return node.attributes.getNamedItem(current.nodeName);
						}
					}
					XPathException.Throw("unable to locate attribute in XML tree");
				case 3: // NODE_TEXT
					var id = current.selectSingleNode("..").attributes.getNamedItem("id");
					if (id)
					{
						var node = document.getElementById(id.value);
						if (node)
						{
							for(child in node.childNodes)
							{
								if (child.nodeType == 3 && child.nodeValue == current.nodeValue)
								{
									return child;
								}
							}
						}
					}
					XPathException.Throw("unable to locate text in XML tree");
			}
			XPathException.Throw("unknown node type");
		}
		
		function activateResult(result)
		{
			if (!result._domResult)
			{
				try
				{
					var expression = result._expression.expressionString;
					
					// adjust expression if contextNode is not a document
					if (result._contextNode != document && expression.indexOf("//") != 0)
					{
						
						expression = "//*[@id = '" + result._contextNode.id + "']" + 
							(expression.indexOf("/") == 0 ? "" : "/") + expression;
					}
					
					if (result._isNodeSet)
					{
						result._domResult = document._XPathMsxmlDocumentHelper.getDom().selectNodes(expression);
					}
					else
					{
						result._domResult = true;
						result._textResult = document._XPathMsxmlDocumentHelper.getTextResult(expression);
					}
					
				}
				catch(error)
				{
					alert(error.description);
					XPathException.ThrowInvalidExpression(error.description);
				}
			}
		}

		function getSingleNode(result)
		{
			var node = getItemNode(result, 0);
			result._domResult = null;
			return node;
		}
		
		function getItemNode(result, index)
		{
			activateResult(result);
			var current = result._domResult.item(index);
			return (current ? findNode(result, current) : null);
		}
		
		function getNextNode(result)
		{
			var current = result._domResult.nextNode;
			if (current)
			{
				return findNode(result, current);
			}
			result._domResult = null;
			return null;
		}
	}
	
	document.reloadDom = function()
	{
		document._XPathMsxmlDocumentHelper.reset();
	}

	document._XPathMsxmlDocumentHelper = new _XPathMsxmlDocumentHelper();
	function _XPathMsxmlDocumentHelper()
	{
		this.getDom = function()
		{
			activateDom(this);
			return this.dom;
		}
		
		this.getXml = function()
		{
			activateDom(this);
			return this.dom.xml;
		}
		
		this.getTextResult = function(expression)
		{
			expression = expression.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "\"");
			var xslText = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
				"<xsl:output method=\"text\"/><xsl:template match=\"*\"><xsl:value-of select=\"" + expression + "\"/>" +
				"</xsl:template></xsl:stylesheet>";
			var xsl = new ActiveXObject("Msxml2.DOMDocument");
			xsl.loadXML(xslText);
			try
			{
				var result = this.getDom().transformNode(xsl);
			}
			catch(error)
			{
				alert("Error: " + error.description);
			}
			return result;
		}
		
		this.reset = function()
		{
			this.dom = null;
		}
		
		function onPropertyChangeEventHandler()
		{
			document._propertyChangeDetected = true;
		}
		
		this.documentChangeDetected = function()
		{
			return (document.ignoreDocumentChanges ? false : this._currentElementCount != document.all.length || document._propertyChangeDetected);
		}
		
		function activateDom(helper)
		{
			if (!helper.dom)
			{
				var dom = new ActiveXObject("Msxml2.DOMDocument");
				dom.async = false;
				dom.resolveExternals = false;
				loadDocument(dom, helper);
				helper.dom = dom;
				helper._currentElementCount = document.all.length;
				document._propertyChangeDetected = false;
			}
			else
			{
				if (helper.documentChangeDetected())
				{
					var dom = helper.dom;
					dom.load("");
					loadDocument(dom, helper);
					helper._currentElementCount = document.all.length;
					document._propertyChangeDetected = false;
				}
			}
		}
		
		function loadDocument(dom, helper)
		{
			return loadNode(dom, dom, document.body, helper);		
		}
		
		function loadNode(dom, domParentNode, node, helper)
		{
			if (node.nodeType == 3)
			{			
				domParentNode.appendChild(dom.createTextNode(node.nodeValue));
			}
			else
			{
				var domNode = dom.createElement(node.nodeName.toLowerCase());
				if (!node.id)
				{
					node.id = node.uniqueID;
				}
				domParentNode.appendChild(domNode);
				loadAttributes(dom, domNode, node);
				var length = node.childNodes.length;
				for(var i = 0; i < length; i ++ )
				{
					loadNode(dom, domNode, node.childNodes[i], helper);
				}
				node.attachEvent("onpropertychange", onPropertyChangeEventHandler);
			}
		}

		function loadAttributes(dom, domParentNode, node)
		{
			for (var i = 0; i < node.attributes.length; i ++ )
			{
				var attribute = node.attributes[i];
				var attributeValue = attribute.nodeValue;
				if (attributeValue && attribute.specified)
				{
					var domAttribute = dom.createAttribute(attribute.nodeName);
					domAttribute.value = attributeValue;
					domParentNode.setAttributeNode(domAttribute);				
				}
			}
		}
	
	}
}
else
{
	document.reloadDom = function() {}
	XPathResult.prototype.getStringValue = function()
	{
		return this.stringValue;
	}
	
	XPathResult.prototype.getNumberValue = function()
	{
		return this.numberValue;
	}
	
	XPathResult.prototype.getBooleanValue = function()
	{
		return this.booleanValue;
	}
	
	XPathResult.prototype.getSingleNodeValue = function()
	{
		return this.singleNodeValue;	
	}
	
	XPathResult.prototype.getInvalidIteratorState = function()
	{
		return this.invalidIteratorState;
	}
	
	XPathResult.prototype.getSnapshotLength = function()
	{
		return this.snapshotLength;
	}
	
	XPathResult.prototype.getResultType = function()
	{
		return this.resultType;
	}
}