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.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..815401f9 100644
--- a/webodf/tests/ops/operationtests.xml
+++ b/webodf/tests/ops/operationtests.xml
@@ -1594,4 +1594,52 @@
ABC
+
+ abc def
+
+
+
+
+
+ abc ef
+
+
+ abc def
+
+
+
+
+
+ abcef
+
+
+ abc def
+
+
+
+
+
+ abc ef
+
+
+
+ abcd ef
+
+
+
+
+
+ abc ef
+
+
+ abcd ef
+
+
+
+
+
+ abc ef
+