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
|
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/mathml/mathml_element.h"
#include "third_party/blink/renderer/bindings/core/v8/js_event_handler_for_content_attribute.h"
#include "third_party/blink/renderer/core/css/css_property_name.h"
#include "third_party/blink/renderer/core/css/parser/css_parser.h"
#include "third_party/blink/renderer/core/css_value_keywords.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/platform/wtf/text/character_visitor.h"
#include "third_party/blink/renderer/platform/wtf/text/string_to_number.h"
namespace blink {
MathMLElement::MathMLElement(const QualifiedName& tagName,
Document& document,
ConstructionType constructionType)
: Element(tagName, &document, constructionType) {}
MathMLElement::~MathMLElement() {}
static inline bool IsValidDirAttribute(const AtomicString& value) {
return EqualIgnoringASCIICase(value, "ltr") ||
EqualIgnoringASCIICase(value, "rtl");
}
// Keywords from MathML3 and CSS font-size are skipped.
static inline bool IsDisallowedMathSizeAttribute(const AtomicString& value) {
return EqualIgnoringASCIICase(value, "medium") ||
value.EndsWith("large", kTextCaseASCIIInsensitive) ||
value.EndsWith("small", kTextCaseASCIIInsensitive) ||
EqualIgnoringASCIICase(value, "smaller") ||
EqualIgnoringASCIICase(value, "larger");
}
bool MathMLElement::IsPresentationAttribute(const QualifiedName& name) const {
if (name == html_names::kDirAttr || name == mathml_names::kMathsizeAttr ||
name == mathml_names::kMathcolorAttr ||
name == mathml_names::kMathbackgroundAttr ||
name == mathml_names::kMathvariantAttr ||
name == mathml_names::kScriptlevelAttr ||
name == mathml_names::kDisplayAttr ||
name == mathml_names::kDisplaystyleAttr)
return true;
return Element::IsPresentationAttribute(name);
}
namespace {
bool ParseScriptLevel(const AtomicString& attributeValue,
unsigned& scriptLevel,
bool& add) {
String value = attributeValue;
if (value.StartsWith("+") || value.StartsWith("-")) {
add = true;
value = value.Right(1);
}
return WTF::VisitCharacters(
value, [&](const auto* position, unsigned length) {
WTF::NumberParsingResult result;
WTF::NumberParsingOptions options(
WTF::NumberParsingOptions::kAcceptMinusZeroForUnsigned);
scriptLevel = CharactersToUInt(position, length, options, &result);
return result == WTF::NumberParsingResult::kSuccess;
});
}
} // namespace
void MathMLElement::CollectStyleForPresentationAttribute(
const QualifiedName& name,
const AtomicString& value,
MutableCSSPropertyValueSet* style) {
if (name == html_names::kDirAttr) {
if (IsValidDirAttribute(value)) {
AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kDirection,
value);
}
} else if (name == mathml_names::kMathsizeAttr) {
if (!IsDisallowedMathSizeAttribute(value)) {
AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kFontSize,
value);
}
} else if (name == mathml_names::kMathbackgroundAttr) {
AddPropertyToPresentationAttributeStyle(
style, CSSPropertyID::kBackgroundColor, value);
} else if (name == mathml_names::kMathcolorAttr) {
AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kColor,
value);
} else if (name == mathml_names::kScriptlevelAttr) {
unsigned scriptLevel = 0;
bool add = false;
if (ParseScriptLevel(value, scriptLevel, add)) {
if (add) {
AddPropertyToPresentationAttributeStyle(
style, CSSPropertyID::kMathDepth, "add(" + value + ")");
} else {
AddPropertyToPresentationAttributeStyle(
style, CSSPropertyID::kMathDepth, scriptLevel,
CSSPrimitiveValue::UnitType::kNumber);
}
}
} else if (name == mathml_names::kDisplayAttr &&
HasTagName(mathml_names::kMathTag)) {
if (EqualIgnoringASCIICase(value, "inline")) {
AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kDisplay,
CSSValueID::kMath);
AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kMathStyle,
CSSValueID::kCompact);
} else if (EqualIgnoringASCIICase(value, "block")) {
AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kDisplay,
"block math");
AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kMathStyle,
CSSValueID::kNormal);
}
} else if (name == mathml_names::kDisplaystyleAttr) {
if (EqualIgnoringASCIICase(value, "false")) {
AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kMathStyle,
CSSValueID::kCompact);
} else if (EqualIgnoringASCIICase(value, "true")) {
AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kMathStyle,
CSSValueID::kNormal);
}
} else if (name == mathml_names::kMathvariantAttr) {
// TODO(crbug.com/1076420): this needs to handle all mathvariant values.
if (EqualIgnoringASCIICase(value, "normal")) {
AddPropertyToPresentationAttributeStyle(
style, CSSPropertyID::kTextTransform, CSSValueID::kNone);
}
} else {
Element::CollectStyleForPresentationAttribute(name, value, style);
}
}
void MathMLElement::ParseAttribute(const AttributeModificationParams& param) {
const AtomicString& event_name =
HTMLElement::EventNameForAttributeName(param.name);
if (!event_name.IsNull()) {
SetAttributeEventListener(
event_name, JSEventHandlerForContentAttribute::Create(
GetExecutionContext(), param.name, param.new_value));
return;
}
Element::ParseAttribute(param);
}
base::Optional<bool> MathMLElement::BooleanAttribute(
const QualifiedName& name) const {
const AtomicString& value = FastGetAttribute(name);
if (EqualIgnoringASCIICase(value, "true"))
return true;
if (EqualIgnoringASCIICase(value, "false"))
return false;
return base::nullopt;
}
base::Optional<Length> MathMLElement::AddMathLengthToComputedStyle(
const CSSToLengthConversionData& conversion_data,
const QualifiedName& attr_name,
AllowPercentages allow_percentages) {
if (!FastHasAttribute(attr_name))
return base::nullopt;
auto value = FastGetAttribute(attr_name);
const CSSPrimitiveValue* parsed_value = CSSParser::ParseLengthPercentage(
value,
StrictCSSParserContext(GetExecutionContext()->GetSecureContextMode()));
if (!parsed_value || parsed_value->IsCalculated() ||
(parsed_value->IsPercentage() &&
(!value.EndsWith('%') || allow_percentages == AllowPercentages::kNo)))
return base::nullopt;
return parsed_value->ConvertToLength(conversion_data);
}
bool MathMLElement::IsTokenElement() const {
return HasTagName(mathml_names::kMiTag) || HasTagName(mathml_names::kMoTag) ||
HasTagName(mathml_names::kMnTag) ||
HasTagName(mathml_names::kMtextTag) ||
HasTagName(mathml_names::kMsTag);
}
} // namespace blink
|