Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Selection & Adding tag dynamically in JavaScript

I need some help about JavaScript on iPhone UIWebView;
I have HTML like below:

<html>
   <head>
      <title>Example</title>
   </head>
   <body>
     <span>this example for selection <b>from</b> UIWebView</span>
   </body>
</html>

I want to make a selection, and add <span> tag with color to the selected text in HTML to write a note just like an e-book reader.

This is my JavaScript code to get the selected text:

NSString *SelectedString = [NSString stringWithFormat:@"function getSelText()"
                     "{"
                     "var txt = '';"
                     " if (window.getSelection)"
                     "{"
                     "txt = window.getSelection();"
                     " }"
                     "else if (document.getSelection)"
                     "{"
                     "txt = document.getSelection();"
                     "}"
                     "else if (document.selection)"
                     "{"
                     "txt = document.selection.createRange().text;"
                     "}"
                     "else return;"
                     "alert(txt);"
                     "}getSelText();"];
[webView stringByEvaluatingJavaScriptFromString:SelectedString];

This works good, and returns me the selected text.

Also this JS code for adding a new tag:

NSString *AddSpanTag = [NSString stringWithFormat:@"function selHTML() {"
                 "if (window.ActiveXObject) {"
                 "var c = document.selection.createRange();"
                 "return c.htmlText;"
                 "}"
                 "var nNd = document.createElement(\"span\");"
                 "var divIdName = \'myelementid\';"
                 "var ColorAttr = \"background-color: #ffffcc\";"
                 "nNd.setAttribute(\'id\',divIdName);"
                 "nNd.setAttribute(\'style\',ColorAttr);"
                 "var w = getSelection().getRangeAt(0);"
                 "w.surroundContents(nNd);"
                 "return nNd.innerHTML;"
                 "}selHTML();"];
[webView stringByEvaluatingJavaScriptFromString:AddSpanTag];

My problem is so:
if I select "example" (look at the HTML text) text from WebView, it works, because it doesn't contain any tag.
But if I select "selection from" (look at the HTML text) text from WebView, it's not working, because it starts the tag of <b>, and </b> is out of my selection, then I can't add a new tag between <b> and </b>.

How can I solve this problem?

like image 539
relower Avatar asked Jan 19 '23 15:01

relower


1 Answers

Since this is a UIWebView, this is Mobile Safari and you can lose a lot of those branches for obtaining the selection. I would suggest using document.execCommand() with the "HiliteColor" command, which is built into the browser and works on the whole selection even when it crosses element boundaries:

var sel = window.getSelection();
if (!sel.isCollapsed) {
    var selRange = sel.getRangeAt(0);
    document.designMode = "on";
    sel.removeAllRanges();
    sel.addRange(selRange);
    document.execCommand("HiliteColor", false, "#ffffcc");
    sel.removeAllRanges();
    document.designMode = "off";
}
like image 121
Tim Down Avatar answered Jan 28 '23 07:01

Tim Down