content/xul/templates/src/nsXMLBinding.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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 "nsXULTemplateQueryProcessorXML.h"
michael@0 7 #include "nsXULTemplateResultXML.h"
michael@0 8 #include "nsXMLBinding.h"
michael@0 9
michael@0 10 NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(nsXMLBindingSet)
michael@0 11 NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE(nsXMLBindingSet)
michael@0 12
michael@0 13 NS_IMPL_CYCLE_COLLECTION_CLASS(nsXMLBindingSet)
michael@0 14
michael@0 15 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXMLBindingSet)
michael@0 16 nsXMLBinding* binding = tmp->mFirst;
michael@0 17 while (binding) {
michael@0 18 binding->mExpr = nullptr;
michael@0 19 binding = binding->mNext;
michael@0 20 }
michael@0 21 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
michael@0 22
michael@0 23 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXMLBindingSet)
michael@0 24 nsXMLBinding* binding = tmp->mFirst;
michael@0 25 while (binding) {
michael@0 26 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "nsXMLBinding::mExpr");
michael@0 27 cb.NoteXPCOMChild(binding->mExpr);
michael@0 28 binding = binding->mNext;
michael@0 29 }
michael@0 30 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
michael@0 31
michael@0 32 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsXMLBindingSet, AddRef)
michael@0 33 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsXMLBindingSet, Release)
michael@0 34
michael@0 35 nsresult
michael@0 36 nsXMLBindingSet::AddBinding(nsIAtom* aVar, nsIDOMXPathExpression* aExpr)
michael@0 37 {
michael@0 38 nsAutoPtr<nsXMLBinding> newbinding(new nsXMLBinding(aVar, aExpr));
michael@0 39 NS_ENSURE_TRUE(newbinding, NS_ERROR_OUT_OF_MEMORY);
michael@0 40
michael@0 41 if (mFirst) {
michael@0 42 nsXMLBinding* binding = mFirst;
michael@0 43
michael@0 44 while (binding) {
michael@0 45 // if the target variable is already used in a binding, ignore it
michael@0 46 // since it won't be useful for anything
michael@0 47 if (binding->mVar == aVar)
michael@0 48 return NS_OK;
michael@0 49
michael@0 50 // add the binding at the end of the list
michael@0 51 if (!binding->mNext) {
michael@0 52 binding->mNext = newbinding;
michael@0 53 break;
michael@0 54 }
michael@0 55
michael@0 56 binding = binding->mNext;
michael@0 57 }
michael@0 58 }
michael@0 59 else {
michael@0 60 mFirst = newbinding;
michael@0 61 }
michael@0 62
michael@0 63 return NS_OK;
michael@0 64 }
michael@0 65
michael@0 66 int32_t
michael@0 67 nsXMLBindingSet::LookupTargetIndex(nsIAtom* aTargetVariable,
michael@0 68 nsXMLBinding** aBinding)
michael@0 69 {
michael@0 70 int32_t idx = 0;
michael@0 71 nsXMLBinding* binding = mFirst;
michael@0 72
michael@0 73 while (binding) {
michael@0 74 if (binding->mVar == aTargetVariable) {
michael@0 75 *aBinding = binding;
michael@0 76 return idx;
michael@0 77 }
michael@0 78 idx++;
michael@0 79 binding = binding->mNext;
michael@0 80 }
michael@0 81
michael@0 82 *aBinding = nullptr;
michael@0 83 return -1;
michael@0 84 }
michael@0 85
michael@0 86 void
michael@0 87 nsXMLBindingValues::GetAssignmentFor(nsXULTemplateResultXML* aResult,
michael@0 88 nsXMLBinding* aBinding,
michael@0 89 int32_t aIndex,
michael@0 90 uint16_t aType,
michael@0 91 nsIDOMXPathResult** aValue)
michael@0 92 {
michael@0 93 *aValue = mValues.SafeObjectAt(aIndex);
michael@0 94
michael@0 95 if (!*aValue) {
michael@0 96 nsCOMPtr<nsIDOMNode> contextNode;
michael@0 97 aResult->GetNode(getter_AddRefs(contextNode));
michael@0 98 if (contextNode) {
michael@0 99 nsCOMPtr<nsISupports> resultsupports;
michael@0 100 aBinding->mExpr->Evaluate(contextNode, aType,
michael@0 101 nullptr, getter_AddRefs(resultsupports));
michael@0 102
michael@0 103 nsCOMPtr<nsIDOMXPathResult> result = do_QueryInterface(resultsupports);
michael@0 104 if (result && mValues.ReplaceObjectAt(result, aIndex))
michael@0 105 *aValue = result;
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 NS_IF_ADDREF(*aValue);
michael@0 110 }
michael@0 111
michael@0 112 void
michael@0 113 nsXMLBindingValues::GetNodeAssignmentFor(nsXULTemplateResultXML* aResult,
michael@0 114 nsXMLBinding* aBinding,
michael@0 115 int32_t aIndex,
michael@0 116 nsIDOMNode** aNode)
michael@0 117 {
michael@0 118 nsCOMPtr<nsIDOMXPathResult> result;
michael@0 119 GetAssignmentFor(aResult, aBinding, aIndex,
michael@0 120 nsIDOMXPathResult::FIRST_ORDERED_NODE_TYPE,
michael@0 121 getter_AddRefs(result));
michael@0 122
michael@0 123 if (result)
michael@0 124 result->GetSingleNodeValue(aNode);
michael@0 125 else
michael@0 126 *aNode = nullptr;
michael@0 127 }
michael@0 128
michael@0 129 void
michael@0 130 nsXMLBindingValues::GetStringAssignmentFor(nsXULTemplateResultXML* aResult,
michael@0 131 nsXMLBinding* aBinding,
michael@0 132 int32_t aIndex,
michael@0 133 nsAString& aValue)
michael@0 134 {
michael@0 135 nsCOMPtr<nsIDOMXPathResult> result;
michael@0 136 GetAssignmentFor(aResult, aBinding, aIndex,
michael@0 137 nsIDOMXPathResult::STRING_TYPE, getter_AddRefs(result));
michael@0 138
michael@0 139 if (result)
michael@0 140 result->GetStringValue(aValue);
michael@0 141 else
michael@0 142 aValue.Truncate();
michael@0 143 }

mercurial