RenderMathMLFenced.cpp [plain text]
#include "config.h"
#include "RenderMathMLFenced.h"
#if ENABLE(MATHML)
#include "FontSelector.h"
#include "MathMLNames.h"
#include "MathMLRowElement.h"
#include "RenderInline.h"
#include "RenderMathMLFencedOperator.h"
#include "RenderText.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
using namespace MathMLNames;
WTF_MAKE_ISO_ALLOCATED_IMPL(RenderMathMLFenced);
static const char* gOpeningBraceChar = "(";
static const char* gClosingBraceChar = ")";
RenderMathMLFenced::RenderMathMLFenced(MathMLRowElement& element, RenderStyle&& style)
: RenderMathMLRow(element, WTFMove(style))
{
}
void RenderMathMLFenced::updateFromElement()
{
const auto& fenced = element();
auto& open = fenced.attributeWithoutSynchronization(MathMLNames::openAttr);
m_open = open.isNull() ? gOpeningBraceChar : open;
auto& close = fenced.attributeWithoutSynchronization(MathMLNames::closeAttr);
m_close = close.isNull() ? gClosingBraceChar : close;
auto& separators = fenced.attributeWithoutSynchronization(MathMLNames::separatorsAttr);
if (!separators.isNull()) {
StringBuilder characters;
for (unsigned i = 0; i < separators.length(); i++) {
if (!isSpaceOrNewline(separators[i]))
characters.append(separators[i]);
}
m_separators = !characters.length() ? 0 : characters.toString().impl();
} else {
m_separators = StringImpl::create(",");
}
if (!firstChild())
makeFences();
else {
if (is<RenderMathMLFencedOperator>(*firstChild()))
downcast<RenderMathMLFencedOperator>(*firstChild()).updateOperatorContent(m_open);
m_closeFenceRenderer->updateOperatorContent(m_close);
}
}
RenderPtr<RenderMathMLFencedOperator> RenderMathMLFenced::createMathMLOperator(const String& operatorString, MathMLOperatorDictionary::Form form, MathMLOperatorDictionary::Flag flag)
{
RenderPtr<RenderMathMLFencedOperator> newOperator = createRenderer<RenderMathMLFencedOperator>(document(), RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK), operatorString, form, flag);
newOperator->initializeStyle();
return newOperator;
}
void RenderMathMLFenced::makeFences()
{
auto openFence = createMathMLOperator(m_open, MathMLOperatorDictionary::Prefix, MathMLOperatorDictionary::Fence);
RenderMathMLRow::addChild(WTFMove(openFence), firstChild());
auto closeFence = createMathMLOperator(m_close, MathMLOperatorDictionary::Postfix, MathMLOperatorDictionary::Fence);
m_closeFenceRenderer = makeWeakPtr(*closeFence);
RenderMathMLRow::addChild(WTFMove(closeFence));
}
void RenderMathMLFenced::addChild(RenderPtr<RenderObject> child, RenderObject* beforeChild)
{
if (!firstChild())
updateFromElement();
RenderPtr<RenderMathMLFencedOperator> separatorRenderer;
if (m_separators.get()) {
unsigned int 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 > m_separators.get()->length())
separator = (*m_separators.get())[m_separators.get()->length() - 1];
else
separator = (*m_separators.get())[count - 1];
StringBuilder builder;
builder.append(separator);
separatorRenderer = createMathMLOperator(builder.toString(), MathMLOperatorDictionary::Infix, MathMLOperatorDictionary::Separator);
}
}
if (beforeChild) {
RenderMathMLRow::addChild(WTFMove(child), beforeChild);
if (separatorRenderer)
RenderMathMLRow::addChild(WTFMove(separatorRenderer), beforeChild);
} else {
if (separatorRenderer)
RenderMathMLRow::addChild(WTFMove(separatorRenderer), m_closeFenceRenderer.get());
RenderMathMLRow::addChild(WTFMove(child), m_closeFenceRenderer.get());
}
}
}
#endif