ProcessingInstruction.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "nsGkAtoms.h"
  6. #include "nsUnicharUtils.h"
  7. #include "mozilla/dom/ProcessingInstruction.h"
  8. #include "mozilla/dom/ProcessingInstructionBinding.h"
  9. #include "mozilla/dom/XMLStylesheetProcessingInstruction.h"
  10. #include "mozilla/IntegerPrintfMacros.h"
  11. #include "nsContentUtils.h"
  12. already_AddRefed<mozilla::dom::ProcessingInstruction>
  13. NS_NewXMLProcessingInstruction(nsNodeInfoManager *aNodeInfoManager,
  14. const nsAString& aTarget,
  15. const nsAString& aData)
  16. {
  17. using mozilla::dom::ProcessingInstruction;
  18. using mozilla::dom::XMLStylesheetProcessingInstruction;
  19. NS_PRECONDITION(aNodeInfoManager, "Missing nodeinfo manager");
  20. nsCOMPtr<nsIAtom> target = NS_Atomize(aTarget);
  21. MOZ_ASSERT(target);
  22. if (target == nsGkAtoms::xml_stylesheet) {
  23. RefPtr<XMLStylesheetProcessingInstruction> pi =
  24. new XMLStylesheetProcessingInstruction(aNodeInfoManager, aData);
  25. return pi.forget();
  26. }
  27. RefPtr<mozilla::dom::NodeInfo> ni;
  28. ni = aNodeInfoManager->GetNodeInfo(nsGkAtoms::processingInstructionTagName,
  29. nullptr, kNameSpaceID_None,
  30. nsIDOMNode::PROCESSING_INSTRUCTION_NODE,
  31. target);
  32. RefPtr<ProcessingInstruction> instance =
  33. new ProcessingInstruction(ni.forget(), aData);
  34. return instance.forget();
  35. }
  36. namespace mozilla {
  37. namespace dom {
  38. ProcessingInstruction::ProcessingInstruction(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
  39. const nsAString& aData)
  40. : nsGenericDOMDataNode(Move(aNodeInfo))
  41. {
  42. MOZ_ASSERT(mNodeInfo->NodeType() == nsIDOMNode::PROCESSING_INSTRUCTION_NODE,
  43. "Bad NodeType in aNodeInfo");
  44. SetTextInternal(0, mText.GetLength(),
  45. aData.BeginReading(), aData.Length(),
  46. false); // Don't notify (bug 420429).
  47. }
  48. ProcessingInstruction::~ProcessingInstruction()
  49. {
  50. }
  51. NS_IMPL_ISUPPORTS_INHERITED(ProcessingInstruction, nsGenericDOMDataNode,
  52. nsIDOMNode, nsIDOMCharacterData,
  53. nsIDOMProcessingInstruction)
  54. JSObject*
  55. ProcessingInstruction::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  56. {
  57. return ProcessingInstructionBinding::Wrap(aCx, this, aGivenProto);
  58. }
  59. NS_IMETHODIMP
  60. ProcessingInstruction::GetTarget(nsAString& aTarget)
  61. {
  62. aTarget = NodeName();
  63. return NS_OK;
  64. }
  65. bool
  66. ProcessingInstruction::GetAttrValue(nsIAtom *aName, nsAString& aValue)
  67. {
  68. nsAutoString data;
  69. GetData(data);
  70. return nsContentUtils::GetPseudoAttributeValue(data, aName, aValue);
  71. }
  72. bool
  73. ProcessingInstruction::IsNodeOfType(uint32_t aFlags) const
  74. {
  75. return !(aFlags & ~(eCONTENT | ePROCESSING_INSTRUCTION | eDATA_NODE));
  76. }
  77. nsGenericDOMDataNode*
  78. ProcessingInstruction::CloneDataNode(mozilla::dom::NodeInfo *aNodeInfo,
  79. bool aCloneText) const
  80. {
  81. nsAutoString data;
  82. nsGenericDOMDataNode::GetData(data);
  83. RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
  84. return new ProcessingInstruction(ni.forget(), data);
  85. }
  86. #ifdef DEBUG
  87. void
  88. ProcessingInstruction::List(FILE* out, int32_t aIndent) const
  89. {
  90. int32_t index;
  91. for (index = aIndent; --index >= 0; ) fputs(" ", out);
  92. fprintf(out, "Processing instruction refcount=%" PRIuPTR "<", mRefCnt.get());
  93. nsAutoString tmp;
  94. ToCString(tmp, 0, mText.GetLength());
  95. tmp.Insert(nsDependentAtomString(NodeInfo()->GetExtraName()).get(), 0);
  96. fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out);
  97. fputs(">\n", out);
  98. }
  99. void
  100. ProcessingInstruction::DumpContent(FILE* out, int32_t aIndent,
  101. bool aDumpAll) const
  102. {
  103. }
  104. #endif
  105. } // namespace dom
  106. } // namespace mozilla