From 3cd6d6a5d130164a3cd8e8fcd72465286c5b27da Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Thu, 6 Feb 2014 20:27:39 +1100 Subject: [PATCH 1/4] Re-implement paragraph & document selection using step iterators Changes in adjustSelectionByNode logic fixes extend-by-paragraph problem (issue #335). --- webodf/lib/gui/SelectionController.js | 176 ++++++++++--------- webodf/tests/gui/SelectionControllerTests.js | 53 ++++++ 2 files changed, 150 insertions(+), 79 deletions(-) diff --git a/webodf/lib/gui/SelectionController.js b/webodf/lib/gui/SelectionController.js index 2e0dec14..70285168 100644 --- a/webodf/lib/gui/SelectionController.js +++ b/webodf/lib/gui/SelectionController.js @@ -567,125 +567,143 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { /** * @param {!number} direction -1 for beginning, 1 for end + * @param {!boolean} extend True to extend the selection * @param {!function(!Node):Node} getContainmentNode Returns a node container for the supplied node. * Usually this will be something like the parent paragraph or root the supplied node is within * @return {undefined} */ - function extendCursorToNodeBoundary(direction, getContainmentNode) { - var cursor = odtDocument.getCursor(inputMemberId), - node = getContainmentNode(cursor.getNode()), + function adjustSelectionByNode(direction, extend, getContainmentNode) { + var validStepFound = false, + cursor = odtDocument.getCursor(inputMemberId), + containmentNode, selection = rangeToSelection(cursor.getSelectedRange(), cursor.hasForwardSelection()), + rootElement = odtDocument.getRootElement(selection.focusNode), + stepIterator, newCursorSelection; - runtime.assert(Boolean(node), "SelectionController: Cursor outside root"); + runtime.assert(Boolean(rootElement), "SelectionController: Cursor outside root"); + stepIterator = odtDocument.createStepIterator(selection.focusNode, selection.focusOffset, [baseFilter, rootFilter], rootElement); + stepIterator.roundToClosestStep(); if (direction < 0) { - selection.focusNode = /**@type{!Node}*/(node); - selection.focusOffset = 0; + if (stepIterator.previousStep()) { + containmentNode = getContainmentNode(stepIterator.container()); + if (containmentNode) { + stepIterator.setPosition(/**@type{!Node}*/(containmentNode), 0); + // Round up to the first walkable step in the containment node + validStepFound = stepIterator.roundToNextStep(); + } + } } else { - selection.focusNode = /**@type{!Node}*/(node); - selection.focusOffset = node.childNodes.length; + if (stepIterator.nextStep()) { + containmentNode = getContainmentNode(stepIterator.container()); + if (containmentNode) { + stepIterator.setPosition(/**@type{!Node}*/(containmentNode), containmentNode.childNodes.length); + // Round down to the last walkable step in the containment node + validStepFound = stepIterator.roundToPreviousStep(); + } + } } - newCursorSelection = odtDocument.convertDomToCursorRange(selection, constrain(getContainmentNode)); - session.enqueue([createOpMoveCursor(newCursorSelection.position, newCursorSelection.length)]); - } - - /** - * @return {!boolean} - */ - function extendSelectionToParagraphStart() { - extendCursorToNodeBoundary(-1, odtDocument.getParagraphElement); - return true; - } - this.extendSelectionToParagraphStart = extendSelectionToParagraphStart; - - /** - * @return {!boolean} - */ - function extendSelectionToParagraphEnd() { - extendCursorToNodeBoundary(1, odtDocument.getParagraphElement); - return true; - } - this.extendSelectionToParagraphEnd = extendSelectionToParagraphEnd; - - /** - * @param {!number} direction -1 for beginning, 1 for end - * @return {!boolean} - */ - function moveCursorToRootBoundary(direction) { - var cursor = odtDocument.getCursor(inputMemberId), - root = odtDocument.getRootElement(cursor.getNode()), - newPosition; - - runtime.assert(Boolean(root), "SelectionController: Cursor outside root"); - if (direction < 0) { - // The anchor node will already be in a walkable position having just been retrieved from the cursor - // The rounding will only impact the new focus node - // Need to round up as (p, 0) is potentially before the first walkable position in the paragraph - newPosition = odtDocument.convertDomPointToCursorStep(root, 0, function (step) { - return step === ops.StepsTranslator.NEXT_STEP; - }); - } else { - // Default behaviour is to round down to the previous walkable step if (p, p.childNodes.length) isn't - // walkable. Either way, this still equates to moving to the last walkable step in the paragraph - newPosition = odtDocument.convertDomPointToCursorStep(root, root.childNodes.length); + if (validStepFound) { + selection.focusNode = stepIterator.container(); + selection.focusOffset = stepIterator.offset(); + if (!extend) { + selection.anchorNode = selection.focusNode; + selection.anchorOffset = selection.focusOffset; + } + newCursorSelection = odtDocument.convertDomToCursorRange(selection); + session.enqueue([createOpMoveCursor(newCursorSelection.position, newCursorSelection.length)]); } - session.enqueue([createOpMoveCursor(newPosition, 0)]); - return true; } /** * @return {!boolean} */ - function moveCursorToDocumentStart() { - moveCursorToRootBoundary(-1); + this.extendSelectionToParagraphStart = function() { + adjustSelectionByNode(-1, true, odtDocument.getParagraphElement); return true; - } - this.moveCursorToDocumentStart = moveCursorToDocumentStart; + }; /** * @return {!boolean} */ - function moveCursorToDocumentEnd() { - moveCursorToRootBoundary(1); + this.extendSelectionToParagraphEnd = function () { + adjustSelectionByNode(1, true, odtDocument.getParagraphElement); return true; - } - this.moveCursorToDocumentEnd = moveCursorToDocumentEnd; + }; /** * @return {!boolean} */ - function extendSelectionToDocumentStart() { - extendCursorToNodeBoundary(-1, odtDocument.getRootElement); + this.moveCursorToParagraphStart = function () { + adjustSelectionByNode(-1, false, odtDocument.getParagraphElement); return true; - } - this.extendSelectionToDocumentStart = extendSelectionToDocumentStart; + }; /** * @return {!boolean} */ - function extendSelectionToDocumentEnd() { - extendCursorToNodeBoundary(1, odtDocument.getRootElement); + this.moveCursorToParagraphEnd = function () { + adjustSelectionByNode(1, false, odtDocument.getParagraphElement); return true; - } - this.extendSelectionToDocumentEnd = extendSelectionToDocumentEnd; + }; + + /** + * @return {!boolean} + */ + this.moveCursorToDocumentStart = function () { + adjustSelectionByNode(-1, false, odtDocument.getRootElement); + return true; + }; + + /** + * @return {!boolean} + */ + this.moveCursorToDocumentEnd = function () { + adjustSelectionByNode(1, false, odtDocument.getRootElement); + return true; + }; + + /** + * @return {!boolean} + */ + this.extendSelectionToDocumentStart = function () { + adjustSelectionByNode(-1, true, odtDocument.getRootElement); + return true; + }; + + /** + * @return {!boolean} + */ + this.extendSelectionToDocumentEnd = function () { + adjustSelectionByNode(1, true, odtDocument.getRootElement); + return true; + }; /** * @return {!boolean} */ function extendSelectionToEntireDocument() { var cursor = odtDocument.getCursor(inputMemberId), - root = odtDocument.getRootElement(cursor.getNode()), - newSelection, + rootElement = odtDocument.getRootElement(cursor.getNode()), + anchorNode, + anchorOffset, + stepIterator, newCursorSelection; - runtime.assert(Boolean(root), "SelectionController: Cursor outside root"); - newSelection = { - anchorNode: root, - anchorOffset: 0, - focusNode: root, - focusOffset: root.childNodes.length - }; - newCursorSelection = odtDocument.convertDomToCursorRange(newSelection, constrain(odtDocument.getRootElement)); + runtime.assert(Boolean(rootElement), "SelectionController: Cursor outside root"); + stepIterator = odtDocument.createStepIterator(rootElement, 0, [baseFilter, rootFilter], rootElement); + stepIterator.roundToClosestStep(); + anchorNode = stepIterator.container(); + anchorOffset = stepIterator.offset(); + + stepIterator.setPosition(rootElement, rootElement.childNodes.length); + stepIterator.roundToClosestStep(); + newCursorSelection = odtDocument.convertDomToCursorRange({ + anchorNode: anchorNode, + anchorOffset: anchorOffset, + focusNode: stepIterator.container(), + focusOffset: stepIterator.offset() + }); session.enqueue([createOpMoveCursor(newCursorSelection.position, newCursorSelection.length)]); return true; } diff --git a/webodf/tests/gui/SelectionControllerTests.js b/webodf/tests/gui/SelectionControllerTests.js index 1d97ae25..2efbc94a 100644 --- a/webodf/tests/gui/SelectionControllerTests.js +++ b/webodf/tests/gui/SelectionControllerTests.js @@ -313,6 +313,55 @@ gui.SelectionControllerTests = function SelectionControllerTests(runner) { r.shouldBe(t, "t.position", "17"); } + function moveCursorToDocumentEnd_EndsBeforeAnnotation() { + t.position = undefined; + createOdtDocument("the quick red foxan"); + + t.selectionController.moveCursorToDocumentEnd(); + + if(t.session.operations.length > 0) { + t.position = t.session.operations[0].spec().position; + } + r.shouldBe(t, "t.position", "21"); + } + + function moveSelectionToParagraphEnd_OverAnnotation() { + var lastPosition, + loopGuard = new core.LoopWatchDog(100000, 100); // Don't care really how long this takes + t.movementPositions = []; + createOdtDocument("aban" + + "cde" + + "fgh"); + + do { + loopGuard.check(); + lastPosition = getCursorPosition().position; + t.movementPositions.push(lastPosition); + t.selectionController.moveCursorToParagraphEnd(); + } while (lastPosition !== getCursorPosition().position); + + r.shouldBe(t, "t.movementPositions", "[0, 6, 10, 14]"); + } + + function moveSelectionToParagraphStart_OverAnnotation() { + var lastPosition, + loopGuard = new core.LoopWatchDog(100000, 100); // Don't care really how long this takes + t.movementPositions = []; + createOdtDocument("anab" + + "cde" + + "fgh"); + t.selectionController.moveCursorToDocumentEnd(); + + do { + loopGuard.check(); + lastPosition = getCursorPosition().position; + t.movementPositions.push(lastPosition); + t.selectionController.moveCursorToParagraphStart(); + } while (lastPosition !== getCursorPosition().position); + + r.shouldBe(t, "t.movementPositions", "[14, 11, 7, 0]"); + } + function expandToWordBoundaries_CollapsedInWord() { var doc = createOdtDocument("one two three"), p = doc.getElementsByTagNameNS(textns, "p")[0], @@ -437,6 +486,10 @@ gui.SelectionControllerTests = function SelectionControllerTests(runner) { extendCursorPastWord_IsWithinWord, moveCursorToDocumentEnd_Simple, + moveCursorToDocumentEnd_EndsBeforeAnnotation, + + moveSelectionToParagraphEnd_OverAnnotation, + moveSelectionToParagraphStart_OverAnnotation, expandToWordBoundaries_CollapsedInWord, expandToWordBoundaries_CollasedAtWordStart, From 4a076886621a8d2369cb06ee4ea56d560ce24b50 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Sat, 8 Mar 2014 02:45:17 +1100 Subject: [PATCH 2/4] Use named constants for movement direction rather than magic numbers --- webodf/lib/gui/SelectionController.js | 68 +++++++++++++++------------ 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/webodf/lib/gui/SelectionController.js b/webodf/lib/gui/SelectionController.js index 70285168..31d4520e 100644 --- a/webodf/lib/gui/SelectionController.js +++ b/webodf/lib/gui/SelectionController.js @@ -52,7 +52,17 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { keyboardMovementsFilter = new core.PositionFilterChain(), rootFilter = odtDocument.createRootFilter(inputMemberId), TRAILING_SPACE = odf.WordBoundaryFilter.IncludeWhitespace.TRAILING, - LEADING_SPACE = odf.WordBoundaryFilter.IncludeWhitespace.LEADING; + LEADING_SPACE = odf.WordBoundaryFilter.IncludeWhitespace.LEADING, + /** + * @const + * @type {!number} + */ + PREVIOUS = -1, + /** + * @const + * @type {!number} + */ + NEXT = 1; /** * Create a new step iterator with the base Odt filter, and a root filter for the current input member. @@ -392,7 +402,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { this.extendSelectionToRight = extendSelectionToRight; /** - * @param {!number} direction -1 for upwards 1 for downwards + * @param {!number} direction PREVIOUS for upwards NEXT for downwards * @param {!boolean} extend * @return {undefined} */ @@ -413,7 +423,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function moveCursorUp() { - moveCursorByLine(-1, false); + moveCursorByLine(PREVIOUS, false); return true; } this.moveCursorUp = moveCursorUp; @@ -422,7 +432,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function moveCursorDown() { - moveCursorByLine(1, false); + moveCursorByLine(NEXT, false); return true; } this.moveCursorDown = moveCursorDown; @@ -431,7 +441,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function extendSelectionUp() { - moveCursorByLine(-1, true); + moveCursorByLine(PREVIOUS, true); return true; } this.extendSelectionUp = extendSelectionUp; @@ -440,13 +450,13 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function extendSelectionDown() { - moveCursorByLine(1, true); + moveCursorByLine(NEXT, true); return true; } this.extendSelectionDown = extendSelectionDown; /** - * @param {!number} direction -1 for beginning 1 for end + * @param {!number} direction PREVIOUS (-1) or NEXT (1) * @param {!boolean} extend * @return {undefined} */ @@ -463,7 +473,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { } /** - * @param {!number} direction -1 for left 1 for right + * @param {!number} direction PREVIOUS (-1) or NEXT (1) * @param {!boolean} extend whether extend the selection instead of moving the cursor * @return {undefined} */ @@ -474,10 +484,10 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { selectionUpdated, stepIterator = createWordBoundaryStepIterator(newSelection.focusNode, newSelection.focusOffset, TRAILING_SPACE); - if (direction >= 0) { - selectionUpdated = stepIterator.nextStep(); - } else { + if (direction === PREVIOUS) { selectionUpdated = stepIterator.previousStep(); + } else { + selectionUpdated = stepIterator.nextStep(); } if (selectionUpdated) { @@ -497,7 +507,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function moveCursorBeforeWord() { - moveCursorByWord(-1, false); + moveCursorByWord(PREVIOUS, false); return true; } this.moveCursorBeforeWord = moveCursorBeforeWord; @@ -506,7 +516,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function moveCursorPastWord() { - moveCursorByWord(1, false); + moveCursorByWord(NEXT, false); return true; } this.moveCursorPastWord = moveCursorPastWord; @@ -515,7 +525,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function extendSelectionBeforeWord() { - moveCursorByWord(-1, true); + moveCursorByWord(PREVIOUS, true); return true; } this.extendSelectionBeforeWord = extendSelectionBeforeWord; @@ -524,7 +534,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function extendSelectionPastWord() { - moveCursorByWord(1, true); + moveCursorByWord(NEXT, true); return true; } this.extendSelectionPastWord = extendSelectionPastWord; @@ -533,7 +543,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function moveCursorToLineStart() { - moveCursorToLineBoundary(-1, false); + moveCursorToLineBoundary(PREVIOUS, false); return true; } this.moveCursorToLineStart = moveCursorToLineStart; @@ -542,7 +552,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function moveCursorToLineEnd() { - moveCursorToLineBoundary(1, false); + moveCursorToLineBoundary(NEXT, false); return true; } this.moveCursorToLineEnd = moveCursorToLineEnd; @@ -551,7 +561,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function extendSelectionToLineStart() { - moveCursorToLineBoundary(-1, true); + moveCursorToLineBoundary(PREVIOUS, true); return true; } this.extendSelectionToLineStart = extendSelectionToLineStart; @@ -560,13 +570,13 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ function extendSelectionToLineEnd() { - moveCursorToLineBoundary(1, true); + moveCursorToLineBoundary(NEXT, true); return true; } this.extendSelectionToLineEnd = extendSelectionToLineEnd; /** - * @param {!number} direction -1 for beginning, 1 for end + * @param {!number} direction PREVIOUS (-1) or NEXT (1) * @param {!boolean} extend True to extend the selection * @param {!function(!Node):Node} getContainmentNode Returns a node container for the supplied node. * Usually this will be something like the parent paragraph or root the supplied node is within @@ -584,7 +594,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { runtime.assert(Boolean(rootElement), "SelectionController: Cursor outside root"); stepIterator = odtDocument.createStepIterator(selection.focusNode, selection.focusOffset, [baseFilter, rootFilter], rootElement); stepIterator.roundToClosestStep(); - if (direction < 0) { + if (direction === PREVIOUS) { if (stepIterator.previousStep()) { containmentNode = getContainmentNode(stepIterator.container()); if (containmentNode) { @@ -619,7 +629,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ this.extendSelectionToParagraphStart = function() { - adjustSelectionByNode(-1, true, odtDocument.getParagraphElement); + adjustSelectionByNode(PREVIOUS, true, odtDocument.getParagraphElement); return true; }; @@ -627,7 +637,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ this.extendSelectionToParagraphEnd = function () { - adjustSelectionByNode(1, true, odtDocument.getParagraphElement); + adjustSelectionByNode(NEXT, true, odtDocument.getParagraphElement); return true; }; @@ -635,7 +645,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ this.moveCursorToParagraphStart = function () { - adjustSelectionByNode(-1, false, odtDocument.getParagraphElement); + adjustSelectionByNode(PREVIOUS, false, odtDocument.getParagraphElement); return true; }; @@ -643,7 +653,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ this.moveCursorToParagraphEnd = function () { - adjustSelectionByNode(1, false, odtDocument.getParagraphElement); + adjustSelectionByNode(NEXT, false, odtDocument.getParagraphElement); return true; }; @@ -651,7 +661,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ this.moveCursorToDocumentStart = function () { - adjustSelectionByNode(-1, false, odtDocument.getRootElement); + adjustSelectionByNode(PREVIOUS, false, odtDocument.getRootElement); return true; }; @@ -659,7 +669,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ this.moveCursorToDocumentEnd = function () { - adjustSelectionByNode(1, false, odtDocument.getRootElement); + adjustSelectionByNode(NEXT, false, odtDocument.getRootElement); return true; }; @@ -667,7 +677,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ this.extendSelectionToDocumentStart = function () { - adjustSelectionByNode(-1, true, odtDocument.getRootElement); + adjustSelectionByNode(PREVIOUS, true, odtDocument.getRootElement); return true; }; @@ -675,7 +685,7 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!boolean} */ this.extendSelectionToDocumentEnd = function () { - adjustSelectionByNode(1, true, odtDocument.getRootElement); + adjustSelectionByNode(NEXT, true, odtDocument.getRootElement); return true; }; From 2af751acddbee1b88534c5de0e8ec43ad8cc4082 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 7 Mar 2014 01:17:30 +1100 Subject: [PATCH 3/4] Crop range selection to the anchor root Fix an unhandled case if the anchor and focus points of a range were in two separate roots. The select range logic will now clip the range to the closest valid edge within the anchor's root. --- webodf/lib/gui/SelectionController.js | 94 ++++++++++++++------ webodf/tests/gui/SelectionControllerTests.js | 32 +++++++ 2 files changed, 100 insertions(+), 26 deletions(-) diff --git a/webodf/lib/gui/SelectionController.js b/webodf/lib/gui/SelectionController.js index 31d4520e..760cf0ef 100644 --- a/webodf/lib/gui/SelectionController.js +++ b/webodf/lib/gui/SelectionController.js @@ -88,29 +88,6 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { return odtDocument.createStepIterator(node, offset, [baseFilter, rootFilter, wordBoundaryFilter], odtDocument.getRootElement(node)); } - - /** - * @param {function(!Node):Node} lookup - * @return {function(!Node,number):function(number,!Node,number):boolean} - */ - /*jslint unparam:true*/ - function constrain(lookup) { - /** - * @param {!Node} originalNode - * @return {function(number,!Node,number):boolean} - */ - return function (originalNode) { - var originalContainer = lookup(originalNode); - /** - * @param {number} step - * @param {!Node} node - */ - return function (step, node) { - return lookup(node) === originalContainer; - }; - }; - } - /*jslint unparam:false*/ /** * Derive a selection-type object from the provided cursor @@ -256,9 +233,64 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { this.expandToParagraphBoundaries = expandToParagraphBoundaries; /** + * Rounds to the closest available step inside the supplied root, and preferably + * inside the original paragraph the node and offset are within. If (node, offset) is + * outside the root, the closest root boundary is used instead. + * This function will assert if no valid step is found within the supplied root. + * + * @param {!Node} root Root to contain iteration within + * @param {!Array.} filters Position filters + * @param {!Range} range Range to modify + * @param {!boolean} modifyStart Set to true to modify the start container & offset. If false, the end + * container and offset will be modified instead. + * + * @return {undefined} + */ + function roundToClosestStep(root, filters, range, modifyStart) { + var stepIterator, + node, + offset; + + if (modifyStart) { + node = /**@type{!Node}*/(range.startContainer); + offset = range.startOffset; + } else { + node = /**@type{!Node}*/(range.endContainer); + offset = range.endOffset; + } + + if (!domUtils.containsNode(root, node)) { + if (domUtils.comparePoints(root, 0, node, offset) < 0) { + offset = 0; + } else { + offset = root.childNodes.length; + } + node = root; + } + stepIterator = odtDocument.createStepIterator(node, offset, filters, odfUtils.getParagraphElement(node) || root); + if (!stepIterator.roundToClosestStep()) { + runtime.assert(false, "No step found in requested range"); + } + if (modifyStart) { + range.setStart(stepIterator.container(), stepIterator.offset()); + } else { + range.setEnd(stepIterator.container(), stepIterator.offset()); + } + } + + /** + * Set the user's cursor to the specified selection. If the start and end containers are in different roots, + * the anchor's root constraint is used (the anchor is the startContainer for a forward selection, or the + * endContainer for a reverse selection). + * + * If both the range start and range end are outside of the canvas element, no operations are generated. + * * @param {!Range} range - * @param {!boolean} hasForwardSelection - * @param {number=} clickCount + * @param {!boolean} hasForwardSelection Set to true to indicate the range is from anchor (startContainer) to focus + * (endContainer) + * @param {number=} clickCount A value of 2 denotes expandToWordBoundaries, while a value of 3 and above will expand + * to paragraph boundaries. + * @return {undefined} */ function selectRange(range, hasForwardSelection, clickCount) { var canvasElement = odtDocument.getOdfCanvas().getElement(), @@ -267,6 +299,8 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { endInsideCanvas, existingSelection, newSelection, + anchorRoot, + filters = [baseFilter], op; startInsideCanvas = domUtils.containsNode(canvasElement, range.startContainer); @@ -284,8 +318,16 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { } } + if (hasForwardSelection) { + anchorRoot = odtDocument.getRootElement(/**@type{!Node}*/(range.startContainer)); + } else { + anchorRoot = odtDocument.getRootElement(/**@type{!Node}*/(range.endContainer)); + } + filters.push(odtDocument.createRootFilter(anchorRoot)); + roundToClosestStep(anchorRoot, filters, range, true); + roundToClosestStep(anchorRoot, filters, range, false); validSelection = rangeToSelection(range, hasForwardSelection); - newSelection = odtDocument.convertDomToCursorRange(validSelection, constrain(odfUtils.getParagraphElement)); + newSelection = odtDocument.convertDomToCursorRange(validSelection); existingSelection = odtDocument.getCursorSelection(inputMemberId); if (newSelection.position !== existingSelection.position || newSelection.length !== existingSelection.length) { op = createOpMoveCursor(newSelection.position, newSelection.length, ops.OdtCursor.RangeSelection); diff --git a/webodf/tests/gui/SelectionControllerTests.js b/webodf/tests/gui/SelectionControllerTests.js index 2efbc94a..bc6e24f6 100644 --- a/webodf/tests/gui/SelectionControllerTests.js +++ b/webodf/tests/gui/SelectionControllerTests.js @@ -45,6 +45,7 @@ gui.SelectionControllerTests = function SelectionControllerTests(runner) { t, testarea, textns = odf.Namespaces.textns, + officens = odf.Namespaces.officens, inputMemberId = "Joe"; /** @@ -362,6 +363,34 @@ gui.SelectionControllerTests = function SelectionControllerTests(runner) { r.shouldBe(t, "t.movementPositions", "[14, 11, 7, 0]"); } + function selectRange_BridgesMultipleRoots_IsConstrainedWithinAnchorRoot() { + var doc = createOdtDocument("anab"), + range = testarea.ownerDocument.createRange(); + + range.setStart(doc.getElementsByTagNameNS(officens, "annotation")[0], 0); + range.setEnd(doc, doc.childNodes.length); + + t.selectionController.selectRange(range, true); + + t.position = getCursorPosition(); + r.shouldBe(t, "t.position.position", "1"); + r.shouldBe(t, "t.position.length", "2"); + } + + function selectRange_BridgesMultipleRoots_IsConstrainedWithinAnchorRoot_Reverse() { + var doc = createOdtDocument("aban"), + range = testarea.ownerDocument.createRange(); + + range.setStart(doc, 0); + range.setEnd(doc.getElementsByTagNameNS(officens, "annotation")[0], 1); + + t.selectionController.selectRange(range, false); + + t.position = getCursorPosition(); + r.shouldBe(t, "t.position.position", "5"); + r.shouldBe(t, "t.position.length", "-2"); + } + function expandToWordBoundaries_CollapsedInWord() { var doc = createOdtDocument("one two three"), p = doc.getElementsByTagNameNS(textns, "p")[0], @@ -491,6 +520,9 @@ gui.SelectionControllerTests = function SelectionControllerTests(runner) { moveSelectionToParagraphEnd_OverAnnotation, moveSelectionToParagraphStart_OverAnnotation, + selectRange_BridgesMultipleRoots_IsConstrainedWithinAnchorRoot, + selectRange_BridgesMultipleRoots_IsConstrainedWithinAnchorRoot_Reverse, + expandToWordBoundaries_CollapsedInWord, expandToWordBoundaries_CollasedAtWordStart, expandToWordBoundaries_CollasedAtWordEnd, From 48d06917b9e2cd8cd83d0855f828b0a2edd87f2e Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 7 Mar 2014 01:29:28 +1100 Subject: [PATCH 4/4] Use the start node rather than cursor for word boundary expansion The SelectionController interfaces allow word-expansion to occur on arbitrary ranges that may not be tied directly to the cursor (e.g., when used via the selectRange interface). This means the correct root to use in all circumstances is the starting node & offset. --- webodf/lib/gui/SelectionController.js | 7 +++--- webodf/tests/gui/SelectionControllerTests.js | 24 +++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/webodf/lib/gui/SelectionController.js b/webodf/lib/gui/SelectionController.js index 760cf0ef..93e5c133 100644 --- a/webodf/lib/gui/SelectionController.js +++ b/webodf/lib/gui/SelectionController.js @@ -84,9 +84,10 @@ gui.SelectionController = function SelectionController(session, inputMemberId) { * @return {!core.StepIterator} */ function createWordBoundaryStepIterator(node, offset, includeWhitespace) { - var wordBoundaryFilter = new odf.WordBoundaryFilter(odtDocument, includeWhitespace); - return odtDocument.createStepIterator(node, offset, [baseFilter, rootFilter, wordBoundaryFilter], - odtDocument.getRootElement(node)); + var wordBoundaryFilter = new odf.WordBoundaryFilter(odtDocument, includeWhitespace), + nodeRoot = odtDocument.getRootElement(node), + nodeRootFilter = odtDocument.createRootFilter(nodeRoot); + return odtDocument.createStepIterator(node, offset, [baseFilter, nodeRootFilter, wordBoundaryFilter], nodeRoot); } /** diff --git a/webodf/tests/gui/SelectionControllerTests.js b/webodf/tests/gui/SelectionControllerTests.js index bc6e24f6..8c94c7af 100644 --- a/webodf/tests/gui/SelectionControllerTests.js +++ b/webodf/tests/gui/SelectionControllerTests.js @@ -475,6 +475,27 @@ gui.SelectionControllerTests = function SelectionControllerTests(runner) { r.shouldBe(t, "t.selection.range.endOffset", "7"); } + function expandToWordBoundaries_RangeInDifferentRootToCursor() { + var doc = createOdtDocument("one two threefour five"), + p = doc.getElementsByTagNameNS(textns, "p")[1], + text = p.childNodes[0]; + + t.selection = t.selectionToRange({ + anchorNode: text, + anchorOffset: 0, + focusNode: text, + focusOffset: 1 + }); + + t.selectionController.expandToWordBoundaries(t.selection.range); + + t.text = text; + r.shouldBe(t, "t.selection.range.startContainer", "t.text"); + r.shouldBe(t, "t.selection.range.startOffset", "0"); + r.shouldBe(t, "t.selection.range.endContainer", "t.text"); + r.shouldBe(t, "t.selection.range.endOffset", "4"); + } + this.setUp = function () { testarea = core.UnitTest.provideTestAreaDiv(); t = { doc: testarea.ownerDocument }; @@ -526,7 +547,8 @@ gui.SelectionControllerTests = function SelectionControllerTests(runner) { expandToWordBoundaries_CollapsedInWord, expandToWordBoundaries_CollasedAtWordStart, expandToWordBoundaries_CollasedAtWordEnd, - expandToWordBoundaries_AlreadyAtWordBoundaries + expandToWordBoundaries_AlreadyAtWordBoundaries, + expandToWordBoundaries_RangeInDifferentRootToCursor ]); }; this.asyncTests = function () {