2011-10-28 22:15:21 -05:00
|
|
|
/* Copyright © 2011 by Neil Jenkins. Licensed under the MIT license. */
|
|
|
|
|
|
|
|
( function () {
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2012-03-29 00:26:01 -05:00
|
|
|
/*global Node, Text, Element, HTMLDocument, window, document */
|
2011-10-28 22:15:21 -05:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2012-03-29 00:26:01 -05:00
|
|
|
var implement = function ( constructors, props ) {
|
|
|
|
var l = constructors.length,
|
|
|
|
proto, prop;
|
|
|
|
while ( l-- ) {
|
|
|
|
proto = constructors[l].prototype;
|
|
|
|
for ( prop in props ) {
|
|
|
|
proto[ prop ] = props[ prop ];
|
|
|
|
}
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var every = function ( nodeList, fn ) {
|
|
|
|
var l = nodeList.length;
|
|
|
|
while ( l-- ) {
|
|
|
|
if ( !fn( nodeList[l] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var $False = function () { return false; };
|
|
|
|
var $True = function () { return true; };
|
|
|
|
|
|
|
|
var inlineNodeNames = /^(?:A(?:BBR|CRONYM)?|B(?:R|D[IO])?|C(?:ITE|ODE)|D(?:FN|EL)|EM|HR|I(?:NPUT|MG|NS)?|KBD|Q|R(?:P|T|UBY)|S(?:U[BP]|PAN|TRONG|AMP)|U)$/;
|
|
|
|
|
2011-11-01 19:55:08 -05:00
|
|
|
var leafNodeNames = {
|
|
|
|
BR: 1,
|
|
|
|
IMG: 1,
|
|
|
|
INPUT: 1
|
|
|
|
};
|
|
|
|
|
2012-03-07 23:19:43 -05:00
|
|
|
var swap = function ( node, node2 ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
var parent = node2.parentNode;
|
|
|
|
if ( parent ) {
|
|
|
|
parent.replaceChild( node, node2 );
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
|
|
|
|
var ELEMENT_NODE = 1, // Node.ELEMENT_NODE,
|
|
|
|
TEXT_NODE = 3, // Node.TEXT_NODE,
|
|
|
|
SHOW_ELEMENT = 1, // NodeFilter.SHOW_ELEMENT,
|
|
|
|
FILTER_ACCEPT = 1, // NodeFilter.FILTER_ACCEPT,
|
|
|
|
FILTER_SKIP = 3; // NodeFilter.FILTER_SKIP;
|
|
|
|
|
2011-11-01 22:02:18 -05:00
|
|
|
var isBlock = function ( el ) {
|
|
|
|
return el.isBlock() ? FILTER_ACCEPT : FILTER_SKIP;
|
2011-10-28 22:15:21 -05:00
|
|
|
};
|
|
|
|
var useTextFixer = !!( window.opera || window.ie );
|
|
|
|
|
2012-03-29 00:26:01 -05:00
|
|
|
implement( window.Node ? [ Node ] : [ Text, Element, HTMLDocument ], {
|
2011-10-28 22:15:21 -05:00
|
|
|
isInline: $False,
|
|
|
|
isBlock: $False,
|
|
|
|
isContainer: $False,
|
|
|
|
getPath: function () {
|
|
|
|
var parent = this.parentNode;
|
|
|
|
return parent ? parent.getPath() : '';
|
|
|
|
},
|
|
|
|
detach: function () {
|
|
|
|
var parent = this.parentNode;
|
|
|
|
if ( parent ) {
|
|
|
|
parent.removeChild( this );
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
replaceWith: function ( node ) {
|
|
|
|
swap( node, this );
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
replaces: function ( node ) {
|
|
|
|
swap( this, node );
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
nearest: function ( tag, attributes ) {
|
|
|
|
var parent = this.parentNode;
|
|
|
|
return parent ? parent.nearest( tag, attributes ) : null;
|
|
|
|
},
|
|
|
|
getPreviousBlock: function () {
|
2011-11-01 22:02:18 -05:00
|
|
|
var doc = this.ownerDocument,
|
|
|
|
walker = doc.createTreeWalker(
|
|
|
|
doc.body, SHOW_ELEMENT, isBlock, false );
|
|
|
|
walker.currentNode = this;
|
|
|
|
return walker.previousNode();
|
2011-10-28 22:15:21 -05:00
|
|
|
},
|
|
|
|
getNextBlock: function () {
|
2011-11-01 22:02:18 -05:00
|
|
|
var doc = this.ownerDocument,
|
|
|
|
walker = doc.createTreeWalker(
|
|
|
|
doc.body, SHOW_ELEMENT, isBlock, false );
|
|
|
|
walker.currentNode = this;
|
|
|
|
return walker.nextNode();
|
2011-10-28 22:15:21 -05:00
|
|
|
},
|
2011-11-04 00:53:12 -05:00
|
|
|
split: function ( node, stopNode ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
return node;
|
|
|
|
},
|
|
|
|
mergeContainers: function () {}
|
|
|
|
});
|
|
|
|
|
2012-03-29 00:26:01 -05:00
|
|
|
implement([ Text ], {
|
2011-11-01 19:55:08 -05:00
|
|
|
isLeaf: $True,
|
2011-11-14 01:55:40 -05:00
|
|
|
isInline: $True,
|
2011-10-28 22:15:21 -05:00
|
|
|
getLength: function () {
|
|
|
|
return this.length;
|
|
|
|
},
|
|
|
|
isLike: function ( node ) {
|
|
|
|
return node.nodeType === TEXT_NODE;
|
|
|
|
},
|
2011-11-04 00:53:12 -05:00
|
|
|
split: function ( offset, stopNode ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
var node = this;
|
2011-11-04 00:53:12 -05:00
|
|
|
if ( node === stopNode ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
return offset;
|
|
|
|
}
|
2011-11-04 00:53:12 -05:00
|
|
|
return node.parentNode.split( node.splitText( offset ), stopNode );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-03-29 00:26:01 -05:00
|
|
|
implement([ Element ], {
|
2011-11-01 19:55:08 -05:00
|
|
|
isLeaf: function () {
|
|
|
|
return !!leafNodeNames[ this.nodeName ];
|
|
|
|
},
|
2011-10-28 22:15:21 -05:00
|
|
|
isInline: function () {
|
|
|
|
return inlineNodeNames.test( this.nodeName );
|
|
|
|
},
|
|
|
|
isBlock: function () {
|
|
|
|
return !this.isInline() && every( this.childNodes, function ( child ) {
|
|
|
|
return child.isInline();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
isContainer: function () {
|
|
|
|
return !this.isInline() && !this.isBlock();
|
|
|
|
},
|
|
|
|
getLength: function () {
|
|
|
|
return this.childNodes.length;
|
|
|
|
},
|
2012-03-07 23:19:43 -05:00
|
|
|
getPath: function () {
|
2011-10-28 22:15:21 -05:00
|
|
|
var tag = this.nodeName;
|
|
|
|
if ( tag === 'BODY' ) {
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
var path = this.parentNode.getPath(),
|
|
|
|
id = this.id,
|
|
|
|
className = this.className.trim();
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
path += '>' + tag;
|
|
|
|
if ( id ) {
|
|
|
|
path += '#' + id;
|
|
|
|
}
|
|
|
|
if ( className ) {
|
|
|
|
className = className.split( /\s\s*/ );
|
|
|
|
className.sort();
|
|
|
|
path += '.';
|
|
|
|
path += className.join( '.' );
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
},
|
|
|
|
wraps: function ( node ) {
|
|
|
|
swap( this, node ).appendChild( node );
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
empty: function () {
|
|
|
|
var frag = this.ownerDocument.createDocumentFragment(),
|
|
|
|
l = this.childNodes.length;
|
|
|
|
while ( l-- ) {
|
|
|
|
frag.appendChild( this.firstChild );
|
|
|
|
}
|
|
|
|
return frag;
|
|
|
|
},
|
|
|
|
is: function ( tag, attributes ) {
|
|
|
|
if ( this.nodeName !== tag ) { return false; }
|
|
|
|
var attr;
|
|
|
|
for ( attr in attributes ) {
|
|
|
|
if ( this.getAttribute( attr ) !== attributes[ attr ] ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
nearest: function ( tag, attributes ) {
|
|
|
|
var el = this;
|
|
|
|
do {
|
|
|
|
if ( el.is( tag, attributes ) ) {
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
} while ( ( el = el.parentNode ) &&
|
|
|
|
( el.nodeType === ELEMENT_NODE ) );
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
isLike: function ( node ) {
|
|
|
|
return (
|
|
|
|
node.nodeType === ELEMENT_NODE &&
|
|
|
|
node.nodeName === this.nodeName &&
|
|
|
|
node.className === this.className &&
|
|
|
|
node.style.cssText === this.style.cssText
|
|
|
|
);
|
|
|
|
},
|
|
|
|
mergeInlines: function ( range ) {
|
|
|
|
var children = this.childNodes,
|
|
|
|
l = children.length,
|
|
|
|
frags = [],
|
|
|
|
child, prev, len;
|
|
|
|
while ( l-- ) {
|
|
|
|
child = children[l];
|
|
|
|
prev = l && children[ l - 1 ];
|
2011-11-02 00:43:42 -05:00
|
|
|
if ( l && child.isInline() && child.isLike( prev ) &&
|
|
|
|
!leafNodeNames[ child.nodeName ] ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
if ( range.startContainer === child ) {
|
|
|
|
range.startContainer = prev;
|
|
|
|
range.startOffset += prev.getLength();
|
|
|
|
}
|
|
|
|
if ( range.endContainer === child ) {
|
|
|
|
range.endContainer = prev;
|
|
|
|
range.endOffset += prev.getLength();
|
|
|
|
}
|
|
|
|
if ( range.startContainer === this ) {
|
|
|
|
if ( range.startOffset > l ) {
|
|
|
|
range.startOffset -= 1;
|
|
|
|
}
|
|
|
|
else if ( range.startOffset === l ) {
|
|
|
|
range.startContainer = prev;
|
|
|
|
range.startOffset = prev.getLength();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( range.endContainer === this ) {
|
|
|
|
if ( range.endOffset > l ) {
|
|
|
|
range.endOffset -= 1;
|
|
|
|
}
|
|
|
|
else if ( range.endOffset === l ) {
|
|
|
|
range.endContainer = prev;
|
|
|
|
range.endOffset = prev.getLength();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
child.detach();
|
|
|
|
if ( child.nodeType === TEXT_NODE ) {
|
|
|
|
prev.appendData( child.data );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
frags.push( child.empty() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( child.nodeType === ELEMENT_NODE ) {
|
|
|
|
len = frags.length;
|
|
|
|
while ( len-- ) {
|
|
|
|
child.appendChild( frags.pop() );
|
|
|
|
}
|
|
|
|
child.mergeInlines( range );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mergeWithBlock: function ( next, range ) {
|
|
|
|
var block = this,
|
|
|
|
container = next,
|
|
|
|
last, offset, _range;
|
|
|
|
while ( container.parentNode.childNodes.length === 1 ) {
|
|
|
|
container = container.parentNode;
|
|
|
|
}
|
|
|
|
container.detach();
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
offset = block.childNodes.length;
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
// Remove extra <BR> fixer if present.
|
|
|
|
last = block.lastChild;
|
|
|
|
if ( last && last.nodeName === 'BR' ) {
|
|
|
|
block.removeChild( last );
|
|
|
|
offset -= 1;
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
_range = {
|
|
|
|
startContainer: block,
|
|
|
|
startOffset: offset,
|
|
|
|
endContainer: block,
|
|
|
|
endOffset: offset
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
block.appendChild( next.empty() );
|
|
|
|
block.mergeInlines( _range );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
range.setStart(
|
|
|
|
_range.startContainer, _range.startOffset );
|
|
|
|
range.collapse( true );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
// Opera inserts a BR if you delete the last piece of text
|
|
|
|
// in a block-level element. Unfortunately, it then gets
|
|
|
|
// confused when setting the selection subsequently and
|
|
|
|
// refuses to accept the range that finishes just before the
|
|
|
|
// BR. Removing the BR fixes the bug.
|
|
|
|
// Steps to reproduce bug: Type "a-b-c" (where - is return)
|
|
|
|
// then backspace twice. The cursor goes to the top instead
|
|
|
|
// of after "b".
|
|
|
|
if ( window.opera && ( last = block.lastChild ) &&
|
|
|
|
last.nodeName === 'BR' ) {
|
|
|
|
block.removeChild( last );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mergeContainers: function () {
|
|
|
|
var prev = this.previousSibling,
|
|
|
|
first = this.firstChild;
|
|
|
|
if ( prev && prev.isLike( this ) && prev.isContainer() ) {
|
|
|
|
prev.appendChild( this.detach().empty() );
|
|
|
|
if ( first ) {
|
|
|
|
first.mergeContainers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2011-11-04 00:53:12 -05:00
|
|
|
split: function ( childNodeToSplitBefore, stopNode ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
var node = this;
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
if ( typeof( childNodeToSplitBefore ) === 'number' ) {
|
2011-11-04 00:53:12 -05:00
|
|
|
childNodeToSplitBefore =
|
|
|
|
childNodeToSplitBefore < node.childNodes.length ?
|
|
|
|
node.childNodes[ childNodeToSplitBefore ] : null;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-11-04 00:53:12 -05:00
|
|
|
if ( node === stopNode ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
return childNodeToSplitBefore;
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
// Clone node without children
|
|
|
|
var parent = node.parentNode,
|
|
|
|
clone = node.cloneNode( false ),
|
|
|
|
next;
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
// Add right-hand siblings to the clone
|
|
|
|
while ( childNodeToSplitBefore ) {
|
|
|
|
next = childNodeToSplitBefore.nextSibling;
|
|
|
|
clone.appendChild( childNodeToSplitBefore );
|
|
|
|
childNodeToSplitBefore = next;
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
// DO NOT NORMALISE. This may undo the fixCursor() call
|
|
|
|
// of a node lower down the tree!
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
// We need something in the element in order for the cursor to appear.
|
|
|
|
node.fixCursor();
|
|
|
|
clone.fixCursor();
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
// Inject clone after original node
|
|
|
|
if ( next = node.nextSibling ) {
|
|
|
|
parent.insertBefore( clone, next );
|
|
|
|
} else {
|
|
|
|
parent.appendChild( clone );
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
// Keep on splitting up the tree
|
2011-11-04 00:53:12 -05:00
|
|
|
return parent.split( clone, stopNode );
|
2011-10-28 22:15:21 -05:00
|
|
|
},
|
|
|
|
fixCursor: function () {
|
|
|
|
// In Webkit and Gecko, block level elements are collapsed and
|
|
|
|
// unfocussable if they have no content. To remedy this, a <BR> must be
|
|
|
|
// inserted. In Opera and IE, we just need a textnode in order for the
|
|
|
|
// cursor to appear.
|
|
|
|
var el = this,
|
|
|
|
doc = el.ownerDocument,
|
|
|
|
fixer, child;
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
if ( el.nodeName === 'BODY' ) {
|
2011-11-03 01:05:18 -05:00
|
|
|
if ( !( child = el.firstChild ) || child.nodeName === 'BR' ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
fixer = doc.createElement( 'DIV' );
|
2011-11-03 01:05:18 -05:00
|
|
|
if ( child ) {
|
|
|
|
el.replaceChild( fixer, child );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
el.appendChild( fixer );
|
|
|
|
}
|
2011-10-28 22:15:21 -05:00
|
|
|
el = fixer;
|
|
|
|
fixer = null;
|
|
|
|
}
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
if ( el.isInline() ) {
|
|
|
|
if ( !el.firstChild ) {
|
|
|
|
fixer = doc.createTextNode( /* isWebkit ? '\u200B' :*/ '' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( useTextFixer ) {
|
2011-11-01 19:55:08 -05:00
|
|
|
while ( !el.isLeaf() ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
child = el.firstChild;
|
|
|
|
if ( !child ) {
|
|
|
|
fixer = doc.createTextNode( '' );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
el = child;
|
|
|
|
}
|
2011-12-08 21:30:58 -05:00
|
|
|
if ( el.isLeaf() ) {
|
|
|
|
if ( el.nodeType !== TEXT_NODE ) {
|
|
|
|
el.parentNode.insertBefore(
|
|
|
|
doc.createTextNode( '' ), el );
|
|
|
|
}
|
|
|
|
// Opera will collapse the block element if it contains
|
|
|
|
// just spaces (but not if it contains no data at all).
|
|
|
|
else if ( /^ +$/.test( el.data ) ) {
|
|
|
|
el.data = '';
|
|
|
|
}
|
2011-11-01 19:55:08 -05:00
|
|
|
}
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2011-11-16 03:24:43 -05:00
|
|
|
else if ( !el.querySelector( 'BR' ) ) {
|
2011-10-28 22:15:21 -05:00
|
|
|
fixer = doc.createElement( 'BR' );
|
|
|
|
while ( ( child = el.lastElementChild ) && !child.isInline() ) {
|
|
|
|
el = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( fixer ) {
|
|
|
|
el.appendChild( fixer );
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2011-10-28 22:15:21 -05:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fix IE9's buggy implementation of Text#splitText.
|
|
|
|
// If the split is at the end of the node, it doesn't insert the newly split
|
|
|
|
// node into the document, and sets its value to undefined rather than ''.
|
|
|
|
// And even if the split is not at the end, the original node is removed from
|
|
|
|
// the document and replaced by another, rather than just having its data
|
|
|
|
// shortened.
|
|
|
|
if ( function () {
|
|
|
|
var div = document.createElement( 'div' ),
|
|
|
|
text = document.createTextNode( '12' );
|
|
|
|
div.appendChild( text );
|
|
|
|
text.splitText( 2 );
|
|
|
|
return div.childNodes.length !== 2;
|
|
|
|
}() ) {
|
|
|
|
Text.prototype.splitText = function ( offset ) {
|
|
|
|
var afterSplit = this.ownerDocument.createTextNode(
|
|
|
|
this.data.slice( offset ) ),
|
|
|
|
next = this.nextSibling,
|
|
|
|
parent = this.parentNode,
|
|
|
|
toDelete = this.length - offset;
|
|
|
|
if ( next ) {
|
|
|
|
parent.insertBefore( afterSplit, next );
|
|
|
|
} else {
|
|
|
|
parent.appendChild( afterSplit );
|
|
|
|
}
|
|
|
|
if ( toDelete ) {
|
|
|
|
this.deleteData( offset, toDelete );
|
|
|
|
}
|
|
|
|
return afterSplit;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}() );
|