2
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3
* (C) 1999 Antti Koivisto (koivisto@kde.org)
4
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public License
17
* along with this library; see the file COPYING.LIB. If not, write to
18
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
* Boston, MA 02110-1301, USA.
25
#include "ExceptionCode.h"
26
#include "NodeRenderingContext.h"
27
#include "RenderCombineText.h"
28
#include "RenderText.h"
31
#include "RenderSVGInlineText.h"
35
#include "StyleInheritedData.h"
36
#include <wtf/text/CString.h>
37
#include <wtf/text/StringBuilder.h>
43
PassRefPtr<Text> Text::create(Document* document, const String& data)
45
return adoptRef(new Text(document, data, CreateText));
48
PassRefPtr<Text> Text::createEditingText(Document* document, const String& data)
50
return adoptRef(new Text(document, data, CreateEditingText));
53
PassRefPtr<Text> Text::splitText(unsigned offset, ExceptionCode& ec)
57
// INDEX_SIZE_ERR: Raised if the specified offset is negative or greater than
58
// the number of 16-bit units in data.
59
if (offset > length()) {
64
String oldStr = data();
65
RefPtr<Text> newText = virtualCreate(oldStr.substring(offset));
66
setDataWithoutUpdate(oldStr.substring(0, offset));
68
dispatchModifiedEvent(oldStr);
71
parentNode()->insertBefore(newText.get(), nextSibling(), ec);
76
document()->textNodeSplit(this);
79
toRenderText(renderer())->setTextWithOffset(dataImpl(), 0, oldStr.length());
81
return newText.release();
84
static const Text* earliestLogicallyAdjacentTextNode(const Text* t)
87
while ((n = n->previousSibling())) {
88
Node::NodeType type = n->nodeType();
89
if (type == Node::TEXT_NODE || type == Node::CDATA_SECTION_NODE) {
90
t = static_cast<const Text*>(n);
94
// We would need to visit EntityReference child text nodes if they existed
95
ASSERT(type != Node::ENTITY_REFERENCE_NODE || !n->hasChildNodes());
101
static const Text* latestLogicallyAdjacentTextNode(const Text* t)
104
while ((n = n->nextSibling())) {
105
Node::NodeType type = n->nodeType();
106
if (type == Node::TEXT_NODE || type == Node::CDATA_SECTION_NODE) {
107
t = static_cast<const Text*>(n);
111
// We would need to visit EntityReference child text nodes if they existed
112
ASSERT(type != Node::ENTITY_REFERENCE_NODE || !n->hasChildNodes());
118
String Text::wholeText() const
120
const Text* startText = earliestLogicallyAdjacentTextNode(this);
121
const Text* endText = latestLogicallyAdjacentTextNode(this);
123
Node* onePastEndText = endText->nextSibling();
124
unsigned resultLength = 0;
125
for (const Node* n = startText; n != onePastEndText; n = n->nextSibling()) {
126
if (!n->isTextNode())
128
const Text* t = static_cast<const Text*>(n);
129
const String& data = t->data();
130
if (std::numeric_limits<unsigned>::max() - data.length() < resultLength)
132
resultLength += data.length();
134
StringBuilder result;
135
result.reserveCapacity(resultLength);
136
for (const Node* n = startText; n != onePastEndText; n = n->nextSibling()) {
137
if (!n->isTextNode())
139
const Text* t = static_cast<const Text*>(n);
140
result.append(t->data());
142
ASSERT(result.length() == resultLength);
144
return result.toString();
147
PassRefPtr<Text> Text::replaceWholeText(const String& newText, ExceptionCode&)
149
// Remove all adjacent text nodes, and replace the contents of this one.
151
// Protect startText and endText against mutation event handlers removing the last ref
152
RefPtr<Text> startText = const_cast<Text*>(earliestLogicallyAdjacentTextNode(this));
153
RefPtr<Text> endText = const_cast<Text*>(latestLogicallyAdjacentTextNode(this));
155
RefPtr<Text> protectedThis(this); // Mutation event handlers could cause our last ref to go away
156
RefPtr<ContainerNode> parent = parentNode(); // Protect against mutation handlers moving this node during traversal
157
ExceptionCode ignored = 0;
158
for (RefPtr<Node> n = startText; n && n != this && n->isTextNode() && n->parentNode() == parent;) {
159
RefPtr<Node> nodeToRemove(n.release());
160
n = nodeToRemove->nextSibling();
161
parent->removeChild(nodeToRemove.get(), ignored);
164
if (this != endText) {
165
Node* onePastEndText = endText->nextSibling();
166
for (RefPtr<Node> n = nextSibling(); n && n != onePastEndText && n->isTextNode() && n->parentNode() == parent;) {
167
RefPtr<Node> nodeToRemove(n.release());
168
n = nodeToRemove->nextSibling();
169
parent->removeChild(nodeToRemove.get(), ignored);
173
if (newText.isEmpty()) {
174
if (parent && parentNode() == parent)
175
parent->removeChild(this, ignored);
179
setData(newText, ignored);
180
return protectedThis.release();
183
String Text::nodeName() const
185
return textAtom.string();
188
Node::NodeType Text::nodeType() const
193
PassRefPtr<Node> Text::cloneNode(bool /*deep*/)
195
return create(document(), data());
198
bool Text::textRendererIsNeeded(const NodeRenderingContext& context)
206
if (context.style()->display() == NONE)
209
bool onlyWS = containsOnlyWhitespace();
213
RenderObject* parent = context.parentRenderer();
214
if (parent->isTable() || parent->isTableRow() || parent->isTableSection() || parent->isRenderTableCol() || parent->isFrameSet())
217
if (context.style()->preserveNewline()) // pre/pre-wrap/pre-line always make renderers.
220
RenderObject* prev = context.previousRenderer();
221
if (prev && prev->isBR()) // <span><br/> <br/></span>
224
if (parent->isRenderInline()) {
225
// <span><div/> <div/></span>
226
if (prev && !prev->isInline())
229
if (parent->isRenderBlock() && !parent->childrenInline() && (!prev || !prev->isInline()))
232
RenderObject* first = parent->firstChild();
233
while (first && first->isFloatingOrOutOfFlowPositioned())
234
first = first->nextSibling();
235
RenderObject* next = context.nextRenderer();
236
if (!first || next == first)
237
// Whitespace at the start of a block just goes away. Don't even
238
// make a render object for this text.
246
static bool isSVGShadowText(Text* text)
248
Node* parentNode = text->parentNode();
249
return parentNode->isShadowRoot() && toShadowRoot(parentNode)->host()->hasTagName(SVGNames::trefTag);
252
static bool isSVGText(Text* text)
254
Node* parentOrHostNode = text->parentOrHostNode();
255
return parentOrHostNode->isSVGElement() && !parentOrHostNode->hasTagName(SVGNames::foreignObjectTag);
259
void Text::createTextRendererIfNeeded()
261
NodeRenderingContext(this).createRendererForTextIfNeeded();
264
RenderText* Text::createTextRenderer(RenderArena* arena, RenderStyle* style)
267
if (isSVGText(this) || isSVGShadowText(this))
268
return new (arena) RenderSVGInlineText(this, dataImpl());
270
if (style->hasTextCombine())
271
return new (arena) RenderCombineText(this, dataImpl());
273
return new (arena) RenderText(this, dataImpl());
278
createTextRendererIfNeeded();
279
CharacterData::attach();
282
void Text::recalcTextStyle(StyleChange change)
284
RenderText* renderer = toRenderText(this->renderer());
285
// The only time we have a renderer and our parent doesn't is if our parent
287
if (change != NoChange && renderer) {
288
if (!parentNode()->isShadowRoot())
289
renderer->setStyle(parentNode()->renderer()->style());
291
else if (isSVGShadowText(this))
292
renderer->setStyle(toShadowRoot(parentNode())->host()->renderer()->style());
296
if (needsStyleRecalc()) {
298
renderer->setText(dataImpl());
302
clearNeedsStyleRecalc();
305
void Text::updateTextRenderer(unsigned offsetOfReplacedData, unsigned lengthOfReplacedData)
309
RenderText* textRenderer = toRenderText(renderer());
310
if (!textRenderer || !textRendererIsNeeded(NodeRenderingContext(this, textRenderer->style()))) {
314
textRenderer->setTextWithOffset(dataImpl(), offsetOfReplacedData, lengthOfReplacedData);
317
bool Text::childTypeAllowed(NodeType) const
322
PassRefPtr<Text> Text::virtualCreate(const String& data)
324
return create(document(), data);
327
PassRefPtr<Text> Text::createWithLengthLimit(Document* document, const String& data, unsigned start, unsigned maxChars)
329
unsigned dataLength = data.length();
331
if (!start && dataLength <= maxChars)
332
return create(document, data);
334
RefPtr<Text> result = Text::create(document, String());
335
result->parserAppendData(data, start, maxChars);
341
void Text::formatForDebugger(char *buffer, unsigned length) const
343
StringBuilder result;
346
result.append(nodeName());
349
if (s.length() > 0) {
351
result.appendLiteral("; ");
352
result.appendLiteral("value=");
356
strncpy(buffer, result.toString().utf8().data(), length - 1);
360
} // namespace WebCore