javascript - Trouble With Selection Object in Firefox and IE -
i've got javascript messes selections. full code complicated stuff i'd rather not go right now, i've got smaller test case don't understand here. if load page should see text , 2 buttons.
- double click on "some" select it, , click "select". "some" gets span around , becomes gray.
- then double click "some" again , hit "unselect". span goes away, , looking @ page content looks it's how started.
- double click "some" 1 last time, hit "select" , nothing gets selected.
this last step failing because start , end of selection reported being in different nodes on page, though sure looks same node, , reported being @ position 0 of both nodes. don't understand why it's behaving differently @ step 1, because looks me step 2 made page before step 1. after things weirder. there's similar, not quite same weird behavior in ie9. in chrome it's fine.
anyone know what's going on? why there 2 different text nodes in step 3? why offsets of selections 0 both, when selection seems extend 0 5, in step 1?
this code has other bugs , such (trying multiple selections fails, lots of other things don't work), i'm trying understand case starting point:
<head> <meta http-equiv="x-ua-compatible" content="ie=edge" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> </head> <div id="text">some test words</div> <button onclick='select()'>select</button> <button onclick='unselect()'>unselect</button> <div id="log"></div> <script type='text/javascript'> function log(message) { var log = document.getelementbyid("log"); log.innerhtml += message + "<br>"; } function logselection(selection) { var range = selection.getrangeat(0); var startcontainer = range.startcontainer; var endcontainer = range.endcontainer; log("node types: " + startcontainer.nodetype + ", " + endcontainer.nodetype + ", " + range.commonancestorcontainer.nodetype) log("startcontainer: " + (startcontainer.nodetype == 3 ? startcontainer.wholetext : startcontainer.innerhtml)); log("endcontainer: " + (endcontainer.nodetype == 3 ? endcontainer.wholetext : endcontainer.innerhtml)); log("commonancestorcontainer: " + (range.commonancestorcontainer.nodetype == 3 ? range.commonancestorcontainer.wholetext : escape(range.commonancestorcontainer.innerhtml))); log("equal: " + (startcontainer == endcontainer)); log("start: " + range.startoffset + " end: " + range.endoffset); } function select() { var selection = window.getselection(); var range = selection.getrangeat(0); var start = range.startoffset; var end = range.endoffset; var startcontainer = range.startcontainer; var endcontainer = range.endcontainer; logselection(selection); startcontainer.parentnode.innerhtml = startcontainer.wholetext.substring(0, start) + "<span style='background-color: gray'>" + startcontainer.wholetext.substring(start, end) + "</span>" + startcontainer.wholetext.substring(end, startcontainer.wholetext.length); selection.removeallranges(); log(""); } function unselect() { var selection = window.getselection(); logselection(selection); selection.removeallranges(); var textdiv = $("#text"); log("start: " + escape(textdiv.html())); textdiv.find("span").each(function () { var $span = $(this) $span.replacewith($span.html()); }); log("final: " + escape(textdiv.html())); log(""); } </script>
the chrome debugger can show assumptions go awry:
before taking action, dom:
after selection, here's how script changed it:
and finally, after "unselection":
this final display reveals problem: rather concatenating string value nodes, created new one. the parent <div>
contains 2 text nodes, selection span them.
to clear, script here problem:
textdiv.find("span").each(function () { var $span = $(this) $span.replacewith($span.html()); });
the replacement of span
span
's html()
string creating new text node.
Comments
Post a Comment