Divec has uploaded a new change for review.
https://gerrit.wikimedia.org/r/190141
Change subject: Preserve nodes better during IME tests
......................................................................
Preserve nodes better during IME tests
Change-Id: I16a9a7b1440752bec6ac46dbc51be4a6ec56b9cd
---
M tests/ce/ve.ce.TestRunner.js
1 file changed, 25 insertions(+), 15 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor
refs/changes/41/190141/1
diff --git a/tests/ce/ve.ce.TestRunner.js b/tests/ce/ve.ce.TestRunner.js
index 38b38a0..03c505b 100644
--- a/tests/ce/ve.ce.TestRunner.js
+++ b/tests/ce/ve.ce.TestRunner.js
@@ -197,23 +197,33 @@
* @param {string} text The new text
*/
ve.ce.TestRunner.prototype.changeText = function ( text ) {
- var extra,
- nativeRange = null;
- if ( this.nativeSelection.rangeCount > 0 ) {
- nativeRange = this.nativeSelection.getRangeAt( 0 );
- }
+ var focusNode, focusOffset;
+ // TODO: This method doesn't handle arbitrary text changes in a
paragraph
+ // with non-text nodes. It just works for the main cases that are
important
+ // in the existing IME tests.
- // TODO: Enable multi-paragraph testing. For now, assuming one
paragraph.
- // FIXME: renaming startNode to startContainer revealed failing tests
- if ( false && nativeRange && nativeRange.startContainer &&
text.indexOf( nativeRange.startContainer.textContent ) === 0 ) {
- // We're just appending
- // FIXME: str.slice( tr.length ) always produces an empty
string...
- extra = nativeRange.startContainer.textContent.slice(
nativeRange.startContainer.textContent.length );
- // This is fine IF startContainer is a TextNode
- nativeRange.startContainer.textContent += extra;
+ // Store the focus before the selection gets clobbered
+ focusNode = this.nativeSelection.focusNode;
+ focusOffset = this.nativeSelection.focusOffset;
+
+ // Empty all descendent text nodes
+ // This may clobber the selection, so the test had better call
changeSel next.
+ $( this.getParagraph() ).find( '*' ).addBack().contents().each(
function () {
+ if ( this.nodeType === Node.TEXT_NODE ) {
+ this.textContent = '';
+ }
+ } );
+
+ // Insert text at the focus
+ if ( focusNode === null ) {
+ throw new Error( 'No focus node' );
+ } else if ( focusNode.nodeType === Node.TEXT_NODE ) {
+ focusNode.textContent = text;
} else {
- // Wipe out the node
- this.getParagraph().textContent = text;
+ focusNode.insertBefore(
+ document.createTextNode( text ),
+ focusNode.childNodes[focusOffset]
+ );
}
this.lastText = text;
};
--
To view, visit https://gerrit.wikimedia.org/r/190141
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I16a9a7b1440752bec6ac46dbc51be4a6ec56b9cd
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Divec <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits