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