RenderMathMLFenced.cpp [plain text]
#include "config.h"
#if ENABLE(MATHML)
#include "RenderMathMLFenced.h"
#include "FontSelector.h"
#include "MathMLNames.h"
#include "RenderInline.h"
#include "RenderMathMLOperator.h"
#include "RenderText.h"
#include <wtf/text/StringBuilder.h>
namespace WebCore {
using namespace MathMLNames;
static const char* gOpeningBraceChar = "(";
static const char* gClosingBraceChar = ")";
RenderMathMLFenced::RenderMathMLFenced(MathMLInlineContainerElement& element, RenderStyle&& style)
: RenderMathMLRow(element, WTFMove(style))
, m_closeFenceRenderer(nullptr)
{
}
void RenderMathMLFenced::updateFromElement()
{
const auto& fenced = element();
AtomicString open = fenced.attributeWithoutSynchronization(MathMLNames::openAttr);
m_open = open.isNull() ? gOpeningBraceChar : open;
AtomicString close = fenced.attributeWithoutSynchronization(MathMLNames::closeAttr);
m_close = close.isNull() ? gClosingBraceChar : close;
AtomicString separators = fenced.attributeWithoutSynchronization(MathMLNames::separatorsAttr);
if (!separators.isNull()) {
StringBuilder characters;
for (unsigned int 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 (isEmpty())
makeFences();
else {
downcast<RenderMathMLOperator>(*firstChild()).updateTokenContent(m_open);
m_closeFenceRenderer->updateTokenContent(m_close);
}
}
RenderPtr<RenderMathMLOperator> RenderMathMLFenced::createMathMLOperator(const String& operatorString, MathMLOperatorDictionary::Form form, MathMLOperatorDictionary::Flag flag)
{
RenderPtr<RenderMathMLOperator> newOperator = createRenderer<RenderMathMLOperator>(document(), RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK), operatorString, form, flag);
newOperator->initializeStyle();
return newOperator;
}
void RenderMathMLFenced::makeFences()
{
RenderPtr<RenderMathMLOperator> openFence = createMathMLOperator(m_open, MathMLOperatorDictionary::Prefix, MathMLOperatorDictionary::Fence);
RenderMathMLRow::addChild(openFence.leakPtr(), firstChild());
RenderPtr<RenderMathMLOperator> closeFence = createMathMLOperator(m_close, MathMLOperatorDictionary::Postfix, MathMLOperatorDictionary::Fence);
m_closeFenceRenderer = closeFence.get();
RenderMathMLRow::addChild(closeFence.leakPtr());
}
void RenderMathMLFenced::addChild(RenderObject* child, RenderObject* beforeChild)
{
if (isEmpty())
updateFromElement();
RenderPtr<RenderMathMLOperator> 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(child, beforeChild);
if (separatorRenderer)
RenderMathMLRow::addChild(separatorRenderer.leakPtr(), beforeChild);
} else {
if (separatorRenderer)
RenderMathMLRow::addChild(separatorRenderer.leakPtr(), m_closeFenceRenderer);
RenderMathMLRow::addChild(child, m_closeFenceRenderer);
}
}
}
#endif