<commit_before>UIATarget.prototype.language = function() {
	var target = UIATarget.localTarget();
	var app = target.frontMostApp();

	// en , fr ...
	return app.preferencesValueForKey("AppleLanguages")[0];
}

UIATarget.prototype.locale = function() {
	var target = UIATarget.localTarget();
	var app = target.frontMostApp();

	// en_GB
	return app.preferencesValueForKey("AppleLocale");
}

UIAApplication.prototype.keyboard2 = function() {
	var keyboard = this.keyboard();
	log("keyboard : " + keyboard)
	if(keyboard.toString() == "[object UIAElementNil]") {
		throw new UIAutomationException("cannot find keyboard", 7);
	} else {
		return keyboard;
	}
}

UIAElementArray.prototype.type = function() {
	return "UIAElementArray";
}

UIAElementArray.prototype.isStale = function() {
	return false;
}

UIAElementArray.prototype.reference = function() {
	if(!this.id) {
		UIAutomation.cache.store(this);
	}
	return this.id;
}

UIAElementArray.prototype.elements2 = function(depth, criteria) {
	var all = this.toArray();
	var res = new Array();
	for(var i = 0; i < all.length; i++) {
		var element = all[i];
		if(element.matches(criteria)) {
			res.push(element);
		}
	}
	return new MyUIAElementArray(res);
}
// TODO freynaud convert to the new OR strategy.
UIAElementArray.prototype.element = function(depth, criteria) {
	var all = this.toArray();
	for(var i = 0; i < all.length; i++) {
		var element = all[i];
		if(element.matches(criteria)) {
			return element;
		}
	}
	throw new UIAutomationException("cannot find element for criteria :" + JSON.stringify(criteria), 7);
}
/**
 * returns the class for the element, as per
 * http://developer.apple.com/library/ios/#documentation/DeveloperTools/Reference/UIAutomationRef
 */
UIAElement.prototype.type = function() {
	return this.toString().replace('[object ', '').replace(']', '');
}

UIAElement.prototype.isInAlert = function() {
	var parent;
	if(this.parent) {
		parent = this.parent();
	}

	while(parent.type() != "UIAElementNil" && parent.type() != "UIATarget") {
		if(parent.type() == "UIAAlert") {
			return true;
		}
		parent = parent.parent();
	}

	return false;
}
UIAElementNil.prototype.type = function() {
	return "UIAElementNil";
}
// TODO freynaud check why this is necessary. key extends elements.
UIAKey.prototype.type = UIAElement.prototype.type;

UIAElement.prototype.tap2 = function() {
	if(this.isVisible()) {
		var rect = this.rect();
		var x = rect.origin.x + (rect.size.width / 2);
		var y = rect.origin.y + (rect.size.height / 2);
		var point = {
			'x' : Math.floor(x),
			'y' : Math.floor(y)
		};
		UIATarget.localTarget().tap(point);
		if(this.isInAlert() && this.type() == "UIAButton") {
			log("tapping in something in an alert.Alert is gone.");
			UIAutomation.cache.clearAlert();
		}

	} else {
		var ex = new UIAutomationException("element is not visible", 11);
		throw ex;
	}
}

UIAAlert.prototype.defaultButton2 = function() {
	var res = this.defaultButton();
	res.tap2 = function() {
		if(this.isVisible()) {
			var rect = this.rect();
			var x = rect.origin.x + (rect.size.width / 2);
			var y = rect.origin.y + (rect.size.height / 2);
			var point = {
				'x' : Math.floor(x),
				'y' : Math.floor(y)
			};
			UIATarget.localTarget().tap(point);
			UIAutomation.cache.clearAlert();
		} else {
			var ex = new UIAutomationException("element is not visible", 11);
			throw ex;
		}
	}
	return res;
}

UIAElement.prototype.reference = function() {
	if(!this.id) {
		UIAutomation.cache.store(this);
	}
	return this.id;
}
UIAElementNil.prototype.reference = UIAElement.prototype.reference;

UIAKey.prototype.reference = UIAElement.prototype.reference;

/**
 * can't find a way to detect stale object. CheckIsValid doesn't do it properly.
 * Trying to scroll to the element seems like a valid approximation.
 */
