MathMLOperatorElement.cpp [plain text]
#include "config.h"
#include "MathMLOperatorElement.h"
#if ENABLE(MATHML)
#include "RenderMathMLOperator.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/unicode/CharacterNames.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(MathMLOperatorElement);
using namespace MathMLNames;
using namespace MathMLOperatorDictionary;
MathMLOperatorElement::MathMLOperatorElement(const QualifiedName& tagName, Document& document)
: MathMLTokenElement(tagName, document)
{
}
Ref<MathMLOperatorElement> MathMLOperatorElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new MathMLOperatorElement(tagName, document));
}
MathMLOperatorElement::OperatorChar MathMLOperatorElement::parseOperatorChar(const String& string)
{
OperatorChar operatorChar;
if (auto codePoint = convertToSingleCodePoint(string)) {
auto character = codePoint.value();
if (character == hyphenMinus)
character = minusSign;
operatorChar.character = character;
operatorChar.isVertical = isVertical(operatorChar.character);
}
return operatorChar;
}
const MathMLOperatorElement::OperatorChar& MathMLOperatorElement::operatorChar()
{
if (!m_operatorChar)
m_operatorChar = parseOperatorChar(textContent());
return m_operatorChar.value();
}
Property MathMLOperatorElement::computeDictionaryProperty()
{
Property dictionaryProperty;
const auto& value = attributeWithoutSynchronization(formAttr);
bool explicitForm = true;
if (value == "prefix")
dictionaryProperty.form = Prefix;
else if (value == "infix")
dictionaryProperty.form = Infix;
else if (value == "postfix")
dictionaryProperty.form = Postfix;
else {
explicitForm = false;
if (!previousSibling() && nextSibling())
dictionaryProperty.form = Prefix;
else if (previousSibling() && !nextSibling())
dictionaryProperty.form = Postfix;
else
dictionaryProperty.form = Infix;
}
if (auto entry = search(operatorChar().character, dictionaryProperty.form, explicitForm))
dictionaryProperty = entry.value();
return dictionaryProperty;
}
const Property& MathMLOperatorElement::dictionaryProperty()
{
if (!m_dictionaryProperty)
m_dictionaryProperty = computeDictionaryProperty();
return m_dictionaryProperty.value();
}
static const QualifiedName& propertyFlagToAttributeName(MathMLOperatorDictionary::Flag flag)
{
switch (flag) {
case Accent:
return accentAttr;
case Fence:
return fenceAttr;
case LargeOp:
return largeopAttr;
case MovableLimits:
return movablelimitsAttr;
case Separator:
return separatorAttr;
case Stretchy:
return stretchyAttr;
case Symmetric:
return symmetricAttr;
}
ASSERT_NOT_REACHED();
return nullQName();
}
void MathMLOperatorElement::computeOperatorFlag(MathMLOperatorDictionary::Flag flag)
{
ASSERT(m_properties.dirtyFlags & flag);
Optional<BooleanValue> property;
const auto& name = propertyFlagToAttributeName(flag);
const BooleanValue& value = cachedBooleanAttribute(name, property);
switch (value) {
case BooleanValue::True:
m_properties.flags |= flag;
break;
case BooleanValue::False:
m_properties.flags &= ~flag;
break;
case BooleanValue::Default:
if (dictionaryProperty().flags & flag)
m_properties.flags |= flag;
else
m_properties.flags &= ~flag;
break;
}
}
bool MathMLOperatorElement::hasProperty(MathMLOperatorDictionary::Flag flag)
{
if (m_properties.dirtyFlags & flag) {
computeOperatorFlag(flag);
m_properties.dirtyFlags &= ~flag;
}
return m_properties.flags & flag;
}
MathMLElement::Length MathMLOperatorElement::defaultLeadingSpace()
{
Length space;
space.type = LengthType::MathUnit;
space.value = static_cast<float>(dictionaryProperty().leadingSpaceInMathUnit);
return space;
}
MathMLElement::Length MathMLOperatorElement::defaultTrailingSpace()
{
Length space;
space.type = LengthType::MathUnit;
space.value = static_cast<float>(dictionaryProperty().trailingSpaceInMathUnit);
return space;
}
const MathMLElement::Length& MathMLOperatorElement::leadingSpace()
{
return cachedMathMLLength(MathMLNames::lspaceAttr, m_leadingSpace);
}
const MathMLElement::Length& MathMLOperatorElement::trailingSpace()
{
return cachedMathMLLength(MathMLNames::rspaceAttr, m_trailingSpace);
}
const MathMLElement::Length& MathMLOperatorElement::minSize()
{
return cachedMathMLLength(MathMLNames::minsizeAttr, m_minSize);
}
const MathMLElement::Length& MathMLOperatorElement::maxSize()
{
return cachedMathMLLength(MathMLNames::maxsizeAttr, m_maxSize);
}
void MathMLOperatorElement::childrenChanged(const ChildChange& change)
{
m_operatorChar = WTF::nullopt;
m_dictionaryProperty = WTF::nullopt;
m_properties.dirtyFlags = MathMLOperatorDictionary::allFlags;
MathMLTokenElement::childrenChanged(change);
}
static Optional<MathMLOperatorDictionary::Flag> attributeNameToPropertyFlag(const QualifiedName& name)
{
if (name == accentAttr)
return Accent;
if (name == fenceAttr)
return Fence;
if (name == largeopAttr)
return LargeOp;
if (name == movablelimitsAttr)
return MovableLimits;
if (name == separatorAttr)
return Separator;
if (name == stretchyAttr)
return Stretchy;
if (name == symmetricAttr)
return Symmetric;
return WTF::nullopt;
}
void MathMLOperatorElement::parseAttribute(const QualifiedName& name, const AtomString& value)
{
if (name == formAttr) {
m_dictionaryProperty = WTF::nullopt;
m_properties.dirtyFlags = MathMLOperatorDictionary::allFlags;
} else if (auto flag = attributeNameToPropertyFlag(name))
m_properties.dirtyFlags |= flag.value();
else if (name == lspaceAttr)
m_leadingSpace = WTF::nullopt;
else if (name == rspaceAttr)
m_trailingSpace = WTF::nullopt;
else if (name == minsizeAttr)
m_minSize = WTF::nullopt;
else if (name == maxsizeAttr)
m_maxSize = WTF::nullopt;
if ((name == stretchyAttr || name == lspaceAttr || name == rspaceAttr || name == movablelimitsAttr) && renderer()) {
downcast<RenderMathMLOperator>(*renderer()).updateFromElement();
return;
}
MathMLTokenElement::parseAttribute(name, value);
}
RenderPtr<RenderElement> MathMLOperatorElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
{
ASSERT(hasTagName(MathMLNames::moTag));
return createRenderer<RenderMathMLOperator>(*this, WTFMove(style));
}
}
#endif // ENABLE(MATHML)