1/*
2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007 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
23#include "config.h"
24#include "RenderTextFragment.h"
25
26#include "RenderBlock.h"
27#include "RenderIterator.h"
28#include "Text.h"
29
30namespace WebCore {
31
32RenderTextFragment::RenderTextFragment(Text& textNode, const String& text, int startOffset, int length)
33    : RenderText(textNode, text.substring(startOffset, length))
34    , m_start(startOffset)
35    , m_end(length)
36    , m_firstLetter(nullptr)
37{
38}
39
40RenderTextFragment::RenderTextFragment(Document& document, const String& text, int startOffset, int length)
41    : RenderText(document, text.substring(startOffset, length))
42    , m_start(startOffset)
43    , m_end(length)
44    , m_firstLetter(nullptr)
45{
46}
47
48RenderTextFragment::RenderTextFragment(Document& textNode, const String& text)
49    : RenderText(textNode, text)
50    , m_start(0)
51    , m_end(text.length())
52    , m_contentString(text)
53    , m_firstLetter(nullptr)
54{
55}
56
57RenderTextFragment::~RenderTextFragment()
58{
59}
60
61bool RenderTextFragment::canBeSelectionLeaf() const
62{
63    return textNode() && textNode()->hasEditableStyle();
64}
65
66void RenderTextFragment::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
67{
68    RenderText::styleDidChange(diff, oldStyle);
69
70    if (RenderBlock* block = blockForAccompanyingFirstLetter()) {
71        block->style().removeCachedPseudoStyle(FIRST_LETTER);
72        block->updateFirstLetter();
73    }
74}
75
76void RenderTextFragment::willBeDestroyed()
77{
78    if (m_firstLetter)
79        m_firstLetter->destroy();
80    RenderText::willBeDestroyed();
81}
82
83void RenderTextFragment::setText(const String& text, bool force)
84{
85    RenderText::setText(text, force);
86
87    m_start = 0;
88    m_end = textLength();
89    if (!m_firstLetter)
90        return;
91    ASSERT(!m_contentString);
92    m_firstLetter->destroy();
93    m_firstLetter = 0;
94    if (!textNode())
95        return;
96    ASSERT(!textNode()->renderer());
97    textNode()->setRenderer(this);
98}
99
100UChar RenderTextFragment::previousCharacter() const
101{
102    if (start()) {
103        String original = textNode() ? textNode()->data() : contentString();
104        if (!original.isNull() && start() <= original.length())
105            return original[start() - 1];
106    }
107
108    return RenderText::previousCharacter();
109}
110
111RenderBlock* RenderTextFragment::blockForAccompanyingFirstLetter()
112{
113    if (!m_firstLetter)
114        return nullptr;
115    for (auto& block : ancestorsOfType<RenderBlock>(*m_firstLetter)) {
116        if (block.style().hasPseudoStyle(FIRST_LETTER) && block.canHaveChildren())
117            return &block;
118    }
119    return nullptr;
120}
121
122} // namespace WebCore
123