Merge pull request #430 from peitschie/empty-text-nodes

Reduce the number of places that create empty text nodes
This commit is contained in:
Philip Peitsch
2014-02-28 21:03:34 +11:00
3 changed files with 93 additions and 6 deletions
+17 -5
View File
@@ -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;
}
+28 -1
View File
@@ -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
+48
View File
@@ -1594,4 +1594,52 @@
</ops>
<after><office:text><text:p><c:anchor c:memberId="Joe"/>AB<c:cursor c:memberId="Joe"/>C</text:p></office:text></after>
</test>
<test name="RemoveText_NearWhitespaceOverBoundary" isFailing="true">
<before><office:text><text:p><text:span>abc <text:s> </text:s></text:span><text:span>def</text:span></text:p></office:text></before>
<ops>
<op optype="AddCursor" memberid="Joe"/>
<op optype="MoveCursor" memberid="Joe" position="4" length="2"/>
<op optype="RemoveText" memberid="Joe" position="4" length="2"/>
</ops>
<after><office:text><text:p><text:span>abc <c:cursor c:memberId="Joe"/></text:span><text:span>ef</text:span></text:p></office:text></after>
</test>
<test name="RemoveText_NearWhitespaceOverBoundary_2">
<before><office:text><text:p><text:span>abc <text:s> </text:s></text:span><text:span>def</text:span></text:p></office:text></before>
<ops>
<op optype="AddCursor" memberid="Joe"/>
<op optype="MoveCursor" memberid="Joe" position="3" length="3"/>
<op optype="RemoveText" memberid="Joe" position="3" length="3"/>
</ops>
<after><office:text><text:p><text:span>abc<c:cursor c:memberId="Joe"/></text:span><text:span>ef</text:span></text:p></office:text></after>
</test>
<test name="RemoveText_NearWhitespaceOverBoundary_3" isFailing="true">
<before><office:text><text:p><text:span>abc </text:span><text:span>def</text:span></text:p></office:text></before>
<ops>
<op optype="AddCursor" memberid="Joe"/>
<op optype="MoveCursor" memberid="Joe" position="4" length="1"/>
<op optype="RemoveText" memberid="Joe" position="4" length="1"/>
</ops>
<after><office:text><text:p><text:span>abc <c:cursor c:memberId="Joe"/></text:span><text:span>ef</text:span></text:p></office:text></after>
</test>
<test name="RemoveText_NearWhitespaceOverBoundary_4">
<!-- This test has a problem with trailing whitespace not being correctly downgraded.
When the trailing spaces become correctly downgraded, this test can be removed and
the following will replace it -->
<before><office:text><text:p><text:span>abc</text:span><text:span>d <text:s> </text:s>ef</text:span></text:p></office:text></before>
<ops>
<op optype="AddCursor" memberid="Joe"/>
<op optype="MoveCursor" memberid="Joe" position="3" length="1"/>
<op optype="RemoveText" memberid="Joe" position="3" length="1"/>
</ops>
<after><office:text><text:p><text:span>abc<c:cursor c:memberId="Joe"/></text:span><text:span><text:s> </text:s><text:s> </text:s>ef</text:span></text:p></office:text></after>
</test>
<test name="RemoveText_NearWhitespaceOverBoundary_4_Ideal" isFailing="true">
<before><office:text><text:p><text:span>abc</text:span><text:span>d <text:s> </text:s>ef</text:span></text:p></office:text></before>
<ops>
<op optype="AddCursor" memberid="Joe"/>
<op optype="MoveCursor" memberid="Joe" position="3" length="1"/>
<op optype="RemoveText" memberid="Joe" position="3" length="1"/>
</ops>
<after><office:text><text:p><text:span>abc<c:cursor c:memberId="Joe"/></text:span><text:span> <text:s> </text:s>ef</text:span></text:p></office:text></after>
</test>
</tests>