Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsMathMLsemanticsFrame.h"
8 #include "nsMimeTypes.h"
9 #include "mozilla/gfx/2D.h"
11 //
12 // <semantics> -- associate annotations with a MathML expression
13 //
15 nsIFrame*
16 NS_NewMathMLsemanticsFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
17 {
18 return new (aPresShell) nsMathMLsemanticsFrame(aContext);
19 }
21 NS_IMPL_FRAMEARENA_HELPERS(nsMathMLsemanticsFrame)
23 nsMathMLsemanticsFrame::~nsMathMLsemanticsFrame()
24 {
25 }
27 nsIFrame*
28 nsMathMLsemanticsFrame::GetSelectedFrame()
29 {
30 // By default, we will display the first child of the <semantics> element.
31 nsIFrame* childFrame = mFrames.FirstChild();
32 mSelectedFrame = childFrame;
34 // An empty <semantics> is invalid
35 if (!childFrame) {
36 mInvalidMarkup = true;
37 return mSelectedFrame;
38 }
39 mInvalidMarkup = false;
41 // Using <annotation> or <annotation-xml> as a first child is invalid.
42 // However some people use this syntax so we take care of this case too.
43 bool firstChildIsAnnotation = false;
44 nsIContent* childContent = childFrame->GetContent();
45 if (childContent->GetNameSpaceID() == kNameSpaceID_MathML &&
46 (childContent->Tag() == nsGkAtoms::annotation_ ||
47 childContent->Tag() == nsGkAtoms::annotation_xml_)) {
48 firstChildIsAnnotation = true;
49 }
51 // If the first child is a presentation MathML element other than
52 // <annotation> or <annotation-xml>, we are done.
53 if (!firstChildIsAnnotation &&
54 childFrame->IsFrameOfType(nsIFrame::eMathML)) {
55 nsIMathMLFrame* mathMLFrame = do_QueryFrame(childFrame);
56 if (mathMLFrame) {
57 TransmitAutomaticData();
58 return mSelectedFrame;
59 }
60 // The first child is not an annotation, so skip it.
61 childFrame = childFrame->GetNextSibling();
62 }
64 // Otherwise, we read the list of annotations and select the first one that
65 // could be displayed in place of the first child of <semantics>. If none is
66 // found, we fallback to this first child.
67 for ( ; childFrame; childFrame = childFrame->GetNextSibling()) {
68 nsIContent* childContent = childFrame->GetContent();
70 if (childContent->GetNameSpaceID() != kNameSpaceID_MathML) continue;
72 if (childContent->Tag() == nsGkAtoms::annotation_) {
74 // If the <annotation> element has an src attribute we ignore it.
75 // XXXfredw Should annotation images be supported? See the related
76 // bug 297465 for mglyph.
77 if (childContent->HasAttr(kNameSpaceID_None, nsGkAtoms::src)) continue;
79 // Otherwise, we assume it is a text annotation that can always be
80 // displayed and stop here.
81 mSelectedFrame = childFrame;
82 break;
83 }
85 if (childContent->Tag() == nsGkAtoms::annotation_xml_) {
87 // If the <annotation-xml> element has an src attribute we ignore it.
88 if (childContent->HasAttr(kNameSpaceID_None, nsGkAtoms::src)) continue;
90 // If the <annotation-xml> element has an encoding attribute
91 // describing presentation MathML, SVG or HTML we assume the content
92 // can be displayed and stop here.
93 //
94 // We recognize the following encoding values:
95 //
96 // - "MathML-Presentation", which is mentioned in the MathML3 REC
97 // - "SVG1.1" which is mentioned in the W3C note
98 // http://www.w3.org/Math/Documents/Notes/graphics.xml
99 // - Other mime Content-Types for SVG and HTML
100 //
101 // We exclude APPLICATION_MATHML_XML = "application/mathml+xml" which
102 // is ambiguous about whether it is Presentation or Content MathML.
103 // Authors must use a more explicit encoding value.
104 nsAutoString value;
105 childContent->GetAttr(kNameSpaceID_None, nsGkAtoms::encoding, value);
106 if (value.EqualsLiteral("application/mathml-presentation+xml") ||
107 value.EqualsLiteral("MathML-Presentation") ||
108 value.EqualsLiteral(IMAGE_SVG_XML) ||
109 value.EqualsLiteral("SVG1.1") ||
110 value.EqualsLiteral(APPLICATION_XHTML_XML) ||
111 value.EqualsLiteral(TEXT_HTML)) {
112 mSelectedFrame = childFrame;
113 break;
114 }
115 }
116 }
118 TransmitAutomaticData();
119 return mSelectedFrame;
120 }