UIAElement.prototype.isStale = function() {
	if(this.checkIsValid() == false) {
		log("checkISValid returns false");
		return true;
	} else {
		try {
			this.scrollToVisible();
			if(this.isVisible() == 1) {
				return false;
			} else {
				return true;
			}
		} catch (err) {
			if(err.message && err.message.indexOf('scrollToVisible cannot be used') != -1) {
				return true;
			}
		}<commit_msg>checking for visibility before scrolling.<commit_after>UIATarget.prototype.language = function() {
	var target = UIATarget.localTarget();
	var app = target.frontMostApp();

	// en , fr ...
	return app.preferencesValueForKey("AppleLanguages")[0];
}

UIATarget.prototype.locale = function() {
	var target = UIATarget.localTarget();
	var app = target.frontMostApp();

	// en_GB
	return app.preferencesValueForKey("AppleLocale");
}

UIAApplication.prototype.keyboard2 = function() {
	var keyboard = this.keyboard();
	log("keyboard : " + keyboard)
	if(keyboard.toString() == "[object UIAElementNil]") {
		throw new UIAutomationException("cannot find keyboard", 7);
	} else {
		return keyboard;
	}
}

UIAElementArray.prototype.type = function() {
	return "UIAElementArray";
}

UIAElementArray.prototype.isStale = function() {
	return false;
}

UIAElementArray.prototype.reference = function() {
	if(!this.id) {
		UIAutomation.cache.store(this);
	}
	return this.id;
}

UIAElementArray.prototype.elements2 = function(depth, criteria) {
	var all = this.toArray();
	var res = new Array();
	for(var i = 0; i < all.length; i++) {
		var element = all[i];
		if(element.matches(criteria)) {
			res.push(element);
		}
	}
	return new MyUIAElementArray(res);
}
// TODO freynaud convert to the new OR strategy.
UIAElementArray.prototype.element = function(depth, criteria) {
	var all = this.toArray();
	for(var i = 0; i < all.length; i++) {
		var element = all[i];
		if(element.matches(criteria)) {
			return element;
		}
	}
	throw new UIAutomationException("cannot find element for criteria :" + JSON.stringify(criteria), 7);
}
/**
 * returns the class for the element, as per
 * http://developer.apple.com/library/ios/#documentation/DeveloperTools/Reference/UIAutomationRef
 */
UIAElement.prototype.type = function() {
	return this.toString().replace('[object ', '').replace(']', '');
}

UIAElement.prototype.isInAlert = function() {
	var parent;
	if(this.parent) {
		parent = this.parent();
	}

	while(parent.type() != "UIAElementNil" && parent.type() != "UIATarget") {
		if(parent.type() == "UIAAlert") {
			return true;
		}
		parent = parent.parent();
	}

	return false;
}
UIAElementNil.prototype.type = function() {
	return "UIAElementNil";
}
// TODO freynaud check why this is necessary. key extends elements.
UIAKey.prototype.type = UIAElement.prototype.type;

UIAElement.prototype.tap2 = function() {
	if(this.isVisible()) {
		var rect = this.rect();
		var x = rect.origin.x + (rect.size.width / 2);
		var y = rect.origin.y + (rect.size.height / 2);
		var point = {
			'x' : Math.floor(x),
			'y' : Math.floor(y)
		};
		UIATarget.localTarget().tap(point);
		if(this.isInAlert() && this.type() == "UIAButton") {
			log("tapping in something in an alert.Alert is gone.");
			UIAutomation.cache.clearAlert();
		}

	} else {
		var ex = new UIAutomationException("element is not visible", 11);
		throw ex;
	}
}

UIAAlert.prototype.defaultButton2 = function() {
	var res = this.defaultButton();
	res.tap2 = function() {
		if(this.isVisible()) {
			var rect = this.rect();
			var x = rect.origin.x + (rect.size.width / 2);
			var y = rect.origin.y + (rect.size.height / 2);
			var point = {
				'x' : Math.floor(x),
				'y' : Math.floor(y)
			};
			UIATarget.localTarget().tap(point);
			UIAutomation.cache.clearAlert();
		} else {
			var ex = new UIAutomationException("element is not visible", 11);
			throw ex;
		}
	}
	return res;
}

UIAElement.prototype.reference = function() {
	if(!this.id) {
		UIAutomation.cache.store(this);
	}
	return this.id;
}
UIAElementNil.prototype.reference = UIAElement.prototype.reference;

UIAKey.prototype.reference = UIAElement.prototype.reference;

/**
 * can't find a way to detect stale object. CheckIsValid doesn't do it properly.
 * Trying to scroll to the element seems like a valid approximation.
 */
UIAElement.prototype.isStale = function() {
	if(this.checkIsValid() == false) {
		log("checkISValid returns false");
		return true;
	} else {
		try {
			if(this.isVisible()) {
				return false;
			}
			this.scrollToVisible();
			if(this.isVisible() == 1) {
				return false;
			} else {
				return true;
			}
		} catch (err) {
			if(err.message && err.message.indexOf('scrollToVisible cannot be used') != -1) {
				return true;
			}
		}
{'fraction_non_alphanumeric': 0.15611314948514618, 'fraction_numerical': 0.004734288081429755, 'mean_word_length': 3.8563218390804597, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5775067', 'n_tokens_mistral': 3128, 'n_tokens_neox': 2903, 'n_words': 639}