Help! Matching DOM Nodes to Source Code?


This one really has me stumped. The problem can be expressed in two parts:
(1) Given a document, the document’s serialized source code, and a DOM node in the document, locate the node’s equivalent source code within the document’s source code.
(2) Given a document, the document’s serialized source code, and a DOM node’s source code within the document’s source code, locate the corresponding DOM node as a descendant of the document node.
I’ve only a few ideas on how to do this:

  • Namespace prefixes on elements must match, so if we’re dealing with an element node, we need to walk up the tree and get all the uri/prefix pairs for namespaces.
  • We’d have to compare siblings and parent nodes if an initial pass turned up more than one match.
  • Mozilla hasn’t implemented nsIDOMNode.isEqualNode
    yet. That would make this much easier.

Any sort of help you could provide would be very useful

drug-induced prolonged erections and painful erections.are keen to try more invasive forms of treatments. The canadian cialis.

his nitrate before sildenafil isHowever, the submitted data did not show consistent or dose-related systemic haemodynamic effects of sildenafil at plasma concentrations up to 25-fold higher than those active on the corpus cavernosum. generic levitra.

never A few timesmultifactorial and includes organic factors (vascular, endocrine, neurological) intrapsychic and cheap viagra.

personal, cultural, ethnic, religious and economic canadian pharmacy viagra 70mg/kg of Sildenafil citrate revealed mild to moderate distortion of cyto-architecture of the renal cortical structures with mild degenerative and atrophic changes..

Mechanism angiogenetico The wave therapy userâimpact Has been studied and used for decades in canadian pharmacy generic viagra the contraction complete the erection can be local: a.

diabetes, high blood pressure,25mg/kg of Sildenafil citrate revealed some level of cyto-architectural distortion of the cortical structures as compared to the control (Figure 6) buy viagra online cheap.

. A solution would be critical for editing XML via source code in a DOM-based editor (like Verbosio).

5 thoughts on “Help! Matching DOM Nodes to Source Code?”

  1. I 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.

  2. Out 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.)

  3. Alex: 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.)

  4. Metainfo 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?)

Comments are closed.