1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #include "config.h"
32
33 #include "RenderRuby.h"
34
35 #include "RenderRubyRun.h"
36 #include "RenderStyle.h"
37 #include <wtf/RefPtr.h>
38
39 namespace WebCore {
40
41 //=== generic helper functions to avoid excessive code duplication ===
42
isAnonymousRubyInlineBlock(const RenderObject * object)43 static inline bool isAnonymousRubyInlineBlock(const RenderObject* object)
44 {
45 ASSERT(!object
46 || !object->parent()->isRuby()
47 || object->isRubyRun()
48 || (object->isInline() && (object->isBeforeContent() || object->isAfterContent()))
49 || (object->isAnonymous() && object->isRenderBlock() && object->style()->display() == INLINE_BLOCK));
50
51 return object
52 && object->parent()->isRuby()
53 && object->isRenderBlock()
54 && !object->isRubyRun();
55 }
56
isRubyBeforeBlock(const RenderObject * object)57 static inline bool isRubyBeforeBlock(const RenderObject* object)
58 {
59 return isAnonymousRubyInlineBlock(object)
60 && !object->previousSibling()
61 && object->firstChild()
62 && object->firstChild()->style()->styleType() == BEFORE;
63 }
64
isRubyAfterBlock(const RenderObject * object)65 static inline bool isRubyAfterBlock(const RenderObject* object)
66 {
67 return isAnonymousRubyInlineBlock(object)
68 && !object->nextSibling()
69 && object->firstChild()
70 && object->firstChild()->style()->styleType() == AFTER;
71 }
72
rubyBeforeBlock(const RenderObject * ruby)73 static inline RenderBlock* rubyBeforeBlock(const RenderObject* ruby)
74 {
75 RenderObject* child = ruby->firstChild();
76 return isRubyBeforeBlock(child) ? static_cast<RenderBlock*>(child) : 0;
77 }
78
rubyAfterBlock(const RenderObject * ruby)79 static inline RenderBlock* rubyAfterBlock(const RenderObject* ruby)
80 {
81 RenderObject* child = ruby->lastChild();
82 return isRubyAfterBlock(child) ? static_cast<RenderBlock*>(child) : 0;
83 }
84
createAnonymousRubyInlineBlock(RenderObject * ruby)85 static RenderBlock* createAnonymousRubyInlineBlock(RenderObject* ruby)
86 {
87 RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyle(ruby->style());
88 newStyle->setDisplay(INLINE_BLOCK);
89
90 RenderBlock* newBlock = new (ruby->renderArena()) RenderBlock(ruby->document() /* anonymous box */);
91 newBlock->setStyle(newStyle.release());
92 return newBlock;
93 }
94
lastRubyRun(const RenderObject * ruby)95 static RenderRubyRun* lastRubyRun(const RenderObject* ruby)
96 {
97 RenderObject* child = ruby->lastChild();
98 if (child && !child->isRubyRun())
99 child = child->previousSibling();
100 ASSERT(!child || child->isRubyRun() || child->isBeforeContent() || child == rubyBeforeBlock(ruby));
101 return child && child->isRubyRun() ? static_cast<RenderRubyRun*>(child) : 0;
102 }
103
findRubyRunParent(RenderObject * child)104 static inline RenderRubyRun* findRubyRunParent(RenderObject* child)
105 {
106 while (child && !child->isRubyRun())
107 child = child->parent();
108 return static_cast<RenderRubyRun*>(child);
109 }
110
111 //=== ruby as inline object ===
112
RenderRubyAsInline(Node * node)113 RenderRubyAsInline::RenderRubyAsInline(Node* node)
114 : RenderInline(node)
115 {
116 }
117
~RenderRubyAsInline()118 RenderRubyAsInline::~RenderRubyAsInline()
119 {
120 }
121
addChild(RenderObject * child,RenderObject * beforeChild)122 void RenderRubyAsInline::addChild(RenderObject* child, RenderObject* beforeChild)
123 {
124 // Insert :before and :after content before/after the RenderRubyRun(s)
125 if (child->isBeforeContent()) {
126 if (child->isInline()) {
127 // Add generated inline content normally
128 RenderInline::addChild(child, firstChild());
129 } else {
130 // Wrap non-inline content with an anonymous inline-block.
131 RenderBlock* beforeBlock = rubyBeforeBlock(this);
132 if (!beforeBlock) {
133 beforeBlock = createAnonymousRubyInlineBlock(this);
134 RenderInline::addChild(beforeBlock, firstChild());
135 }
136 beforeBlock->addChild(child);
137 }
138 return;
139 }
140 if (child->isAfterContent()) {
141 if (child->isInline()) {
142 // Add generated inline content normally
143 RenderInline::addChild(child);
144 } else {
145 // Wrap non-inline content with an anonymous inline-block.
146 RenderBlock* afterBlock = rubyAfterBlock(this);
147 if (!afterBlock) {
148 afterBlock = createAnonymousRubyInlineBlock(this);
149 RenderInline::addChild(afterBlock);
150 }
151 afterBlock->addChild(child);
152 }
153 return;
154 }
155
156 // If the child is a ruby run, just add it normally.
157 if (child->isRubyRun()) {
158 RenderInline::addChild(child, beforeChild);
159 return;
160 }
161
162 if (beforeChild && !isAfterContent(beforeChild)) {
163 // insert child into run
164 ASSERT(!beforeChild->isRubyRun());
165 RenderObject* run = beforeChild;
166 while (run && !run->isRubyRun())
167 run = run->parent();
168 if (run) {
169 run->addChild(child, beforeChild);
170 return;
171 }
172 ASSERT_NOT_REACHED(); // beforeChild should always have a run as parent!
173 // Emergency fallback: fall through and just append.
174 }
175
176 // If the new child would be appended, try to add the child to the previous run
177 // if possible, or create a new run otherwise.
178 // (The RenderRubyRun object will handle the details)
179 RenderRubyRun* lastRun = lastRubyRun(this);
180 if (!lastRun || lastRun->hasRubyText()) {
181 lastRun = RenderRubyRun::staticCreateRubyRun(this);
182 RenderInline::addChild(lastRun);
183 }
184 lastRun->addChild(child);
185 }
186
removeChild(RenderObject * child)187 void RenderRubyAsInline::removeChild(RenderObject* child)
188 {
189 // If the child's parent is *this (must be a ruby run or generated content or anonymous block),
190 // just use the normal remove method.
191 if (child->parent() == this) {
192 ASSERT(child->isRubyRun() || child->isBeforeContent() || child->isAfterContent() || isAnonymousRubyInlineBlock(child));
193 RenderInline::removeChild(child);
194 return;
195 }
196 // If the child's parent is an anoymous block (must be generated :before/:after content)
197 // just use the block's remove method.
198 if (isAnonymousRubyInlineBlock(child->parent())) {
199 ASSERT(child->isBeforeContent() || child->isAfterContent());
200 child->parent()->removeChild(child);
201 removeChild(child->parent());
202 return;
203 }
204
205 // Otherwise find the containing run and remove it from there.
206 RenderRubyRun* run = findRubyRunParent(child);
207 ASSERT(run);
208 run->removeChild(child);
209 }
210
211
212 //=== ruby as block object ===
213
RenderRubyAsBlock(Node * node)214 RenderRubyAsBlock::RenderRubyAsBlock(Node* node)
215 : RenderBlock(node)
216 {
217 }
218
~RenderRubyAsBlock()219 RenderRubyAsBlock::~RenderRubyAsBlock()
220 {
221 }
222
addChild(RenderObject * child,RenderObject * beforeChild)223 void RenderRubyAsBlock::addChild(RenderObject* child, RenderObject* beforeChild)
224 {
225 // Insert :before and :after content before/after the RenderRubyRun(s)
226 if (child->isBeforeContent()) {
227 if (child->isInline()) {
228 // Add generated inline content normally
229 RenderBlock::addChild(child, firstChild());
230 } else {
231 // Wrap non-inline content with an anonymous inline-block.
232 RenderBlock* beforeBlock = rubyBeforeBlock(this);
233 if (!beforeBlock) {
234 beforeBlock = createAnonymousRubyInlineBlock(this);
235 RenderBlock::addChild(beforeBlock, firstChild());
236 }
237 beforeBlock->addChild(child);
238 }
239 return;
240 }
241 if (child->isAfterContent()) {
242 if (child->isInline()) {
243 // Add generated inline content normally
244 RenderBlock::addChild(child);
245 } else {
246 // Wrap non-inline content with an anonymous inline-block.
247 RenderBlock* afterBlock = rubyAfterBlock(this);
248 if (!afterBlock) {
249 afterBlock = createAnonymousRubyInlineBlock(this);
250 RenderBlock::addChild(afterBlock);
251 }
252 afterBlock->addChild(child);
253 }
254 return;
255 }
256
257 // If the child is a ruby run, just add it normally.
258 if (child->isRubyRun()) {
259 RenderBlock::addChild(child, beforeChild);
260 return;
261 }
262
263 if (beforeChild && !isAfterContent(beforeChild)) {
264 // insert child into run
265 ASSERT(!beforeChild->isRubyRun());
266 RenderObject* run = beforeChild;
267 while (run && !run->isRubyRun())
268 run = run->parent();
269 if (run) {
270 run->addChild(child, beforeChild);
271 return;
272 }
273 ASSERT_NOT_REACHED(); // beforeChild should always have a run as parent!
274 // Emergency fallback: fall through and just append.
275 }
276
277 // If the new child would be appended, try to add the child to the previous run
278 // if possible, or create a new run otherwise.
279 // (The RenderRubyRun object will handle the details)
280 RenderRubyRun* lastRun = lastRubyRun(this);
281 if (!lastRun || lastRun->hasRubyText()) {
282 lastRun = RenderRubyRun::staticCreateRubyRun(this);
283 RenderBlock::addChild(lastRun);
284 }
285 lastRun->addChild(child);
286 }
287
removeChild(RenderObject * child)288 void RenderRubyAsBlock::removeChild(RenderObject* child)
289 {
290 // If the child's parent is *this (must be a ruby run or generated content or anonymous block),
291 // just use the normal remove method.
292 if (child->parent() == this) {
293 ASSERT(child->isRubyRun() || child->isBeforeContent() || child->isAfterContent() || isAnonymousRubyInlineBlock(child));
294 RenderBlock::removeChild(child);
295 return;
296 }
297 // If the child's parent is an anoymous block (must be generated :before/:after content)
298 // just use the block's remove method.
299 if (isAnonymousRubyInlineBlock(child->parent())) {
300 ASSERT(child->isBeforeContent() || child->isAfterContent());
301 child->parent()->removeChild(child);
302 removeChild(child->parent());
303 return;
304 }
305
306 // Otherwise find the containing run and remove it from there.
307 RenderRubyRun* run = findRubyRunParent(child);
308 ASSERT(run);
309 run->removeChild(child);
310 }
311
312 } // namespace WebCore
313