Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine character index in HTML source given a DOMRange from a WebKit selection
    primarykey
    data
    text
    <p>I'm attempting to synchronize a <a href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html" rel="nofollow noreferrer">DOMRange</a> (representing a user-selection from a <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/Reference/Reference.html" rel="nofollow noreferrer">Cocoa WebView</a>) to the original HTML source currently rendered in that view, as a kind of Dreamweaver-split-editor:</p> <p><img src="https://i.stack.imgur.com/MuKrf.png" alt="Dreamweaver code-design splitview"></p> <p>My first idea was to get the DOMRange object's <code>startContainer</code> and <code>offset</code> and walk up the DOM tree from there, accumulating the overall character offset up to the body tag.</p> <p>Unfortunately this task presents some problems:</p> <ol> <li>Clearly the document's outerHTML will differ from the original HTML source if the DOM was manipulated via Javascript or the parser needed to clean up malformed tags.</li> <li>I can't figure out how to get the offset of a node within its parent text node (e.g., 4 characters to <em>target</em> in <code>&lt;p&gt;some&lt;div&gt;target&lt;/div&gt;text&lt;/p&gt;</code>), and <a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-normalize" rel="nofollow noreferrer">normalize</a> doesn't seem to make this any easier.</li> <li>Trying to account for some of the problems in #1, or just going from HTML source to WebView will probably require separately parsing the HTML and then correlating the two DOM-trees.</li> </ol> <p>One ray of hope is that HTML5 specifies a standard parsing algorithm for dealing with invalid HTML (which WebKit has since adopted), so in theory it should be possible to use an off-the-shelf HTML5 parser to generate the same tree as WebKit — right?</p> <p>This is the most similar existing question I could find, but it's for a slightly different problem:<br> <a href="https://stackoverflow.com/questions/2046381/getting-source-html-from-a-webview-in-cocoa">Getting source HTML from a WebView in Cocoa</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload