UIWebView cannot programmatically select/deselect text via JavaScript

Originator:invalidname
Number:rdar://8707236 Date Originated:11/29/10
Status:Duplicate/7605151 Resolved:
Product:iPad Product Version:4.2.1 (8C148)
Classification:other bug Reproducible:always
 
Summary:
JavaScript functions that can set and clear the selection programmatically on web pages do not work in a UIWebView

Steps to Reproduce:
1. Start with the attached project. UIWebViewSelectBuglet.  This is an iPad application that contains an HTML file and a JavaScript, which are copied to the application bundle.  If you open the HTML file in Safari, WebKit Nightly, or Firefox on your Mac, you can test out the three buttons at the top of the document, which call functions in the .js file.  These functions respectively display an alert, select some text in the document, and clear the current selection.

2. Now build and run the application. This loads the same HTML file into a UIWebView, and provides three UIButtons that also call the JavaScript functions, by means of -[UIWebView stringByEvaluatingJavaScriptFromString:].

Expected Results:

When tapped, the buttons in the HTML document and the UIButtons should behave as they do in the desktop browsers: the alert button should show a JavaScript alert, the select text button should select some text, and the clear selection should clear the selection (whether set by the second button or by a user gesture)

Actual Results:

Only the alert button works.  The buttons to set and clear the selection do nothing.  The functions appear to complete their execution (you can uncomment the alert() functions at the bottom of each to confirm this), but simply have no effect.

Regression:

Notes:
Here are the makeSelection() and clearSelection() functions:
function makeSelection () {
   	var pathResult = document.evaluate ("/HTML[1]/BODY[1]//P[4]/text()[1]",
                                        document,
                                        null,
                                        XPathResult.ANY_UNORDERED_NODE_TYPE,
                                        null);
    var startContainer = pathResult.singleNodeValue;
    pathResult =  document.evaluate ("/HTML[1]/BODY[1]//P[5]/text()[1]",
									 document,
									 null,
									 XPathResult.ANY_UNORDERED_NODE_TYPE,
									 null);
    var endContainer = pathResult.singleNodeValue;
	if (startContainer == null) {
        console.log ("didn't get startContainer");
        return;
    }
    if (endContainer == null) {
        console.log ("didn't get endContainer");
        return;
    }
    var selectionRange = document.createRange();
    selectionRange.setStart (startContainer, 10);
    selectionRange.setEnd (endContainer, 20);
	
    window.getSelection().removeAllRanges();
    window.getSelection().addRange (selectionRange);
	
	// alert ('bottom of makeSelection()');
}


function clearSelection() {
	// window.getSelection().removeAllRanges(); // works on desktop safari
	// http://stackoverflow.com/questions/3169786/clear-text-selection-with-javascript
	if (window.getSelection) {
		if (window.getSelection().empty) {  // Chrome
			window.getSelection().empty();
		} else if (window.getSelection().removeAllRanges) {  // Firefox
			window.getSelection().removeAllRanges();
		}
	} else if (document.selection) {  // IE?
		document.selection.empty();
	}
	// alert ('bottom of clearSelection()');
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!