1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "third_party/blink/renderer/core/mathml/mathml_padded_element.h"
6
7 #include "third_party/blink/renderer/core/css/style_change_reason.h"
8 #include "third_party/blink/renderer/core/layout/ng/mathml/layout_ng_mathml_block_with_anonymous_mrow.h"
9
10 namespace blink {
11
MathMLPaddedElement(Document & document)12 MathMLPaddedElement::MathMLPaddedElement(Document& document)
13 : MathMLRowElement(mathml_names::kMpaddedTag, document) {}
14
AddMathBaselineIfNeeded(ComputedStyle & style,const CSSToLengthConversionData & conversion_data)15 void MathMLPaddedElement::AddMathBaselineIfNeeded(
16 ComputedStyle& style,
17 const CSSToLengthConversionData& conversion_data) {
18 if (auto length_or_percentage_value = AddMathLengthToComputedStyle(
19 conversion_data, mathml_names::kHeightAttr, AllowPercentages::kNo))
20 style.SetMathBaseline(std::move(*length_or_percentage_value));
21 }
22
AddMathPaddedDepthIfNeeded(ComputedStyle & style,const CSSToLengthConversionData & conversion_data)23 void MathMLPaddedElement::AddMathPaddedDepthIfNeeded(
24 ComputedStyle& style,
25 const CSSToLengthConversionData& conversion_data) {
26 if (auto length_or_percentage_value = AddMathLengthToComputedStyle(
27 conversion_data, mathml_names::kDepthAttr, AllowPercentages::kNo))
28 style.SetMathPaddedDepth(std::move(*length_or_percentage_value));
29 }
30
AddMathPaddedLSpaceIfNeeded(ComputedStyle & style,const CSSToLengthConversionData & conversion_data)31 void MathMLPaddedElement::AddMathPaddedLSpaceIfNeeded(
32 ComputedStyle& style,
33 const CSSToLengthConversionData& conversion_data) {
34 if (auto length_or_percentage_value = AddMathLengthToComputedStyle(
35 conversion_data, mathml_names::kLspaceAttr))
36 style.SetMathLSpace(std::move(*length_or_percentage_value));
37 }
38
AddMathPaddedVOffsetIfNeeded(ComputedStyle & style,const CSSToLengthConversionData & conversion_data)39 void MathMLPaddedElement::AddMathPaddedVOffsetIfNeeded(
40 ComputedStyle& style,
41 const CSSToLengthConversionData& conversion_data) {
42 if (auto length_or_percentage_value = AddMathLengthToComputedStyle(
43 conversion_data, mathml_names::kVoffsetAttr))
44 style.SetMathPaddedVOffset(std::move(*length_or_percentage_value));
45 }
46
ParseAttribute(const AttributeModificationParams & param)47 void MathMLPaddedElement::ParseAttribute(
48 const AttributeModificationParams& param) {
49 if (param.name == mathml_names::kLspaceAttr ||
50 param.name == mathml_names::kVoffsetAttr) {
51 SetNeedsStyleRecalc(
52 kLocalStyleChange,
53 StyleChangeReasonForTracing::Create(style_change_reason::kAttribute));
54 if (GetLayoutObject() && GetLayoutObject()->IsMathML()) {
55 GetLayoutObject()
56 ->SetNeedsLayoutAndIntrinsicWidthsRecalcAndFullPaintInvalidation(
57 layout_invalidation_reason::kAttributeChanged);
58 }
59 }
60
61 MathMLRowElement::ParseAttribute(param);
62 }
63
IsPresentationAttribute(const QualifiedName & name) const64 bool MathMLPaddedElement::IsPresentationAttribute(
65 const QualifiedName& name) const {
66 if (name == mathml_names::kWidthAttr)
67 return true;
68 return MathMLElement::IsPresentationAttribute(name);
69 }
70
CollectStyleForPresentationAttribute(const QualifiedName & name,const AtomicString & value,MutableCSSPropertyValueSet * style)71 void MathMLPaddedElement::CollectStyleForPresentationAttribute(
72 const QualifiedName& name,
73 const AtomicString& value,
74 MutableCSSPropertyValueSet* style) {
75 if (name == mathml_names::kWidthAttr) {
76 if (!value.EndsWith('%')) {
77 AddPropertyToPresentationAttributeStyle(style, CSSPropertyID::kWidth,
78 value);
79 }
80 } else {
81 MathMLElement::CollectStyleForPresentationAttribute(name, value, style);
82 }
83 }
84
CreateLayoutObject(const ComputedStyle & style,LegacyLayout legacy)85 LayoutObject* MathMLPaddedElement::CreateLayoutObject(
86 const ComputedStyle& style,
87 LegacyLayout legacy) {
88 DCHECK(!style.IsDisplayMathType() || legacy != LegacyLayout::kForce);
89 if (!RuntimeEnabledFeatures::MathMLCoreEnabled() ||
90 !style.IsDisplayMathType())
91 return MathMLElement::CreateLayoutObject(style, legacy);
92 return new LayoutNGMathMLBlockWithAnonymousMrow(this);
93 }
94
95 } // namespace blink
96