Attachment #8918161: P2: Backout changeset 8f77d260780d (bug 1384661 part 2) for bug #1406395

View | Details | Raw Unified | Return to bug 1406395
Collapse All | Expand All

(-)a/dom/base/FragmentOrElement.cpp (-71 lines)
Line     Link Here 
 Lines 570-646   nsAttrChildContentList::IndexOf(nsIConte Link Here 
570
}
570
}
571
571
572
//----------------------------------------------------------------------
572
//----------------------------------------------------------------------
573
NS_IMETHODIMP
574
nsParentNodeChildContentList::GetLength(uint32_t* aLength)
575
{
576
  if (!mIsCacheValid && !ValidateCache()) {
577
    *aLength = 0;
578
    return NS_OK;
579
  }
580
581
  MOZ_ASSERT(mIsCacheValid);
582
583
  *aLength = mCachedChildArray.Length();
584
  return NS_OK;
585
}
586
587
NS_IMETHODIMP
588
nsParentNodeChildContentList::Item(uint32_t aIndex, nsIDOMNode** aReturn)
589
{
590
  nsINode* node = Item(aIndex);
591
  if (!node) {
592
    *aReturn = nullptr;
593
    return NS_OK;
594
  }
595
596
  return CallQueryInterface(node, aReturn);
597
}
598
599
nsIContent*
600
nsParentNodeChildContentList::Item(uint32_t aIndex)
601
{
602
  if (!mIsCacheValid && !ValidateCache()) {
603
    return nullptr;
604
  }
605
606
  MOZ_ASSERT(mIsCacheValid);
607
608
  return mCachedChildArray.SafeElementAt(aIndex, nullptr);
609
}
610
611
int32_t
612
nsParentNodeChildContentList::IndexOf(nsIContent* aContent)
613
{
614
  if (!mIsCacheValid && !ValidateCache()) {
615
    return -1;
616
  }
617
618
  MOZ_ASSERT(mIsCacheValid);
619
620
  return mCachedChildArray.IndexOf(aContent);
621
}
622
623
bool
624
nsParentNodeChildContentList::ValidateCache()
625
{
626
  MOZ_ASSERT(!mIsCacheValid);
627
  MOZ_ASSERT(mCachedChildArray.IsEmpty());
628
629
  nsINode* parent = GetParentObject();
630
  if (!parent) {
631
    return false;
632
  }
633
634
  for (nsIContent* node = parent->GetFirstChild(); node;
635
       node = node->GetNextSibling()) {
636
    mCachedChildArray.AppendElement(node);
637
  }
638
  mIsCacheValid = true;
639
640
  return true;
641
}
642
643
//----------------------------------------------------------------------
644
573
645
nsIHTMLCollection*
574
nsIHTMLCollection*
646
FragmentOrElement::Children()
575
FragmentOrElement::Children()
(-)a/dom/base/nsChildContentList.h (-48 / +5 lines)
Line     Link Here 
 Lines 8-14    Link Here 
8
#define nsChildContentList_h__
8
#define nsChildContentList_h__
9
9
10
#include "nsISupportsImpl.h"
10
#include "nsISupportsImpl.h"
11
#include "nsINodeList.h"      // base class
11
#include "nsINodeList.h"            // base class
12
#include "js/TypeDecls.h"     // for Handle, Value, JSObject, JSContext
12
#include "js/TypeDecls.h"     // for Handle, Value, JSObject, JSContext
13
13
14
class nsIContent;
14
class nsIContent;
 Lines 20-26   class nsINode; Link Here 
20
 * and Item to its existing child list.
20
 * and Item to its existing child list.
21
 * @see nsIDOMNodeList
21
 * @see nsIDOMNodeList
22
 */
22
 */
23
class nsAttrChildContentList : public nsINodeList
23
class nsAttrChildContentList final : public nsINodeList
24
{
24
{
25
public:
25
public:
26
  explicit nsAttrChildContentList(nsINode* aNode)
26
  explicit nsAttrChildContentList(nsINode* aNode)
 Lines 42-48   public: Link Here 
42
  virtual int32_t IndexOf(nsIContent* aContent) override;
42
  virtual int32_t IndexOf(nsIContent* aContent) override;
43
  virtual nsIContent* Item(uint32_t aIndex) override;
43
  virtual nsIContent* Item(uint32_t aIndex) override;
44
44
45
  virtual void DropReference()
45
  void DropReference()
46
  {
46
  {
47
    mNode = nullptr;
47
    mNode = nullptr;
48
  }
48
  }
 Lines 52-107   public: Link Here 
52
    return mNode;
52
    return mNode;
53
  }
53
  }
54
54
55
protected:
55
private:
56
  virtual ~nsAttrChildContentList() {}
56
  ~nsAttrChildContentList() {}
57
57
58
private:
59
  // The node whose children make up the list.
58
  // The node whose children make up the list.
60
  // This is a non-owning ref which is safe because it's set to nullptr by
59
  // This is a non-owning ref which is safe because it's set to nullptr by
61
  // DropReference() by the node slots get destroyed.
60
  // DropReference() by the node slots get destroyed.
62
  nsINode* MOZ_NON_OWNING_REF mNode;
61
  nsINode* MOZ_NON_OWNING_REF mNode;
63
};
62
};
64
63
65
class nsParentNodeChildContentList final : public nsAttrChildContentList
66
{
67
public:
68
  explicit nsParentNodeChildContentList(nsINode* aNode)
69
    : nsAttrChildContentList(aNode)
70
    , mIsCacheValid(false)
71
  {
72
    ValidateCache();
73
  }
74
75
  // nsIDOMNodeList interface
76
  NS_DECL_NSIDOMNODELIST
77
78
  // nsINodeList interface
79
  virtual int32_t IndexOf(nsIContent* aContent) override;
80
  virtual nsIContent* Item(uint32_t aIndex) override;
81
82
  void DropReference() override
83
  {
84
    InvalidateCache();
85
    nsAttrChildContentList::DropReference();
86
  }
87
88
  void InvalidateCache()
89
  {
90
    mIsCacheValid = false;
91
    mCachedChildArray.Clear();
92
  }
93
94
private:
95
  ~nsParentNodeChildContentList() {}
96
97
  // Return true if validation succeeds, false otherwise
98
  bool ValidateCache();
99
100
  // Whether cached array of child nodes is valid
101
  bool mIsCacheValid;
102
103
  // Cached array of child nodes
104
  AutoTArray<nsIContent*, 8> mCachedChildArray;
105
};
106
107
#endif /* nsChildContentList_h__ */
64
#endif /* nsChildContentList_h__ */

Return to bug 1406395