Mailing Lists: Apple Mailing Lists

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

Editing issues, replaceSelectionWithNode: and DOMRange setStart::



Hi!

I'm trying to make a little editor based on WebView.

In my document window, I have a WebView with my document, and on the side there's a NSTableView with a list of possible paragraph styles. I want the user to be able select a paragraph style (Heading 1, Heading 2, Heading 3, Paragraph, Some special paragraph style) from the table view to change the style of the current paragraph.

So what I do, on the WebKit level, is create a new DOMElement with the correct name (that is, correct tag: h1, h2 or p, for example), and set it's contents to be that of the old element, and then replace the old element with the new one.

I have this basically working, but there are some issues that have come up.


First question: Why does replaceSelectionWithNode: mess with the markup?


My markup looks like this:

<h1>Heading</h1>

<p>Paragraph full of text</p>

What I end up with using replaceSelectionWithNode: is this:

<p><FONT class="Apple-style-span" face="SomeFont" size="7"><SPAN class="Apple-style-span" style="font-size: 28px;">Heading</SPAN></ FONT>Paragraph full of text</p>

So the "Heading" text gets inserted into the paragraph. What I expected was this:

<h2>Heading</h2>

<p>Paragraph full of text</p>


Here's what I do, step by step.

When the user clicks on the tableView, I change the selection in the WebView so it encompasses the whole element selected (so I change the whole paragraph). This is the code:

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


element could look like this when printed:

<DOMHTMLElement [H1]: 0x158f70b0 ''>

Then I create new element, using the tableView selection to get the correct element name, and the old element to get the correct contents. Then I try to replace the selection with my new element:

[mWebView replaceSelectionWithNode:newElement];

The result is that my markup will look like what I described above. Why does the WebView take the appearance of my new element and insert it in the next p element, instead of actually inserting the element I'm giving it to the position in the DOM tree where the previous element is?

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?



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?


Thanks for all help and ideas!


-- Petteri Kamppuri email@hidden


_______________________________________________ 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


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.