summaryrefslogtreecommitdiffstats
path: root/dom/xul/templates/nsXMLBinding.cpp
blob: 9c1965ce2c912efe3607372bc890be6ccfbe4bf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsXULTemplateQueryProcessorXML.h"
#include "nsXULTemplateResultXML.h"
#include "nsXMLBinding.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/XPathResult.h"

using namespace mozilla;
using namespace mozilla::dom;

nsXMLBindingSet::~nsXMLBindingSet()
{}

void
nsXMLBindingSet::AddBinding(nsIAtom* aVar, nsAutoPtr<XPathExpression>&& aExpr)
{
  nsAutoPtr<nsXMLBinding> newbinding(new nsXMLBinding(aVar, Move(aExpr)));

  if (mFirst) {
    nsXMLBinding* binding = mFirst;

    while (binding) {
      // if the target variable is already used in a binding, ignore it
      // since it won't be useful for anything
      if (binding->mVar == aVar)
        return;

      // add the binding at the end of the list
      if (!binding->mNext) {
        binding->mNext = newbinding;
        return;
      }

      binding = binding->mNext;
    }
  }
  else {
    mFirst = newbinding;
  }
}

int32_t
nsXMLBindingSet::LookupTargetIndex(nsIAtom* aTargetVariable,
                                   nsXMLBinding** aBinding)
{
  int32_t idx = 0;
  nsXMLBinding* binding = mFirst;

  while (binding) {
    if (binding->mVar == aTargetVariable) {
      *aBinding = binding;
      return idx;
    }
    idx++;
    binding = binding->mNext;
  }

  *aBinding = nullptr;
  return -1;
}

XPathResult*
nsXMLBindingValues::GetAssignmentFor(nsXULTemplateResultXML* aResult,
                                     nsXMLBinding* aBinding,
                                     int32_t aIndex,
                                     uint16_t aType)
{
  XPathResult* value = mValues.SafeElementAt(aIndex);
  if (value) {
    return value;
  }

  nsINode* contextNode = aResult->Node();
  if (!contextNode) {
    return nullptr;
  }

  mValues.EnsureLengthAtLeast(aIndex + 1);

  ErrorResult ignored;
  mValues[aIndex] = aBinding->mExpr->Evaluate(*contextNode, aType, nullptr,
                                              ignored);

  return mValues[aIndex];
}

nsINode*
nsXMLBindingValues::GetNodeAssignmentFor(nsXULTemplateResultXML* aResult,
                                         nsXMLBinding* aBinding,
                                         int32_t aIndex)
{
  XPathResult* result = GetAssignmentFor(aResult, aBinding, aIndex,
                                         XPathResult::FIRST_ORDERED_NODE_TYPE);

  ErrorResult rv;
  return result ? result->GetSingleNodeValue(rv) : nullptr;
}

void
nsXMLBindingValues::GetStringAssignmentFor(nsXULTemplateResultXML* aResult,
                                           nsXMLBinding* aBinding,
                                           int32_t aIndex,
                                           nsAString& aValue)
{
  XPathResult* result = GetAssignmentFor(aResult, aBinding, aIndex,
                                         XPathResult::STRING_TYPE);

  if (result) {
    ErrorResult rv;
    result->GetStringValue(aValue, rv);
  } else {
    aValue.Truncate();
  }
}