RenderTreeBuilderMathML.cpp [plain text]
#include "config.h"
#include "RenderTreeBuilderMathML.h"
#if ENABLE(MATHML)
#include "RenderMathMLFenced.h"
#include "RenderMathMLFencedOperator.h"
#include "RenderTreeBuilderBlock.h"
namespace WebCore {
RenderTreeBuilder::MathML::MathML(RenderTreeBuilder& builder)
: m_builder(builder)
{
}
RenderPtr<RenderMathMLFencedOperator> RenderTreeBuilder::MathML::createMathMLOperator(RenderMathMLFenced& parent, const String& operatorString,
MathMLOperatorDictionary::Form form, MathMLOperatorDictionary::Flag flag)
{
RenderPtr<RenderMathMLFencedOperator> newOperator = createRenderer<RenderMathMLFencedOperator>(parent.document(), RenderStyle::createAnonymousStyleWithDisplay(parent.style(), DisplayType::Block), operatorString, form, flag);
newOperator->initializeStyle();
return newOperator;
}
void RenderTreeBuilder::MathML::makeFences(RenderMathMLFenced& parent)
{
auto openFence = createMathMLOperator(parent, parent.openingBrace(), MathMLOperatorDictionary::Prefix, MathMLOperatorDictionary::Fence);
m_builder.blockBuilder().attach(parent, WTFMove(openFence), parent.firstChild());
auto closeFence = createMathMLOperator(parent, parent.closingBrace(), MathMLOperatorDictionary::Postfix, MathMLOperatorDictionary::Fence);
parent.setCloseFenceRenderer(*closeFence);
m_builder.blockBuilder().attach(parent, WTFMove(closeFence), nullptr);
}
void RenderTreeBuilder::MathML::attach(RenderMathMLFenced& parent, RenderPtr<RenderObject> child, RenderObject* beforeChild)
{
if (!parent.firstChild()) {
parent.updateFromElement();
makeFences(parent);
}
RenderPtr<RenderMathMLFencedOperator> separatorRenderer;
auto* separators = parent.separators();
if (separators) {
unsigned count = 0;
for (Node* position = child->node(); position; position = position->previousSibling()) {
if (position->isElementNode())
count++;
}
if (!beforeChild) {
--count;
}
if (count > 0) {
UChar separator;
if (count > separators->length())
separator = (*separators)[separators->length() - 1];
else
separator = (*separators)[count - 1];
StringBuilder stringBuilder;
stringBuilder.append(separator);
separatorRenderer = createMathMLOperator(parent, stringBuilder.toString(), MathMLOperatorDictionary::Infix, MathMLOperatorDictionary::Separator);
}
}
if (beforeChild) {
m_builder.blockBuilder().attach(parent, WTFMove(child), beforeChild);
if (separatorRenderer)
m_builder.blockBuilder().attach(parent, WTFMove(separatorRenderer), beforeChild);
} else {
if (separatorRenderer)
m_builder.blockBuilder().attach(parent, WTFMove(separatorRenderer), parent.closeFenceRenderer());
m_builder.blockBuilder().attach(parent, WTFMove(child), parent.closeFenceRenderer());
}
}
}
#endif // ENABLE(MATHML)