1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.io.PrintWriter;
18
19 import org.htmlunit.SgmlPage;
20 import org.htmlunit.html.impl.SelectionDelegate;
21 import org.htmlunit.html.impl.SimpleSelectionDelegate;
22 import org.htmlunit.util.StringUtils;
23 import org.w3c.dom.DOMException;
24 import org.w3c.dom.Text;
25
26
27
28
29
30
31
32
33
34
35
36
37 public class DomText extends DomCharacterData implements Text {
38
39 private SelectionDelegate selectionDelegate_;
40 private DoTypeProcessor doTypeProcessor_;
41
42
43 public static final String NODE_NAME = "#text";
44
45
46
47
48
49
50
51 public DomText(final SgmlPage page, final String data) {
52 super(page, data);
53 }
54
55
56
57
58 @Override
59 public DomText splitText(final int offset) {
60 if (offset < 0 || offset > getLength()) {
61 throw new IllegalArgumentException("offset: " + offset + " data.length: " + getLength());
62 }
63
64
65 final DomText newText = createSplitTextNode(offset);
66 setData(getData().substring(0, offset));
67
68
69 if (getParentNode() != null) {
70 getParentNode().insertBefore(newText, getNextSibling());
71 }
72 return newText;
73 }
74
75
76
77
78
79
80
81
82 protected DomText createSplitTextNode(final int offset) {
83 return new DomText(getPage(), getData().substring(offset));
84 }
85
86
87
88
89
90 @Override
91 public boolean isElementContentWhitespace() {
92 throw new UnsupportedOperationException("DomText.isElementContentWhitespace is not yet implemented.");
93 }
94
95
96
97
98 @Override
99 public String getWholeText() {
100
101
102 return getNodeValue();
103 }
104
105
106
107
108
109 @Override
110 public Text replaceWholeText(final String content) throws DOMException {
111 throw new UnsupportedOperationException("DomText.replaceWholeText is not yet implemented.");
112 }
113
114
115
116
117 @Override
118 public short getNodeType() {
119 return org.w3c.dom.Node.TEXT_NODE;
120 }
121
122
123
124
125 @Override
126 public String getNodeName() {
127 return NODE_NAME;
128 }
129
130
131
132
133 @Override
134 protected boolean printXml(final String indent, final boolean tagBefore, final PrintWriter printWriter) {
135 String data = getData();
136 boolean tag = tagBefore;
137 if (org.apache.commons.lang3.StringUtils.isNotBlank(data)) {
138 if (!(getParentNode() instanceof HtmlStyle) || !data.startsWith("<!--") || !data.endsWith("-->")) {
139 data = StringUtils.escapeXmlChars(data);
140 }
141 printWriter.print(data);
142 tag = false;
143 }
144 return printChildrenAsXml(indent, tag, printWriter);
145 }
146
147
148
149
150
151 @Override
152 public String toString() {
153 return asNormalizedText();
154 }
155
156
157
158
159
160
161
162 protected void doType(final char c, final HtmlElement htmlElement, final boolean lastType) {
163 initDoTypeProcessor();
164 doTypeProcessor_.doType(getData(), selectionDelegate_, c, htmlElement, lastType);
165 }
166
167
168
169
170
171
172
173
174 protected void doType(final int keyCode, final HtmlElement htmlElement, final boolean lastType) {
175 initDoTypeProcessor();
176 doTypeProcessor_.doType(getData(), selectionDelegate_, keyCode, htmlElement, lastType);
177 }
178
179 private void initDoTypeProcessor() {
180 if (selectionDelegate_ == null) {
181 selectionDelegate_ = new SimpleSelectionDelegate();
182 doTypeProcessor_ = new DoTypeProcessor(this);
183 }
184 }
185
186
187
188
189
190
191 protected boolean acceptChar(final char c) {
192
193
194 return (c < '\uE000' || c > '\uF8FF') && (c == ' ' || !Character.isWhitespace(c));
195 }
196
197
198
199
200 @Override
201 public DomNode cloneNode(final boolean deep) {
202 final DomText newnode = (DomText) super.cloneNode(deep);
203 selectionDelegate_ = new SimpleSelectionDelegate();
204 doTypeProcessor_ = new DoTypeProcessor(this);
205
206 return newnode;
207 }
208
209
210
211
212 public void moveSelectionToEnd() {
213 initDoTypeProcessor();
214 selectionDelegate_.setSelectionStart(getData().length());
215 }
216
217
218
219
220 @Override
221 public void setPrefix(final String prefix) {
222
223 }
224 }