1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is MathML DOM code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * mozilla.org.
19 : * Portions created by the Initial Developer are Copyright (C) 2007
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Vlad Sukhoy <vladimir.sukhoy@gmail.com> (original developer)
24 : * Daniel Kraft <d@domob.eu> (nsMathMLElement patch, attachment 262925)
25 : * Frederic Wang <fred.wang@free.fr>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "mozilla/Util.h"
42 :
43 : #include "nsMathMLElement.h"
44 : #include "nsDOMClassInfoID.h" // for eDOMClassInfo_MathElement_id.
45 : #include "nsGkAtoms.h"
46 : #include "nsCRT.h"
47 : #include "nsRuleData.h"
48 : #include "nsCSSValue.h"
49 : #include "nsMappedAttributes.h"
50 : #include "nsStyleConsts.h"
51 : #include "nsIDocument.h"
52 : #include "nsEventStates.h"
53 : #include "nsIPresShell.h"
54 : #include "nsPresContext.h"
55 : #include "mozAutoDocUpdate.h"
56 :
57 : using namespace mozilla;
58 :
59 : //----------------------------------------------------------------------
60 : // nsISupports methods:
61 :
62 0 : DOMCI_NODE_DATA(MathMLElement, nsMathMLElement)
63 :
64 0 : NS_INTERFACE_TABLE_HEAD(nsMathMLElement)
65 : NS_NODE_OFFSET_AND_INTERFACE_TABLE_BEGIN(nsMathMLElement)
66 : NS_INTERFACE_TABLE_ENTRY(nsMathMLElement, nsIDOMNode)
67 : NS_INTERFACE_TABLE_ENTRY(nsMathMLElement, nsIDOMElement)
68 : NS_INTERFACE_TABLE_ENTRY(nsMathMLElement, nsILink)
69 : NS_INTERFACE_TABLE_ENTRY(nsMathMLElement, Link)
70 0 : NS_OFFSET_AND_INTERFACE_TABLE_END
71 0 : NS_ELEMENT_INTERFACE_TABLE_TO_MAP_SEGUE
72 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MathMLElement)
73 0 : NS_ELEMENT_INTERFACE_MAP_END
74 :
75 0 : NS_IMPL_ADDREF_INHERITED(nsMathMLElement, nsMathMLElementBase)
76 0 : NS_IMPL_RELEASE_INHERITED(nsMathMLElement, nsMathMLElementBase)
77 :
78 : nsresult
79 0 : nsMathMLElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
80 : nsIContent* aBindingParent,
81 : bool aCompileEventHandlers)
82 : {
83 : static const char kMathMLStyleSheetURI[] = "resource://gre-resources/mathml.css";
84 :
85 0 : Link::ResetLinkState(false);
86 :
87 : nsresult rv = nsMathMLElementBase::BindToTree(aDocument, aParent,
88 : aBindingParent,
89 0 : aCompileEventHandlers);
90 0 : NS_ENSURE_SUCCESS(rv, rv);
91 :
92 0 : if (aDocument) {
93 0 : aDocument->RegisterPendingLinkUpdate(this);
94 :
95 0 : if (!aDocument->GetMathMLEnabled()) {
96 : // Enable MathML and setup the style sheet during binding, not element
97 : // construction, because we could move a MathML element from the document
98 : // that created it to another document.
99 0 : aDocument->SetMathMLEnabled();
100 0 : aDocument->EnsureCatalogStyleSheet(kMathMLStyleSheetURI);
101 :
102 : // Rebuild style data for the presshell, because style system
103 : // optimizations may have taken place assuming MathML was disabled.
104 : // (See nsRuleNode::CheckSpecifiedProperties.)
105 0 : nsCOMPtr<nsIPresShell> shell = aDocument->GetShell();
106 0 : if (shell) {
107 0 : shell->GetPresContext()->PostRebuildAllStyleDataEvent(nsChangeHint(0));
108 : }
109 : }
110 : }
111 :
112 0 : return rv;
113 : }
114 :
115 : void
116 0 : nsMathMLElement::UnbindFromTree(bool aDeep, bool aNullParent)
117 : {
118 : // If this link is ever reinserted into a document, it might
119 : // be under a different xml:base, so forget the cached state now.
120 0 : Link::ResetLinkState(false);
121 :
122 0 : nsIDocument* doc = GetCurrentDoc();
123 0 : if (doc) {
124 0 : doc->UnregisterPendingLinkUpdate(this);
125 : }
126 :
127 0 : nsMathMLElementBase::UnbindFromTree(aDeep, aNullParent);
128 0 : }
129 :
130 : bool
131 0 : nsMathMLElement::ParseAttribute(PRInt32 aNamespaceID,
132 : nsIAtom* aAttribute,
133 : const nsAString& aValue,
134 : nsAttrValue& aResult)
135 : {
136 0 : if (aNamespaceID == kNameSpaceID_None) {
137 0 : if (aAttribute == nsGkAtoms::color ||
138 : aAttribute == nsGkAtoms::mathcolor_ ||
139 : aAttribute == nsGkAtoms::background ||
140 : aAttribute == nsGkAtoms::mathbackground_) {
141 0 : return aResult.ParseColor(aValue);
142 : }
143 : }
144 :
145 : return nsMathMLElementBase::ParseAttribute(aNamespaceID, aAttribute,
146 0 : aValue, aResult);
147 : }
148 :
149 : static nsGenericElement::MappedAttributeEntry sMtableStyles[] = {
150 : { &nsGkAtoms::width },
151 : { nsnull }
152 : };
153 :
154 : static nsGenericElement::MappedAttributeEntry sTokenStyles[] = {
155 : { &nsGkAtoms::mathsize_ },
156 : { &nsGkAtoms::fontsize_ },
157 : { &nsGkAtoms::color },
158 : { &nsGkAtoms::fontfamily_ },
159 : { nsnull }
160 : };
161 :
162 : static nsGenericElement::MappedAttributeEntry sEnvironmentStyles[] = {
163 : { &nsGkAtoms::scriptlevel_ },
164 : { &nsGkAtoms::scriptminsize_ },
165 : { &nsGkAtoms::scriptsizemultiplier_ },
166 : { &nsGkAtoms::background },
167 : { nsnull }
168 : };
169 :
170 : static nsGenericElement::MappedAttributeEntry sCommonPresStyles[] = {
171 : { &nsGkAtoms::mathcolor_ },
172 : { &nsGkAtoms::mathbackground_ },
173 : { nsnull }
174 : };
175 :
176 : bool
177 0 : nsMathMLElement::IsAttributeMapped(const nsIAtom* aAttribute) const
178 : {
179 : static const MappedAttributeEntry* const mtableMap[] = {
180 : sMtableStyles,
181 : sCommonPresStyles
182 : };
183 : static const MappedAttributeEntry* const tokenMap[] = {
184 : sTokenStyles,
185 : sCommonPresStyles
186 : };
187 : static const MappedAttributeEntry* const mstyleMap[] = {
188 : sTokenStyles,
189 : sEnvironmentStyles,
190 : sCommonPresStyles
191 : };
192 : static const MappedAttributeEntry* const commonPresMap[] = {
193 : sCommonPresStyles
194 : };
195 :
196 : // We don't support mglyph (yet).
197 0 : nsIAtom* tag = Tag();
198 0 : if (tag == nsGkAtoms::ms_ || tag == nsGkAtoms::mi_ ||
199 : tag == nsGkAtoms::mn_ || tag == nsGkAtoms::mo_ ||
200 : tag == nsGkAtoms::mtext_ || tag == nsGkAtoms::mspace_)
201 0 : return FindAttributeDependence(aAttribute, tokenMap);
202 0 : if (tag == nsGkAtoms::mstyle_ ||
203 : tag == nsGkAtoms::math)
204 0 : return FindAttributeDependence(aAttribute, mstyleMap);
205 :
206 0 : if (tag == nsGkAtoms::mtable_)
207 0 : return FindAttributeDependence(aAttribute, mtableMap);
208 :
209 0 : if (tag == nsGkAtoms::maction_ ||
210 : tag == nsGkAtoms::maligngroup_ ||
211 : tag == nsGkAtoms::malignmark_ ||
212 : tag == nsGkAtoms::menclose_ ||
213 : tag == nsGkAtoms::merror_ ||
214 : tag == nsGkAtoms::mfenced_ ||
215 : tag == nsGkAtoms::mfrac_ ||
216 : tag == nsGkAtoms::mover_ ||
217 : tag == nsGkAtoms::mpadded_ ||
218 : tag == nsGkAtoms::mphantom_ ||
219 : tag == nsGkAtoms::mprescripts_ ||
220 : tag == nsGkAtoms::mroot_ ||
221 : tag == nsGkAtoms::mrow_ ||
222 : tag == nsGkAtoms::msqrt_ ||
223 : tag == nsGkAtoms::msub_ ||
224 : tag == nsGkAtoms::msubsup_ ||
225 : tag == nsGkAtoms::msup_ ||
226 : tag == nsGkAtoms::mtd_ ||
227 : tag == nsGkAtoms::mtr_ ||
228 : tag == nsGkAtoms::munder_ ||
229 : tag == nsGkAtoms::munderover_ ||
230 : tag == nsGkAtoms::none) {
231 0 : return FindAttributeDependence(aAttribute, commonPresMap);
232 : }
233 :
234 0 : return false;
235 : }
236 :
237 : nsMapRuleToAttributesFunc
238 0 : nsMathMLElement::GetAttributeMappingFunction() const
239 : {
240 : // It doesn't really matter what our tag is here, because only attributes
241 : // that satisfy IsAttributeMapped will be stored in the mapped attributes
242 : // list and available to the mapping function
243 0 : return &MapMathMLAttributesInto;
244 : }
245 :
246 : // ================
247 : // Utilities for parsing and retrieving numeric values
248 :
249 : /*
250 : The REC says:
251 : An explicit plus sign ('+') is not allowed as part of a numeric value
252 : except when it is specifically listed in the syntax (as a quoted '+'
253 : or "+"),
254 :
255 : Units allowed
256 : ID Description
257 : em ems (font-relative unit traditionally used for horizontal lengths)
258 : ex exs (font-relative unit traditionally used for vertical lengths)
259 : px pixels, or pixel size of a "typical computer display"
260 : in inches (1 inch = 2.54 centimeters)
261 : cm centimeters
262 : mm millimeters
263 : pt points (1 point = 1/72 inch)
264 : pc picas (1 pica = 12 points)
265 : % percentage of default value
266 :
267 : Implementation here:
268 : The numeric value is valid only if it is of the form [-] nnn.nnn
269 : [h/v-unit]
270 : */
271 :
272 : /* static */ bool
273 0 : nsMathMLElement::ParseNumericValue(const nsString& aString,
274 : nsCSSValue& aCSSValue,
275 : PRUint32 aFlags)
276 : {
277 0 : nsAutoString str(aString);
278 0 : str.CompressWhitespace(); // aString is const in this code...
279 :
280 0 : PRInt32 stringLength = str.Length();
281 0 : if (!stringLength)
282 0 : return false;
283 :
284 0 : nsAutoString number, unit;
285 :
286 : // see if the negative sign is there
287 0 : PRInt32 i = 0;
288 0 : PRUnichar c = str[0];
289 0 : if (c == '-') {
290 0 : number.Append(c);
291 0 : i++;
292 :
293 : // skip any space after the negative sign
294 0 : if (i < stringLength && nsCRT::IsAsciiSpace(str[i]))
295 0 : i++;
296 : }
297 :
298 : // Gather up characters that make up the number
299 0 : bool gotDot = false;
300 0 : for ( ; i < stringLength; i++) {
301 0 : c = str[i];
302 0 : if (gotDot && c == '.')
303 0 : return false; // two dots encountered
304 0 : else if (c == '.')
305 0 : gotDot = true;
306 0 : else if (!nsCRT::IsAsciiDigit(c)) {
307 0 : str.Right(unit, stringLength - i);
308 : // some authors leave blanks before the unit, but that shouldn't
309 : // be allowed, so don't CompressWhitespace on 'unit'.
310 0 : break;
311 : }
312 0 : number.Append(c);
313 : }
314 :
315 : // Convert number to floating point
316 : PRInt32 errorCode;
317 0 : float floatValue = number.ToFloat(&errorCode);
318 0 : if (NS_FAILED(errorCode))
319 0 : return false;
320 0 : if (floatValue < 0 && !(aFlags & PARSE_ALLOW_NEGATIVE))
321 0 : return false;
322 :
323 : nsCSSUnit cssUnit;
324 0 : if (unit.IsEmpty()) {
325 0 : if (aFlags & PARSE_ALLOW_UNITLESS) {
326 : // no explicit unit, this is a number that will act as a multiplier
327 0 : cssUnit = eCSSUnit_Number;
328 : } else {
329 : // We are supposed to have a unit, but there isn't one.
330 : // If the value is 0 we can just call it "pixels" otherwise
331 : // this is illegal.
332 0 : if (floatValue != 0.0)
333 0 : return false;
334 0 : cssUnit = eCSSUnit_Pixel;
335 : }
336 : }
337 0 : else if (unit.EqualsLiteral("%")) {
338 0 : aCSSValue.SetPercentValue(floatValue / 100.0f);
339 0 : return true;
340 : }
341 0 : else if (unit.EqualsLiteral("em")) cssUnit = eCSSUnit_EM;
342 0 : else if (unit.EqualsLiteral("ex")) cssUnit = eCSSUnit_XHeight;
343 0 : else if (unit.EqualsLiteral("px")) cssUnit = eCSSUnit_Pixel;
344 0 : else if (unit.EqualsLiteral("in")) cssUnit = eCSSUnit_Inch;
345 0 : else if (unit.EqualsLiteral("cm")) cssUnit = eCSSUnit_Centimeter;
346 0 : else if (unit.EqualsLiteral("mm")) cssUnit = eCSSUnit_Millimeter;
347 0 : else if (unit.EqualsLiteral("pt")) cssUnit = eCSSUnit_Point;
348 0 : else if (unit.EqualsLiteral("pc")) cssUnit = eCSSUnit_Pica;
349 : else // unexpected unit
350 0 : return false;
351 :
352 0 : aCSSValue.SetFloatValue(floatValue, cssUnit);
353 0 : return true;
354 : }
355 :
356 : void
357 0 : nsMathMLElement::MapMathMLAttributesInto(const nsMappedAttributes* aAttributes,
358 : nsRuleData* aData)
359 : {
360 0 : if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Font)) {
361 : const nsAttrValue* value =
362 0 : aAttributes->GetAttr(nsGkAtoms::scriptsizemultiplier_);
363 : nsCSSValue* scriptSizeMultiplier =
364 0 : aData->ValueForScriptSizeMultiplier();
365 0 : if (value && value->Type() == nsAttrValue::eString &&
366 0 : scriptSizeMultiplier->GetUnit() == eCSSUnit_Null) {
367 0 : nsAutoString str(value->GetStringValue());
368 0 : str.CompressWhitespace();
369 : // MathML numbers can't have leading '+'
370 0 : if (str.Length() > 0 && str.CharAt(0) != '+') {
371 : PRInt32 errorCode;
372 0 : float floatValue = str.ToFloat(&errorCode);
373 : // Negative scriptsizemultipliers are not parsed
374 0 : if (NS_SUCCEEDED(errorCode) && floatValue >= 0.0f) {
375 0 : scriptSizeMultiplier->SetFloatValue(floatValue, eCSSUnit_Number);
376 : }
377 : }
378 : }
379 :
380 0 : value = aAttributes->GetAttr(nsGkAtoms::scriptminsize_);
381 0 : nsCSSValue* scriptMinSize = aData->ValueForScriptMinSize();
382 0 : if (value && value->Type() == nsAttrValue::eString &&
383 0 : scriptMinSize->GetUnit() == eCSSUnit_Null) {
384 0 : ParseNumericValue(value->GetStringValue(), *scriptMinSize, 0);
385 : }
386 :
387 0 : value = aAttributes->GetAttr(nsGkAtoms::scriptlevel_);
388 0 : nsCSSValue* scriptLevel = aData->ValueForScriptLevel();
389 0 : if (value && value->Type() == nsAttrValue::eString &&
390 0 : scriptLevel->GetUnit() == eCSSUnit_Null) {
391 0 : nsAutoString str(value->GetStringValue());
392 0 : str.CompressWhitespace();
393 0 : if (str.Length() > 0) {
394 : PRInt32 errorCode;
395 0 : PRInt32 intValue = str.ToInteger(&errorCode);
396 0 : if (NS_SUCCEEDED(errorCode)) {
397 : // This is kind of cheesy ... if the scriptlevel has a sign,
398 : // then it's a relative value and we store the nsCSSValue as an
399 : // Integer to indicate that. Otherwise we store it as a Number
400 : // to indicate that the scriptlevel is absolute.
401 0 : PRUnichar ch = str.CharAt(0);
402 0 : if (ch == '+' || ch == '-') {
403 0 : scriptLevel->SetIntValue(intValue, eCSSUnit_Integer);
404 : } else {
405 0 : scriptLevel->SetFloatValue(intValue, eCSSUnit_Number);
406 : }
407 : }
408 : }
409 : }
410 :
411 0 : bool parseSizeKeywords = true;
412 0 : value = aAttributes->GetAttr(nsGkAtoms::mathsize_);
413 0 : if (!value) {
414 0 : parseSizeKeywords = false;
415 0 : value = aAttributes->GetAttr(nsGkAtoms::fontsize_);
416 : }
417 0 : nsCSSValue* fontSize = aData->ValueForFontSize();
418 0 : if (value && value->Type() == nsAttrValue::eString &&
419 0 : fontSize->GetUnit() == eCSSUnit_Null) {
420 0 : nsAutoString str(value->GetStringValue());
421 0 : if (!ParseNumericValue(str, *fontSize, 0) &&
422 : parseSizeKeywords) {
423 : static const char sizes[3][7] = { "small", "normal", "big" };
424 : static const PRInt32 values[NS_ARRAY_LENGTH(sizes)] = {
425 : NS_STYLE_FONT_SIZE_SMALL, NS_STYLE_FONT_SIZE_MEDIUM,
426 : NS_STYLE_FONT_SIZE_LARGE
427 : };
428 0 : str.CompressWhitespace();
429 0 : for (PRUint32 i = 0; i < ArrayLength(sizes); ++i) {
430 0 : if (str.EqualsASCII(sizes[i])) {
431 0 : fontSize->SetIntValue(values[i], eCSSUnit_Enumerated);
432 0 : break;
433 : }
434 : }
435 : }
436 : }
437 :
438 0 : value = aAttributes->GetAttr(nsGkAtoms::fontfamily_);
439 0 : nsCSSValue* fontFamily = aData->ValueForFontFamily();
440 0 : if (value && value->Type() == nsAttrValue::eString &&
441 0 : fontFamily->GetUnit() == eCSSUnit_Null) {
442 0 : fontFamily->SetStringValue(value->GetStringValue(), eCSSUnit_Families);
443 : }
444 : }
445 :
446 0 : if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Background)) {
447 : const nsAttrValue* value =
448 0 : aAttributes->GetAttr(nsGkAtoms::mathbackground_);
449 0 : if (!value) {
450 0 : value = aAttributes->GetAttr(nsGkAtoms::background);
451 : }
452 0 : nsCSSValue* backgroundColor = aData->ValueForBackgroundColor();
453 0 : if (value && backgroundColor->GetUnit() == eCSSUnit_Null) {
454 : nscolor color;
455 0 : if (value->GetColorValue(color)) {
456 0 : backgroundColor->SetColorValue(color);
457 : }
458 : }
459 : }
460 :
461 0 : if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Color)) {
462 0 : const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::mathcolor_);
463 0 : if (!value) {
464 0 : value = aAttributes->GetAttr(nsGkAtoms::color);
465 : }
466 : nscolor color;
467 0 : nsCSSValue* colorValue = aData->ValueForColor();
468 0 : if (value && value->GetColorValue(color) &&
469 0 : colorValue->GetUnit() == eCSSUnit_Null) {
470 0 : colorValue->SetColorValue(color);
471 : }
472 : }
473 :
474 0 : if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Position)) {
475 : // width: value
476 0 : nsCSSValue* width = aData->ValueForWidth();
477 0 : if (width->GetUnit() == eCSSUnit_Null) {
478 0 : const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::width);
479 : // This does not handle auto and unitless values
480 0 : if (value && value->Type() == nsAttrValue::eString) {
481 0 : ParseNumericValue(value->GetStringValue(), *width, 0);
482 : }
483 : }
484 : }
485 :
486 0 : }
487 :
488 : nsresult
489 0 : nsMathMLElement::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
490 : {
491 0 : nsresult rv = nsGenericElement::PreHandleEvent(aVisitor);
492 0 : NS_ENSURE_SUCCESS(rv, rv);
493 :
494 0 : return PreHandleEventForLinks(aVisitor);
495 : }
496 :
497 : nsresult
498 0 : nsMathMLElement::PostHandleEvent(nsEventChainPostVisitor& aVisitor)
499 : {
500 0 : return PostHandleEventForLinks(aVisitor);
501 : }
502 :
503 0 : NS_IMPL_ELEMENT_CLONE(nsMathMLElement)
504 :
505 : nsEventStates
506 0 : nsMathMLElement::IntrinsicState() const
507 : {
508 : return Link::LinkState() | nsMathMLElementBase::IntrinsicState() |
509 0 : (mIncrementScriptLevel ? NS_EVENT_STATE_INCREMENT_SCRIPT_LEVEL : nsEventStates());
510 : }
511 :
512 : bool
513 0 : nsMathMLElement::IsNodeOfType(PRUint32 aFlags) const
514 : {
515 0 : return !(aFlags & ~eCONTENT);
516 : }
517 :
518 : void
519 0 : nsMathMLElement::SetIncrementScriptLevel(bool aIncrementScriptLevel,
520 : bool aNotify)
521 : {
522 0 : if (aIncrementScriptLevel == mIncrementScriptLevel)
523 0 : return;
524 0 : mIncrementScriptLevel = aIncrementScriptLevel;
525 :
526 0 : NS_ASSERTION(aNotify, "We always notify!");
527 :
528 0 : UpdateState(true);
529 : }
530 :
531 : bool
532 0 : nsMathMLElement::IsFocusable(PRInt32 *aTabIndex, bool aWithMouse)
533 : {
534 0 : nsCOMPtr<nsIURI> uri;
535 0 : if (IsLink(getter_AddRefs(uri))) {
536 0 : if (aTabIndex) {
537 0 : *aTabIndex = ((sTabFocusModel & eTabFocus_linksMask) == 0 ? -1 : 0);
538 : }
539 0 : return true;
540 : }
541 :
542 0 : if (aTabIndex) {
543 0 : *aTabIndex = -1;
544 : }
545 :
546 0 : return false;
547 : }
548 :
549 : bool
550 0 : nsMathMLElement::IsLink(nsIURI** aURI) const
551 : {
552 : // http://www.w3.org/TR/2010/REC-MathML3-20101021/chapter6.html#interf.link
553 : // The REC says that the following elements should not be linking elements:
554 0 : nsIAtom* tag = Tag();
555 0 : if (tag == nsGkAtoms::mprescripts_ ||
556 : tag == nsGkAtoms::none ||
557 : tag == nsGkAtoms::malignmark_ ||
558 : tag == nsGkAtoms::maligngroup_) {
559 0 : *aURI = nsnull;
560 0 : return false;
561 : }
562 :
563 0 : bool hasHref = false;
564 : const nsAttrValue* href = mAttrsAndChildren.GetAttr(nsGkAtoms::href,
565 0 : kNameSpaceID_None);
566 0 : if (href) {
567 : // MathML href
568 : // The REC says: "When user agents encounter MathML elements with both href
569 : // and xlink:href attributes, the href attribute should take precedence."
570 0 : hasHref = true;
571 : } else {
572 : // To be a clickable XLink for styling and interaction purposes, we require:
573 : //
574 : // xlink:href - must be set
575 : // xlink:type - must be unset or set to "" or set to "simple"
576 : // xlink:show - must be unset or set to "", "new" or "replace"
577 : // xlink:actuate - must be unset or set to "" or "onRequest"
578 : //
579 : // For any other values, we're either not a *clickable* XLink, or the end
580 : // result is poorly specified. Either way, we return false.
581 :
582 : static nsIContent::AttrValuesArray sTypeVals[] =
583 : { &nsGkAtoms::_empty, &nsGkAtoms::simple, nsnull };
584 :
585 : static nsIContent::AttrValuesArray sShowVals[] =
586 : { &nsGkAtoms::_empty, &nsGkAtoms::_new, &nsGkAtoms::replace, nsnull };
587 :
588 : static nsIContent::AttrValuesArray sActuateVals[] =
589 : { &nsGkAtoms::_empty, &nsGkAtoms::onRequest, nsnull };
590 :
591 : // Optimization: check for href first for early return
592 : href = mAttrsAndChildren.GetAttr(nsGkAtoms::href,
593 0 : kNameSpaceID_XLink);
594 0 : if (href &&
595 : FindAttrValueIn(kNameSpaceID_XLink, nsGkAtoms::type,
596 0 : sTypeVals, eCaseMatters) !=
597 : nsIContent::ATTR_VALUE_NO_MATCH &&
598 : FindAttrValueIn(kNameSpaceID_XLink, nsGkAtoms::show,
599 0 : sShowVals, eCaseMatters) !=
600 : nsIContent::ATTR_VALUE_NO_MATCH &&
601 : FindAttrValueIn(kNameSpaceID_XLink, nsGkAtoms::actuate,
602 0 : sActuateVals, eCaseMatters) !=
603 : nsIContent::ATTR_VALUE_NO_MATCH) {
604 0 : hasHref = true;
605 : }
606 : }
607 :
608 0 : if (hasHref) {
609 0 : nsCOMPtr<nsIURI> baseURI = GetBaseURI();
610 : // Get absolute URI
611 0 : nsAutoString hrefStr;
612 0 : href->ToString(hrefStr);
613 : nsContentUtils::NewURIWithDocumentCharset(aURI, hrefStr,
614 0 : OwnerDoc(), baseURI);
615 : // must promise out param is non-null if we return true
616 0 : return !!*aURI;
617 : }
618 :
619 0 : *aURI = nsnull;
620 0 : return false;
621 : }
622 :
623 : void
624 0 : nsMathMLElement::GetLinkTarget(nsAString& aTarget)
625 : {
626 : const nsAttrValue* target = mAttrsAndChildren.GetAttr(nsGkAtoms::target,
627 0 : kNameSpaceID_XLink);
628 0 : if (target) {
629 0 : target->ToString(aTarget);
630 : }
631 :
632 0 : if (aTarget.IsEmpty()) {
633 :
634 : static nsIContent::AttrValuesArray sShowVals[] =
635 : { &nsGkAtoms::_new, &nsGkAtoms::replace, nsnull };
636 :
637 0 : switch (FindAttrValueIn(kNameSpaceID_XLink, nsGkAtoms::show,
638 0 : sShowVals, eCaseMatters)) {
639 : case 0:
640 0 : aTarget.AssignLiteral("_blank");
641 0 : return;
642 : case 1:
643 0 : return;
644 : }
645 0 : OwnerDoc()->GetBaseTarget(aTarget);
646 : }
647 : }
648 :
649 : nsLinkState
650 0 : nsMathMLElement::GetLinkState() const
651 : {
652 0 : return Link::GetLinkState();
653 : }
654 :
655 : already_AddRefed<nsIURI>
656 0 : nsMathMLElement::GetHrefURI() const
657 : {
658 0 : nsCOMPtr<nsIURI> hrefURI;
659 0 : return IsLink(getter_AddRefs(hrefURI)) ? hrefURI.forget() : nsnull;
660 : }
661 :
662 : nsresult
663 0 : nsMathMLElement::SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
664 : nsIAtom* aPrefix, const nsAString& aValue,
665 : bool aNotify)
666 : {
667 : nsresult rv = nsMathMLElementBase::SetAttr(aNameSpaceID, aName, aPrefix,
668 0 : aValue, aNotify);
669 :
670 : // The ordering of the parent class's SetAttr call and Link::ResetLinkState
671 : // is important here! The attribute is not set until SetAttr returns, and
672 : // we will need the updated attribute value because notifying the document
673 : // that content states have changed will call IntrinsicState, which will try
674 : // to get updated information about the visitedness from Link.
675 0 : if (aName == nsGkAtoms::href &&
676 : (aNameSpaceID == kNameSpaceID_None ||
677 : aNameSpaceID == kNameSpaceID_XLink)) {
678 0 : Link::ResetLinkState(!!aNotify);
679 : }
680 :
681 0 : return rv;
682 : }
683 :
684 : nsresult
685 0 : nsMathMLElement::UnsetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttr,
686 : bool aNotify)
687 : {
688 0 : nsresult rv = nsMathMLElementBase::UnsetAttr(aNameSpaceID, aAttr, aNotify);
689 :
690 : // The ordering of the parent class's UnsetAttr call and Link::ResetLinkState
691 : // is important here! The attribute is not unset until UnsetAttr returns, and
692 : // we will need the updated attribute value because notifying the document
693 : // that content states have changed will call IntrinsicState, which will try
694 : // to get updated information about the visitedness from Link.
695 0 : if (aAttr == nsGkAtoms::href &&
696 : (aNameSpaceID == kNameSpaceID_None ||
697 : aNameSpaceID == kNameSpaceID_XLink)) {
698 0 : Link::ResetLinkState(!!aNotify);
699 : }
700 :
701 0 : return rv;
702 : }
|