/
RenderTreeBuilderRuby.cpp
458 lines (413 loc) · 18.3 KB
/
RenderTreeBuilderRuby.cpp
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "RenderTreeBuilderRuby.h"
#include "RenderRuby.h"
#include "RenderRubyBase.h"
#include "RenderRubyRun.h"
#include "RenderTreeBuilder.h"
namespace WebCore {
static inline RenderRubyRun& findRubyRunParent(RenderObject& child)
{
return *lineageOfType<RenderRubyRun>(child).first();
}
static inline bool isAnonymousRubyInlineBlock(const RenderObject* object)
{
ASSERT(!object
|| !isRuby(object->parent())
|| is<RenderRubyRun>(*object)
|| (object->isInline() && (object->isBeforeContent() || object->isAfterContent()))
|| (object->isAnonymous() && is<RenderBlock>(*object) && object->style().display() == INLINE_BLOCK));
return object
&& isRuby(object->parent())
&& is<RenderBlock>(*object)
&& !is<RenderRubyRun>(*object);
}
static inline bool isRubyBeforeBlock(const RenderObject* object)
{
return isAnonymousRubyInlineBlock(object)
&& !object->previousSibling()
&& downcast<RenderBlock>(*object).firstChild()
&& downcast<RenderBlock>(*object).firstChild()->style().styleType() == BEFORE;
}
static inline bool isRubyAfterBlock(const RenderObject* object)
{
return isAnonymousRubyInlineBlock(object)
&& !object->nextSibling()
&& downcast<RenderBlock>(*object).firstChild()
&& downcast<RenderBlock>(*object).firstChild()->style().styleType() == AFTER;
}
#ifndef ASSERT_DISABLED
static inline bool isRubyChildForNormalRemoval(const RenderObject& object)
{
return object.isRubyRun()
|| object.isBeforeContent()
|| object.isAfterContent()
|| object.isRenderMultiColumnFlow()
|| object.isRenderMultiColumnSet()
|| isAnonymousRubyInlineBlock(&object);
}
#endif
static inline RenderBlock* rubyBeforeBlock(const RenderElement* ruby)
{
RenderObject* child = ruby->firstChild();
return isRubyBeforeBlock(child) ? downcast<RenderBlock>(child) : nullptr;
}
static inline RenderBlock* rubyAfterBlock(const RenderElement* ruby)
{
RenderObject* child = ruby->lastChild();
return isRubyAfterBlock(child) ? downcast<RenderBlock>(child) : nullptr;
}
static auto createAnonymousRubyInlineBlock(RenderObject& ruby)
{
auto newBlock = createRenderer<RenderBlockFlow>(ruby.document(), RenderStyle::createAnonymousStyleWithDisplay(ruby.style(), INLINE_BLOCK));
newBlock->initializeStyle();
return newBlock;
}
static RenderRubyRun* lastRubyRun(const RenderElement* ruby)
{
RenderObject* child = ruby->lastChild();
if (child && !is<RenderRubyRun>(*child))
child = child->previousSibling();
if (!is<RenderRubyRun>(child)) {
ASSERT(!child || child->isBeforeContent() || child == rubyBeforeBlock(ruby));
return nullptr;
}
return downcast<RenderRubyRun>(child);
}
RenderTreeBuilder::Ruby::Ruby(RenderTreeBuilder& builder)
: m_builder(builder)
{
}
void RenderTreeBuilder::Ruby::moveInlineChildren(RenderRubyBase& from, RenderRubyBase& to, RenderObject* beforeChild)
{
ASSERT(from.childrenInline());
if (!from.firstChild())
return;
RenderBlock* toBlock = nullptr;
if (to.childrenInline()) {
// The standard and easy case: move the children into the target base
toBlock = &to;
} else {
// We need to wrap the inline objects into an anonymous block.
// If toBase has a suitable block, we re-use it, otherwise create a new one.
auto* lastChild = to.lastChild();
if (lastChild && lastChild->isAnonymousBlock() && lastChild->childrenInline())
toBlock = downcast<RenderBlock>(lastChild);
else {
auto newToBlock = to.createAnonymousBlock();
toBlock = newToBlock.get();
to.insertChildInternal(WTFMove(newToBlock), nullptr);
}
}
// Move our inline children into the target block we determined above.
from.moveChildrenTo(m_builder, toBlock, from.firstChild(), beforeChild, RenderBoxModelObject::NormalizeAfterInsertion::No);
}
void RenderTreeBuilder::Ruby::moveBlockChildren(RenderRubyBase& from, RenderRubyBase& to, RenderObject* beforeChild)
{
ASSERT(!from.childrenInline());
if (!from.firstChild())
return;
if (to.childrenInline())
m_builder.makeChildrenNonInline(to);
// If an anonymous block would be put next to another such block, then merge those.
auto* firstChildHere = from.firstChild();
auto* lastChildThere = to.lastChild();
if (firstChildHere->isAnonymousBlock() && firstChildHere->childrenInline()
&& lastChildThere && lastChildThere->isAnonymousBlock() && lastChildThere->childrenInline()) {
auto* anonBlockHere = downcast<RenderBlock>(firstChildHere);
auto* anonBlockThere = downcast<RenderBlock>(lastChildThere);
anonBlockHere->moveAllChildrenTo(m_builder, anonBlockThere, RenderBoxModelObject::NormalizeAfterInsertion::Yes);
anonBlockHere->deleteLines();
anonBlockHere->removeFromParentAndDestroy(m_builder);
}
// Move all remaining children normally.
from.moveChildrenTo(m_builder, &to, from.firstChild(), beforeChild, RenderBoxModelObject::NormalizeAfterInsertion::No);
}
void RenderTreeBuilder::Ruby::moveChildren(RenderRubyBase& from, RenderRubyBase& to)
{
moveChildrenInternal(from, to);
from.addFloatsToNewParent(to);
}
void RenderTreeBuilder::Ruby::moveChildrenInternal(RenderRubyBase& from, RenderRubyBase& to, RenderObject* beforeChild)
{
// This function removes all children that are before (!) beforeChild
// and appends them to toBase.
if (beforeChild && beforeChild->parent() != &from)
beforeChild = m_builder.splitAnonymousBoxesAroundChild(from, beforeChild);
if (from.childrenInline())
moveInlineChildren(from, to, beforeChild);
else
moveBlockChildren(from, to, beforeChild);
from.setNeedsLayoutAndPrefWidthsRecalc();
to.setNeedsLayoutAndPrefWidthsRecalc();
}
void RenderTreeBuilder::Ruby::insertChild(RenderRubyRun& parent, RenderPtr<RenderObject> child, RenderObject* beforeChild)
{
if (child->isRubyText()) {
if (!beforeChild) {
// RenderRuby has already ascertained that we can add the child here.
ASSERT(!parent.hasRubyText());
// prepend ruby texts as first child
parent.addChild(m_builder, WTFMove(child), parent.firstChild());
return;
}
if (beforeChild->isRubyText()) {
// New text is inserted just before another.
// In this case the new text takes the place of the old one, and
// the old text goes into a new run that is inserted as next sibling.
ASSERT(beforeChild->parent() == &parent);
RenderElement* ruby = parent.parent();
ASSERT(isRuby(ruby));
auto newRun = RenderRubyRun::staticCreateRubyRun(ruby);
m_builder.insertChild(*ruby, WTFMove(newRun), parent.nextSibling());
// Add the new ruby text and move the old one to the new run
// Note: Doing it in this order and not using RenderRubyRun's methods,
// in order to avoid automatic removal of the ruby run in case there is no
// other child besides the old ruby text.
parent.addChild(m_builder, WTFMove(child), beforeChild);
auto takenBeforeChild = parent.RenderBlockFlow::takeChild(m_builder, *beforeChild);
m_builder.insertChild(*newRun, WTFMove(takenBeforeChild));
return;
}
if (parent.hasRubyBase()) {
// Insertion before a ruby base object.
// In this case we need insert a new run before the current one and split the base.
RenderElement* ruby = parent.parent();
auto newRun = RenderRubyRun::staticCreateRubyRun(ruby);
auto& run = *newRun;
m_builder.insertChild(*ruby, WTFMove(newRun), &parent);
m_builder.insertChild(run, WTFMove(child));
moveChildrenInternal(rubyBaseSafe(parent), rubyBaseSafe(run), beforeChild);
}
return;
}
// child is not a text -> insert it into the base
// (append it instead if beforeChild is the ruby text)
if (beforeChild && beforeChild->isRubyText())
beforeChild = nullptr;
m_builder.insertChild(rubyBaseSafe(parent), WTFMove(child), beforeChild);
}
RenderElement& RenderTreeBuilder::Ruby::findOrCreateParentForChild(RenderRubyAsBlock& parent, const RenderObject& child, RenderObject*& beforeChild)
{
// Insert :before and :after content before/after the RenderRubyRun(s)
if (child.isBeforeContent()) {
// Add generated inline content normally
if (child.isInline())
return parent;
// Wrap non-inline content in an anonymous inline-block.
auto* beforeBlock = rubyBeforeBlock(&parent);
if (!beforeBlock) {
auto newBlock = createAnonymousRubyInlineBlock(parent);
beforeBlock = newBlock.get();
m_builder.insertChildToRenderBlockFlow(parent, WTFMove(newBlock), parent.firstChild());
}
beforeChild = nullptr;
return *beforeBlock;
}
if (child.isAfterContent()) {
// Add generated inline content normally
if (child.isInline())
return parent;
// Wrap non-inline content with an anonymous inline-block.
auto* afterBlock = rubyAfterBlock(&parent);
if (!afterBlock) {
auto newBlock = createAnonymousRubyInlineBlock(parent);
afterBlock = newBlock.get();
m_builder.insertChildToRenderBlockFlow(parent, WTFMove(newBlock));
}
beforeChild = nullptr;
return *afterBlock;
}
// If the child is a ruby run, just add it normally.
if (child.isRubyRun())
return parent;
if (beforeChild && !parent.isAfterContent(beforeChild)) {
// insert child into run
ASSERT(!beforeChild->isRubyRun());
auto* run = beforeChild->parent();
while (run && !run->isRubyRun())
run = run->parent();
if (run)
return *run;
ASSERT_NOT_REACHED(); // beforeChild should always have a run as parent!
// Emergency fallback: fall through and just append.
}
// If the new child would be appended, try to add the child to the previous run
// if possible, or create a new run otherwise.
// (The RenderRubyRun object will handle the details)
auto* lastRun = lastRubyRun(&parent);
if (!lastRun || lastRun->hasRubyText()) {
auto newRun = RenderRubyRun::staticCreateRubyRun(&parent);
lastRun = newRun.get();
m_builder.insertChildToRenderBlockFlow(parent, WTFMove(newRun), beforeChild);
}
beforeChild = nullptr;
return *lastRun;
}
RenderElement& RenderTreeBuilder::Ruby::findOrCreateParentForChild(RenderRubyAsInline& parent, const RenderObject& child, RenderObject*& beforeChild)
{
// Insert :before and :after content before/after the RenderRubyRun(s)
if (child.isBeforeContent()) {
// Add generated inline content normally
if (child.isInline())
return parent;
// Wrap non-inline content with an anonymous inline-block.
auto* beforeBlock = rubyBeforeBlock(&parent);
if (!beforeBlock) {
auto newBlock = createAnonymousRubyInlineBlock(parent);
beforeBlock = newBlock.get();
m_builder.insertChildToRenderInline(parent, WTFMove(newBlock), parent.firstChild());
}
beforeChild = nullptr;
return *beforeBlock;
}
if (child.isAfterContent()) {
// Add generated inline content normally
if (child.isInline())
return parent;
// Wrap non-inline content with an anonymous inline-block.
auto* afterBlock = rubyAfterBlock(&parent);
if (!afterBlock) {
auto newBlock = createAnonymousRubyInlineBlock(parent);
afterBlock = newBlock.get();
m_builder.insertChildToRenderInline(parent, WTFMove(newBlock));
}
beforeChild = nullptr;
return *afterBlock;
}
// If the child is a ruby run, just add it normally.
if (child.isRubyRun())
return parent;
if (beforeChild && !parent.isAfterContent(beforeChild)) {
// insert child into run
ASSERT(!beforeChild->isRubyRun());
auto* run = beforeChild->parent();
while (run && !run->isRubyRun())
run = run->parent();
if (run)
return *run;
ASSERT_NOT_REACHED(); // beforeChild should always have a run as parent!
// Emergency fallback: fall through and just append.
}
// If the new child would be appended, try to add the child to the previous run
// if possible, or create a new run otherwise.
// (The RenderRubyRun object will handle the details)
auto* lastRun = lastRubyRun(&parent);
if (!lastRun || lastRun->hasRubyText()) {
auto newRun = RenderRubyRun::staticCreateRubyRun(&parent);
lastRun = newRun.get();
m_builder.insertChildToRenderInline(parent, WTFMove(newRun), beforeChild);
}
beforeChild = nullptr;
return *lastRun;
}
RenderRubyBase& RenderTreeBuilder::Ruby::rubyBaseSafe(RenderRubyRun& rubyRun)
{
auto* base = rubyRun.rubyBase();
if (!base) {
auto newBase = rubyRun.createRubyBase();
base = newBase.get();
m_builder.insertChildToRenderBlockFlow(rubyRun, WTFMove(newBase));
}
return *base;
}
RenderPtr<RenderObject> RenderTreeBuilder::Ruby::takeChild(RenderRubyAsInline& parent, RenderObject& child)
{
// If the child's parent is *this (must be a ruby run or generated content or anonymous block),
// just use the normal remove method.
if (child.parent() == &parent) {
#ifndef ASSERT_DISABLED
ASSERT(isRubyChildForNormalRemoval(child));
#endif
return parent.RenderInline::takeChild(m_builder, child);
}
// If the child's parent is an anoymous block (must be generated :before/:after content)
// just use the block's remove method.
if (isAnonymousRubyInlineBlock(child.parent())) {
ASSERT(child.isBeforeContent() || child.isAfterContent());
auto& parent = *child.parent();
auto takenChild = parent.takeChild(m_builder, child);
parent.removeFromParentAndDestroy(m_builder);
return takenChild;
}
// Otherwise find the containing run and remove it from there.
return findRubyRunParent(child).takeChild(m_builder, child);
}
RenderPtr<RenderObject> RenderTreeBuilder::Ruby::takeChild(RenderRubyAsBlock& parent, RenderObject& child)
{
// If the child's parent is *this (must be a ruby run or generated content or anonymous block),
// just use the normal remove method.
if (child.parent() == &parent) {
#ifndef ASSERT_DISABLED
ASSERT(isRubyChildForNormalRemoval(child));
#endif
return parent.RenderBlockFlow::takeChild(m_builder, child);
}
// If the child's parent is an anoymous block (must be generated :before/:after content)
// just use the block's remove method.
if (isAnonymousRubyInlineBlock(child.parent())) {
ASSERT(child.isBeforeContent() || child.isAfterContent());
auto& parent = *child.parent();
auto takenChild = parent.takeChild(m_builder, child);
parent.removeFromParentAndDestroy(m_builder);
return takenChild;
}
// Otherwise find the containing run and remove it from there.
return findRubyRunParent(child).takeChild(m_builder, child);
}
RenderPtr<RenderObject> RenderTreeBuilder::Ruby::takeChild(RenderRubyRun& parent, RenderObject& child)
{
// If the child is a ruby text, then merge the ruby base with the base of
// the right sibling run, if possible.
if (!parent.beingDestroyed() && !parent.renderTreeBeingDestroyed() && child.isRubyText()) {
RenderRubyBase* base = parent.rubyBase();
RenderObject* rightNeighbour = parent.nextSibling();
if (base && is<RenderRubyRun>(rightNeighbour)) {
// Ruby run without a base can happen only at the first run.
RenderRubyRun& rightRun = downcast<RenderRubyRun>(*rightNeighbour);
if (rightRun.hasRubyBase()) {
RenderRubyBase* rightBase = rightRun.rubyBase();
// Collect all children in a single base, then swap the bases.
m_builder.moveRubyChildren(*rightBase, *base);
parent.moveChildTo(m_builder, &rightRun, base, RenderBoxModelObject::NormalizeAfterInsertion::No);
rightRun.moveChildTo(m_builder, &parent, rightBase, RenderBoxModelObject::NormalizeAfterInsertion::No);
// The now empty ruby base will be removed below.
ASSERT(!parent.rubyBase()->firstChild());
}
}
}
auto takenChild = parent.RenderBlockFlow::takeChild(m_builder, child);
if (!parent.beingDestroyed() && !parent.renderTreeBeingDestroyed()) {
// Check if our base (if any) is now empty. If so, destroy it.
RenderBlock* base = parent.rubyBase();
if (base && !base->firstChild()) {
auto takenBase = parent.RenderBlockFlow::takeChild(m_builder, *base);
base->deleteLines();
}
}
return takenChild;
}
}