Path: blob/trunk/javascript/atoms/locators/link_text.js
3991 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617goog.provide('bot.locators.linkText');18goog.provide('bot.locators.partialLinkText');1920goog.require('bot');21goog.require('bot.dom');22goog.require('bot.locators.css');23goog.require('goog.array');24goog.require('goog.dom');25goog.require('goog.dom.TagName');262728/**29* Find an element by using the text value of a link30* @param {string} target The link text to search for.31* @param {!(Document|Element)} root The document or element to perform the32* search under.33* @param {boolean=} opt_isPartial Whether the link text needs to be matched34* only partially.35* @return {Element} The first matching element found in the DOM, or null if no36* such element could be found.37* @private38*/39bot.locators.linkText.single_ = function (target, root, opt_isPartial) {40var elements;41try {42elements = bot.locators.css.many('a', root);43} catch (e) {44// Old versions of browsers don't support CSS. They won't have XHTML45// support. Sorry.46elements = goog.dom.getDomHelper(root).getElementsByTagNameAndClass(47goog.dom.TagName.A, /*className=*/null, root);48}4950var element = goog.array.find(elements, function (element) {51var text = bot.dom.getVisibleText(element);52// getVisibleText replaces non-breaking spaces with plain53// spaces, so if these are present at the beginning or end54// of the link text, we need to trim the regular spaces off55// to be spec compliant for matching on link text.56text = text.replace(/^[\s]+|[\s]+$/g, '');57return (opt_isPartial && text.indexOf(target) != -1) || text == target;58});59return /**@type{Element}*/ (element);60};616263/**64* Find many elements by using the value of the link text65* @param {string} target The link text to search for.66* @param {!(Document|Element)} root The document or element to perform the67* search under.68* @param {boolean=} opt_isPartial Whether the link text needs to be matched69* only partially.70* @return {!IArrayLike} All matching elements, or an empty list.71* @private72*/73bot.locators.linkText.many_ = function (target, root, opt_isPartial) {74var elements;75try {76elements = bot.locators.css.many('a', root);77} catch (e) {78// Old versions of browsers don't support CSS. They won't have XHTML79// support. Sorry.80elements = goog.dom.getDomHelper(root).getElementsByTagNameAndClass(81goog.dom.TagName.A, /*className=*/null, root);82}8384return goog.array.filter(elements, function (element) {85var text = bot.dom.getVisibleText(element);86// getVisibleText replaces non-breaking spaces with plain87// spaces, so if these are present at the beginning or end88// of the link text, we need to trim the regular spaces off89// to be spec compliant for matching on link text.90text = text.replace(/^[\s]+|[\s]+$/g, '');91return (opt_isPartial && text.indexOf(target) != -1) || text == target;92});93};949596/**97* Find an element by using the text value of a link98* @param {string} target The link text to search for.99* @param {!(Document|Element)} root The document or element to perform the100* search under.101* @return {Element} The first matching element found in the DOM, or null if no102* such element could be found.103*/104bot.locators.linkText.single = function (target, root) {105return bot.locators.linkText.single_(target, root, false);106};107108109/**110* Find many elements by using the value of the link text111* @param {string} target The link text to search for.112* @param {!(Document|Element)} root The document or element to perform the113* search under.114* @return {IArrayLike} All matching elements, or an empty list.115*/116bot.locators.linkText.many = function (target, root) {117return bot.locators.linkText.many_(target, root, false);118};119120121/**122* Find an element by using part of the text value of a link.123* @param {string} target The link text to search for.124* @param {!(Document|Element)} root The document or element to perform the125* search under.126* @return {Element} The first matching element found in the DOM, or null if no127* such element could be found.128*/129bot.locators.partialLinkText.single = function (target, root) {130return bot.locators.linkText.single_(target, root, true);131};132133134/**135* Find many elements by using part of the value of the link text.136* @param {string} target The link text to search for.137* @param {!(Document|Element)} root The document or element to perform the138* search under.139* @return {IArrayLike} All matching elements, or an empty list.140*/141bot.locators.partialLinkText.many = function (target, root) {142return bot.locators.linkText.many_(target, root, true);143};144145146