1 | /* |
2 | * Copyright (C) 2009 Alex Milowski (alex@milowski.com). All rights reserved. |
3 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
4 | * Copyright (C) 2010 François Sausset (sausset@gmail.com). All rights reserved. |
5 | * Copyright (C) 2016 Igalia S.L. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #include "config.h" |
30 | #include "MathMLElement.h" |
31 | |
32 | #if ENABLE(MATHML) |
33 | |
34 | #include "EventHandler.h" |
35 | #include "FrameLoader.h" |
36 | #include "HTMLAnchorElement.h" |
37 | #include "HTMLParserIdioms.h" |
38 | #include "MathMLNames.h" |
39 | #include "MouseEvent.h" |
40 | #include "RenderTableCell.h" |
41 | #include <wtf/IsoMallocInlines.h> |
42 | #include <wtf/text/StringConcatenateNumbers.h> |
43 | |
44 | namespace WebCore { |
45 | |
46 | WTF_MAKE_ISO_ALLOCATED_IMPL(MathMLElement); |
47 | |
48 | using namespace MathMLNames; |
49 | |
50 | MathMLElement::MathMLElement(const QualifiedName& tagName, Document& document) |
51 | : StyledElement(tagName, document, CreateMathMLElement) |
52 | { |
53 | } |
54 | |
55 | Ref<MathMLElement> MathMLElement::create(const QualifiedName& tagName, Document& document) |
56 | { |
57 | return adoptRef(*new MathMLElement(tagName, document)); |
58 | } |
59 | |
60 | unsigned MathMLElement::colSpan() const |
61 | { |
62 | if (!hasTagName(mtdTag)) |
63 | return 1u; |
64 | auto& colSpanValue = attributeWithoutSynchronization(columnspanAttr); |
65 | return std::max(1u, limitToOnlyHTMLNonNegative(colSpanValue, 1u)); |
66 | } |
67 | |
68 | unsigned MathMLElement::rowSpan() const |
69 | { |
70 | if (!hasTagName(mtdTag)) |
71 | return 1u; |
72 | auto& rowSpanValue = attributeWithoutSynchronization(rowspanAttr); |
73 | static const unsigned maxRowspan = 8190; // This constant comes from HTMLTableCellElement. |
74 | return std::max(1u, std::min(limitToOnlyHTMLNonNegative(rowSpanValue, 1u), maxRowspan)); |
75 | } |
76 | |
77 | void MathMLElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
78 | { |
79 | if (name == hrefAttr) { |
80 | bool wasLink = isLink(); |
81 | setIsLink(!value.isNull() && !shouldProhibitLinks(this)); |
82 | if (wasLink != isLink()) |
83 | invalidateStyleForSubtree(); |
84 | } else if (name == rowspanAttr) { |
85 | if (is<RenderTableCell>(renderer()) && hasTagName(mtdTag)) |
86 | downcast<RenderTableCell>(*renderer()).colSpanOrRowSpanChanged(); |
87 | } else if (name == columnspanAttr) { |
88 | if (is<RenderTableCell>(renderer()) && hasTagName(mtdTag)) |
89 | downcast<RenderTableCell>(renderer())->colSpanOrRowSpanChanged(); |
90 | } else |
91 | StyledElement::parseAttribute(name, value); |
92 | } |
93 | |
94 | bool MathMLElement::isPresentationAttribute(const QualifiedName& name) const |
95 | { |
96 | if (name == backgroundAttr || name == colorAttr || name == dirAttr || name == fontfamilyAttr || name == fontsizeAttr || name == fontstyleAttr || name == fontweightAttr || name == mathbackgroundAttr || name == mathcolorAttr || name == mathsizeAttr) |
97 | return true; |
98 | return StyledElement::isPresentationAttribute(name); |
99 | } |
100 | |
101 | static String convertMathSizeIfNeeded(const AtomicString& value) |
102 | { |
103 | if (value == "small" ) |
104 | return "0.75em" ; |
105 | if (value == "normal" ) |
106 | return "1em" ; |
107 | if (value == "big" ) |
108 | return "1.5em" ; |
109 | |
110 | // FIXME: mathsize accepts any MathML length, including named spaces (see parseMathMLLength). |
111 | // FIXME: Might be better to use double than float. |
112 | // FIXME: Might be better to use "shortest" numeric formatting instead of fixed width. |
113 | bool ok = false; |
114 | float unitlessValue = value.toFloat(&ok); |
115 | if (!ok) |
116 | return value; |
117 | return makeString(FormattedNumber::fixedWidth(unitlessValue * 100, 3), '%'); |
118 | } |
119 | |
120 | void MathMLElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStyleProperties& style) |
121 | { |
122 | if (name == mathbackgroundAttr) |
123 | addPropertyToPresentationAttributeStyle(style, CSSPropertyBackgroundColor, value); |
124 | else if (name == mathsizeAttr) |
125 | addPropertyToPresentationAttributeStyle(style, CSSPropertyFontSize, convertMathSizeIfNeeded(value)); |
126 | else if (name == mathcolorAttr) |
127 | addPropertyToPresentationAttributeStyle(style, CSSPropertyColor, value); |
128 | // FIXME: The following are deprecated attributes that should lose if there is a conflict with a non-deprecated attribute. |
129 | else if (name == fontsizeAttr) |
130 | addPropertyToPresentationAttributeStyle(style, CSSPropertyFontSize, value); |
131 | else if (name == backgroundAttr) |
132 | addPropertyToPresentationAttributeStyle(style, CSSPropertyBackgroundColor, value); |
133 | else if (name == colorAttr) |
134 | addPropertyToPresentationAttributeStyle(style, CSSPropertyColor, value); |
135 | else if (name == fontstyleAttr) |
136 | addPropertyToPresentationAttributeStyle(style, CSSPropertyFontStyle, value); |
137 | else if (name == fontweightAttr) |
138 | addPropertyToPresentationAttributeStyle(style, CSSPropertyFontWeight, value); |
139 | else if (name == fontfamilyAttr) |
140 | addPropertyToPresentationAttributeStyle(style, CSSPropertyFontFamily, value); |
141 | else if (name == dirAttr) { |
142 | if (hasTagName(mathTag) || hasTagName(mrowTag) || hasTagName(mstyleTag) || isMathMLToken()) |
143 | addPropertyToPresentationAttributeStyle(style, CSSPropertyDirection, value); |
144 | } else { |
145 | ASSERT(!isPresentationAttribute(name)); |
146 | StyledElement::collectStyleForPresentationAttribute(name, value, style); |
147 | } |
148 | } |
149 | |
150 | bool MathMLElement::childShouldCreateRenderer(const Node& child) const |
151 | { |
152 | // In general, only MathML children are allowed. Text nodes are only visible in token MathML elements. |
153 | return is<MathMLElement>(child); |
154 | } |
155 | |
156 | bool MathMLElement::willRespondToMouseClickEvents() |
157 | { |
158 | return isLink() || StyledElement::willRespondToMouseClickEvents(); |
159 | } |
160 | |
161 | void MathMLElement::defaultEventHandler(Event& event) |
162 | { |
163 | if (isLink()) { |
164 | if (focused() && isEnterKeyKeydownEvent(event)) { |
165 | event.setDefaultHandled(); |
166 | dispatchSimulatedClick(&event); |
167 | return; |
168 | } |
169 | if (MouseEvent::canTriggerActivationBehavior(event)) { |
170 | auto& href = attributeWithoutSynchronization(hrefAttr); |
171 | const auto& url = stripLeadingAndTrailingHTMLSpaces(href); |
172 | event.setDefaultHandled(); |
173 | if (auto* frame = document().frame()) |
174 | frame->loader().urlSelected(document().completeURL(url), "_self" , &event, LockHistory::No, LockBackForwardList::No, MaybeSendReferrer, document().shouldOpenExternalURLsPolicyToPropagate()); |
175 | return; |
176 | } |
177 | } |
178 | |
179 | StyledElement::defaultEventHandler(event); |
180 | } |
181 | |
182 | bool MathMLElement::canStartSelection() const |
183 | { |
184 | if (!isLink()) |
185 | return StyledElement::canStartSelection(); |
186 | |
187 | return hasEditableStyle(); |
188 | } |
189 | |
190 | bool MathMLElement::isKeyboardFocusable(KeyboardEvent* event) const |
191 | { |
192 | if (isFocusable() && StyledElement::supportsFocus()) |
193 | return StyledElement::isKeyboardFocusable(event); |
194 | |
195 | if (isLink()) |
196 | return document().frame()->eventHandler().tabsToLinks(event); |
197 | |
198 | return StyledElement::isKeyboardFocusable(event); |
199 | } |
200 | |
201 | bool MathMLElement::isMouseFocusable() const |
202 | { |
203 | // Links are focusable by default, but only allow links with tabindex or contenteditable to be mouse focusable. |
204 | // https://bugs.webkit.org/show_bug.cgi?id=26856 |
205 | if (isLink()) |
206 | return StyledElement::supportsFocus(); |
207 | |
208 | return StyledElement::isMouseFocusable(); |
209 | } |
210 | |
211 | bool MathMLElement::isURLAttribute(const Attribute& attribute) const |
212 | { |
213 | return attribute.name().localName() == hrefAttr || StyledElement::isURLAttribute(attribute); |
214 | } |
215 | |
216 | bool MathMLElement::supportsFocus() const |
217 | { |
218 | if (hasEditableStyle()) |
219 | return StyledElement::supportsFocus(); |
220 | // If not a link we should still be able to focus the element if it has tabIndex. |
221 | return isLink() || StyledElement::supportsFocus(); |
222 | } |
223 | |
224 | int MathMLElement::tabIndex() const |
225 | { |
226 | // Skip the supportsFocus check in StyledElement. |
227 | return Element::tabIndex(); |
228 | } |
229 | |
230 | } |
231 | |
232 | #endif // ENABLE(MATHML) |
233 | |