I was able to select a range of text in tinyMCE editor for both
firefox and IE using DOM Range. Conceptually range objects are quite
different in IE and firefox. Thought I would share the code just in
case if anyone stumbles on the same problem.
startindex and endindex are the starting position and end character
position respectively.
String script =
" if(window.getSelection)"+ //firefox
"{"+
"var range = document.createRange();"+
"var start = arguments[0].firstChild;"+
"range.setStart(start, "+startindex+");"+
"range.setEnd(start, "+endindex+");"+
"window.getSelection().addRange(range);"+
"}"+
"if(document.selection)"+ //IE
"{"+
"document.selection.empty();"+
"var txtRange = document.body.createTextRange();"+
"var start = arguments[0];"+
"txtRange.moveToElementText(start);"+
"txtRange.setEndPoint(\"EndToEnd\", txtRange);"+
"txtRange.moveStart('character',"+startindex+");"+
"txtRange.moveEnd('character',"+endindex+");"+ //endindex is negative
for selected text
"txtRange.select();"+
"}";
js.executeScript(script, objArea); //objArea is tinyMCE editor object
If you want to select all in firefox then the script would slightly
change but I did not want to post convoluted code here to add to
confusion.
var start= arguments[0];
Object obj=(Object) js.executeScript("return
arguments[0].childNodes.length;", txtArea);
endindex = Integer.parseInt(obj.toString());
Hope that helps!
-Nilesh
On Dec 14 2010, 7:21 pm, Nilesh <
nilesh.c...@gmail.com> wrote:
> I found an example of the editor that is available herehttp://
tinymce.moxiecode.com/examples/full.php
>
> WebDriver driver = new FirefoxDriver();
> driver.get("
http://tinymce.moxiecode.com/examples/full.php");
> JavascriptExecutor js = (JavascriptExecutor)driver;
> driver.switchTo().frame("content_ifr");
> WebElement tiny = driver.findElement(By.id("tinymce"));
> WebElement p = tiny.findElement(By.tagName("p"));
> System.out.println(p.getText());
> js.executeScript("arguments[0].focus();arguments[0].setSelectionRange(0,10) ;",tiny);
> Thread.sleep(5000);
>
> Throws exception! I am stumped. Anybody?
>
> On Dec 14, 12:22 pm, Nilesh <
nilesh.c...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hi All,
> > My application hasTinyMCEHTML editor and I am trying to select a
> > range of text inside that editor. I have a javascript code that
> > selects a range of text but it is only working for textArea/input
> > objects and not fortinyMCEeditor. I am using 2.0a4, FF3.6 on Windows