tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

XPathResult.h (4684B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      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/. */
      5 
      6 #ifndef mozilla_dom_XPathResult_h
      7 #define mozilla_dom_XPathResult_h
      8 
      9 #include "mozilla/ErrorResult.h"
     10 #include "nsCOMPtr.h"
     11 #include "nsCycleCollectionParticipant.h"
     12 #include "nsINode.h"
     13 #include "nsIWeakReferenceUtils.h"
     14 #include "nsString.h"
     15 #include "nsStubMutationObserver.h"
     16 #include "nsTArray.h"
     17 #include "nsWrapperCache.h"
     18 
     19 class txAExprResult;
     20 
     21 namespace mozilla::dom {
     22 
     23 /**
     24 * A class for evaluating an XPath expression string
     25 */
     26 class XPathResult final : public nsStubMutationObserver, public nsWrapperCache {
     27  ~XPathResult();
     28 
     29 public:
     30  explicit XPathResult(nsINode* aParent);
     31  XPathResult(const XPathResult& aResult);
     32 
     33  enum {
     34    ANY_TYPE = 0U,
     35    NUMBER_TYPE = 1U,
     36    STRING_TYPE = 2U,
     37    BOOLEAN_TYPE = 3U,
     38    UNORDERED_NODE_ITERATOR_TYPE = 4U,
     39    ORDERED_NODE_ITERATOR_TYPE = 5U,
     40    UNORDERED_NODE_SNAPSHOT_TYPE = 6U,
     41    ORDERED_NODE_SNAPSHOT_TYPE = 7U,
     42    ANY_UNORDERED_NODE_TYPE = 8U,
     43    FIRST_ORDERED_NODE_TYPE = 9U
     44  };
     45 
     46  // nsISupports interface
     47  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     48  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(XPathResult)
     49 
     50  virtual JSObject* WrapObject(JSContext* aCx,
     51                               JS::Handle<JSObject*> aGivenProto) override;
     52  nsINode* GetParentObject() const { return mParent; }
     53  uint16_t ResultType() const { return mResultType; }
     54  double GetNumberValue(ErrorResult& aRv) const {
     55    if (mResultType != NUMBER_TYPE) {
     56      aRv.ThrowTypeError("Result is not a number");
     57      return 0;
     58    }
     59 
     60    return mNumberResult;
     61  }
     62  void GetStringValue(nsAString& aStringValue, ErrorResult& aRv) const {
     63    if (mResultType != STRING_TYPE) {
     64      aRv.ThrowTypeError("Result is not a string");
     65      return;
     66    }
     67 
     68    aStringValue = mStringResult;
     69  }
     70  bool GetBooleanValue(ErrorResult& aRv) const {
     71    if (mResultType != BOOLEAN_TYPE) {
     72      aRv.ThrowTypeError("Result is not a boolean");
     73      return false;
     74    }
     75 
     76    return mBooleanResult;
     77  }
     78  nsINode* GetSingleNodeValue(ErrorResult& aRv) const {
     79    if (!isNode()) {
     80      aRv.ThrowTypeError("Result is not a node");
     81      return nullptr;
     82    }
     83 
     84    return mResultNodes.SafeElementAt(0);
     85  }
     86  bool InvalidIteratorState() const {
     87    return isIterator() && mInvalidIteratorState;
     88  }
     89  uint32_t GetSnapshotLength(ErrorResult& aRv) const {
     90    if (!isSnapshot()) {
     91      aRv.ThrowTypeError("Result is not a snapshot");
     92      return 0;
     93    }
     94 
     95    return (uint32_t)mResultNodes.Length();
     96  }
     97  nsINode* IterateNext(ErrorResult& aRv);
     98  nsINode* SnapshotItem(uint32_t aIndex, ErrorResult& aRv) const {
     99    if (!isSnapshot()) {
    100      aRv.ThrowTypeError("Result is not a snapshot");
    101      return nullptr;
    102    }
    103 
    104    return mResultNodes.SafeElementAt(aIndex);
    105  }
    106 
    107  // nsIMutationObserver interface
    108  NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
    109  NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
    110  NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
    111  NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
    112  NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
    113  NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
    114 
    115  void SetExprResult(txAExprResult* aExprResult, uint16_t aResultType,
    116                     nsINode* aContextNode, ErrorResult& aRv);
    117  nsresult GetExprResult(txAExprResult** aExprResult);
    118  already_AddRefed<XPathResult> Clone(ErrorResult& aError);
    119  void RemoveObserver();
    120 
    121 private:
    122  static bool isSnapshot(uint16_t aResultType) {
    123    return aResultType == UNORDERED_NODE_SNAPSHOT_TYPE ||
    124           aResultType == ORDERED_NODE_SNAPSHOT_TYPE;
    125  }
    126  static bool isIterator(uint16_t aResultType) {
    127    return aResultType == UNORDERED_NODE_ITERATOR_TYPE ||
    128           aResultType == ORDERED_NODE_ITERATOR_TYPE;
    129  }
    130  static bool isNode(uint16_t aResultType) {
    131    return aResultType == FIRST_ORDERED_NODE_TYPE ||
    132           aResultType == ANY_UNORDERED_NODE_TYPE;
    133  }
    134  bool isSnapshot() const { return isSnapshot(mResultType); }
    135  bool isIterator() const { return isIterator(mResultType); }
    136  bool isNode() const { return isNode(mResultType); }
    137 
    138  void Invalidate(const nsIContent* aChangeRoot);
    139 
    140  nsCOMPtr<nsINode> mParent;
    141  RefPtr<txAExprResult> mResult;
    142  nsTArray<nsCOMPtr<nsINode>> mResultNodes;
    143  RefPtr<Document> mDocument;
    144  nsWeakPtr mContextNode;
    145  uint32_t mCurrentPos;
    146  uint16_t mResultType;
    147  bool mInvalidIteratorState;
    148  bool mBooleanResult;
    149  double mNumberResult;
    150  nsString mStringResult;
    151 };
    152 
    153 }  // namespace mozilla::dom
    154 
    155 #endif /* mozilla_dom_XPathResult_h */