I imagine Nvu faces a similar problem when preserving the selection while switching between WYSIWYG and source view. (At least) part of that code must be in https://bugzilla.mozilla.org/show_bug.cgi?id=47040 , perhaps you should just ask Daniel for a few pointers.
Posted by jens.b at May 26, 2005 11:34 PMI think I have only a part of your problem. I only have to preserve the selection when I switch the mode. I do that inserting two special paired elements in the markup representing the colored source.
Posted by Daniel Glazman at May 27, 2005 12:22 AMOut of curiosity, why do you need to locate the node's source code from within the document, rather than (say) node.innerHTML? I guess this supposed to work for XML nodes that don't define node.innerHTML?
(From Alex: innerHTML does not work in XML.)
I assume you'd define the "equivalent source code" for an node to be whatever the page author has saved on disk? (e.g. iframes not including the linked page source)
(From Alex: Not necessarily. XMLSerializer is a nice tool.)
(2) is tricky indeed... can you assume that Mozilla will automatically close tags correctly in the serialized source? If so, you can probably tak e the slice of document source from beginning to target node, extract all the tags with a simple regexp, then iterate in reverse to determine (A) how many siblings the target node has, and (B) how many parents (and, I suppose, the relevant node names). With that, you could probably construct an XPath expression to return the requisite node.
(From Alex: That's one option. I'm still working on it.)
Daniel brings up a good point: if changing the serialized document source changes the DOM (like with innerHTML), then you can simply replace the target node source with itself, prefixed by a node with a unique id, and then fetch that unique-id-node's nextSibling().
I don't recall offhand which DOM properties are HTML-specific, though I suspect they'd probably be helpful.
(From Alex: Although replacing source is certainly a possibility, it leaves a really nasty taste in my mouth. E-mail me privately if you want to continue this discussion, please -- ajvincent at the big g mail service.)
Posted by Ben Karel at May 27, 2005 1:01 PMAlex: hmm. Why have a blog if you direct any sizable discussion to email? Purely due to technical limitations in the blogging software (e.g. no comment threads?)
(From Alex: You'll be seeing an e-mail from me soon. Simply put, it's a matter of personal discretion in how I manage the blog. I don't always redirect conversations to e-mail.)
Posted by Ben Karel at May 28, 2005 10:05 PMMetainfo would be the most obvious answer.
DOM -> Source is easy - as You parse the source, creating XML nodes, attach a special "marker" to the node that describes the start and end location of the node in the source.
Source -> DOM could easily build on these same markers - You just keep a collection of those markers and if You ever need to translate the source location to DOM node, you look up the most relevant marker and get the reference to the DOM Node...
(From Alex: Could you e-mail me a sample?)
Posted by Roland at May 30, 2005 12:20 AM