#include "config.h"
#if ENABLE(RUBY)
#include "RenderRubyRun.h"
#include "RenderRubyBase.h"
#include "RenderRubyText.h"
#include "RenderView.h"
using namespace std;
namespace WebCore {
RenderRubyRun::RenderRubyRun(Node* node)
: RenderBlock(node)
, m_beingDestroyed(false)
{
setReplaced(true);
setInline(true);
}
RenderRubyRun::~RenderRubyRun()
{
}
void RenderRubyRun::destroy()
{
m_beingDestroyed = true;
RenderBlock::destroy();
}
bool RenderRubyRun::hasRubyText() const
{
return firstChild() && firstChild()->isRubyText();
}
bool RenderRubyRun::hasRubyBase() const
{
return lastChild() && lastChild()->isRubyBase();
}
bool RenderRubyRun::isEmpty() const
{
return !hasRubyText() && !hasRubyBase();
}
RenderRubyText* RenderRubyRun::rubyText() const
{
RenderObject* child = firstChild();
return child && child->isRubyText() ? static_cast<RenderRubyText*>(child) : 0;
}
RenderRubyBase* RenderRubyRun::rubyBase() const
{
RenderObject* child = lastChild();
return child && child->isRubyBase() ? static_cast<RenderRubyBase*>(child) : 0;
}
RenderRubyBase* RenderRubyRun::rubyBaseSafe()
{
RenderRubyBase* base = rubyBase();
if (!base) {
base = createRubyBase();
RenderBlock::addChild(base);
}
return base;
}
RenderBlock* RenderRubyRun::firstLineBlock() const
{
return 0;
}
void RenderRubyRun::updateFirstLetter()
{
}
bool RenderRubyRun::isChildAllowed(RenderObject* child, RenderStyle*) const
{
return child->isRubyText() || child->isInline();
}
void RenderRubyRun::addChild(RenderObject* child, RenderObject* beforeChild)
{
ASSERT(child);
if (child->isRubyText()) {
if (!beforeChild) {
ASSERT(!hasRubyText());
RenderBlock::addChild(child, firstChild());
} else if (beforeChild->isRubyText()) {
ASSERT(beforeChild->parent() == this);
RenderObject* ruby = parent();
ASSERT(ruby->isRuby());
RenderBlock* newRun = staticCreateRubyRun(ruby);
ruby->addChild(newRun, nextSibling());
RenderBlock::addChild(child, beforeChild);
RenderBlock::removeChild(beforeChild);
newRun->addChild(beforeChild);
} else {
ASSERT(hasRubyBase()); RenderObject* ruby = parent();
RenderRubyRun* newRun = staticCreateRubyRun(ruby);
ruby->addChild(newRun, this);
newRun->addChild(child);
rubyBaseSafe()->moveChildren(newRun->rubyBaseSafe(), beforeChild);
}
} else {
if (beforeChild && beforeChild->isRubyText())
beforeChild = 0;
rubyBaseSafe()->addChild(child, beforeChild);
}
}
void RenderRubyRun::removeChild(RenderObject* child)
{
if (!m_beingDestroyed && !documentBeingDestroyed() && child->isRubyText()) {
RenderRubyBase* base = rubyBase();
RenderObject* rightNeighbour = nextSibling();
if (base && rightNeighbour && rightNeighbour->isRubyRun()) {
RenderRubyRun* rightRun = static_cast<RenderRubyRun*>(rightNeighbour);
ASSERT(rightRun->hasRubyBase());
RenderRubyBase* rightBase = rightRun->rubyBaseSafe();
rightBase->moveChildren(base);
moveChildTo(rightRun, rightRun->children(), base);
rightRun->moveChildTo(this, children(), rightBase);
}
}
RenderBlock::removeChild(child);
if (!m_beingDestroyed && !documentBeingDestroyed()) {
RenderBlock* base = rubyBase();
if (base && !base->firstChild()) {
RenderBlock::removeChild(base);
base->deleteLineBoxTree();
base->destroy();
}
if (isEmpty()) {
parent()->removeChild(this);
deleteLineBoxTree();
destroy();
}
}
}
RenderRubyBase* RenderRubyRun::createRubyBase() const
{
RenderRubyBase* rb = new (renderArena()) RenderRubyBase(document() );
RefPtr<RenderStyle> newStyle = RenderStyle::create();
newStyle->inheritFrom(style());
newStyle->setDisplay(BLOCK);
newStyle->setTextAlign(CENTER); rb->setStyle(newStyle.release());
return rb;
}
RenderRubyRun* RenderRubyRun::staticCreateRubyRun(const RenderObject* parentRuby)
{
ASSERT(parentRuby && parentRuby->isRuby());
RenderRubyRun* rr = new (parentRuby->renderArena()) RenderRubyRun(parentRuby->document() );
RefPtr<RenderStyle> newStyle = RenderStyle::create();
newStyle->inheritFrom(parentRuby->style());
newStyle->setDisplay(INLINE_BLOCK);
rr->setStyle(newStyle.release());
return rr;
}
}
#endif