~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/dom/Text.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
#include "Text.h"
 
24
 
 
25
#include "ExceptionCode.h"
 
26
#include "NodeRenderingContext.h"
 
27
#include "RenderCombineText.h"
 
28
#include "RenderText.h"
 
29
 
 
30
#if ENABLE(SVG)
 
31
#include "RenderSVGInlineText.h"
 
32
#include "SVGNames.h"
 
33
#endif
 
34
 
 
35
#include "StyleInheritedData.h"
 
36
#include <wtf/text/CString.h>
 
37
#include <wtf/text/StringBuilder.h>
 
38
 
 
39
using namespace std;
 
40
 
 
41
namespace WebCore {
 
42
 
 
43
PassRefPtr<Text> Text::create(Document* document, const String& data)
 
44
{
 
45
    return adoptRef(new Text(document, data, CreateText));
 
46
}
 
47
 
 
48
PassRefPtr<Text> Text::createEditingText(Document* document, const String& data)
 
49
{
 
50
    return adoptRef(new Text(document, data, CreateEditingText));
 
51
}
 
52
 
 
53
PassRefPtr<Text> Text::splitText(unsigned offset, ExceptionCode& ec)
 
54
{
 
55
    ec = 0;
 
56
 
 
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()) {
 
60
        ec = INDEX_SIZE_ERR;
 
61
        return 0;
 
62
    }
 
63
 
 
64
    String oldStr = data();
 
65
    RefPtr<Text> newText = virtualCreate(oldStr.substring(offset));
 
66
    setDataWithoutUpdate(oldStr.substring(0, offset));
 
67
 
 
68
    dispatchModifiedEvent(oldStr);
 
69
 
 
70
    if (parentNode())
 
71
        parentNode()->insertBefore(newText.get(), nextSibling(), ec);
 
72
    if (ec)
 
73
        return 0;
 
74
 
 
75
    if (parentNode())
 
76
        document()->textNodeSplit(this);
 
77
 
 
78
    if (renderer())
 
79
        toRenderText(renderer())->setTextWithOffset(dataImpl(), 0, oldStr.length());
 
80
 
 
81
    return newText.release();
 
82
}
 
83
 
 
84
static const Text* earliestLogicallyAdjacentTextNode(const Text* t)
 
85
{
 
86
    const Node* n = 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);
 
91
            continue;
 
92
        }
 
93
 
 
94
        // We would need to visit EntityReference child text nodes if they existed
 
95
        ASSERT(type != Node::ENTITY_REFERENCE_NODE || !n->hasChildNodes());
 
96
        break;
 
97
    }
 
98
    return t;
 
99
}
 
100
 
 
101
static const Text* latestLogicallyAdjacentTextNode(const Text* t)
 
102
{
 
103
    const Node* n = 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);
 
108
            continue;
 
109
        }
 
110
 
 
111
        // We would need to visit EntityReference child text nodes if they existed
 
112
        ASSERT(type != Node::ENTITY_REFERENCE_NODE || !n->hasChildNodes());
 
113
        break;
 
114
    }
 
115
    return t;
 
116
}
 
117
 
 
118
String Text::wholeText() const
 
119
{
 
120
    const Text* startText = earliestLogicallyAdjacentTextNode(this);
 
121
    const Text* endText = latestLogicallyAdjacentTextNode(this);
 
122
 
 
123
    Node* onePastEndText = endText->nextSibling();
 
124
    unsigned resultLength = 0;
 
125
    for (const Node* n = startText; n != onePastEndText; n = n->nextSibling()) {
 
126
        if (!n->isTextNode())
 
127
            continue;
 
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)
 
131
            CRASH();
 
132
        resultLength += data.length();
 
133
    }
 
134
    StringBuilder result;
 
135
    result.reserveCapacity(resultLength);
 
136
    for (const Node* n = startText; n != onePastEndText; n = n->nextSibling()) {
 
137
        if (!n->isTextNode())
 
138
            continue;
 
139
        const Text* t = static_cast<const Text*>(n);
 
140
        result.append(t->data());
 
141
    }
 
142
    ASSERT(result.length() == resultLength);
 
143
 
 
144
    return result.toString();
 
145
}
 
146
 
 
147
PassRefPtr<Text> Text::replaceWholeText(const String& newText, ExceptionCode&)
 
148
{
 
149
    // Remove all adjacent text nodes, and replace the contents of this one.
 
150
 
 
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));
 
154
 
 
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);
 
162
    }
 
163
 
 
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);
 
170
        }
 
171
    }
 
172
 
 
173
    if (newText.isEmpty()) {
 
174
        if (parent && parentNode() == parent)
 
175
            parent->removeChild(this, ignored);
 
176
        return 0;
 
177
    }
 
178
 
 
179
    setData(newText, ignored);
 
180
    return protectedThis.release();
 
181
}
 
182
 
 
183
String Text::nodeName() const
 
184
{
 
185
    return textAtom.string();
 
186
}
 
187
 
 
188
Node::NodeType Text::nodeType() const
 
189
{
 
190
    return TEXT_NODE;
 
191
}
 
192
 
 
193
PassRefPtr<Node> Text::cloneNode(bool /*deep*/)
 
194
{
 
195
    return create(document(), data());
 
196
}
 
197
 
 
198
bool Text::textRendererIsNeeded(const NodeRenderingContext& context)
 
