Files
WebODF/webodf/lib/ops/OpRemoveText.js
T

290 lines
12 KiB
JavaScript

/**
* @license
* Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
*
* @licstart
* The JavaScript code in this page is free software: you can redistribute it
* and/or modify it under the terms of the GNU Affero General Public License
* (GNU AGPL) as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. The code is distributed
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
*
* As additional permission under GNU AGPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* As a special exception to the AGPL, any HTML file which merely makes function
* calls to this code, and for that purpose includes it by reference shall be
* deemed a separate work for copyright law purposes. In addition, the copyright
* holders of this code give you permission to combine this code with free
* software libraries that are released under the GNU LGPL. You may copy and
* distribute such a system following the terms of the GNU AGPL for this code
* and the LGPL for the libraries. If you modify this code, you may extend this
* exception to your version of the code, but you are not obligated to do so.
* If you do not wish to do so, delete this exception statement from your
* version.
*
* This license applies to this entire compilation.
* @licend
* @source: http://www.webodf.org/
* @source: http://gitorious.org/webodf/webodf/
*/
/*global ops, runtime, odf, core*/
runtime.loadClass("odf.OdfUtils");
runtime.loadClass("core.DomUtils");
/**
* @constructor
* @implements ops.Operation
*/
ops.OpRemoveText = function OpRemoveText() {
"use strict";
var memberid, timestamp,
/**@type {number}*/
position,
/**@type {number}*/
length,
text, odfUtils,
domUtils,
editinfons = 'urn:webodf:names:editinfo';
this.init = function (data) {
runtime.assert(data.length >= 0, "OpRemoveText only supports positive lengths");
memberid = data.memberid;
timestamp = data.timestamp;
position = parseInt(data.position, 10);
length = parseInt(data.length, 10);
text = data.text;
odfUtils = new odf.OdfUtils();
domUtils = new core.DomUtils();
};
/**
* Returns true if the supplied node contains no text or ODF character elements
* @param {Node} node
* @returns {boolean}
*/
function isEmpty(node) {
var childNode;
if (!odfUtils.isParagraph(node) && (odfUtils.isGroupingElement(node) || odfUtils.isCharacterElement(node)) && node.textContent.length === 0) {
childNode = node.firstChild;
while (childNode) {
if (odfUtils.isCharacterElement(childNode)) {
return false;
}
childNode = childNode.nextSibling;
}
return true;
}
return false;
}
/**
* Merges the 'second' paragraph into the 'first' paragraph,
* appending the contents by default. If 'prepend' is true,
* the contents are prepended.
* @param {!Node} first
* @param {!Node} second
* @param {!boolean} prepend
* @return {undefined}
*/
function mergeParagraphs(first, second, prepend) {
var parent,
child,
insertionPoint;
child = prepend ? second.lastChild : second.firstChild;
if (prepend) {
insertionPoint = first.getElementsByTagNameNS(editinfons, 'editinfo')[0]
|| first.firstChild;
}
while (child) {
second.removeChild(child);
if (child.localName !== 'editinfo') {
if (isEmpty(child)) {
// Even though the node is devoid of ODT elements,
// it may still contain cursors and other things.
// In this case, merge all child elements into the parent
// before throwing the node away.
while(child.firstChild) {
first.insertBefore(child.firstChild, insertionPoint);
}
} else {
first.insertBefore(child, insertionPoint);
}
}
child = prepend ? second.lastChild : second.firstChild;
}
parent = second.parentNode;
parent.removeChild(second);
if (odfUtils.isListItem(parent) && parent.childNodes.length === 0) {
parent.parentNode.removeChild(parent);
}
}
/**
* Takes a given position and signed length, and returns an extended neighborhood
* that can be safely iterated on for deletion. The initial textnode might have
* extra characters alongwith the characters of deletion interest. This function
* deletes the target characters, and then computes a neighborhood that consists
* of the remaining text nodes, which is all that is needed for further deletion.
* @param {!ops.OdtDocument } odtDocument
* @param {!number} position
* @param {!number} length
* @return {!{paragraphElement: !Node, neighborhood: ?Array.<!Node>, remainingLength: !number}}
*/
function getPreprocessedNeighborhood(odtDocument, position, length) {
// First, upgrade whitespaces before any deletion, around the initial position
odtDocument.upgradeWhitespacesAtPosition(position);
var domPosition = odtDocument.getPositionInTextNode(position),
initialTextNode = domPosition.textNode,
initialTextOffset = domPosition.offset,
initialParentElement = initialTextNode.parentNode,
paragraphElement = odtDocument.getParagraphElement(initialParentElement),
remainingLength = length,
neighborhood,
difference;
if (initialTextNode.data === "") {
// Sometimes the initialTextNode returned by getPositionInTextNode is "" -
// for example when the position is beside a space, etc. These text nodes should be
// cleaned up, so remove it
initialParentElement.removeChild(initialTextNode);
neighborhood = odtDocument.getTextNeighborhood(position, length);
} else if (initialTextOffset !== 0) {
// If the deletion is around a cursor, this initialTextoffset is 0.
// But if not, the neighborhood may come with the first node containing extra
// characters than the ones we want to delete.
// To avoid that, we must first delete the target content from the initial text node,
// and then request the neighborhood, so that the first element in the neighborhood
// can be easily operated upon by the deletion loop
difference = remainingLength < (initialTextNode.length - initialTextOffset)
? remainingLength
: (initialTextNode.length - initialTextOffset);
initialTextNode.deleteData(initialTextOffset, difference);
// Now the new post-'collapse' position is the same as the old, because the
// data to the right is deleted.
// Upgrade the whitespaces there.
odtDocument.upgradeWhitespacesAtPosition(position);
neighborhood = odtDocument.getTextNeighborhood(position, length + difference);
remainingLength -= difference;
if (difference && neighborhood[0] === initialTextNode) {
// The initial text node has already been truncated.
// Therefore, forget it from the neighborhood
neighborhood.splice(0, 1);
}
} else {
neighborhood = odtDocument.getTextNeighborhood(position, length);
}
return {
paragraphElement: paragraphElement,
neighborhood: neighborhood,
remainingLength: remainingLength
};
}
this.execute = function (odtDocument) {
var neighborhood = [],
paragraphElement,
currentParagraphElement,
nextParagraphElement,
remainingLength,
currentTextNode = null,
currentParent = null,
currentLength,
preprocessedNeighborhood;
preprocessedNeighborhood = getPreprocessedNeighborhood(odtDocument, position, length);
neighborhood = preprocessedNeighborhood.neighborhood;
remainingLength = preprocessedNeighborhood.remainingLength;
paragraphElement = preprocessedNeighborhood.paragraphElement;
while (remainingLength) {
if (neighborhood[0]) {
currentTextNode = neighborhood[0];
currentParent = currentTextNode.parentNode;
currentLength = currentTextNode.length;
}
currentParagraphElement = odtDocument.getParagraphElement(currentTextNode);
if (paragraphElement !== currentParagraphElement) {
// If paragraph element of the current textnode from the neighborhood is different
// from the original paragraphElement, a merging must be performed.
nextParagraphElement = odtDocument.getNeighboringParagraph(paragraphElement, 1);
if (nextParagraphElement) {
// An empty paragraph should never win new childnodes. therefore, check if
// the walkable length of a paragraph is > 1 (non-empty).
if (odtDocument.getWalkableParagraphLength(paragraphElement) > 1) {
mergeParagraphs(paragraphElement, nextParagraphElement, false);
} else {
mergeParagraphs(nextParagraphElement, paragraphElement,/*prepend*/true);
paragraphElement = nextParagraphElement;
}
}
// A paragraph merging is worth 1 delete length
remainingLength -= 1;
} else {
if (currentLength <= remainingLength) {
currentParent.removeChild(currentTextNode);
// If the parent is a span that contains no further text
// after deletion, remove the span.
odtDocument.fixCursorPositions(memberid);
// If the current node is text:s or span and is empty, it should
// be removed.
while (isEmpty(currentParent)) {
currentParent = domUtils.mergeIntoParent(currentParent);
}
remainingLength -= currentLength;
neighborhood.splice(0, 1);
} else {
currentTextNode.deleteData(0, remainingLength);
// Now the new post-'collapse' position is the same as the old, because the
// data to the right is deleted.
// Upgrade the whitespaces there.
odtDocument.upgradeWhitespacesAtPosition(position);
remainingLength = 0;
}
}
}
odtDocument.fixCursorPositions(memberid);
odtDocument.getOdfCanvas().refreshSize();
odtDocument.emit(ops.OdtDocument.signalParagraphChanged, {
paragraphElement: paragraphElement,
memberId: memberid,
timeStamp: timestamp
});
odtDocument.emit(ops.OdtDocument.signalCursorMoved, odtDocument.getCursor(memberid));
return true;
};
this.spec = function () {
return {
optype: "RemoveText",
memberid: memberid,
timestamp: timestamp,
position: position,
length: length,
text: text
};
};
};