1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsMathMLsemanticsFrame.h"
9 #include "nsMimeTypes.h"
10 #include "mozilla/PresShell.h"
11 #include "mozilla/dom/Element.h"
12 #include "mozilla/gfx/2D.h"
14 using namespace mozilla
;
17 // <semantics> -- associate annotations with a MathML expression
20 nsIFrame
* NS_NewMathMLsemanticsFrame(PresShell
* aPresShell
,
21 ComputedStyle
* aStyle
) {
22 return new (aPresShell
)
23 nsMathMLsemanticsFrame(aStyle
, aPresShell
->GetPresContext());
26 NS_IMPL_FRAMEARENA_HELPERS(nsMathMLsemanticsFrame
)
28 nsMathMLsemanticsFrame::~nsMathMLsemanticsFrame() = default;
30 nsIFrame
* nsMathMLsemanticsFrame::GetSelectedFrame() {
31 // By default, we will display the first child of the <semantics> element.
32 nsIFrame
* childFrame
= mFrames
.FirstChild();
33 mSelectedFrame
= childFrame
;
35 // An empty <semantics> is invalid
37 mInvalidMarkup
= true;
38 return mSelectedFrame
;
40 mInvalidMarkup
= false;
42 // Using <annotation> or <annotation-xml> as a first child is invalid.
43 // However some people use this syntax so we take care of this case too.
44 bool firstChildIsAnnotation
= false;
45 nsIContent
* childContent
= childFrame
->GetContent();
46 if (childContent
->IsAnyOfMathMLElements(nsGkAtoms::annotation_
,
47 nsGkAtoms::annotation_xml_
)) {
48 firstChildIsAnnotation
= true;
51 // If the first child is a presentation MathML element other than
52 // <annotation> or <annotation-xml>, we are done.
53 if (!firstChildIsAnnotation
&& childFrame
->IsFrameOfType(nsIFrame::eMathML
)) {
54 nsIMathMLFrame
* mathMLFrame
= do_QueryFrame(childFrame
);
56 TransmitAutomaticData();
57 return mSelectedFrame
;
59 // The first child is not an annotation, so skip it.
60 childFrame
= childFrame
->GetNextSibling();
63 // Otherwise, we read the list of annotations and select the first one that
64 // could be displayed in place of the first child of <semantics>. If none is
65 // found, we fallback to this first child.
66 for (; childFrame
; childFrame
= childFrame
->GetNextSibling()) {
67 nsIContent
* childContent
= childFrame
->GetContent();
69 if (childContent
->IsMathMLElement(nsGkAtoms::annotation_
)) {
70 // If the <annotation> element has an src attribute we ignore it.
71 // XXXfredw Should annotation images be supported? See the related
72 // bug 297465 for mglyph.
73 if (childContent
->AsElement()->HasAttr(kNameSpaceID_None
, nsGkAtoms::src
))
76 // Otherwise, we assume it is a text annotation that can always be
77 // displayed and stop here.
78 mSelectedFrame
= childFrame
;
82 if (childContent
->IsMathMLElement(nsGkAtoms::annotation_xml_
)) {
83 // If the <annotation-xml> element has an src attribute we ignore it.
84 if (childContent
->AsElement()->HasAttr(kNameSpaceID_None
, nsGkAtoms::src
))
87 // If the <annotation-xml> element has an encoding attribute
88 // describing presentation MathML, SVG or HTML we assume the content
89 // can be displayed and stop here.
91 // We recognize the following encoding values:
93 // - "MathML-Presentation", which is mentioned in the MathML3 REC
94 // - "SVG1.1" which is mentioned in the W3C note
95 // http://www.w3.org/Math/Documents/Notes/graphics.xml
96 // - Other mime Content-Types for SVG and HTML
98 // We exclude APPLICATION_MATHML_XML = "application/mathml+xml" which
99 // is ambiguous about whether it is Presentation or Content MathML.
100 // Authors must use a more explicit encoding value.
102 childContent
->AsElement()->GetAttr(kNameSpaceID_None
, nsGkAtoms::encoding
,
104 if (value
.EqualsLiteral("application/mathml-presentation+xml") ||
105 value
.EqualsLiteral("MathML-Presentation") ||
106 value
.EqualsLiteral(IMAGE_SVG_XML
) || value
.EqualsLiteral("SVG1.1") ||
107 value
.EqualsLiteral(APPLICATION_XHTML_XML
) ||
108 value
.EqualsLiteral(TEXT_HTML
)) {
109 mSelectedFrame
= childFrame
;
115 TransmitAutomaticData();
116 return mSelectedFrame
;