1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
* Copyright (C) 2016 Igalia S.L. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#include "MathMLOperatorElement.h"
#if ENABLE(MATHML)
#include "ElementInlines.h"
#include "NodeName.h"
#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;
// FIXME: This operator dictionary does not accept multiple characters (https://webkit.org/b/124828).
if (auto codePoint = convertToSingleCodePoint(string)) {
auto character = codePoint.value();
// The minus sign renders better than the hyphen sign used in some MathML formulas.
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;
// We first determine the form attribute and use the default spacing and properties.
const auto& value = attributeWithoutSynchronization(formAttr);
bool explicitForm = true;
if (value == "prefix"_s)
dictionaryProperty.form = Prefix;
else if (value == "infix"_s)
dictionaryProperty.form = Infix;
else if (value == "postfix"_s)
dictionaryProperty.form = Postfix;
else {
// FIXME: We should use more advanced heuristics indicated in the specification to determine the operator form (https://bugs.webkit.org/show_bug.cgi?id=124829).
explicitForm = false;
if (!previousSibling() && nextSibling())
dictionaryProperty.form = Prefix;
else if (previousSibling() && !nextSibling())
dictionaryProperty.form = Postfix;
else
dictionaryProperty.form = Infix;
}
// We then try and find an entry in the operator dictionary to override the default values.
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);
std::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:
// By default, we use the value specified in the operator dictionary.
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 = std::nullopt;
m_dictionaryProperty = std::nullopt;
m_properties.dirtyFlags = MathMLOperatorDictionary::allFlags;
MathMLTokenElement::childrenChanged(change);
}
void MathMLOperatorElement::attributeChanged(const QualifiedName& name, const AtomString& oldValue, const AtomString& newValue, AttributeModificationReason attributeModificationReason)
{
switch (name.nodeName()) {
case AttributeNames::formAttr:
m_dictionaryProperty = std::nullopt;
m_properties.dirtyFlags = MathMLOperatorDictionary::allFlags;
break;
case AttributeNames::lspaceAttr:
m_leadingSpace = std::nullopt;
if (renderer())
downcast<RenderMathMLOperator>(*renderer()).updateFromElement();
break;
case AttributeNames::rspaceAttr:
m_trailingSpace = std::nullopt;
if (renderer())
downcast<RenderMathMLOperator>(*renderer()).updateFromElement();
break;
case AttributeNames::minsizeAttr:
m_minSize = std::nullopt;
break;
case AttributeNames::maxsizeAttr:
m_maxSize = std::nullopt;
break;
case AttributeNames::stretchyAttr:
m_properties.dirtyFlags |= Stretchy;
if (renderer())
downcast<RenderMathMLOperator>(*renderer()).updateFromElement();
break;
case AttributeNames::movablelimitsAttr:
m_properties.dirtyFlags |= MovableLimits;
if (renderer())
downcast<RenderMathMLOperator>(*renderer()).updateFromElement();
break;
case AttributeNames::accentAttr:
m_properties.dirtyFlags |= Accent;
break;
case AttributeNames::fenceAttr:
m_properties.dirtyFlags |= Fence;
break;
case AttributeNames::largeopAttr:
m_properties.dirtyFlags |= LargeOp;
break;
case AttributeNames::separatorAttr:
m_properties.dirtyFlags |= Separator;
break;
case AttributeNames::symmetricAttr:
m_properties.dirtyFlags |= Symmetric;
break;
default:
break;
}
MathMLTokenElement::attributeChanged(name, oldValue, newValue, attributeModificationReason);
}
RenderPtr<RenderElement> MathMLOperatorElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
{
ASSERT(hasTagName(MathMLNames::moTag));
return createRenderer<RenderMathMLOperator>(*this, WTFMove(style));
}
}
#endif // ENABLE(MATHML)
|