1 | /*
|
---|
2 | * file: InsertTextAction.js
|
---|
3 | *
|
---|
4 | * @BEGINLICENSE
|
---|
5 | * Copyright 2010 Brook Novak (email : [email protected])
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with this program; if not, write to the Free Software
|
---|
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
17 | * @ENDLICENSE
|
---|
18 | */
|
---|
19 |
|
---|
20 | // @DEPENDS: UndoMan
|
---|
21 | bootstrap.provides("actions.InsertTextAction");
|
---|
22 |
|
---|
23 | _registerAction("InsertText", {
|
---|
24 |
|
---|
25 | exec : function(domNode, text, index) {
|
---|
26 |
|
---|
27 | debug.assert(index >= 0);
|
---|
28 |
|
---|
29 | debug.assert(
|
---|
30 | (domNode.nodeType == Node.TEXT_NODE && index <= domNode.nodeValue.length) ||
|
---|
31 | (domNode.nodeType == Node.ELEMENT_NODE && index <= 1)
|
---|
32 | );
|
---|
33 |
|
---|
34 | text = _parseHTMLString(_escapeTextToHTML(text)); // Escape into HTML and convert back into text with correct encoding
|
---|
35 |
|
---|
36 | var phRoot;
|
---|
37 |
|
---|
38 | // If the target node is a placeholder, then the placeholder should be replaced with the new text
|
---|
39 | if (de.doc.isESPlaceHolder(domNode, false) || de.doc.isMNPlaceHolder(domNode, false)) {
|
---|
40 | // Get the placeholder root node
|
---|
41 | phRoot = domNode;
|
---|
42 | while (!(de.doc.isESPlaceHolder(phRoot, true) || de.doc.isMNPlaceHolder(phRoot, true))) {
|
---|
43 | phRoot = phRoot.parentNode;
|
---|
44 | }
|
---|
45 | domNode = phRoot;
|
---|
46 | }
|
---|
47 |
|
---|
48 | if (domNode.nodeType == Node.TEXT_NODE) {
|
---|
49 |
|
---|
50 | debug.assert(!phRoot);
|
---|
51 |
|
---|
52 | // Insert the text
|
---|
53 | _execOp(_Operation.INSERT_TEXT, domNode, text, index);
|
---|
54 |
|
---|
55 | // Set the cursor to after the inserted text
|
---|
56 | if (this.flags & de.UndoMan.ExecFlag.UPDATE_SELECTION)
|
---|
57 | this.selAfter = {startNode : domNode, startIndex: index + text.length};
|
---|
58 |
|
---|
59 | } else {
|
---|
60 |
|
---|
61 | // Create a new text node and add it to the document
|
---|
62 | var textNode = document.createTextNode(text);
|
---|
63 |
|
---|
64 | _execOp(_Operation.INSERT_NODE, textNode, domNode.parentNode, _indexInParent(domNode) + (index == 0 ? 0 : 1));
|
---|
65 |
|
---|
66 | // Remove the placeholder from the document if there was any
|
---|
67 | if (phRoot)
|
---|
68 | _execOp(_Operation.REMOVE_NODE, phRoot);
|
---|
69 |
|
---|
70 | if (this.flags & de.UndoMan.ExecFlag.UPDATE_SELECTION)
|
---|
71 | this.selAfter = {startNode : textNode, startIndex: index + _nodeLength(textNode)};
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | // Normalize any inserted non breaking spaces
|
---|
76 | if (text.indexOf(_NBSP) != -1)
|
---|
77 | _normalizeNBSP(domNode.parentNode);
|
---|
78 |
|
---|
79 | }
|
---|
80 | });
|
---|
81 |
|
---|