dom/xbl/XBLChildrenElement.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xbl/XBLChildrenElement.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,218 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 sw=2 et tw=79: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "mozilla/dom/XBLChildrenElement.h"
    1.11 +#include "nsCharSeparatedTokenizer.h"
    1.12 +#include "mozilla/dom/NodeListBinding.h"
    1.13 +
    1.14 +namespace mozilla {
    1.15 +namespace dom {
    1.16 +
    1.17 +XBLChildrenElement::~XBLChildrenElement()
    1.18 +{
    1.19 +}
    1.20 +
    1.21 +NS_IMPL_ADDREF_INHERITED(XBLChildrenElement, Element)
    1.22 +NS_IMPL_RELEASE_INHERITED(XBLChildrenElement, Element)
    1.23 +
    1.24 +NS_INTERFACE_TABLE_HEAD(XBLChildrenElement)
    1.25 +  NS_INTERFACE_TABLE_INHERITED(XBLChildrenElement, nsIDOMNode,
    1.26 +                               nsIDOMElement)
    1.27 +  NS_ELEMENT_INTERFACE_TABLE_TO_MAP_SEGUE
    1.28 +NS_INTERFACE_MAP_END_INHERITING(Element)
    1.29 +
    1.30 +NS_IMPL_ELEMENT_CLONE(XBLChildrenElement)
    1.31 +
    1.32 +nsIAtom*
    1.33 +XBLChildrenElement::GetIDAttributeName() const
    1.34 +{
    1.35 +  return nullptr;
    1.36 +}
    1.37 +
    1.38 +nsIAtom*
    1.39 +XBLChildrenElement::DoGetID() const
    1.40 +{
    1.41 +  return nullptr;
    1.42 +}
    1.43 +
    1.44 +nsresult
    1.45 +XBLChildrenElement::UnsetAttr(int32_t aNameSpaceID, nsIAtom* aAttribute,
    1.46 +                              bool aNotify)
    1.47 +{
    1.48 +  if (aAttribute == nsGkAtoms::includes &&
    1.49 +      aNameSpaceID == kNameSpaceID_None) {
    1.50 +    mIncludes.Clear();
    1.51 +  }
    1.52 +
    1.53 +  return Element::UnsetAttr(aNameSpaceID, aAttribute, aNotify);
    1.54 +}
    1.55 +
    1.56 +bool
    1.57 +XBLChildrenElement::ParseAttribute(int32_t aNamespaceID,
    1.58 +                                   nsIAtom* aAttribute,
    1.59 +                                   const nsAString& aValue,
    1.60 +                                   nsAttrValue& aResult)
    1.61 +{
    1.62 +  if (aAttribute == nsGkAtoms::includes &&
    1.63 +      aNamespaceID == kNameSpaceID_None) {
    1.64 +    mIncludes.Clear();
    1.65 +    nsCharSeparatedTokenizer tok(aValue, '|',
    1.66 +                                 nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
    1.67 +    while (tok.hasMoreTokens()) {
    1.68 +      nsCOMPtr<nsIAtom> atom = do_GetAtom(tok.nextToken());
    1.69 +      mIncludes.AppendElement(atom);
    1.70 +    }
    1.71 +  }
    1.72 +
    1.73 +  return false;
    1.74 +}
    1.75 +
    1.76 +} // namespace mozilla
    1.77 +} // namespace dom
    1.78 +
    1.79 +using namespace mozilla::dom;
    1.80 +
    1.81 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(nsAnonymousContentList, mParent)
    1.82 +
    1.83 +NS_IMPL_CYCLE_COLLECTING_ADDREF(nsAnonymousContentList)
    1.84 +NS_IMPL_CYCLE_COLLECTING_RELEASE(nsAnonymousContentList)
    1.85 +
    1.86 +NS_INTERFACE_TABLE_HEAD(nsAnonymousContentList)
    1.87 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.88 +  NS_INTERFACE_TABLE_INHERITED(nsAnonymousContentList, nsINodeList,
    1.89 +                               nsIDOMNodeList)
    1.90 +  NS_INTERFACE_TABLE_TO_MAP_SEGUE
    1.91 +  NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsAnonymousContentList)
    1.92 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.93 +NS_INTERFACE_MAP_END
    1.94 +
    1.95 +NS_IMETHODIMP
    1.96 +nsAnonymousContentList::GetLength(uint32_t* aLength)
    1.97 +{
    1.98 +  if (!mParent) {
    1.99 +    *aLength = 0;
   1.100 +    return NS_OK;
   1.101 +  }
   1.102 +
   1.103 +  uint32_t count = 0;
   1.104 +  for (nsIContent* child = mParent->GetFirstChild();
   1.105 +       child;
   1.106 +       child = child->GetNextSibling()) {
   1.107 +    if (child->NodeInfo()->Equals(nsGkAtoms::children, kNameSpaceID_XBL)) {
   1.108 +      XBLChildrenElement* point = static_cast<XBLChildrenElement*>(child);
   1.109 +      if (!point->mInsertedChildren.IsEmpty()) {
   1.110 +        count += point->mInsertedChildren.Length();
   1.111 +      }
   1.112 +      else {
   1.113 +        count += point->GetChildCount();
   1.114 +      }
   1.115 +    }
   1.116 +    else {
   1.117 +      ++count;
   1.118 +    }
   1.119 +  }
   1.120 +
   1.121 +  *aLength = count;
   1.122 +
   1.123 +  return NS_OK;
   1.124 +}
   1.125 +
   1.126 +NS_IMETHODIMP
   1.127 +nsAnonymousContentList::Item(uint32_t aIndex, nsIDOMNode** aReturn)
   1.128 +{
   1.129 +  nsIContent* item = Item(aIndex);
   1.130 +  if (!item) {
   1.131 +    return NS_ERROR_FAILURE;
   1.132 +  }
   1.133 +
   1.134 +  return CallQueryInterface(item, aReturn);
   1.135 +}
   1.136 +
   1.137 +nsIContent*
   1.138 +nsAnonymousContentList::Item(uint32_t aIndex)
   1.139 +{
   1.140 +  if (!mParent) {
   1.141 +    return nullptr;
   1.142 +  }
   1.143 +
   1.144 +  uint32_t remIndex = aIndex;
   1.145 +  for (nsIContent* child = mParent->GetFirstChild();
   1.146 +       child;
   1.147 +       child = child->GetNextSibling()) {
   1.148 +    if (child->NodeInfo()->Equals(nsGkAtoms::children, kNameSpaceID_XBL)) {
   1.149 +      XBLChildrenElement* point = static_cast<XBLChildrenElement*>(child);
   1.150 +      if (!point->mInsertedChildren.IsEmpty()) {
   1.151 +        if (remIndex < point->mInsertedChildren.Length()) {
   1.152 +          return point->mInsertedChildren[remIndex];
   1.153 +        }
   1.154 +        remIndex -= point->mInsertedChildren.Length();
   1.155 +      }
   1.156 +      else {
   1.157 +        if (remIndex < point->GetChildCount()) {
   1.158 +          return point->GetChildAt(remIndex);
   1.159 +        }
   1.160 +        remIndex -= point->GetChildCount();
   1.161 +      }
   1.162 +    }
   1.163 +    else {
   1.164 +      if (remIndex == 0) {
   1.165 +        return child;
   1.166 +      }
   1.167 +      --remIndex;
   1.168 +    }
   1.169 +  }
   1.170 +
   1.171 +  return nullptr;
   1.172 +}
   1.173 +
   1.174 +int32_t
   1.175 +nsAnonymousContentList::IndexOf(nsIContent* aContent)
   1.176 +{
   1.177 +  NS_ASSERTION(!aContent->NodeInfo()->Equals(nsGkAtoms::children,
   1.178 +                                             kNameSpaceID_XBL),
   1.179 +               "Looking for insertion point");
   1.180 +
   1.181 +  if (!mParent) {
   1.182 +    return -1;
   1.183 +  }
   1.184 +
   1.185 +  uint32_t index = 0;
   1.186 +  for (nsIContent* child = mParent->GetFirstChild();
   1.187 +       child;
   1.188 +       child = child->GetNextSibling()) {
   1.189 +    if (child->NodeInfo()->Equals(nsGkAtoms::children, kNameSpaceID_XBL)) {
   1.190 +      XBLChildrenElement* point = static_cast<XBLChildrenElement*>(child);
   1.191 +      if (!point->mInsertedChildren.IsEmpty()) {
   1.192 +        uint32_t insIndex = point->mInsertedChildren.IndexOf(aContent);
   1.193 +        if (insIndex != point->mInsertedChildren.NoIndex) {
   1.194 +          return index + insIndex;
   1.195 +        }
   1.196 +        index += point->mInsertedChildren.Length();
   1.197 +      }
   1.198 +      else {
   1.199 +        int32_t insIndex = point->IndexOf(aContent);
   1.200 +        if (insIndex != -1) {
   1.201 +          return index + (uint32_t)insIndex;
   1.202 +        }
   1.203 +        index += point->GetChildCount();
   1.204 +      }
   1.205 +    }
   1.206 +    else {
   1.207 +      if (child == aContent) {
   1.208 +        return index;
   1.209 +      }
   1.210 +      ++index;
   1.211 +    }
   1.212 +  }
   1.213 +
   1.214 +  return -1;
   1.215 +}
   1.216 +
   1.217 +JSObject*
   1.218 +nsAnonymousContentList::WrapObject(JSContext *cx)
   1.219 +{
   1.220 +  return mozilla::dom::NodeListBinding::Wrap(cx, this);
   1.221 +}

mercurial