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.

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

mercurial