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