Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/dom/nodeoffset.js
2868 views
1
// Copyright 2005 The Closure Library Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS-IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
* @fileoverview Object to store the offset from one node to another in a way
17
* that works on any similar DOM structure regardless of whether it is the same
18
* actual nodes.
19
*
20
* @author [email protected] (Robby Walker)
21
*/
22
23
goog.provide('goog.dom.NodeOffset');
24
25
goog.require('goog.Disposable');
26
goog.require('goog.dom.TagName');
27
28
29
30
/**
31
* Object to store the offset from one node to another in a way that works on
32
* any similar DOM structure regardless of whether it is the same actual nodes.
33
* @param {Node} node The node to get the offset for.
34
* @param {Node} baseNode The node to calculate the offset from.
35
* @extends {goog.Disposable}
36
* @constructor
37
* @final
38
*/
39
goog.dom.NodeOffset = function(node, baseNode) {
40
goog.Disposable.call(this);
41
42
/**
43
* A stack of childNode offsets.
44
* @type {Array<number>}
45
* @private
46
*/
47
this.offsetStack_ = [];
48
49
/**
50
* A stack of childNode names.
51
* @type {Array<string>}
52
* @private
53
*/
54
this.nameStack_ = [];
55
56
while (node && node.nodeName != goog.dom.TagName.BODY && node != baseNode) {
57
// Compute the sibling offset.
58
var siblingOffset = 0;
59
var sib = node.previousSibling;
60
while (sib) {
61
sib = sib.previousSibling;
62
++siblingOffset;
63
}
64
this.offsetStack_.unshift(siblingOffset);
65
this.nameStack_.unshift(node.nodeName);
66
67
node = node.parentNode;
68
}
69
};
70
goog.inherits(goog.dom.NodeOffset, goog.Disposable);
71
72
73
/**
74
* @return {string} A string representation of this object.
75
* @override
76
*/
77
goog.dom.NodeOffset.prototype.toString = function() {
78
var strs = [];
79
var name;
80
for (var i = 0; name = this.nameStack_[i]; i++) {
81
strs.push(this.offsetStack_[i] + ',' + name);
82
}
83
return strs.join('\n');
84
};
85
86
87
/**
88
* Walk the dom and find the node relative to baseNode. Returns null on
89
* failure.
90
* @param {Node} baseNode The node to start walking from. Should be equivalent
91
* to the node passed in to the constructor, in that it should have the
92
* same contents.
93
* @return {Node} The node relative to baseNode, or null on failure.
94
*/
95
goog.dom.NodeOffset.prototype.findTargetNode = function(baseNode) {
96
var name;
97
var curNode = baseNode;
98
for (var i = 0; name = this.nameStack_[i]; ++i) {
99
curNode = curNode.childNodes[this.offsetStack_[i]];
100
101
// Sanity check and make sure the element names match.
102
if (!curNode || curNode.nodeName != name) {
103
return null;
104
}
105
}
106
return curNode;
107
};
108
109
110
/** @override */
111
goog.dom.NodeOffset.prototype.disposeInternal = function() {
112
delete this.offsetStack_;
113
delete this.nameStack_;
114
};
115
116