Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Editing issues, replaceSelectionWithNode: and DOMRange setStart::



On Aug 26, 2007, at 11:17 AM, Petteri Kamppuri wrote:

element = ... (get this by massaging [mWebView selectedDOMRange])
DOMRange *selection = [[element ownerDocument] createRange];
[selection selectNode:element];
[mWebView setSelectedDOMRange:selection affinity:NSSelectionAffinityDownstream];

The issue here is that selections aren't arbitrary DOM ranges. They correspond only to what users see; so the selection begins at the first character inside the H1 element and ends at the first character on the next line.


I can get the correct results I want by directly using the DOM:

[[element parent] replaceChild:newElement :element];

However, this requires me to manually take care of undo. Not nice. Any ideas?

I think that WebKit ought to make it easier to do these kinds of changes in an undoable way.


Second question: What's the correct syntax for DOMRange setStart:: and setEnd::? After the new node has been inserted, I try to get the selection back to what it was. If I use [range setStart:newElement :[oldRange startOffset]]; and [range endStart:newElement :[oldRange endOffset]]; I get an exception. I can get the end or beginning of the replaced paragraph to be selected, by using either 0 or 1 for the offset on both method calls, but I'd like to get the caret back to the exact position it was in. Do I have to generate mouseDown events or is there an easy way to get the DOMRange to behave?

DOMRange is working as designed.

Mapping a selection from the old content to the new is complicated.

Offsets are relative to the container node. In an HTML element, the offset is a child index. In a text node, the offset is a character index. This is the DOM standard, nothing specific to WebKit.

If your original selection was inside some text, then the container of the range endpoint was a text node, and the offset was the character inside the text where the selection began. To make a corresponding range you'll have to find the new text node and use it as the container. You're getting exceptions because you're trying to use an offset within a text node but you're passing the H1 element as the container.

If you can come up with some way of finding the corresponding node in the new content, then you can pass it along with the appropriate offset.

    -- Darin

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webkitsdk-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webkitsdk-dev/email@hidden

This email sent to email@hidden
References: 
 >Editing issues, replaceSelectionWithNode: and DOMRange setStart:: (From: Petteri Kamppuri <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.