2012-04-30 23:36:02 +00:00
|
|
|
/**
|
2012-04-30 23:58:41 +00:00
|
|
|
* ContentEditable namespace.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 23:36:02 +00:00
|
|
|
* All classes and functions will be attached to this object to keep the global namespace clean.
|
|
|
|
*/
|
|
|
|
ve.ce = {
|
2012-05-31 22:20:58 +00:00
|
|
|
//'nodeFactory': Initialized in ve.ce.NodeFactory.js
|
2012-04-30 23:36:02 +00:00
|
|
|
};
|
2012-05-02 19:28:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RegExp pattern for matching all whitespaces in HTML text.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-02 19:28:27 +00:00
|
|
|
* \u0020 (32) space
|
|
|
|
* \u00A0 (160) non-breaking space
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-02 19:28:27 +00:00
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
*/
|
|
|
|
ve.ce.whitespacePattern = /[\u0020\u00A0]/g;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the plain text of a DOM element.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-02 19:28:27 +00:00
|
|
|
* In the returned string only the contents of text nodes are included.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-02 19:28:27 +00:00
|
|
|
* TODO: The idea of using this method over jQuery's .text() was that it will not traverse into
|
|
|
|
* elements that are not contentEditable, however this appears to be missing.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-02 19:28:27 +00:00
|
|
|
* @static
|
|
|
|
* @member
|
2012-05-02 19:54:42 +00:00
|
|
|
* @param {DOMElement} element DOM element to get text of
|
|
|
|
* @returns {String} Plain text of DOM element
|
2012-05-02 19:28:27 +00:00
|
|
|
*/
|
2012-05-02 19:54:42 +00:00
|
|
|
ve.ce.getDomText = function( element ) {
|
2012-06-14 05:04:52 +00:00
|
|
|
var func = function( element ) {
|
|
|
|
var nodeType = element.nodeType,
|
|
|
|
text = '';
|
|
|
|
if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
|
|
|
|
// Use textContent || innerText for elements
|
|
|
|
if ( typeof element.textContent === 'string' ) {
|
|
|
|
return element.textContent;
|
|
|
|
} else if ( typeof element.innerText === 'string' ) {
|
|
|
|
// Replace IE's carriage returns
|
|
|
|
return element.innerText.replace( /\r\n/g, '' );
|
|
|
|
} else {
|
|
|
|
// Traverse it's children
|
|
|
|
for ( element = element.firstChild; element; element = element.nextSibling) {
|
|
|
|
text += func( element );
|
|
|
|
}
|
2012-05-02 19:28:27 +00:00
|
|
|
}
|
2012-06-14 05:04:52 +00:00
|
|
|
} else if ( nodeType === 3 || nodeType === 4 ) {
|
|
|
|
return element.nodeValue;
|
2012-05-02 19:28:27 +00:00
|
|
|
}
|
2012-06-14 05:04:52 +00:00
|
|
|
return text;
|
2012-05-02 19:28:27 +00:00
|
|
|
}
|
|
|
|
// Return the text, replacing spaces and non-breaking spaces with spaces?
|
|
|
|
// TODO: Why are we replacing spaces (\u0020) with spaces (' ')
|
2012-06-14 05:04:52 +00:00
|
|
|
return func( element ).replace( ve.ce.whitespacePattern, ' ' );
|
2012-05-02 19:28:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a hash of a DOM element's structure.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-02 19:28:27 +00:00
|
|
|
* In the returned string text nodes are repesented as "#" and elements are represented as "<type>"
|
|
|
|
* and "</type>" where "type" is their element name. This effectively generates an HTML
|
|
|
|
* serialization without any attributes or text contents. This can be used to observer structural
|
|
|
|
* changes.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-02 19:28:27 +00:00
|
|
|
* @static
|
|
|
|
* @member
|
2012-05-02 19:54:42 +00:00
|
|
|
* @param {DOMElement} element DOM element to get hash of
|
|
|
|
* @returns {String} Hash of DOM element
|
2012-05-02 19:28:27 +00:00
|
|
|
*/
|
2012-05-02 19:54:42 +00:00
|
|
|
ve.ce.getDomHash = function( element ) {
|
|
|
|
var nodeType = element.nodeType,
|
|
|
|
nodeName = element.nodeName,
|
2012-05-02 19:28:27 +00:00
|
|
|
hash = '';
|
|
|
|
|
|
|
|
if ( nodeType === 3 || nodeType === 4 ) {
|
|
|
|
return '#';
|
|
|
|
} else if ( nodeType === 1 || nodeType === 9 ) {
|
|
|
|
hash += '<' + nodeName + '>';
|
|
|
|
// Traverse it's children
|
2012-05-02 19:54:42 +00:00
|
|
|
for ( element = element.firstChild; element; element = element.nextSibling) {
|
|
|
|
hash += ve.ce.getDomHash( element );
|
2012-05-02 19:28:27 +00:00
|
|
|
}
|
|
|
|
hash += '</' + nodeName + '>';
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
};
|