Sha256: a52341367a02ab53ee7a43756aa9327f792cf1a66ce7ec4f147d486c4a7eaeaa

Contents?: true

Size: 1.45 KB

Versions: 13

Compression:

Stored size: 1.45 KB

Contents

// Credit to: http://simonwillison.net/2006/Jan/20/escape/
RegExp.escape = function(text) {
  if (!arguments.callee.sRE) {
    var specials = [
      '/', '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
};

var allLabels = inDocument.getElementsByTagName("label");
var regExp = new RegExp('^\\W*' + RegExp.escape(locator) + '(\\b|$)', 'i');

var candidateLabels = $A(allLabels).select(function(candidateLabel){
  var labelText = getText(candidateLabel).strip();
  return (labelText.search(regExp) >= 0);
});

if (candidateLabels.length == 0) {
  return null;
}

//reverse length sort
candidateLabels = candidateLabels.sortBy(function(s) {
  return s.length * -1;
});

var locatedLabel = candidateLabels.first();
var labelFor = null;

if (locatedLabel.getAttribute('for')) {
  labelFor = locatedLabel.getAttribute('for');
} else if (locatedLabel.attributes['for']) { // IE
  labelFor = locatedLabel.attributes['for'].nodeValue;
}

if ((labelFor == null) && (locatedLabel.hasChildNodes())) {
  return locatedLabel.getElementsByTagName('button')[0]
    || locatedLabel.getElementsByTagName('input')[0]
    || locatedLabel.getElementsByTagName('textarea')[0]
    || locatedLabel.getElementsByTagName('select')[0];
}

return selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow);

Version data entries

13 entries across 13 versions & 7 rubygems

Version Path
radiant-1.0.0 ruby-debug/ruby/1.8/gems/webrat-0.7.3/lib/webrat/selenium/location_strategy_javascript/label.js
indirect-webrat-0.7.5 lib/webrat/selenium/location_strategy_javascript/label.js
indirect-webrat-0.7.4 lib/webrat/selenium/location_strategy_javascript/label.js
webrat-0.7.3 lib/webrat/selenium/location_strategy_javascript/label.js
jbd-webrat-0.7.2.rails3 lib/webrat/selenium/location_strategy_javascript/label.js
webrat-0.7.2 lib/webrat/selenium/location_strategy_javascript/label.js
webrat-0.7.2.beta.2 lib/webrat/selenium/location_strategy_javascript/label.js
mutle-webrat-0.7.2.beta.1 lib/webrat/selenium/location_strategy_javascript/label.js
thoughtbot-webrat-0.7.2.pre lib/webrat/selenium/location_strategy_javascript/label.js
webrat-0.7.2.beta.1 lib/webrat/selenium/location_strategy_javascript/label.js
honkster-webrat-0.7.1.2 lib/webrat/selenium/location_strategy_javascript/label.js
honkster-webrat-0.7.1.1 lib/webrat/selenium/location_strategy_javascript/label.js
webrat-0.7.1 lib/webrat/selenium/location_strategy_javascript/label.js