For example, if I have the HTML:
<div id="foo">
<p>Some text in a paragraph</p>
<p>More text in a paragraph</p>
</div>
And someone selects from the start of "in" (paragraph 1) to the end of "More" (paragraph 2), I want to get the selection info like:
{
"object": [div #foo], /* commonAncestorContainer DOM element of the selection */
"source": "<div id="foo">\n<p>Some text in a paragraph</p>\n<p>More text in a paragraph</p>\n</div>", /* outerHTML of the commonAncestorContainer */
"startOffset": 28, /* offset of selection starting point in the source code */
"endOffset": 54 /* offset of selection ending point in the source code */
}
Here are some problems when I attempt to do this:
We can use
Range.commonAncestorContainer
to get commonAncestorContainer of a range. However how do we get the real commonAncestorContainer if a selection contains multiple ranges?How to get the startOffset and endOffset of the selection range in the source code fragment?