I have a piece of HTML which I am displaying inside a UIWebView using Webkit stylesheet attributes. I use Webkit to display the HTML in columns in order to simulate a book.
Only one column is visible at a time (one column represents one page). Now, I am trying to find the range of the visible HTML so that I can insert a span element right before the first visible word.
I managed to get the HTML element which contains the first visible word by using the JavaScript function, document.elementAtPoint(I might have the function name wrong), and changed its CSS class. but that just isn’t accurate enough for me. I need it to be accurate up to the first visible word.
The idea is the create a column break at the first visible word when the fontsize is increased or decreased. I can using JavaScript to figure out in which column the element is, and programmatically scroll the user to that column, but first I need to get the element in there.
Can anyone help me?
The CSSOM View Module specification adds
caretPositionFromPoint(x, y)to theDocumentinterface, which returns a caret position for the specified x and y co-ordinates. WebKit supportscaretRangeFromPoint, a close analogue from an earlier specification, which returns aRange.It is possible that the word has been hyphenated and thus spans two columns, so rather than wrapping the first word in a span you may wish to consider the more naive approach of inserting the
spandirectly at the cursor point. Here’s an example:Demo (WebKit only—click to insert spans): http://jsfiddle.net/Jordan/Aw9aV/
One final consideration: it is possible that WebKit will eventually stop supporting
caretRangeFromPointin lieu ofcaretPositionFromPoint; if so, you will need to adapt your code. Also note that the latter returns aCaretPositionwhich may not implement theinsertNodemethod. The spec is still at WD, so be mindful that it is still in flux.