content/xul/templates/src/nsXULTemplateResultXML.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsIServiceManager.h"
michael@0 7 #include "nsIDOMNode.h"
michael@0 8 #include "nsIDOMElement.h"
michael@0 9 #include "nsIContent.h"
michael@0 10
michael@0 11 #include "nsIRDFService.h"
michael@0 12
michael@0 13 #include "nsXULTemplateResultXML.h"
michael@0 14 #include "nsXMLBinding.h"
michael@0 15
michael@0 16 static uint32_t sTemplateId = 0;
michael@0 17
michael@0 18 NS_IMPL_ISUPPORTS(nsXULTemplateResultXML, nsIXULTemplateResult)
michael@0 19
michael@0 20 nsXULTemplateResultXML::nsXULTemplateResultXML(nsXMLQuery* aQuery,
michael@0 21 nsIDOMNode* aNode,
michael@0 22 nsXMLBindingSet* aBindings)
michael@0 23 : mQuery(aQuery), mNode(aNode)
michael@0 24 {
michael@0 25 nsCOMPtr<nsIContent> content = do_QueryInterface(mNode);
michael@0 26
michael@0 27 // If the node has an id, create the uri from it. Otherwise, there isn't
michael@0 28 // anything to identify the node with so just use a somewhat random number.
michael@0 29 nsCOMPtr<nsIAtom> id = content->GetID();
michael@0 30 if (id) {
michael@0 31 nsCOMPtr<nsIURI> uri = content->GetBaseURI();
michael@0 32 nsAutoCString spec;
michael@0 33 uri->GetSpec(spec);
michael@0 34
michael@0 35 mId = NS_ConvertUTF8toUTF16(spec);
michael@0 36
michael@0 37 nsAutoString idstr;
michael@0 38 id->ToString(idstr);
michael@0 39 mId += NS_LITERAL_STRING("#") + idstr;
michael@0 40 }
michael@0 41 else {
michael@0 42 nsAutoString rowid(NS_LITERAL_STRING("row"));
michael@0 43 rowid.AppendInt(++sTemplateId);
michael@0 44 mId.Assign(rowid);
michael@0 45 }
michael@0 46
michael@0 47 if (aBindings)
michael@0 48 mRequiredValues.SetBindingSet(aBindings);
michael@0 49 }
michael@0 50
michael@0 51 NS_IMETHODIMP
michael@0 52 nsXULTemplateResultXML::GetIsContainer(bool* aIsContainer)
michael@0 53 {
michael@0 54 // a node is considered a container if it has children
michael@0 55 if (mNode)
michael@0 56 mNode->HasChildNodes(aIsContainer);
michael@0 57 else
michael@0 58 *aIsContainer = false;
michael@0 59 return NS_OK;
michael@0 60 }
michael@0 61
michael@0 62 NS_IMETHODIMP
michael@0 63 nsXULTemplateResultXML::GetIsEmpty(bool* aIsEmpty)
michael@0 64 {
michael@0 65 // a node is considered empty if it has no elements as children
michael@0 66 nsCOMPtr<nsIContent> content = do_QueryInterface(mNode);
michael@0 67 if (content) {
michael@0 68 for (nsIContent* child = content->GetFirstChild();
michael@0 69 child;
michael@0 70 child = child->GetNextSibling()) {
michael@0 71 if (child->IsElement()) {
michael@0 72 *aIsEmpty = false;
michael@0 73 return NS_OK;
michael@0 74 }
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 *aIsEmpty = true;
michael@0 79 return NS_OK;
michael@0 80 }
michael@0 81
michael@0 82 NS_IMETHODIMP
michael@0 83 nsXULTemplateResultXML::GetMayProcessChildren(bool* aMayProcessChildren)
michael@0 84 {
michael@0 85 *aMayProcessChildren = true;
michael@0 86 return NS_OK;
michael@0 87 }
michael@0 88
michael@0 89 NS_IMETHODIMP
michael@0 90 nsXULTemplateResultXML::GetId(nsAString& aId)
michael@0 91 {
michael@0 92 aId = mId;
michael@0 93 return NS_OK;
michael@0 94 }
michael@0 95
michael@0 96 NS_IMETHODIMP
michael@0 97 nsXULTemplateResultXML::GetResource(nsIRDFResource** aResource)
michael@0 98 {
michael@0 99 *aResource = nullptr;
michael@0 100 return NS_OK;
michael@0 101 }
michael@0 102
michael@0 103 NS_IMETHODIMP
michael@0 104 nsXULTemplateResultXML::GetType(nsAString& aType)
michael@0 105 {
michael@0 106 aType.Truncate();
michael@0 107 return NS_OK;
michael@0 108 }
michael@0 109
michael@0 110 NS_IMETHODIMP
michael@0 111 nsXULTemplateResultXML::GetBindingFor(nsIAtom* aVar, nsAString& aValue)
michael@0 112 {
michael@0 113 NS_ENSURE_ARG_POINTER(aVar);
michael@0 114
michael@0 115 // get the position of the atom in the variables table
michael@0 116 nsXMLBinding* binding;
michael@0 117
michael@0 118 int32_t idx = mRequiredValues.LookupTargetIndex(aVar, &binding);
michael@0 119 if (idx >= 0) {
michael@0 120 mRequiredValues.GetStringAssignmentFor(this, binding, idx, aValue);
michael@0 121 return NS_OK;
michael@0 122 }
michael@0 123
michael@0 124 idx = mOptionalValues.LookupTargetIndex(aVar, &binding);
michael@0 125 if (idx >= 0) {
michael@0 126 mOptionalValues.GetStringAssignmentFor(this, binding, idx, aValue);
michael@0 127 return NS_OK;
michael@0 128 }
michael@0 129
michael@0 130 // if the variable is not bound, just use the variable name as the name of
michael@0 131 // an attribute to retrieve
michael@0 132 nsAutoString attr;
michael@0 133 aVar->ToString(attr);
michael@0 134
michael@0 135 if (attr.Length() > 1) {
michael@0 136 nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mNode);
michael@0 137 if (element)
michael@0 138 return element->GetAttribute(Substring(attr, 1), aValue);
michael@0 139 }
michael@0 140
michael@0 141 aValue.Truncate();
michael@0 142 return NS_OK;
michael@0 143 }
michael@0 144
michael@0 145 NS_IMETHODIMP
michael@0 146 nsXULTemplateResultXML::GetBindingObjectFor(nsIAtom* aVar, nsISupports** aValue)
michael@0 147 {
michael@0 148 NS_ENSURE_ARG_POINTER(aVar);
michael@0 149
michael@0 150 nsXMLBinding* binding;
michael@0 151 nsCOMPtr<nsIDOMNode> node;
michael@0 152
michael@0 153 if (mQuery && aVar == mQuery->GetMemberVariable()) {
michael@0 154 node = mNode;
michael@0 155 }
michael@0 156 else {
michael@0 157 int32_t idx = mRequiredValues.LookupTargetIndex(aVar, &binding);
michael@0 158 if (idx > 0) {
michael@0 159 mRequiredValues.GetNodeAssignmentFor(this, binding, idx,
michael@0 160 getter_AddRefs(node));
michael@0 161 }
michael@0 162 else {
michael@0 163 idx = mOptionalValues.LookupTargetIndex(aVar, &binding);
michael@0 164 if (idx > 0) {
michael@0 165 mOptionalValues.GetNodeAssignmentFor(this, binding, idx,
michael@0 166 getter_AddRefs(node));
michael@0 167 }
michael@0 168 }
michael@0 169 }
michael@0 170
michael@0 171 *aValue = node;
michael@0 172 NS_IF_ADDREF(*aValue);
michael@0 173 return NS_OK;
michael@0 174 }
michael@0 175
michael@0 176 NS_IMETHODIMP
michael@0 177 nsXULTemplateResultXML::RuleMatched(nsISupports* aQueryNode,
michael@0 178 nsIDOMNode* aRuleNode)
michael@0 179 {
michael@0 180 // when a rule matches, set the bindings that must be used.
michael@0 181 nsXULTemplateQueryProcessorXML* processor = mQuery ? mQuery->Processor() :
michael@0 182 nullptr;
michael@0 183 if (processor) {
michael@0 184 nsXMLBindingSet* bindings =
michael@0 185 processor->GetOptionalBindingsForRule(aRuleNode);
michael@0 186 if (bindings)
michael@0 187 mOptionalValues.SetBindingSet(bindings);
michael@0 188 }
michael@0 189
michael@0 190 return NS_OK;
michael@0 191 }
michael@0 192
michael@0 193 NS_IMETHODIMP
michael@0 194 nsXULTemplateResultXML::HasBeenRemoved()
michael@0 195 {
michael@0 196 return NS_OK;
michael@0 197 }
michael@0 198
michael@0 199 void
michael@0 200 nsXULTemplateResultXML::GetNode(nsIDOMNode** aNode)
michael@0 201 {
michael@0 202 *aNode = mNode;
michael@0 203 NS_IF_ADDREF(*aNode);
michael@0 204 }

mercurial