1/*
2 * Copyright (C) 2013 The MathJax Consortium. 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
30#include "MathMLSelectElement.h"
31
32#include "MathMLNames.h"
33#include "RenderMathMLRow.h"
34
35namespace WebCore {
36
37using namespace MathMLNames;
38
39MathMLSelectElement::MathMLSelectElement(const QualifiedName& tagName, Document& document)
40 : MathMLInlineContainerElement(tagName, document), m_selectedChild(nullptr)
41{
42}
43
44PassRefPtr<MathMLSelectElement> MathMLSelectElement::create(const QualifiedName& tagName, Document& document)
45{
46 return adoptRef(new MathMLSelectElement(tagName, document));
47}
48
49RenderElement* MathMLSelectElement::createRenderer(PassRef<RenderStyle> style)
50{
51 return new RenderMathMLRow(*this, std::move(style));
52}
53
54bool MathMLSelectElement::childShouldCreateRenderer(const Node* child) const
55{
56 return child == m_selectedChild;
57}
58
59void MathMLSelectElement::finishParsingChildren()
60{
61 updateSelectedChild();
62 MathMLInlineContainerElement::finishParsingChildren();
63}
64
65void MathMLSelectElement::childrenChanged(const ChildChange& change)
66{
67 updateSelectedChild();
68 MathMLInlineContainerElement::childrenChanged(change);
69}
70
71void MathMLSelectElement::attributeChanged(const QualifiedName& name, const AtomicString& newValue, AttributeModificationReason reason)
72{
73 if (hasLocalName(mactionTag) && (name == MathMLNames::actiontypeAttr || name == MathMLNames::selectionAttr))
74 updateSelectedChild();
75
76 MathMLInlineContainerElement::attributeChanged(name, newValue, reason);
77}
78
79void MathMLSelectElement::updateSelectedChild()
80{
81 Element* newSelectedChild = firstElementChild();
82
83 if (newSelectedChild) {
84 if (hasLocalName(mactionTag)) {
85 // FIXME: implement user interaction for the "tooltip", "statusline" and "toggle" action types. See bug 85734.
86 const AtomicString& actiontype = fastGetAttribute(MathMLNames::actiontypeAttr);
87 if (actiontype != "tooltip" && actiontype != "statusline") {
88 // For the "toggle" action type or any unknown action type, we rely on the value of the selection attribute to determine the visible child.
89 int selection = fastGetAttribute(MathMLNames::selectionAttr).toInt();
90 for (int i = 1; i < selection; i++) {
91 Element* nextChild = newSelectedChild->nextElementSibling();
92 if (!nextChild)
93 break;
94 newSelectedChild = nextChild;
95 }
96 }
97 } else {
98 ASSERT(hasLocalName(semanticsTag));
99 // FIXME: implement Gecko's selection algorithm for <semantics>. See bug 100626.
100 }
101 }
102
103 if (m_selectedChild == newSelectedChild)
104 return;
105
106 if (m_selectedChild && m_selectedChild->renderer())
107 Style::detachRenderTree(*m_selectedChild);
108
109 m_selectedChild = newSelectedChild;
110 setNeedsStyleRecalc();
111}
112
113}
114
115#endif // ENABLE(MATHML)