#include "config.h"
#if ENABLE(RUBY)
#include "RenderRuby.h"
#include "RenderRubyRun.h"
namespace WebCore {
static RenderRubyRun* lastRubyRun(const RenderObject* ruby)
{
RenderObject* child = ruby->lastChild();
if (child && ruby->isAfterContent(child))
child = child->previousSibling();
ASSERT(!child || child->isRubyRun());
return static_cast<RenderRubyRun*>(child);
}
static inline RenderRubyRun* findRubyRunParent(RenderObject* child)
{
while (child && !child->isRubyRun())
child = child->parent();
return static_cast<RenderRubyRun*>(child);
}
RenderRubyAsInline::RenderRubyAsInline(Node* node)
: RenderInline(node)
{
}
RenderRubyAsInline::~RenderRubyAsInline()
{
}
bool RenderRubyAsInline::isChildAllowed(RenderObject* child, RenderStyle*) const
{
return child->isRubyText()
|| child->isRubyRun()
|| child->isInline();
}
void RenderRubyAsInline::addChild(RenderObject* child, RenderObject* beforeChild)
{
if (child->isRubyRun()) {
RenderInline::addChild(child, beforeChild);
return;
}
if (beforeChild && !isAfterContent(beforeChild) && !beforeChild->isRubyRun()) {
if (RenderRubyRun* run = findRubyRunParent(beforeChild)) {
run->addChild(child, beforeChild);
return;
}
ASSERT_NOT_REACHED(); }
RenderRubyRun* lastRun = lastRubyRun(this);
if (!lastRun || lastRun->hasRubyText()) {
lastRun = RenderRubyRun::staticCreateRubyRun(this);
RenderInline::addChild(lastRun);
}
lastRun->addChild(child);
}
void RenderRubyAsInline::removeChild(RenderObject* child)
{
if (child->parent() == this) {
ASSERT(child->isRubyRun() || child->isAfterContent());
RenderInline::removeChild(child);
return;
}
RenderRubyRun* run = findRubyRunParent(child);
ASSERT(run);
run->removeChild(child);
}
RenderRubyAsBlock::RenderRubyAsBlock(Node* node)
: RenderBlock(node)
{
}
RenderRubyAsBlock::~RenderRubyAsBlock()
{
}
bool RenderRubyAsBlock::isChildAllowed(RenderObject* child, RenderStyle*) const
{
return child->isRubyText()
|| child->isRubyRun()
|| child->isInline();
}
void RenderRubyAsBlock::addChild(RenderObject* child, RenderObject* beforeChild)
{
if (child->isRubyRun()) {
RenderBlock::addChild(child, beforeChild);
return;
}
if (beforeChild && !isAfterContent(beforeChild)) {
ASSERT(!beforeChild->isRubyRun());
RenderObject* run = beforeChild;
while (run && !run->isRubyRun())
run = run->parent();
if (run) {
run->addChild(child, beforeChild);
return;
}
ASSERT(false); }
RenderRubyRun* lastRun = lastRubyRun(this);
if (!lastRun || lastRun->hasRubyText()) {
lastRun = RenderRubyRun::staticCreateRubyRun(this);
RenderBlock::addChild(lastRun);
}
lastRun->addChild(child);
}
void RenderRubyAsBlock::removeChild(RenderObject* child)
{
if (child->parent() == this) {
RenderBlock::removeChild(child);
return;
}
RenderRubyRun* run = findRubyRunParent(child);
ASSERT(run);
run->removeChild(child);
}
}
#endif // ENABLE(RUBY)