XMLStylesheetProcessingInstruction.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* -*- Mode: C++; tab-width: 8; 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 "XMLStylesheetProcessingInstruction.h"
  6. #include "mozilla/dom/XMLStylesheetProcessingInstructionBinding.h"
  7. #include "nsContentUtils.h"
  8. #include "nsNetUtil.h"
  9. namespace mozilla {
  10. namespace dom {
  11. // nsISupports implementation
  12. NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(XMLStylesheetProcessingInstruction)
  13. NS_INTERFACE_TABLE_INHERITED(XMLStylesheetProcessingInstruction, nsIDOMNode,
  14. nsIDOMProcessingInstruction,
  15. nsIStyleSheetLinkingElement)
  16. NS_INTERFACE_TABLE_TAIL_INHERITING(ProcessingInstruction)
  17. NS_IMPL_ADDREF_INHERITED(XMLStylesheetProcessingInstruction,
  18. ProcessingInstruction)
  19. NS_IMPL_RELEASE_INHERITED(XMLStylesheetProcessingInstruction,
  20. ProcessingInstruction)
  21. NS_IMPL_CYCLE_COLLECTION_CLASS(XMLStylesheetProcessingInstruction)
  22. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(XMLStylesheetProcessingInstruction,
  23. ProcessingInstruction)
  24. tmp->nsStyleLinkElement::Traverse(cb);
  25. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  26. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(XMLStylesheetProcessingInstruction,
  27. ProcessingInstruction)
  28. tmp->nsStyleLinkElement::Unlink();
  29. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  30. XMLStylesheetProcessingInstruction::~XMLStylesheetProcessingInstruction()
  31. {
  32. }
  33. JSObject*
  34. XMLStylesheetProcessingInstruction::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  35. {
  36. return XMLStylesheetProcessingInstructionBinding::Wrap(aCx, this, aGivenProto);
  37. }
  38. // nsIContent
  39. nsresult
  40. XMLStylesheetProcessingInstruction::BindToTree(nsIDocument* aDocument,
  41. nsIContent* aParent,
  42. nsIContent* aBindingParent,
  43. bool aCompileEventHandlers)
  44. {
  45. nsresult rv = ProcessingInstruction::BindToTree(aDocument, aParent,
  46. aBindingParent,
  47. aCompileEventHandlers);
  48. NS_ENSURE_SUCCESS(rv, rv);
  49. void (XMLStylesheetProcessingInstruction::*update)() =
  50. &XMLStylesheetProcessingInstruction::UpdateStyleSheetInternal;
  51. nsContentUtils::AddScriptRunner(NewRunnableMethod(this, update));
  52. return rv;
  53. }
  54. void
  55. XMLStylesheetProcessingInstruction::UnbindFromTree(bool aDeep, bool aNullParent)
  56. {
  57. nsCOMPtr<nsIDocument> oldDoc = GetUncomposedDoc();
  58. ProcessingInstruction::UnbindFromTree(aDeep, aNullParent);
  59. UpdateStyleSheetInternal(oldDoc, nullptr);
  60. }
  61. // nsIDOMNode
  62. void
  63. XMLStylesheetProcessingInstruction::SetNodeValueInternal(const nsAString& aNodeValue,
  64. ErrorResult& aError)
  65. {
  66. nsGenericDOMDataNode::SetNodeValueInternal(aNodeValue, aError);
  67. if (!aError.Failed()) {
  68. UpdateStyleSheetInternal(nullptr, nullptr, true);
  69. }
  70. }
  71. // nsStyleLinkElement
  72. NS_IMETHODIMP
  73. XMLStylesheetProcessingInstruction::GetCharset(nsAString& aCharset)
  74. {
  75. return GetAttrValue(nsGkAtoms::charset, aCharset) ? NS_OK : NS_ERROR_FAILURE;
  76. }
  77. /* virtual */ void
  78. XMLStylesheetProcessingInstruction::OverrideBaseURI(nsIURI* aNewBaseURI)
  79. {
  80. mOverriddenBaseURI = aNewBaseURI;
  81. }
  82. already_AddRefed<nsIURI>
  83. XMLStylesheetProcessingInstruction::GetStyleSheetURL(bool* aIsInline)
  84. {
  85. *aIsInline = false;
  86. nsAutoString href;
  87. if (!GetAttrValue(nsGkAtoms::href, href)) {
  88. return nullptr;
  89. }
  90. nsIURI *baseURL;
  91. nsAutoCString charset;
  92. nsIDocument *document = OwnerDoc();
  93. baseURL = mOverriddenBaseURI ?
  94. mOverriddenBaseURI.get() :
  95. document->GetDocBaseURI();
  96. charset = document->GetDocumentCharacterSet();
  97. nsCOMPtr<nsIURI> aURI;
  98. NS_NewURI(getter_AddRefs(aURI), href, charset.get(), baseURL);
  99. return aURI.forget();
  100. }
  101. void
  102. XMLStylesheetProcessingInstruction::GetStyleSheetInfo(nsAString& aTitle,
  103. nsAString& aType,
  104. nsAString& aMedia,
  105. bool* aIsScoped,
  106. bool* aIsAlternate,
  107. bool* aIsExplicitlyEnabled)
  108. {
  109. aTitle.Truncate();
  110. aType.Truncate();
  111. aMedia.Truncate();
  112. *aIsScoped = false;
  113. *aIsAlternate = false;
  114. *aIsExplicitlyEnabled = false;
  115. // xml-stylesheet PI is special only in prolog
  116. if (!nsContentUtils::InProlog(this)) {
  117. return;
  118. }
  119. nsAutoString data;
  120. GetData(data);
  121. nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::title, aTitle);
  122. nsAutoString alternate;
  123. nsContentUtils::GetPseudoAttributeValue(data,
  124. nsGkAtoms::alternate,
  125. alternate);
  126. // if alternate, does it have title?
  127. if (alternate.EqualsLiteral("yes")) {
  128. if (aTitle.IsEmpty()) { // alternates must have title
  129. return;
  130. }
  131. *aIsAlternate = true;
  132. }
  133. nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::media, aMedia);
  134. nsAutoString type;
  135. nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::type, type);
  136. nsAutoString mimeType, notUsed;
  137. nsContentUtils::SplitMimeType(type, mimeType, notUsed);
  138. if (!mimeType.IsEmpty() && !mimeType.LowerCaseEqualsLiteral("text/css")) {
  139. aType.Assign(type);
  140. return;
  141. }
  142. // If we get here we assume that we're loading a css file, so set the
  143. // type to 'text/css'
  144. aType.AssignLiteral("text/css");
  145. return;
  146. }
  147. nsGenericDOMDataNode*
  148. XMLStylesheetProcessingInstruction::CloneDataNode(mozilla::dom::NodeInfo *aNodeInfo,
  149. bool aCloneText) const
  150. {
  151. nsAutoString data;
  152. nsGenericDOMDataNode::GetData(data);
  153. RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
  154. return new XMLStylesheetProcessingInstruction(ni.forget(), data);
  155. }
  156. } // namespace dom
  157. } // namespace mozilla