199
{
 
200
    if (isEditingText())
 
201
        return true;
 
202
 
 
203
    if (!length())
 
204
        return false;
 
205
 
 
206
    if (context.style()->display() == NONE)
 
207
        return false;
 
208
 
 
209
    bool onlyWS = containsOnlyWhitespace();
 
210
    if (!onlyWS)
 
211
        return true;
 
212
 
 
213
    RenderObject* parent = context.parentRenderer();
 
214
    if (parent->isTable() || parent->isTableRow() || parent->isTableSection() || parent->isRenderTableCol() || parent->isFrameSet())
 
215
        return false;
 
216
    
 
217
    if (context.style()->preserveNewline()) // pre/pre-wrap/pre-line always make renderers.
 
218
        return true;
 
219
    
 
220
    RenderObject* prev = context.previousRenderer();
 
221
    if (prev && prev->isBR()) // <span><br/> <br/></span>
 
222
        return false;
 
223
        
 
224
    if (parent->isRenderInline()) {
 
225
        // <span><div/> <div/></span>
 
226
        if (prev && !prev->isInline())
 
227
            return false;
 
228
    } else {
 
229
        if (parent->isRenderBlock() && !parent->childrenInline() && (!prev || !prev->isInline()))
 
230
            return false;
 
231
        
 
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.
 
239
            return false;
 
240
    }
 
241
    
 
242
    return true;
 
243
}
 
244
 
 
245
#if ENABLE(SVG)
 
246
static bool isSVGShadowText(Text* text)
 
247
{
 
248
    Node* parentNode = text->parentNode();
 
249
    return parentNode->isShadowRoot() && toShadowRoot(parentNode)->host()->hasTagName(SVGNames::trefTag);
 
250
}
 
251
 
 
252
static bool isSVGText(Text* text)
 
253
{
 
254
    Node* parentOrHostNode = text->parentOrHostNode();
 
255
    return parentOrHostNode->isSVGElement() && !parentOrHostNode->hasTagName(SVGNames::foreignObjectTag);
 
256
}
 
257
#endif
 
258
 
 
259
void Text::createTextRendererIfNeeded()
 
260
{
 
261
    NodeRenderingContext(this).createRendererForTextIfNeeded();
 
262
}
 
263
 
 
264
RenderText* Text::createTextRenderer(RenderArena* arena, RenderStyle* style)
 
265
{
 
266
#if ENABLE(SVG)
 
267
    if (isSVGText(this) || isSVGShadowText(this))
 
268
        return new (arena) RenderSVGInlineText(this, dataImpl());
 
269
#endif
 
270
    if (style->hasTextCombine())
 
271
        return new (arena) RenderCombineText(this, dataImpl());
 
272
 
 
273
    return new (arena) RenderText(this, dataImpl());
 
274
}
 
275
 
 
276
void Text::attach()
 
277
{
 
278
    createTextRendererIfNeeded();
 
279
    CharacterData::attach();
 
280
}
 
281
 
 
282
void Text::recalcTextStyle(StyleChange change)
 
283
{
 
284
    RenderText* renderer = toRenderText(this->renderer());
 
285
    // The only time we have a renderer and our parent doesn't is if our parent
 
286
    // is a shadow root.
 
287
    if (change != NoChange && renderer) {
 
288
        if (!parentNode()->isShadowRoot())
 
289
            renderer->setStyle(parentNode()->renderer()->style());
 
290
#if ENABLE(SVG)
 
291
        else if (isSVGShadowText(this))
 
292
            renderer->setStyle(toShadowRoot(parentNode())->host()->renderer()->style());
 
293
#endif
 
294
    }
 
295
 
 
296
    if (needsStyleRecalc()) {
 
297
        if (renderer)
 
298
            renderer->setText(dataImpl());
 
299
        else
 
300
            reattach();
 
301
    }
 
302
    clearNeedsStyleRecalc();
 
303
}
 
304
 
 
305
void Text::updateTextRenderer(unsigned offsetOfReplacedData, unsigned lengthOfReplacedData)
 
306
{
 
307
    if (!attached())
 
308
        return;
 
309
    RenderText* textRenderer = toRenderText(renderer());
 
310
    if (!textRenderer || !textRendererIsNeeded(NodeRenderingContext(this, textRenderer->style()))) {
 
311
        reattach();
 
312
        return;
 
313
    }
 
314
    textRenderer->setTextWithOffset(dataImpl(), offsetOfReplacedData, lengthOfReplacedData);
 
315
}
 
316
 
 
317
bool Text::childTypeAllowed(NodeType) const
 
318
{
 
319
    return false;
 
320
}
 
321
 
 
322
PassRefPtr<Text> Text::virtualCreate(const String& data)
 
323
{
 
324
    return create(document(), data);
 
325
}
 
326
 
 
327
PassRefPtr<Text> Text::createWithLengthLimit(Document* document, const String& data, unsigned start, unsigned maxChars)
 
328
{
 
329
    unsigned dataLength = data.length();
 
330
 
 
331
    if (!start && dataLength <= maxChars)
 
332
        return create(document, data);
 
333
 
 
334
    RefPtr<Text> result = Text::create(document, String());
 
335
    result->parserAppendData(data, start, maxChars);
 
336
 
 
337
    return result;
 
338
}
 
339
 
 
340
#ifndef NDEBUG
 
341
void Text::formatForDebugger(char *buffer, unsigned length) const
 
342
{
 
343
    StringBuilder result;
 
344
    String s;
 
345
 
 
346
    result.append(nodeName());
 
347
 
 
348
    s = data();
 
349
    if (s.length() > 0) {
 
350
        if (result.length())
 
351
            result.appendLiteral("; ");
 
352
        result.appendLiteral("value=");
 
353
        result.append(s);
 
354
    }
 
355
 
 
356
    strncpy(buffer, result.toString().utf8().data(), length - 1);
 
357
}
 
358
#endif
 
359
 
 
360
} // namespace WebCore