-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrange.js
More file actions
2 lines (2 loc) · 3.77 KB
/
range.js
File metadata and controls
2 lines (2 loc) · 3.77 KB
1
2
/*! Mini-utils 2014-02-06 */
define(function(){var a,b,c;return b=function(a){var b;return b=a.nextSibling,a=b?b:a.parentNode},c=function(a){var b,c;for(c=0,b=a.previousSibling;b instanceof Text;)c+=b.length,b=b.previousSibling;return c},a=function(){function a(a,b){var c,d,e,f,g,h;null==b&&(b=!0),a?(a=a.cloneRange(),b&&(f=a.startContainer,g=a.startOffset,c=a.endContainer,d=a.endOffset,0!==d&&(c instanceof Text&&(c.splitText(d),d=c.length),h=c),0!==g&&(f instanceof Text?(e=f.splitText(g),g=0,f===h&&(h=e,d=h.length)):e=f),e&&a.setStart(e,g),h&&a.setEnd(h,d)),this._range=a):this._range=document.createRange()}return a.START=1,a.END=2,a.ACCEPT=3,a.REJECT=4,a.SKIP=5,a._range=null,a.clone_range=function(b){return new a(b._range.cloneRange())},a.prototype.select=function(a,b){return this.start(a).end(b),this},a.prototype.start=function(a){return this._range.setStart(a,0),this},a.prototype.end=function(a){var b;return a instanceof Text?b=a.length:a.hasChildNodes()&&(b=a.childNodes.length),this._range.setEnd(a,b),this},a.prototype.start_after=function(a){return this._range.setStartAfter(a),this},a.prototype.start_before=function(a){return this._range.setStartBefore(a),this},a.prototype.end_after=function(a){return this._range.setEndAfter(a),this},a.prototype.end_before=function(a){return this._range.setEndBefore(a),this},a.prototype.select_node=function(a){return this._range.selectNode(a),this},a.prototype.select_node_contents=function(a){return this._range.selectNodeContents(a),this},a.prototype.collapse=function(a){if(a!==this.START&&a!==this.END)throw"parameter isn't the start or the end of the range.";return this._range.collapse(a),this},a.prototype.clone=function(){return this._range.cloneContents()},a.prototype["delete"]=function(){return this._range.deleteContents(),this.dispose(),this},a.prototype.extract=function(){var a,b;return a=this._range.cloneRange(),b=a.extractContents(),a.detach(),this.start_after(this.from()),b},a.prototype.insert=function(a){return this._range.insertNode(a),this},a.prototype.dispose=function(){return this._range.detach(),this},a.prototype.text=function(){return this._range.toString()},a.prototype.single_node=function(){var a;return this._range.startContainer===(a=this._range.endContainer)&&a===this._range.commonAncestorContainer},a.prototype.from=function(){return this._range.startContainer},a.prototype.to=function(){return this._range.endContainer},a.prototype.ascendant=function(){return this._range.commonAncestorContainer},a.prototype.collapsed=function(){return this._range.collapsed},a.prototype.walk=function(c){var d,e,f,g;return null==c&&(c=function(){return a.ACCEPT}),f=[],e=this.from(),d=this.to(),g=null,function(){var h,i,j;for(h=e;j!==a.ACCEPT&&null!==e;){if(i=e,e===d||j===a.REJECT&&e instanceof HTMLElement&&e.contains(d)){e=null;break}if(e.hasChildNodes()&&j!==a.REJECT&&!e.contains(g))f.push(e),e=e.firstChild;else for(e=b(e);-1!==f.indexOf(e);)e=b(e);g=i,j=c(e)}return h}},a.prototype.normalize_ascendant=function(){var a,b,d,e,f,g,h;return d=this.single_node(),d||(b=this.from(),h=this.to(),g=c(b),a=c(h)+h.length),this.ascendant().normalize(),!d&&(b=this.from(),h=this.to(),f=b===h,f&&(a-=g),b instanceof Text&&g>0&&(e=b.splitText(g),this.start(e),f&&(h=e)),h instanceof Text&&a>0)?(h.splitText(a),this.end(h)):void 0},a.prototype.split_ascendant=function(b){var c,d,e,f,g,h,i,j,k,l;return c=b||this.ascendant(),k=c.parentNode,l=this.from(),e=this.to(),f=c.firstChild===l,h=c.lastChild===e,g=this.extract(),j=g.firstChild,i=g.lastChild,f&&h?k.replaceChild(g,c):f||h?f?(k.insertBefore(g,c),c.textContent.match(/^\s*$/)&&k.removeChild(c)):h&&k.insertBefore(g,c.nextSibling):(d=document.createElement(c.tagName),d.appendChild((new a).start_after(e).end(c.lastChild).extract()),k.insertBefore(d,c.nextSibling),k.insertBefore(g,c.nextSibling)),(new a).select(j,i)},a}()});