!function(e,n){"function"==typeof define&&define.amd?define(["./rangy-core"],e):"undefined"!=typeof module&&"object"==typeof exports?module.exports=e(require("rangy")):e(n.rangy)}(function(e){return e.createModule("SaveRestore",["WrappedSelection"],function(d,s){var o=d.dom,i=o.removeNode,l=d.Selection.isDirectionBackward,a="\ufeff";function c(e,n){return(n||document).getElementById(e)}function u(e,n){var r="selectionBoundary_"+ +new Date+"_"+(""+Math.random()).slice(2),t=o.getDocument(e.startContainer),e=e.cloneRange();return e.collapse(n),(n=t.createElement("span")).id=r,n.style.lineHeight="0",n.style.display="none",n.className="rangySelectionBoundary",n.appendChild(t.createTextNode(a)),e.insertNode(n),n}function f(e,n,r,t){r=c(r,e);r?(n[t?"setStartBefore":"setEndBefore"](r),i(r)):s.warn("Marker element has been removed. Cannot restore selection.")}function g(e,n){return n.compareBoundaryPoints(e.START_TO_START,e)}function m(e,n){var r,t=d.DomRange.getRangeDocument(e),o=e.toString(),n=l(n);return e.collapsed?{document:t,markerId:(r=u(e,!1)).id,collapsed:!0}:(r=u(e,!1),{document:t,startMarkerId:u(e,!0).id,endMarkerId:r.id,collapsed:!1,backward:n,toString:function(){return"original text: '"+o+"', new text: '"+e.toString()+"'"}})}function t(e,n){var r,t,o=e.document,a=(void 0===n&&(n=!0),d.createRange(o));return e.collapsed?(r=c(e.markerId,o))?(r.style.display="inline",(t=r.previousSibling)&&3==t.nodeType?(i(r),a.collapseToPoint(t,t.length)):(a.collapseBefore(r),i(r))):s.warn("Marker element has been removed. Cannot restore selection."):(f(o,a,e.startMarkerId,!0),f(o,a,e.endMarkerId,!1)),n&&a.normalizeBoundaries(),a}function p(e,n){var r,t,o=[],a=l(n);(e=e.slice(0)).sort(g);for(var s=0,i=e.length;s<i;++s)o[s]=m(e[s],a);for(s=i-1;0<=s;--s)r=e[s],t=d.DomRange.getRangeDocument(r),r.collapsed?r.collapseAfter(c(o[s].markerId,t)):(r.setEndBefore(c(o[s].endMarkerId,t)),r.setStartAfter(c(o[s].startMarkerId,t)));return o}function v(e){for(var n=[],r=e.length-1;0<=r;r--)n[r]=t(e[r],!0);return n}function k(e,n){n=c(n,e);n&&i(n)}d.util.extend(d,{saveRange:m,restoreRange:t,saveRanges:p,restoreRanges:v,saveSelection:function(e){if(!d.isSelectionValid(e))return s.warn("Cannot save selection. This usually happens when the selection is collapsed and the selection document has lost focus."),null;var n=d.getSelection(e),r=n.getAllRanges(),t=1==r.length&&n.isBackward(),o=p(r,t);return t?n.setSingleRange(r[0],t):n.setRanges(r),{win:e,rangeInfos:o,restored:!1}},restoreSelection:function(e,n){var r,t,o;e.restored||(r=e.rangeInfos,t=d.getSelection(e.win),o=v(r),1==r.length&&n&&d.features.selectionHasExtend&&r[0].backward?(t.removeAllRanges(),t.addRange(o[0],!0)):t.setRanges(o),e.restored=!0)},removeMarkerElement:k,removeMarkers:function(e){for(var n,r=e.rangeInfos,t=0,o=r.length;t<o;++t)(n=r[t]).collapsed?k(e.doc,n.markerId):(k(e.doc,n.startMarkerId),k(e.doc,n.endMarkerId))}})}),e},this);