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 "RenderMultiColumnFlow.h"
29#include "RenderTreeBuilder.h"
30#include "Text.h"
31#include <wtf/IsoMallocInlines.h>
32
33namespace WebCore {
34
35WTF_MAKE_ISO_ALLOCATED_IMPL(RenderTextFragment);
36
37RenderTextFragment::RenderTextFragment(Text& textNode, const String& text, int startOffset, int length)
38 : RenderText(textNode, text.substring(startOffset, length))
39 , m_start(startOffset)
40 , m_end(length)
41 , m_firstLetter(nullptr)
42{
43}
44
45RenderTextFragment::RenderTextFragment(Document& document, const String& text, int startOffset, int length)
46 : RenderText(document, text.substring(startOffset, length))
47 , m_start(startOffset)
48 , m_end(length)
49 , m_firstLetter(nullptr)
50{
51}
52
53RenderTextFragment::RenderTextFragment(Document& textNode, const String& text)
54 : RenderText(textNode, text)
55 , m_start(0)
56 , m_end(text.length())
57 , m_contentString(text)
58 , m_firstLetter(nullptr)
59{
60}
61
62RenderTextFragment::~RenderTextFragment()
63{
64 ASSERT(!m_firstLetter);
65}
66
67bool RenderTextFragment::canBeSelectionLeaf() const
68{
69 return textNode() && textNode()->hasEditableStyle();
70}
71
72void RenderTextFragment::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
73{
74 RenderText::styleDidChange(diff, oldStyle);
75
76 if (RenderBlock* block = blockForAccompanyingFirstLetter())
77 block->mutableStyle().removeCachedPseudoStyle(PseudoId::FirstLetter);
78}
79
80void RenderTextFragment::setText(const String& newText, bool force)
81{
82 RenderText::setText(newText, force);
83 m_start = 0;
84 m_end = text().length();
85 if (!m_firstLetter)
86 return;
87 if (RenderTreeBuilder::current())
88 RenderTreeBuilder::current()->destroy(*m_firstLetter);
89 else
90 RenderTreeBuilder(*document().renderView()).destroy(*m_firstLetter);
91 ASSERT(!m_firstLetter);
92 ASSERT(!textNode() || textNode()->renderer() == this);
93}
94
95UChar RenderTextFragment::previousCharacter() const
96{
97 if (start()) {
98 String original = textNode() ? textNode()->data() : contentString();
99 if (!original.isNull() && start() <= original.length())
100 return original[start() - 1];
101 }
102
103 return RenderText::previousCharacter();
104}
105
106RenderBlock* RenderTextFragment::blockForAccompanyingFirstLetter()
107{
108 if (!m_firstLetter)
109 return nullptr;
110 for (auto& block : ancestorsOfType<RenderBlock>(*m_firstLetter)) {
111 if (is<RenderMultiColumnFlow>(block))
112 break;
113 if (block.style().hasPseudoStyle(PseudoId::FirstLetter) && block.canHaveChildren())
114 return &block;
115 }
116 return nullptr;
117}
118
119void RenderTextFragment::setContentString(const String& text)
120{
121 m_contentString = text;
122 setText(text);
123}
124
125} // namespace WebCore
126