1/*
2 * Copyright (C) 2016 Igalia S.L. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#include "config.h"
28#include "MathMLFractionElement.h"
29
30#if ENABLE(MATHML)
31
32#include "RenderMathMLFraction.h"
33#include "Settings.h"
34#include <wtf/IsoMallocInlines.h>
35
36namespace WebCore {
37
38WTF_MAKE_ISO_ALLOCATED_IMPL(MathMLFractionElement);
39
40using namespace MathMLNames;
41
42inline MathMLFractionElement::MathMLFractionElement(const QualifiedName& tagName, Document& document)
43 : MathMLPresentationElement(tagName, document)
44{
45}
46
47Ref<MathMLFractionElement> MathMLFractionElement::create(const QualifiedName& tagName, Document& document)
48{
49 return adoptRef(*new MathMLFractionElement(tagName, document));
50}
51
52const MathMLElement::Length& MathMLFractionElement::lineThickness()
53{
54 if (m_lineThickness)
55 return m_lineThickness.value();
56
57 auto& thickness = attributeWithoutSynchronization(linethicknessAttr);
58 if (document().settings().coreMathMLEnabled()) {
59 m_lineThickness = parseMathMLLength(thickness);
60 return m_lineThickness.value();
61 }
62
63 // The MathML3 recommendation states that "medium" is the default thickness.
64 // However, it only states that "thin" and "thick" are respectively thiner and thicker.
65 // The MathML in HTML5 implementation note suggests 50% and 200% and these values are also used in Gecko.
66 m_lineThickness = Length();
67 if (equalLettersIgnoringASCIICase(thickness, "thin")) {
68 m_lineThickness.value().type = LengthType::UnitLess;
69 m_lineThickness.value().value = .5;
70 } else if (equalLettersIgnoringASCIICase(thickness, "medium")) {
71 m_lineThickness.value().type = LengthType::UnitLess;
72 m_lineThickness.value().value = 1;
73 } else if (equalLettersIgnoringASCIICase(thickness, "thick")) {
74 m_lineThickness.value().type = LengthType::UnitLess;
75 m_lineThickness.value().value = 2;
76 } else
77 m_lineThickness = parseMathMLLength(thickness);
78 return m_lineThickness.value();
79}
80
81MathMLFractionElement::FractionAlignment MathMLFractionElement::cachedFractionAlignment(const QualifiedName& name, Optional<FractionAlignment>& alignment)
82{
83 if (alignment)
84 return alignment.value();
85
86 auto& value = attributeWithoutSynchronization(name);
87 if (equalLettersIgnoringASCIICase(value, "left"))
88 alignment = FractionAlignmentLeft;
89 else if (equalLettersIgnoringASCIICase(value, "right"))
90 alignment = FractionAlignmentRight;
91 else
92 alignment = FractionAlignmentCenter;
93 return alignment.value();
94}
95
96MathMLFractionElement::FractionAlignment MathMLFractionElement::numeratorAlignment()
97{
98 return cachedFractionAlignment(numalignAttr, m_numeratorAlignment);
99}
100
101MathMLFractionElement::FractionAlignment MathMLFractionElement::denominatorAlignment()
102{
103 return cachedFractionAlignment(denomalignAttr, m_denominatorAlignment);
104}
105
106void MathMLFractionElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
107{
108 if (name == linethicknessAttr)
109 m_lineThickness = WTF::nullopt;
110 else if (name == numalignAttr)
111 m_numeratorAlignment = WTF::nullopt;
112 else if (name == denomalignAttr)
113 m_denominatorAlignment = WTF::nullopt;
114
115 MathMLElement::parseAttribute(name, value);
116}
117
118RenderPtr<RenderElement> MathMLFractionElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
119{
120 ASSERT(hasTagName(MathMLNames::mfracTag));
121 return createRenderer<RenderMathMLFraction>(*this, WTFMove(style));
122}
123
124}
125
126#endif // ENABLE(MATHML)
127