nsMathMLsemanticsFrame.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/. */
  5. #include "nsMathMLsemanticsFrame.h"
  6. #include "nsMimeTypes.h"
  7. #include "mozilla/gfx/2D.h"
  8. //
  9. // <semantics> -- associate annotations with a MathML expression
  10. //
  11. nsIFrame*
  12. NS_NewMathMLsemanticsFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
  13. {
  14. return new (aPresShell) nsMathMLsemanticsFrame(aContext);
  15. }
  16. NS_IMPL_FRAMEARENA_HELPERS(nsMathMLsemanticsFrame)
  17. nsMathMLsemanticsFrame::~nsMathMLsemanticsFrame()
  18. {
  19. }
  20. nsIFrame*
  21. nsMathMLsemanticsFrame::GetSelectedFrame()
  22. {
  23. // By default, we will display the first child of the <semantics> element.
  24. nsIFrame* childFrame = mFrames.FirstChild();
  25. mSelectedFrame = childFrame;
  26. // An empty <semantics> is invalid
  27. if (!childFrame) {
  28. mInvalidMarkup = true;
  29. return mSelectedFrame;
  30. }
  31. mInvalidMarkup = false;
  32. // Using <annotation> or <annotation-xml> as a first child is invalid.
  33. // However some people use this syntax so we take care of this case too.
  34. bool firstChildIsAnnotation = false;
  35. nsIContent* childContent = childFrame->GetContent();
  36. if (childContent->IsAnyOfMathMLElements(nsGkAtoms::annotation_,
  37. nsGkAtoms::annotation_xml_)) {
  38. firstChildIsAnnotation = true;
  39. }
  40. // If the first child is a presentation MathML element other than
  41. // <annotation> or <annotation-xml>, we are done.
  42. if (!firstChildIsAnnotation &&
  43. childFrame->IsFrameOfType(nsIFrame::eMathML)) {
  44. nsIMathMLFrame* mathMLFrame = do_QueryFrame(childFrame);
  45. if (mathMLFrame) {
  46. TransmitAutomaticData();
  47. return mSelectedFrame;
  48. }
  49. // The first child is not an annotation, so skip it.
  50. childFrame = childFrame->GetNextSibling();
  51. }
  52. // Otherwise, we read the list of annotations and select the first one that
  53. // could be displayed in place of the first child of <semantics>. If none is
  54. // found, we fallback to this first child.
  55. for ( ; childFrame; childFrame = childFrame->GetNextSibling()) {
  56. nsIContent* childContent = childFrame->GetContent();
  57. if (childContent->IsMathMLElement(nsGkAtoms::annotation_)) {
  58. // If the <annotation> element has an src attribute we ignore it.
  59. // XXXfredw Should annotation images be supported? See the related
  60. // bug 297465 for mglyph.
  61. if (childContent->HasAttr(kNameSpaceID_None, nsGkAtoms::src)) continue;
  62. // Otherwise, we assume it is a text annotation that can always be
  63. // displayed and stop here.
  64. mSelectedFrame = childFrame;
  65. break;
  66. }
  67. if (childContent->IsMathMLElement(nsGkAtoms::annotation_xml_)) {
  68. // If the <annotation-xml> element has an src attribute we ignore it.
  69. if (childContent->HasAttr(kNameSpaceID_None, nsGkAtoms::src)) continue;
  70. // If the <annotation-xml> element has an encoding attribute
  71. // describing presentation MathML, SVG or HTML we assume the content
  72. // can be displayed and stop here.
  73. //
  74. // We recognize the following encoding values:
  75. //
  76. // - "MathML-Presentation", which is mentioned in the MathML3 REC
  77. // - "SVG1.1" which is mentioned in the W3C note
  78. // http://www.w3.org/Math/Documents/Notes/graphics.xml
  79. // - Other mime Content-Types for SVG and HTML
  80. //
  81. // We exclude APPLICATION_MATHML_XML = "application/mathml+xml" which
  82. // is ambiguous about whether it is Presentation or Content MathML.
  83. // Authors must use a more explicit encoding value.
  84. nsAutoString value;
  85. childContent->GetAttr(kNameSpaceID_None, nsGkAtoms::encoding, value);
  86. if (value.EqualsLiteral("application/mathml-presentation+xml") ||
  87. value.EqualsLiteral("MathML-Presentation") ||
  88. value.EqualsLiteral(IMAGE_SVG_XML) ||
  89. value.EqualsLiteral("SVG1.1") ||
  90. value.EqualsLiteral(APPLICATION_XHTML_XML) ||
  91. value.EqualsLiteral(TEXT_HTML)) {
  92. mSelectedFrame = childFrame;
  93. break;
  94. }
  95. }
  96. }
  97. TransmitAutomaticData();
  98. return mSelectedFrame;
  99. }