summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/rendering/RenderQuote.cpp
blob: 4523df8e42f54aa8c30e576396c06c2fa65409a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
 * Copyright (C) 2011 Nokia Inc.  All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "RenderQuote.h"

#include "Document.h"
#include "Element.h"
#include "HTMLElement.h"
#include "QuotesData.h"
#include "RenderStyle.h"
#include <algorithm>
#include <wtf/text/AtomicString.h>
#include <wtf/text/CString.h>

#define UNKNOWN_DEPTH -1

namespace WebCore {
static inline void adjustDepth(int &depth, QuoteType type)
{
    switch (type) {
    case OPEN_QUOTE:
    case NO_OPEN_QUOTE:
        ++depth;
        break;
    case CLOSE_QUOTE:
    case NO_CLOSE_QUOTE:
        if (depth)
            --depth;
        break;
    default:
        ASSERT_NOT_REACHED();
    }
}

RenderQuote::RenderQuote(Document* node, QuoteType quote)
    : RenderText(node, StringImpl::empty())
    , m_type(quote)
    , m_depth(UNKNOWN_DEPTH)
    , m_next(0)
    , m_previous(0)
{
}

RenderQuote::~RenderQuote()
{
}

const char* RenderQuote::renderName() const
{
    return "RenderQuote";
}

// This function places a list of quote renderers starting at "this" in the list of quote renderers already
// in the document's renderer tree.
// The assumptions are made (for performance):
// 1. The list of quotes already in the renderers tree of the document is already in a consistent state
// (All quote renderers are linked and have the correct depth set)
// 2. The quote renderers of the inserted list are in a tree of renderers of their own which has been just
// inserted in the main renderer tree with its root as child of some renderer.
// 3. The quote renderers in the inserted list have depths consistent with their position in the list relative
// to "this", thus if "this" does not need to change its depth upon insertion, the other renderers in the list don't
// need to either.
void RenderQuote::placeQuote()
{
    RenderQuote* head = this;
    ASSERT(!head->m_previous);
    RenderQuote* tail = 0;
    for (RenderObject* predecessor = head->previousInPreOrder(); predecessor; predecessor = predecessor->previousInPreOrder()) {
        if (!predecessor->isQuote())
            continue;
        head->m_previous = toRenderQuote(predecessor);
        if (head->m_previous->m_next) {
            // We need to splice the list of quotes headed by head into the document's list of quotes.
            tail = head;
            while (tail->m_next)
                 tail = tail->m_next;
            tail->m_next = head->m_previous->m_next;
            ASSERT(tail->m_next->m_previous == head->m_previous);
            tail->m_next->m_previous =  tail;
            tail = tail->m_next; // This marks the splicing point here there may be a depth discontinuity
        }
        head->m_previous->m_next = head;
        ASSERT(head->m_previous->m_depth != UNKNOWN_DEPTH);
        break;
    }
    int newDepth;
    if (!head->m_previous) {
        newDepth = 0;
        goto skipNewDepthCalc;
    }
    newDepth = head->m_previous->m_depth;
    do {
        adjustDepth(newDepth, head->m_previous->m_type);
skipNewDepthCalc:
        if (head->m_depth == newDepth) { // All remaining depth should be correct except if splicing was done.
            if (!tail) // We've done the post splicing section already or there was no splicing.
                break;
            head = tail; // Continue after the splicing point
            tail = 0; // Mark the possible splicing point discontinuity fixed.
            newDepth = head->m_previous->m_depth;
            continue;
        }
        head->m_depth = newDepth;
        // FIXME: If the width and height of the quotation characters does not change we may only need to
        // Invalidate the renderer's area not a relayout.
        head->setNeedsLayoutAndPrefWidthsRecalc();
        head = head->m_next;
        if (head == tail) // We are at the splicing point
            tail = 0; // Mark the possible depth discontinuity fixed.
    } while (head);
}

#define ARRAY_SIZE(Carray) (sizeof(Carray) / sizeof(*Carray))
#define LANGUAGE_DATA(name, languageSourceArray) { name, languageSourceArray, ARRAY_SIZE(languageSourceArray) }
#define U(x) ((const UChar*)L##x)

static const UChar* simpleQuotes[] = {U("\""), U("\""), U("'"), U("'")};

static const UChar* englishQuotes[] = {U("\x201C"), U("\x201D"), U("\x2018"), U("\x2019")};
static const UChar* norwegianQuotes[] = { U("\x00AB"), U("\x00BB"), U("\x2039"), U("\x203A") };
static const UChar* romanianQuotes[] = { U("\x201E"), U("\x201D")};
static const UChar* russianQuotes[] = { U("\x00AB"), U("\x00BB"), U("\x201E"), U("\x201C") };
#undef U

struct LanguageData {
    const char *name;
    const UChar* const* const array;
    const int arraySize;
    bool operator<(const LanguageData& compareTo) const
    {
        return strcmp(name, compareTo.name);
    }
};

// Data mast be alphabetically sorted and in all lower case for fast comparison
LanguageData languageData[] = {
    LANGUAGE_DATA("en", englishQuotes),
    LANGUAGE_DATA("no", norwegianQuotes),
    LANGUAGE_DATA("ro", romanianQuotes),
    LANGUAGE_DATA("ru", russianQuotes)
};
#undef LANGUAGE_DATA
const LanguageData* const languageDataEnd = languageData + ARRAY_SIZE(languageData);

#define defaultLanguageQuotesSource simpleQuotes
#define defaultLanguageQuotesCount ARRAY_SIZE(defaultLanguageQuotesSource)

static QuotesData* defaultLanguageQuotesValue = 0;
static const QuotesData* defaultLanguageQuotes()
{
    if (!defaultLanguageQuotesValue) {
        defaultLanguageQuotesValue = QuotesData::create(defaultLanguageQuotesCount);
        if (!defaultLanguageQuotesValue)
            return 0;
        String* data = defaultLanguageQuotesValue->data();
        for (size_t i = 0; i < defaultLanguageQuotesCount; ++i)
            data[i] = defaultLanguageQuotesSource[i];
    }
    return defaultLanguageQuotesValue;
}
#undef defaultLanguageQuotesSource
#undef defaultLanguageQuotesCount

typedef HashMap<RefPtr<AtomicStringImpl>, QuotesData* > QuotesMap;

static QuotesMap& quotesMap()
{
    DEFINE_STATIC_LOCAL(QuotesMap, staticQuotesMap, ());
    return staticQuotesMap;
}

static const QuotesData* quotesForLanguage(AtomicStringImpl* language)
{
    QuotesData* returnValue;
    AtomicString lower(language->lower());
    returnValue = quotesMap().get(lower.impl());
    if (returnValue)
        return returnValue;
    CString s(static_cast<const String&>(lower).ascii());
    LanguageData request = { s.buffer()->data(), 0, 0 };
    const LanguageData* lowerBound = std::lower_bound<const LanguageData*, const LanguageData>(languageData, languageDataEnd, request);
    if (lowerBound == languageDataEnd)
        return defaultLanguageQuotes();
    if (strncmp(lowerBound->name, request.name, strlen(lowerBound->name)))
        return defaultLanguageQuotes();
    returnValue = QuotesData::create(lowerBound->arraySize);
    if (!returnValue)
        return defaultLanguageQuotes();
    String* data = returnValue->data();
    for (int i = 0; i < lowerBound->arraySize; ++i)
        data[i] = lowerBound->array[i];
    quotesMap().set(lower.impl(), returnValue);
    return returnValue;
}
#undef ARRAY_SIZE

static const QuotesData* defaultQuotes(const RenderObject* object)
{
    DEFINE_STATIC_LOCAL(String, langString, ("lang"));
    Node* node =  object->generatingNode();
    Element* element;
    if (!node) {
        element = object->document()->body();
        if (!element)
            element = object->document()->documentElement();
    } else if (!node->isElementNode()) {
        element = node->parentElement();
        if (!element)
            return defaultLanguageQuotes();
    } else
      element = toElement(node);
    const AtomicString* language;
    while ((language = &element->getAttribute(langString)) && language->isNull()) {
        element = element->parentElement();
        if (!element)
            return defaultLanguageQuotes();
    }
    return quotesForLanguage(language->impl());
}

PassRefPtr<StringImpl> RenderQuote::originalText() const
{
    if (!parent())
        return 0;
    ASSERT(m_depth != UNKNOWN_DEPTH);
    const QuotesData* quotes = style()->quotes();
    if (!quotes)
        quotes = defaultQuotes(this);
    if (!quotes->length)
        return emptyAtom.impl();
    int index = m_depth * 2;
    switch (m_type) {
    case NO_OPEN_QUOTE:
    case NO_CLOSE_QUOTE:
        return String("").impl();
    case CLOSE_QUOTE:
        if (index)
            --index;
        else
            ++index;
        break;
    case OPEN_QUOTE:
        break;
    default:
        ASSERT_NOT_REACHED();
        return emptyAtom.impl();
    }
    if (index >= quotes->length)
        index = (quotes->length-2) | (index & 1);
    if (index < 0)
        return emptyAtom.impl();
    return quotes->data()[index].impl();
}

void RenderQuote::computePreferredLogicalWidths(float lead)
{
    setTextInternal(originalText());
    RenderText::computePreferredLogicalWidths(lead);
}

void RenderQuote::rendererSubtreeAttached(RenderObject* renderer)
{
    if (renderer->documentBeingDestroyed())
        return;
    for (RenderObject* descendant = renderer; descendant; descendant = descendant->nextInPreOrder(renderer))
        if (descendant->isQuote()) {
            toRenderQuote(descendant)->placeQuote();
            break;
        }
}

void RenderQuote::rendererRemovedFromTree(RenderObject* subtreeRoot)
{
    if (subtreeRoot->documentBeingDestroyed())
        return;
    for (RenderObject* descendant = subtreeRoot; descendant; descendant = descendant->nextInPreOrder(subtreeRoot))
        if (descendant->isQuote()) {
            RenderQuote* removedQuote = toRenderQuote(descendant);
            RenderQuote* lastQuoteBefore = removedQuote->m_previous;
            removedQuote->m_previous = 0;
            int depth = removedQuote->m_depth;
            for (descendant = descendant->nextInPreOrder(subtreeRoot); descendant; descendant = descendant->nextInPreOrder(subtreeRoot))
                if (descendant->isQuote())
                    removedQuote = toRenderQuote(descendant);
            RenderQuote* quoteAfter = removedQuote->m_next;
            removedQuote->m_next = 0;
            if (lastQuoteBefore)
                lastQuoteBefore->m_next = quoteAfter;
            if (quoteAfter) {
                quoteAfter->m_previous = lastQuoteBefore;
                do {
                    if (depth == quoteAfter->m_depth)
                        break;
                    quoteAfter->m_depth = depth;
                    quoteAfter->setNeedsLayoutAndPrefWidthsRecalc();
                    adjustDepth(depth, quoteAfter->m_type);
                    quoteAfter = quoteAfter->m_next;
                } while (quoteAfter);
            }
            break;
        }
}

void RenderQuote::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
    const QuotesData* newQuotes = style()->quotes();
    const QuotesData* oldQuotes = oldStyle ? oldStyle->quotes() : 0;
    if (!((newQuotes && oldQuotes && (*newQuotes == *oldQuotes)) || (!newQuotes && !oldQuotes)))
        setNeedsLayoutAndPrefWidthsRecalc();
    RenderText::styleDidChange(diff, oldStyle);
}

} // namespace WebCore