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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
* Copyright (C) 2013 The MathJax Consortium. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "MathMLSelectElement.h"
#if ENABLE(MATHML)
#include "Event.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "MathMLNames.h"
#include "RenderMathMLRow.h"
#include "SVGElement.h"
#include "SVGNames.h"
#include "StyleTreeResolver.h"
namespace WebCore {
using namespace MathMLNames;
MathMLSelectElement::MathMLSelectElement(const QualifiedName& tagName, Document& document)
: MathMLInlineContainerElement(tagName, document)
, m_selectedChild(nullptr)
{
}
Ref<MathMLSelectElement> MathMLSelectElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new MathMLSelectElement(tagName, document));
}
RenderPtr<RenderElement> MathMLSelectElement::createElementRenderer(Ref<RenderStyle>&& style, const RenderTreePosition&)
{
return createRenderer<RenderMathMLRow>(*this, WTFMove(style));
}
// We recognize the following values for the encoding attribute of the <semantics> element:
//
// - "MathML-Presentation", which is mentioned in the MathML 3 recommendation.
// - "SVG1.1" which is mentioned in the W3C note.
// http://www.w3.org/Math/Documents/Notes/graphics.xml
// - Other MIME Content-Types for MathML, SVG and HTML.
//
// We exclude "application/mathml+xml" which is ambiguous about whether it is Presentation or Content MathML. Authors must use a more explicit encoding value.
bool MathMLSelectElement::isMathMLEncoding(const AtomicString& value)
{
return value == "application/mathml-presentation+xml" || value == "MathML-Presentation";
}
bool MathMLSelectElement::isSVGEncoding(const AtomicString& value)
{
return value == "image/svg+xml" || value == "SVG1.1";
}
bool MathMLSelectElement::isHTMLEncoding(const AtomicString& value)
{
return value == "application/xhtml+xml" || value == "text/html";
}
bool MathMLSelectElement::childShouldCreateRenderer(const Node& child) const
{
return MathMLElement::childShouldCreateRenderer(child) && m_selectedChild == &child;
}
void MathMLSelectElement::finishParsingChildren()
{
updateSelectedChild();
MathMLInlineContainerElement::finishParsingChildren();
}
void MathMLSelectElement::childrenChanged(const ChildChange& change)
{
updateSelectedChild();
MathMLInlineContainerElement::childrenChanged(change);
}
void MathMLSelectElement::attributeChanged(const QualifiedName& name, const AtomicString& oldValue, const AtomicString& newValue, AttributeModificationReason reason)
{
if (hasTagName(mactionTag) && (name == MathMLNames::actiontypeAttr || name == MathMLNames::selectionAttr))
updateSelectedChild();
MathMLInlineContainerElement::attributeChanged(name, oldValue, newValue, reason);
}
int MathMLSelectElement::getSelectedActionChildAndIndex(Element*& selectedChild)
{
ASSERT(hasTagName(mactionTag));
// We "round up or down to the closest allowable value" of the selection attribute, as suggested by the MathML specification.
selectedChild = firstElementChild();
if (!selectedChild)
return 1;
int selection = fastGetAttribute(MathMLNames::selectionAttr).toInt();
int i;
for (i = 1; i < selection; i++) {
Element* nextChild = selectedChild->nextElementSibling();
if (!nextChild)
break;
selectedChild = nextChild;
}
return i;
}
Element* MathMLSelectElement::getSelectedActionChild()
{
ASSERT(hasTagName(mactionTag));
Element* child = firstElementChild();
if (!child)
return child;
// The value of the actiontype attribute is case-sensitive.
const AtomicString& actiontype = fastGetAttribute(MathMLNames::actiontypeAttr);
if (actiontype == "statusline")
// FIXME: implement user interaction for the "statusline" action type (http://wkbug/124922).
{ }
else if (actiontype == "tooltip")
// FIXME: implement user interaction for the "tooltip" action type (http://wkbug/124921).
{ }
else {
// For the "toggle" action type or any unknown action type, we rely on the value of the selection attribute to determine the visible child.
getSelectedActionChildAndIndex(child);
}
return child;
}
Element* MathMLSelectElement::getSelectedSemanticsChild()
{
ASSERT(hasTagName(semanticsTag));
Element* child = firstElementChild();
if (!child)
return nullptr;
if (!is<MathMLElement>(*child) || !downcast<MathMLElement>(*child).isPresentationMathML()) {
// The first child is not a presentation MathML element. Hence we move to the second child and start searching an annotation child that could be displayed.
child = child->nextElementSibling();
} else if (!downcast<MathMLElement>(*child).isSemanticAnnotation()) {
// The first child is a presentation MathML but not an annotation, so we can just display it.
return child;
}
// Otherwise, the first child is an <annotation> or <annotation-xml> element. This is invalid, but some people use this syntax so we take care of this case too and start the search from this first child.
for ( ; child; child = child->nextElementSibling()) {
if (!is<MathMLElement>(*child))
continue;
if (child->hasTagName(MathMLNames::annotationTag)) {
// If the <annotation> element has an src attribute then it is a reference to arbitrary binary data and it is not clear whether we can display it. Hence we just ignore the annotation.
if (child->hasAttribute(MathMLNames::srcAttr))
continue;
// Otherwise, we assume it is a text annotation that can always be displayed and we stop here.
return child;
}
if (child->hasTagName(MathMLNames::annotation_xmlTag)) {
// If the <annotation-xml> element has an src attribute then it is a reference to arbitrary binary data and it is not clear whether we can display it. Hence we just ignore the annotation.
if (child->hasAttribute(MathMLNames::srcAttr))
continue;
// If the <annotation-xml> element has an encoding attribute describing presentation MathML, SVG or HTML we assume the content can be displayed and we stop here.
const AtomicString& value = child->fastGetAttribute(MathMLNames::encodingAttr);
if (isMathMLEncoding(value) || isSVGEncoding(value) || isHTMLEncoding(value))
return child;
}
}
// We fallback to the first child.
return firstElementChild();
}
void MathMLSelectElement::updateSelectedChild()
{
Element* newSelectedChild = hasTagName(mactionTag) ? getSelectedActionChild() : getSelectedSemanticsChild();
if (m_selectedChild == newSelectedChild)
return;
if (m_selectedChild && m_selectedChild->renderer())
Style::detachRenderTree(*m_selectedChild);
m_selectedChild = newSelectedChild;
setNeedsStyleRecalc();
}
void MathMLSelectElement::defaultEventHandler(Event* event)
{
if (event->type() == eventNames().clickEvent) {
if (fastGetAttribute(MathMLNames::actiontypeAttr) == "toggle") {
toggle();
event->setDefaultHandled();
return;
}
}
MathMLInlineContainerElement::defaultEventHandler(event);
}
bool MathMLSelectElement::willRespondToMouseClickEvents()
{
return fastGetAttribute(MathMLNames::actiontypeAttr) == "toggle";
}
void MathMLSelectElement::toggle()
{
// Select the successor of the currently selected child
// or the first child if the currently selected child is the last.
Element* selectedChild;
int newSelectedChildIndex = getSelectedActionChildAndIndex(selectedChild) + 1;
if (!selectedChild || !selectedChild->nextElementSibling())
newSelectedChildIndex = 1;
// We update the attribute value of the selection attribute.
// This will also call MathMLSelectElement::attributeChanged to update the selected child.
setAttribute(MathMLNames::selectionAttr, AtomicString::number(newSelectedChildIndex));
}
}
#endif // ENABLE(MATHML)
|