1/*
2 * Copyright (C) 2014 Gurpreet Kaur (k.gurpreet@samsung.com). 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#include "config.h"
27
28#if ENABLE(MATHML)
29#include "MathMLMencloseElement.h"
30
31#include "MathMLNames.h"
32#include "RenderElement.h"
33#include "RenderMathMLMenclose.h"
34#include "RenderObject.h"
35#include "TextRun.h"
36#include <wtf/Ref.h>
37#include <wtf/text/StringBuilder.h>
38
39
40namespace WebCore {
41
42MathMLMencloseElement::MathMLMencloseElement(const QualifiedName& tagName, Document& document)
43 : MathMLInlineContainerElement(tagName, document)
44 , m_isRadicalValue(false)
45{
46}
47
48PassRefPtr<MathMLMencloseElement> MathMLMencloseElement::create(const QualifiedName& tagName, Document& document)
49{
50 return adoptRef(new MathMLMencloseElement(tagName, document));
51}
52
53RenderPtr<RenderElement> MathMLMencloseElement::createElementRenderer(PassRef<RenderStyle> style)
54{
55 return createRenderer<RenderMathMLMenclose>(*this, std::move(style));
56}
57
58bool MathMLMencloseElement::isPresentationAttribute(const QualifiedName& name) const
59{
60 if (name == MathMLNames::notationAttr)
61 return true;
62 return MathMLElement::isPresentationAttribute(name);
63}
64
65void MathMLMencloseElement::finishParsingChildren()
66{
67 MathMLInlineContainerElement::finishParsingChildren();
68 // when notation value is radical and menclose does not have any child then
69 // we add anonymous squareroot child to menclose so that square root symbol
70 // can be rendered.
71 if (m_isRadicalValue && !firstElementChild())
72 renderer()->addChild(nullptr, nullptr);
73}
74
75void MathMLMencloseElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStyleProperties& style)
76{
77 if (name == MathMLNames::notationAttr) {
78 String val = value;
79 if (!val.isEmpty()) {
80 val.split(" ", m_notationValues);
81 size_t attrvaluesSize = m_notationValues.size();
82 for (size_t i = 0; i < attrvaluesSize; i++) {
83 if (m_notationValues[i] == "top" || m_notationValues[i] == "longdiv") {
84 if (m_notationValues[i]== "longdiv")
85 addPropertyToPresentationAttributeStyle(style, CSSPropertyPaddingLeft, longDivLeftPadding());
86 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderTopStyle, "solid");
87 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderTopWidth, "thin");
88 addPropertyToPresentationAttributeStyle(style, CSSPropertyPaddingTop, ".3ex");
89 } else if (m_notationValues[i] == "bottom") {
90 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderBottomStyle, "solid");
91 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderBottomWidth, "thin");
92 addPropertyToPresentationAttributeStyle(style, CSSPropertyPaddingBottom, ".3ex");
93 } else if (m_notationValues[i] == "left") {
94 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderLeftStyle, "solid");
95 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderLeftWidth, "thin");
96 addPropertyToPresentationAttributeStyle(style, CSSPropertyPaddingLeft, ".3ex");
97 } else if (m_notationValues[i] == "right") {
98 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderRightStyle, "solid");
99 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderRightWidth, "thin");
100 addPropertyToPresentationAttributeStyle(style, CSSPropertyPaddingRight, ".3ex");
101 } else if (m_notationValues[i] == "box" || m_notationValues[i] == "roundedbox") {
102 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderStyle, "solid");
103 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderWidth, "thin");
104 addPropertyToPresentationAttributeStyle(style, CSSPropertyPadding, ".3ex");
105 if (m_notationValues[i] == "roundedbox")
106 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderRadius, ASCIILiteral("5px"));
107 } else if (m_notationValues[i] == "actuarial" || m_notationValues[i] == "madruwb") {
108 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderRightStyle, "solid");
109 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderRightWidth, "thin");
110 addPropertyToPresentationAttributeStyle(style, CSSPropertyPaddingRight, ".3ex");
111 if (m_notationValues[i] == "actuarial") {
112 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderTopStyle, "solid");
113 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderTopWidth, "thin");
114 addPropertyToPresentationAttributeStyle(style, CSSPropertyPaddingTop, ".3ex");
115 } else if (m_notationValues[i] == "madruwb") {
116 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderBottomStyle, "solid");
117 addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderBottomWidth, "thin");
118 addPropertyToPresentationAttributeStyle(style, CSSPropertyPaddingBottom, ".3ex");
119 }
120 } else if (m_notationValues[i] == "radical")
121 m_isRadicalValue = true;
122 }
123 }
124 } else
125 MathMLInlineContainerElement::collectStyleForPresentationAttribute(name, value, style);
126}
127
128
129String MathMLMencloseElement::longDivLeftPadding() const
130{
131 StringBuilder padding;
132 float fontSize = 0;
133 String closingBrace = ")";
134 TextRun run(closingBrace.impl(), closingBrace.length());
135 Node* node = parentNode();
136 if (node && node->renderer()) {
137 const Font& font = node->renderer()->style().font();
138 fontSize = font.width(run);
139 padding.append(String::number(fontSize));
140 padding.append("px");
141 }
142 return padding.toString();
143}
144
145}
146#endif // ENABLE(MATHML)