Path: blob/trunk/javascript/atoms/locators/link_text.js
2884 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');252627/**28* Find an element by using the text value of a link29* @param {string} target The link text to search for.30* @param {!(Document|Element)} root The document or element to perform the31* search under.32* @param {boolean=} opt_isPartial Whether the link text needs to be matched33* only partially.34* @return {Element} The first matching element found in the DOM, or null if no35* such element could be found.36* @private37*/38bot.locators.linkText.single_ = function (target, root, opt_isPartial) {39var elements;40try {41elements = bot.locators.css.many('a', root);42} catch (e) {43// Old versions of browsers don't support CSS. They won't have XHTML44// support. Sorry.45elements = goog.dom.getDomHelper(root).getElementsByTagNameAndClass(46goog.dom.TagName.A, /*className=*/null, root);47}4849var element = goog.array.find(elements, function (element) {50var text = bot.dom.getVisibleText(element);51// getVisibleText replaces non-breaking spaces with plain52// spaces, so if these are present at the beginning or end53// of the link text, we need to trim the regular spaces off54// to be spec compliant for matching on link text.55text = text.replace(/^[\s]+|[\s]+$/g, '');56return (opt_isPartial && text.indexOf(target) != -1) || text == target;57});58return /**@type{Element}*/ (element);59};606162/**63* Find many elements by using the value of the link text64* @param {string} target The link text to search for.65* @param {!(Document|Element)} root The document or element to perform the66* search under.67* @param {boolean=} opt_isPartial Whether the link text needs to be matched68* only partially.69* @return {!IArrayLike} All matching elements, or an empty list.70* @private71*/72bot.locators.linkText.many_ = function (target, root, opt_isPartial) {73var elements;74try {75elements = bot.locators.css.many('a', root);76} catch (e) {77// Old versions of browsers don't support CSS. They won't have XHTML78// support. Sorry.79elements = goog.dom.getDomHelper(root).getElementsByTagNameAndClass(80goog.dom.TagName.A, /*className=*/null, root);81}8283return goog.array.filter(elements, function (element) {84var text = bot.dom.getVisibleText(element);85// getVisibleText replaces non-breaking spaces with plain86// spaces, so if these are present at the beginning or end87// of the link text, we need to trim the regular spaces off88// to be spec compliant for matching on link text.89text = text.replace(/^[\s]+|[\s]+$/g, '');90return (opt_isPartial && text.indexOf(target) != -1) || text == target;91});92};939495/**96* Find an element by using the text value of a link97* @param {string} target The link text to search for.98* @param {!(Document|Element)} root The document or element to perform the99* search under.100* @return {Element} The first matching element found in the DOM, or null if no101* such element could be found.102*/103bot.locators.linkText.single = function (target, root) {104return bot.locators.linkText.single_(target, root, false);105};106107108/**109* Find many elements by using the value of the link text110* @param {string} target The link text to search for.111* @param {!(Document|Element)} root The document or element to perform the112* search under.113* @return {IArrayLike} All matching elements, or an empty list.114*/115bot.locators.linkText.many = function (target, root) {116return bot.locators.linkText.many_(target, root, false);117};118119120/**121* Find an element by using part of the text value of a link.122* @param {string} target The link text to search for.123* @param {!(Document|Element)} root The document or element to perform the124* search under.125* @return {Element} The first matching element found in the DOM, or null if no126* such element could be found.127*/128bot.locators.partialLinkText.single = function (target, root) {129return bot.locators.linkText.single_(target, root, true);130};131132133/**134* Find many elements by using part of the value of the link text.135* @param {string} target The link text to search for.136* @param {!(Document|Element)} root The document or element to perform the137* search under.138* @return {IArrayLike} All matching elements, or an empty list.139*/140bot.locators.partialLinkText.many = function (target, root) {141return bot.locators.linkText.many_(target, root, true);142};143144145