2012-02-06 23:50:56 +00:00
|
|
|
/**
|
2012-07-19 00:11:26 +00:00
|
|
|
* VisualEditor content editable namespace.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Namespace for all VisualEditor content editable classes, static methods and static properties.
|
2012-02-06 23:50:56 +00:00
|
|
|
*/
|
2012-03-06 22:39:43 +00:00
|
|
|
ve.ce = {
|
2012-06-20 01:20:28 +00:00
|
|
|
//'nodeFactory': Initialized in ve.ce.NodeFactory.js
|
|
|
|
};
|
|
|
|
|
2012-09-17 23:53:03 +00:00
|
|
|
/* Static Members */
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
/**
|
|
|
|
* RegExp pattern for matching all whitespaces in HTML text.
|
|
|
|
*
|
Remainder JSHint fixes on modules/ve/*
[jshint]
ce/ve.ce.Surface.js: line 670, col 9, Too many var statements.
ce/ve.ce.Surface.js: line 695, col 6, Missing semicolon.
ce/ve.ce.Surface.js: line 726, col 22, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 726, col 41, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 733, col 13, Too many var statements.
ce/ve.ce.Surface.js: line 734, col 24, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 1013, col 13, Too many var statements.
ce/ve.ce.Surface.js: line 1019, col 17, Too many var statements.
ce/ve.ce.Surface.js: line 1023, col 18, Too many ar statements.
ce/ve.ce.Surface.js: line 1027, col 13, Too many var statements.
dm/annotations/ve.dm.LinkAnnotation.js: line 70, col 52, Insecure '.'.
dm/ve.dm.Converter.js: line 383, col 29, Empty block.
dm/ve.dm.Converter.js: line 423, col 33, Empty block.
Commands:
* jshint .
* ack '(if|else|function|switch|for|while)\('
* Sublime Text 2:
Find(*): (if|else|function|switch|for|while)\(
Replace: $1 (
* ack ' ' -Q # double spaces, except in certain comments
Change-Id: I8e34bf2924bc8688fdf8acef08bbc4f6707e93be
2012-09-02 21:45:01 +00:00
|
|
|
* \u0020 (32) space
|
2012-06-20 01:20:28 +00:00
|
|
|
* \u00A0 (160) non-breaking space
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
*/
|
|
|
|
ve.ce.whitespacePattern = /[\u0020\u00A0]/g;
|
2012-02-10 22:19:12 +00:00
|
|
|
|
2012-09-17 23:53:03 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
/**
|
2012-08-22 18:08:11 +00:00
|
|
|
* Gets the plain text of a DOM element (that is a node canContainContent === true)
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2012-08-18 00:21:15 +00:00
|
|
|
* In the returned string only the contents of text nodes are included, and the contents of
|
|
|
|
* non-editable elements are excluded (but replaced with the appropriate number of characters
|
|
|
|
* so the offsets match up with the linear model).
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
* @param {DOMElement} element DOM element to get text of
|
|
|
|
* @returns {String} Plain text of DOM element
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.getDomText = function ( element ) {
|
|
|
|
var func = function ( element ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
var nodeType = element.nodeType,
|
2012-08-18 00:21:15 +00:00
|
|
|
text = '',
|
2012-08-22 18:08:11 +00:00
|
|
|
numChars,
|
|
|
|
$element = $( element );
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
|
2012-10-01 18:37:39 +00:00
|
|
|
if ( $element.hasClass( 've-ce-slug' ) ) {
|
|
|
|
// Slugs are not represented in the model at all, but they do
|
|
|
|
// contain a single nbsp/FEFF character in the DOM, so make sure
|
|
|
|
// that character isn't counted
|
|
|
|
return '';
|
|
|
|
} else if ( $element.hasClass( 've-ce-leafNode' ) ) {
|
2012-08-22 18:08:11 +00:00
|
|
|
// For leaf nodes, don't return the content, but return
|
2012-08-18 00:21:15 +00:00
|
|
|
// the right amount of characters so the offsets match up
|
2012-08-22 18:08:11 +00:00
|
|
|
numChars = $element.data( 'node' ).getOuterLength();
|
2012-08-18 00:21:15 +00:00
|
|
|
return new Array( numChars + 1 ).join( '\u2603' );
|
2012-03-09 22:45:22 +00:00
|
|
|
} else {
|
2012-06-20 23:01:02 +00:00
|
|
|
// Traverse its children
|
2012-10-01 18:37:39 +00:00
|
|
|
for ( element = element.firstChild; element; element = element.nextSibling ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
text += func( element );
|
2012-03-09 22:45:22 +00:00
|
|
|
}
|
2012-02-10 22:19:12 +00:00
|
|
|
}
|
2012-03-09 22:45:22 +00:00
|
|
|
} else if ( nodeType === 3 || nodeType === 4 ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
return element.nodeValue;
|
2012-02-10 22:19:12 +00:00
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
return text;
|
2012-06-20 23:01:02 +00:00
|
|
|
};
|
2012-06-20 01:20:28 +00:00
|
|
|
// Return the text, replacing spaces and non-breaking spaces with spaces?
|
|
|
|
// TODO: Why are we replacing spaces (\u0020) with spaces (' ')
|
|
|
|
return func( element ).replace( ve.ce.whitespacePattern, ' ' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a hash of a DOM element's structure.
|
|
|
|
*
|
2012-08-18 00:21:15 +00:00
|
|
|
* In the returned string text nodes are represented as "#" and elements are represented as "<type>"
|
2012-06-20 01:20:28 +00:00
|
|
|
* and "</type>" where "type" is their element name. This effectively generates an HTML
|
2012-08-18 00:21:15 +00:00
|
|
|
* serialization without any attributes or text contents. This can be used to observe structural
|
2012-06-20 01:20:28 +00:00
|
|
|
* changes.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
* @param {DOMElement} element DOM element to get hash of
|
|
|
|
* @returns {String} Hash of DOM element
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.getDomHash = function ( element ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
var nodeType = element.nodeType,
|
|
|
|
nodeName = element.nodeName,
|
|
|
|
hash = '';
|
2012-02-10 22:19:12 +00:00
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( nodeType === 3 || nodeType === 4 ) {
|
|
|
|
return '#';
|
|
|
|
} else if ( nodeType === 1 || nodeType === 9 ) {
|
|
|
|
hash += '<' + nodeName + '>';
|
2012-06-20 23:01:02 +00:00
|
|
|
// Traverse its children
|
2012-06-20 01:20:28 +00:00
|
|
|
for ( element = element.firstChild; element; element = element.nextSibling) {
|
|
|
|
hash += ve.ce.getDomHash( element );
|
|
|
|
}
|
|
|
|
hash += '</' + nodeName + '>';
|
2012-03-09 22:45:22 +00:00
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
return hash;
|
2012-03-09 22:45:22 +00:00
|
|
|
};
|
2012-10-04 20:59:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the linear offset from a given DOM node and offset within it.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
* @param {DOM Node} domNode DOM node
|
|
|
|
* @param {Integer} domOffset DOM offset within the DOM node
|
2012-10-20 00:17:38 +00:00
|
|
|
* @returns {Number} Linear model offset
|
2012-10-04 20:59:29 +00:00
|
|
|
*/
|
|
|
|
ve.ce.getOffset = function ( domNode, domOffset ) {
|
|
|
|
if ( domNode.nodeType === Node.TEXT_NODE ) {
|
|
|
|
return ve.ce.getOffsetFromTextNode( domNode, domOffset );
|
|
|
|
} else {
|
2012-10-20 00:17:38 +00:00
|
|
|
return ve.ce.getOffsetFromElementNode( domNode, domOffset );
|
2012-10-04 20:59:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the linear offset from a given text node and offset within it.
|
|
|
|
*
|
2012-10-20 00:17:38 +00:00
|
|
|
* @method
|
|
|
|
* @param {DOMElement} domNode DOM node
|
|
|
|
* @param {Number} domOffset DOM offset within the DOM Element
|
|
|
|
* @returns {Number} Linear model offset
|
2012-10-04 20:59:29 +00:00
|
|
|
*/
|
2012-10-20 00:17:38 +00:00
|
|
|
ve.ce.getOffsetFromTextNode = function ( domNode, domOffset ) {
|
|
|
|
var $node, nodeModel, current, stack, item, offset, $item;
|
2012-10-04 20:59:29 +00:00
|
|
|
|
2012-10-20 00:17:38 +00:00
|
|
|
$node = $( domNode ).closest(
|
2012-11-21 20:01:21 +00:00
|
|
|
'.ve-ce-branchNode, .ve-ce-leafNode'
|
2012-10-20 00:17:38 +00:00
|
|
|
);
|
|
|
|
nodeModel = $node.data( 'node' ).getModel();
|
2012-10-04 20:59:29 +00:00
|
|
|
|
|
|
|
if ( ! $node.hasClass( 've-ce-branchNode' ) ) {
|
2012-10-20 00:17:38 +00:00
|
|
|
return nodeModel.getOffset();
|
2012-10-04 20:59:29 +00:00
|
|
|
}
|
|
|
|
|
2012-10-20 00:17:38 +00:00
|
|
|
current = [$node.contents(), 0];
|
|
|
|
stack = [current];
|
2012-10-04 20:59:29 +00:00
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
while ( stack.length > 0 ) {
|
|
|
|
if ( current[1] >= current[0].length ) {
|
|
|
|
stack.pop();
|
|
|
|
current = stack[ stack.length - 1 ];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
item = current[0][current[1]];
|
|
|
|
if ( item.nodeType === Node.TEXT_NODE ) {
|
|
|
|
if ( item === domNode ) {
|
|
|
|
offset += domOffset;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
offset += item.textContent.length;
|
|
|
|
}
|
|
|
|
} else if ( item.nodeType === Node.ELEMENT_NODE ) {
|
|
|
|
$item = current[0].eq( current[1] );
|
|
|
|
if ( $item.hasClass( 've-ce-slug' ) ) {
|
|
|
|
if ( $item.contents()[0] === domNode ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if ( $item.hasClass( 've-ce-leafNode' ) ) {
|
2012-10-20 00:17:38 +00:00
|
|
|
offset += 2;
|
|
|
|
} else if ( $item.hasClass( 've-ce-branchNode' ) ) {
|
2012-10-04 20:59:29 +00:00
|
|
|
offset += $item.data( 'node' ).getOuterLength();
|
|
|
|
} else {
|
2012-10-20 00:17:38 +00:00
|
|
|
stack.push( [$item.contents(), 0 ] );
|
2012-10-04 20:59:29 +00:00
|
|
|
current[1]++;
|
2012-10-20 00:17:38 +00:00
|
|
|
current = stack[stack.length-1];
|
2012-10-04 20:59:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current[1]++;
|
|
|
|
}
|
2012-10-20 00:17:38 +00:00
|
|
|
return offset + nodeModel.getOffset() + ( nodeModel.isWrapped() ? 1 : 0 );
|
2012-10-04 20:59:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the linear offset from a given element node and offset within it.
|
|
|
|
*
|
2012-10-20 00:17:38 +00:00
|
|
|
* @method
|
|
|
|
* @param {DOMElement} domNode DOM node
|
|
|
|
* @param {Number} domOffset DOM offset within the DOM Element
|
|
|
|
* @param {Boolean} [addOuterLength] Use outer length, which includes wrappers if any exist
|
|
|
|
* @returns {Number} Linear model offset
|
2012-10-04 20:59:29 +00:00
|
|
|
*/
|
2012-10-20 00:17:38 +00:00
|
|
|
ve.ce.getOffsetFromElementNode = function ( domNode, domOffset, addOuterLength ) {
|
|
|
|
var $domNode = $( domNode ),
|
|
|
|
nodeModel,
|
|
|
|
node;
|
|
|
|
|
|
|
|
if ( $domNode.hasClass( 've-ce-slug' ) ) {
|
|
|
|
if ( $domNode.prev().length ) {
|
|
|
|
nodeModel = $domNode.prev().data( 'node' ).getModel();
|
|
|
|
return nodeModel.getOffset() + nodeModel.getOuterLength();
|
|
|
|
}
|
|
|
|
if ( $domNode.next().length ) {
|
|
|
|
nodeModel = $domNode.next().data( 'node' ).getModel();
|
|
|
|
return nodeModel.getOffset();
|
2012-10-04 20:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-20 00:17:38 +00:00
|
|
|
|
|
|
|
if ( domOffset === 0 ) {
|
|
|
|
node = $domNode.data( 'node' );
|
|
|
|
if ( node ) {
|
|
|
|
nodeModel = $domNode.data( 'node' ).getModel();
|
|
|
|
if ( addOuterLength === true ) {
|
|
|
|
return nodeModel.getOffset() + nodeModel.getOuterLength();
|
|
|
|
} else {
|
|
|
|
return nodeModel.getOffset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
node = $domNode.contents().last()[0];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
node = $domNode.contents()[ domOffset - 1 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( node.nodeType === Node.TEXT_NODE ) {
|
|
|
|
return ve.ce.getOffsetFromTextNode( node, node.length );
|
|
|
|
} else {
|
|
|
|
return ve.ce.getOffsetFromElementNode( node, 0, true );
|
|
|
|
}
|
2012-10-04 20:59:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the linear offset of a given slug
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
* @param {jQuery} $node jQuery slug selection
|
|
|
|
* @returns {Integer} Linear model offset
|
2012-10-22 22:52:19 +00:00
|
|
|
* @throws Error
|
2012-10-04 20:59:29 +00:00
|
|
|
*/
|
|
|
|
ve.ce.getOffsetOfSlug = function ( $node ) {
|
|
|
|
var model;
|
|
|
|
if ( $node.index() === 0 ) {
|
|
|
|
model = $node.parent().data( 'node' ).getModel();
|
|
|
|
return model.getOffset() + ( model.isWrapped() ? 1 : 0 );
|
|
|
|
} else if ( $node.prev().length ) {
|
|
|
|
model = $node.prev().data( 'node' ).getModel();
|
|
|
|
return model.getOffset() + model.getOuterLength();
|
|
|
|
} else {
|
2012-10-22 22:52:19 +00:00
|
|
|
throw new Error( 'Incorrect slug location' );
|
2012-10-04 20:59:29 +00:00
|
|
|
}
|
2012-10-20 00:17:38 +00:00
|
|
|
};
|