You need to be logged in to post in the forums. If you do not have an account, please sign up first.

24. October 2009, 16:10:49

zim32

Posts: 3

range object

Hello everyone. Can you tell me where i can find information about range object (it's properties and methods). I have a working Javascript code in Firefox, but it's not working in Opera 10. Thanks very much

25. October 2009, 13:53:16

JeroenH

Posts: 434

The standard itself documents all standard properties. If something is not working, we can't help you without an example: we need a full document or, preferably, a minimal test case.

25. October 2009, 17:21:24

zim32

Posts: 3

W3C is good ). I've changed my code. Now it's working but i have some trouble (in opera) using this peace of code (this is a part of code which is doing some formatting stuff with selected text inside content editable div)

next_element = bound_element.nextSibling; //bound_element is span, next_element seems to be text node
new_range = document.createRange();
new_range.setStart(sel_range.startContainer,sel_range.startOffset);//sel_range - previous selection
new_range.setEnd(next_element,0);
new_range.surroundContents(newNode);
newNode.style.fontWeight = "bold";

After using this part of code all is working fine but opera doesn't respond for 5-10 seconds. Finally newNode appears. Where is the problem? Thanks!

26. October 2009, 10:05:06

JeroenH

Posts: 434

Without more context about "bound_element", it's still not possible to help you any further. Where does it come from?

Wrapping just the selection with new_node works fine for me.

Also, the W3C Range api is horrible, for one specific reason: surroundContents. It throws BAD_BOUNDARYPOINTS_ERR for its main use case.
Did you look at the execCommand api in html5?

26. October 2009, 11:48:25 (edited)

zim32

Posts: 3

I know that opera doesn't support execCommand... Am i wrong?

"Also, the W3C Range api is horrible, for one specific reason: surroundContents. It throws BAD_BOUNDARYPOINTS_ERR for its main use case." that is why i have to use nextSibling. And after using nextSibling i have a problem discribed earlier.

26. October 2009, 12:37:08

JeroenH

Posts: 434

Originally posted by zim32:

I know that opera doesn't support execCommand... Am i wrong?


Opera does support it since Opera 9, although the clipboard functions aren't supported.

Originally posted by zim32:

And after using nextSibling i have a problem discribed earlier.

I haven't been able to reproduce that. Your code always fails with the exception, because your range starts in the startContainer and ends in the next_element. It should end at the end of startContainer if you want to avoid the exception. Without knowing how you get bound_element, we can't get any further.

20. May 2010, 10:16:02

TedBeer

Posts: 15

Opera calculates range boundaries incorrectly.
Here the simple example working in FireFox, Safari, Chrome and throwing BAD_BOUNDARYPOINTS_ERR exception in Opera:
        //the 'start' element is a SPAN
	var oTextNode = document.getElementById('start').childNodes[0];

	var sel = document.createRange();
	sel.setStart( oTextNode, 14);
	sel.setEndAfter( oTextNode);
	sel.surroundContents(document.createElement('span'));

It does not correspond the standard which says:

The surroundContents() method raises an exception if the Range partially selects a non-Text node.


I'm trying to surround a part of the Text node only but Opera throws the exception.

20. May 2010, 11:52:42

TedBeer

Posts: 15

the following workarounds work:
1. sel.setEnd( oTextNode, oTextNode.nodeValue.length);

2. sel.setEnd( oTextNode.parentNode.insertBefore(
	document.createTextNode(''), oTextNode.nextSibling), 0);

3.	oTextNode.splitText(oTextNode.nodeValue.length);
	sel.setEnd( oTextNode.nextSibling, 0);


So the obvious usage of API:
sel.setEndAfter( oTextNode);
is also expected to work.

Forums » Dev.Opera » General Web Development Discussions