2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "RenderRuby.h"
36 #include "RenderRubyRun.h"
40 //=== generic helper functions to avoid excessive code duplication ===
42 static RenderRubyRun* lastRubyRun(const RenderObject* ruby)
44 RenderObject* child = ruby->lastChild();
45 if (child && ruby->isAfterContent(child))
46 child = child->previousSibling();
47 ASSERT(!child || child->isRubyRun());
48 return static_cast<RenderRubyRun*>(child);
51 static inline RenderRubyRun* findRubyRunParent(RenderObject* child)
53 while (child && !child->isRubyRun())
54 child = child->parent();
55 return static_cast<RenderRubyRun*>(child);
58 //=== ruby as inline object ===
60 RenderRubyAsInline::RenderRubyAsInline(Node* node)
65 RenderRubyAsInline::~RenderRubyAsInline()
69 bool RenderRubyAsInline::isChildAllowed(RenderObject* child, RenderStyle*) const
71 return child->isRubyText()
76 void RenderRubyAsInline::addChild(RenderObject* child, RenderObject* beforeChild)
78 // Note: ':after' content is handled implicitely below
80 // if child is a ruby run, just add it normally
81 if (child->isRubyRun()) {
82 RenderInline::addChild(child, beforeChild);
86 if (beforeChild && !isAfterContent(beforeChild)) {
87 // insert child into run
88 ASSERT(!beforeChild->isRubyRun());
89 RenderRubyRun* run = findRubyRunParent(beforeChild);
90 ASSERT(run); // beforeChild should always have a run as parent
92 run->addChild(child, beforeChild);
95 ASSERT(false); // beforeChild should always have a run as parent!
96 // Emergency fallback: fall through and just append.
99 // If the new child would be appended, try to add the child to the previous run
100 // if possible, or create a new run otherwise.
101 // (The RenderRubyRun object will handle the details)
102 RenderRubyRun* lastRun = lastRubyRun(this);
103 if (!lastRun || lastRun->hasRubyText()) {
104 lastRun = RenderRubyRun::staticCreateRubyRun(this);
105 RenderInline::addChild(lastRun);
107 lastRun->addChild(child);
110 void RenderRubyAsInline::removeChild(RenderObject* child)
112 // If the child's parent is *this, i.e. a ruby run or ':after' content,
113 // just use the normal remove method.
114 if (child->parent() == this) {
115 ASSERT(child->isRubyRun() || child->isAfterContent());
116 RenderInline::removeChild(child);
120 // Find the containing run
121 RenderRubyRun* run = findRubyRunParent(child);
123 run->removeChild(child);
127 //=== ruby as block object ===
129 RenderRubyAsBlock::RenderRubyAsBlock(Node* node)
134 RenderRubyAsBlock::~RenderRubyAsBlock()
138 bool RenderRubyAsBlock::isChildAllowed(RenderObject* child, RenderStyle*) const
140 return child->isRubyText()
141 || child->isRubyRun()
142 || child->isInline();
145 void RenderRubyAsBlock::addChild(RenderObject* child, RenderObject* beforeChild)
147 // Note: ':after' content is handled implicitely below
149 // if child is a ruby run, just add it normally
150 if (child->isRubyRun()) {
151 RenderBlock::addChild(child, beforeChild);
155 if (beforeChild && !isAfterContent(beforeChild)) {
156 // insert child into run
157 ASSERT(!beforeChild->isRubyRun());
158 RenderObject* run = beforeChild;
159 while (run && !run->isRubyRun())
162 run->addChild(child, beforeChild);
165 ASSERT(false); // beforeChild should always have a run as parent!
166 // Emergency fallback: fall through and just append.
169 // If the new child would be appended, try to add the child to the previous run
170 // if possible, or create a new run otherwise.
171 // (The RenderRubyRun object will handle the details)
172 RenderRubyRun* lastRun = lastRubyRun(this);
173 if (!lastRun || lastRun->hasRubyText()) {
174 lastRun = RenderRubyRun::staticCreateRubyRun(this);
175 RenderBlock::addChild(lastRun);
177 lastRun->addChild(child);
180 void RenderRubyAsBlock::removeChild(RenderObject* child)
182 // If the child's parent is *this, just use the normal remove method.
183 if (child->parent() == this) {
184 // This should happen only during destruction of the whole ruby element, though.
185 RenderBlock::removeChild(child);
189 // Find the containing run
190 RenderRubyRun* run = findRubyRunParent(child);
192 run->removeChild(child);
195 } // namespace WebCore