Catrope has uploaded a new change for review.
https://gerrit.wikimedia.org/r/149920
Change subject: Generate only one pair of snowmen for multi-sibling nodes
......................................................................
Generate only one pair of snowmen for multi-sibling nodes
If you had a leaf node rendered as 3 siblings (i.e. $element.length = 3),
then ve.ce.getDomText() would return 6 snowman characters, but the
correct number is 2. Put in a guard against double-snowmanning these nodes.
Also clean up the surrounding code a bit:
* Use Node.FOO_NODE constants directly
* Get rid of check for Node.CDATA_SECTION_NODE (who uses that?!)
* Explicitly mention the word "snowman" in comments
Change-Id: Ibf64fc0d3d03c639b2ad0f434949e75a42cef9ef
---
M modules/ve/ce/ve.ce.js
1 file changed, 18 insertions(+), 16 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor
refs/changes/20/149920/1
diff --git a/modules/ve/ce/ve.ce.js b/modules/ve/ce/ve.ce.js
index 804e47e..9278bb9 100644
--- a/modules/ve/ce/ve.ce.js
+++ b/modules/ve/ce/ve.ce.js
@@ -32,8 +32,8 @@
* Gets the plain text of a DOM element (that is a node canContainContent ===
true)
*
* 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).
+ * non-editable elements are excluded (but replaced with the appropriate
number of snowman
+ * characters so the offsets match up with the linear model).
*
* @method
* @param {HTMLElement} element DOM element to get text of
@@ -42,15 +42,16 @@
ve.ce.getDomText = function ( element ) {
// Inspired by jQuery.text / Sizzle.getText
var func = function ( element ) {
- var nodeType = element.nodeType,
- text = '',
- numChars,
- $element = $( element );
+ var viewNode, olaf,
+ nodeType = element.nodeType,
+ $element = $( element ),
+ text = '';
- // Node.ELEMENT_NODE = 1
- // Node.DOCUMENT_NODE = 9
- // Node.DOCUMENT_FRAGMENT_NODE = 11
- if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
+ if (
+ nodeType === Node.ELEMENT_NODE ||
+ nodeType === Node.DOCUMENT_NODE ||
+ nodeType === Node.DOCUMENT_FRAGMENT_NODE
+ ) {
if ( $element.hasClass( 've-ce-branchNode-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
@@ -58,19 +59,20 @@
return '';
} else if ( $element.hasClass( 've-ce-leafNode' ) ) {
// For leaf nodes, don't return the content,
but return
- // the right amount of characters so the
offsets match up
- numChars = $element.data( 'view'
).getOuterLength();
+ // the right number of placeholder characters
so the offsets match up.
+ viewNode = $element.data( 'view' );
// \u2603 is the snowman character: ☃
- return new Array( numChars + 1 ).join( '\u2603'
);
+ olaf = new Array( viewNode.getOuterLength() + 1
).join( '\u2603' );
+ // Only return snowmen for the first element in
a sibling group: otherwise
+ // we'll double-count this node
+ return element === viewNode.$element[0] ? olaf
: '';
} else {
// Traverse its children
for ( element = element.firstChild; element;
element = element.nextSibling ) {
text += func( element );
}
}
- // Node.TEXT_NODE = 3
- // Node.CDATA_SECTION_NODE = 4 (historical, unused in HTML5)
- } else if ( nodeType === 3 || nodeType === 4 ) {
+ } else if ( nodeType === Node.TEXT_NODE ) {
return element.data;
}
return text;
--
To view, visit https://gerrit.wikimedia.org/r/149920
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibf64fc0d3d03c639b2ad0f434949e75a42cef9ef
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits