Skip to content

Commit

Permalink
Another attempt to fight XPath in old IE versions
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Nov 5, 2014
1 parent 3947ebc commit b6fb4cb
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions javascript/atoms/locators/xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,41 +95,50 @@ bot.locators.xpath.evaluate_ = function(node, path, resultType) {
}

try {
var reversedNamespaces = {};
var allNodes = doc.getElementsByTagName("*");
for (var i = 0; i < allNodes.length; ++i) {
var n = allNodes[i];
var ns = n.namespaceURI;
if (!reversedNamespaces[ns]) {
var prefix = n.lookupPrefix(ns);
if (!prefix) {
var m = ns.match('.*/(\\w+)/?$');
if (m) {
prefix = m[1];
} else {
prefix = 'xhtml';
}
}
reversedNamespaces[ns] = prefix;
}
}
var namespaces = {};
for (var key in reversedNamespaces) {
namespaces[reversedNamespaces[key]] = key;
}
var resolver = function(prefix) {
return namespaces[prefix] || null;
}
var resolver = doc.createNSResolver ?
doc.createNSResolver(doc.documentElement) :
bot.locators.xpath.DEFAULT_RESOLVER_;

if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(7)) {
// IE6, and only IE6, has an issue where calling a custom function
// directly attached to the document object does not correctly propagate
// thrown errors. So in that case *only* we will use apply().
return doc.evaluate.call(doc, path, node, resolver, resultType, null);

} else {
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(9)) {
var reversedNamespaces = {};
var allNodes = doc.getElementsByTagName("*");
for (var i = 0; i < allNodes.length; ++i) {
var n = allNodes[i];
var ns = n.namespaceURI;
if (!reversedNamespaces[ns]) {
var prefix = n.lookupPrefix(ns);
if (!prefix) {
var m = ns.match('.*/(\\w+)/?$');
if (m) {
prefix = m[1];
} else {
prefix = 'xhtml';
}
}
reversedNamespaces[ns] = prefix;
}
}
var namespaces = {};
for (var key in reversedNamespaces) {
namespaces[reversedNamespaces[key]] = key;
}
resolver = function(prefix) {
return namespaces[prefix] || null;
}
}

try {
return doc.evaluate(path, node, resolver, resultType, null);
} catch (te) {
if (te.name === 'TypeError') {
// fallback to simplified implementation
resolver = doc.createNSResolver ?
doc.createNSResolver(doc.documentElement) :
bot.locators.xpath.DEFAULT_RESOLVER_;
Expand Down

0 comments on commit b6fb4cb

Please sign in to comment.