From ff6e83e6a12109643ef9f1fd3499f0e74f0f2f24 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 28 Feb 2014 18:56:12 +1100 Subject: [PATCH 1/2] Add checks for empty text nodes in OperationTests Empty text nodes should never be left behind by an operation. Adding these extra checks have caused some operations to fail. --- webodf/tests/ops/OperationTests.js | 29 ++++++++++++++++++++- webodf/tests/ops/operationtests.xml | 40 +++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/webodf/tests/ops/OperationTests.js b/webodf/tests/ops/OperationTests.js index 195ed814..c16c2c05 100644 --- a/webodf/tests/ops/OperationTests.js +++ b/webodf/tests/ops/OperationTests.js @@ -33,7 +33,7 @@ * @source: http://www.webodf.org/ * @source: https://github.com/kogmbh/WebODF/ */ -/*global Node, runtime, core, gui, ops, odf, xmldom*/ +/*global Node, NodeFilter, runtime, core, gui, ops, odf, xmldom*/ /** * @constructor @@ -143,6 +143,32 @@ ops.OperationTests = function OperationTests(runner) { } return true; } + + /** + * Returns true if the specified node is an empty text node + * @param {!Node} node + * @return {!number} + */ + function emptyNodes(node) { + if (node.nodeType === Node.TEXT_NODE && node.length === 0) { + return NodeFilter.FILTER_ACCEPT; + } + return NodeFilter.FILTER_REJECT; + } + + /** + * Check that there are no empty text nodes in the supplied rootElement + * @param {!Node} rootElement + * @return {undefined} + */ + function checkForEmptyTextNodes(rootElement) { + var walker = rootElement.ownerDocument.createTreeWalker(rootElement, NodeFilter.SHOW_TEXT, emptyNodes, false), + node; + node = walker.nextNode(); + if (node) { + r.testFailed("Empty text nodes were found"); + } + } function parseTest(name, node) { var hasSetup = node.getAttribute("hasSetup") === "true", isFailing = node.getAttribute("isFailing") === "true", @@ -265,6 +291,7 @@ ops.OperationTests = function OperationTests(runner) { if (metabefore) { t.odtDocument.emit(ops.OdtDocument.signalOperationEnd, op); } + checkForEmptyTextNodes(t.odtDocument.getCanvas().getElement()); } // check result diff --git a/webodf/tests/ops/operationtests.xml b/webodf/tests/ops/operationtests.xml index a6418df7..be3481bf 100644 --- a/webodf/tests/ops/operationtests.xml +++ b/webodf/tests/ops/operationtests.xml @@ -808,7 +808,7 @@ hello world - + hello world @@ -817,7 +817,7 @@ hello world - + hello world @@ -1594,4 +1594,40 @@ ABC + + abc def + + + + + + abc ef + + + abc def + + + + + + abcef + + + abc def + + + + + + abc ef + + + abcd ef + + + + + + abc ef + From e05974ef210ea1ade24e5c3d90f2cc87555aeb01 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 28 Feb 2014 19:01:35 +1100 Subject: [PATCH 2/2] Stop whitespace upgrade logic from creating empty text nodes --- webodf/lib/ops/OdtDocument.js | 22 +++++++++++++++++----- webodf/tests/ops/operationtests.xml | 18 +++++++++++++++--- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/webodf/lib/ops/OdtDocument.js b/webodf/lib/ops/OdtDocument.js index d3f26bb3..b0d9e45f 100644 --- a/webodf/lib/ops/OdtDocument.js +++ b/webodf/lib/ops/OdtDocument.js @@ -506,14 +506,26 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { function upgradeWhitespaceToElement(textNode, offset) { runtime.assert(textNode.data[offset] === ' ', "upgradeWhitespaceToElement: textNode.data[offset] should be a literal space"); - var space = textNode.ownerDocument.createElementNS(odf.Namespaces.textns, 'text:s'); + var space = textNode.ownerDocument.createElementNS(odf.Namespaces.textns, 'text:s'), + container = textNode.parentNode, + adjacentNode = textNode; + space.appendChild(textNode.ownerDocument.createTextNode(' ')); - textNode.deleteData(offset, 1); - if (offset > 0) { // Don't create an empty text node if the offset is 0... - textNode = /**@type {!Text}*/(textNode.splitText(offset)); + if (textNode.length === 1) { + // The space is the only element in this node. Can simply replace it + container.replaceChild(space, textNode); + } else { + textNode.deleteData(offset, 1); + if (offset > 0) { // Don't create an empty text node if the offset is 0... + if (offset < textNode.length) { + // Don't split if offset === textNode.length as this would add an empty text node after + textNode.splitText(offset); + } + adjacentNode = textNode.nextSibling; + } + container.insertBefore(space, adjacentNode); } - textNode.parentNode.insertBefore(space, textNode); return space; } diff --git a/webodf/tests/ops/operationtests.xml b/webodf/tests/ops/operationtests.xml index be3481bf..815401f9 100644 --- a/webodf/tests/ops/operationtests.xml +++ b/webodf/tests/ops/operationtests.xml @@ -808,7 +808,7 @@ hello world - + hello world @@ -817,7 +817,7 @@ hello world - + hello world @@ -1621,7 +1621,10 @@ abc ef - + + abcd ef @@ -1630,4 +1633,13 @@ abc ef + + abcd ef + + + + + + abc ef +