⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 117033 in webkit


Ignore:
Timestamp:
May 14, 2012, 11:30:25 PM (14 years ago)
Author:
morrita@google.com
Message:

[Refactoring] Get rid of ContentDistribution::Item
https://bugs.webkit.org/show_bug.cgi?id=86350

This change replaces the linked list on ContentDistribution with a Vector.
We no longer link item class ContentDistribution::Item.
This simplification also allows ContentDistribution to go.
ContentDistribution is now just a typedef of Vector<RefPtr<Node> >.

Reviewed by Dimitri Glazkov.

No new tests. Covered by existing tests.

  • dom/ComposedShadowTreeWalker.cpp:

(WebCore::ComposedShadowTreeWalker::traverseNode):
(WebCore::ComposedShadowTreeWalker::traverseSiblingOrBackToInsertionPoint):

  • dom/ElementShadow.cpp:
  • dom/ElementShadow.h:

(ElementShadow):

  • dom/NodeRenderingContext.cpp:

(WebCore::nextRendererOfInsertionPoint):
(WebCore::previousRendererOfInsertionPoint):
(WebCore::firstRendererOfInsertionPoint):
(WebCore::lastRendererOfInsertionPoint):

  • html/shadow/ContentDistributor.cpp:

(WebCore::ContentDistributor::distribute):
(WebCore::ContentDistributor::clearDistribution):
(WebCore::ContentDistributor::findInsertionPointFor):

  • html/shadow/ContentDistributor.h:

(WebCore):
(ContentDistributor):

  • html/shadow/InsertionPoint.cpp:

(WebCore::InsertionPoint::InsertionPoint):
(WebCore::InsertionPoint::attachDistributedNode):
(WebCore::InsertionPoint::assignShadowRoot):
(WebCore::InsertionPoint::nextTo):
(WebCore):
(WebCore::InsertionPoint::previousTo):

  • html/shadow/InsertionPoint.h: Added a set of delegate method to m_distribution.

(WebCore::InsertionPoint::hasDistribution):
(WebCore::InsertionPoint::indexOf):
(WebCore::InsertionPoint::size):
(WebCore::InsertionPoint::at):
(WebCore::InsertionPoint::first):
(WebCore::InsertionPoint::last):
(InsertionPoint):

Location:
trunk/Source/WebCore
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r117032 r117033  
     12012-05-14  MORITA Hajime  <morrita@google.com>
     2
     3        [Refactoring] Get rid of ContentDistribution::Item
     4        https://bugs.webkit.org/show_bug.cgi?id=86350
     5
     6        This change replaces the linked list on ContentDistribution with a Vector.
     7        We no longer link item class ContentDistribution::Item.
     8        This simplification also allows ContentDistribution to go.
     9        ContentDistribution is now just a typedef of Vector<RefPtr<Node> >.
     10
     11        Reviewed by Dimitri Glazkov.
     12
     13        No new tests. Covered by existing tests.
     14
     15        * dom/ComposedShadowTreeWalker.cpp:
     16        (WebCore::ComposedShadowTreeWalker::traverseNode):
     17        (WebCore::ComposedShadowTreeWalker::traverseSiblingOrBackToInsertionPoint):
     18        * dom/ElementShadow.cpp:
     19        * dom/ElementShadow.h:
     20        (ElementShadow):
     21        * dom/NodeRenderingContext.cpp:
     22        (WebCore::nextRendererOfInsertionPoint):
     23        (WebCore::previousRendererOfInsertionPoint):
     24        (WebCore::firstRendererOfInsertionPoint):
     25        (WebCore::lastRendererOfInsertionPoint):
     26        * html/shadow/ContentDistributor.cpp:
     27        (WebCore::ContentDistributor::distribute):
     28        (WebCore::ContentDistributor::clearDistribution):
     29        (WebCore::ContentDistributor::findInsertionPointFor):
     30        * html/shadow/ContentDistributor.h:
     31        (WebCore):
     32        (ContentDistributor):
     33        * html/shadow/InsertionPoint.cpp:
     34        (WebCore::InsertionPoint::InsertionPoint):
     35        (WebCore::InsertionPoint::attachDistributedNode):
     36        (WebCore::InsertionPoint::assignShadowRoot):
     37        (WebCore::InsertionPoint::nextTo):
     38        (WebCore):
     39        (WebCore::InsertionPoint::previousTo):
     40        * html/shadow/InsertionPoint.h: Added a set of delegate method to m_distribution.
     41        (WebCore::InsertionPoint::hasDistribution):
     42        (WebCore::InsertionPoint::indexOf):
     43        (WebCore::InsertionPoint::size):
     44        (WebCore::InsertionPoint::at):
     45        (WebCore::InsertionPoint::first):
     46        (WebCore::InsertionPoint::last):
     47        (InsertionPoint):
     48
    1492012-05-14  Tim Horton  <timothy_horton@apple.com>
    250
  • trunk/Source/WebCore/dom/ComposedShadowTreeWalker.cpp

    r116974 r117033  
    114114    ASSERT(node);
    115115    if (isInsertionPoint(node)) {
    116         const ContentDistribution* distribution = toInsertionPoint(node)->distribution();
    117         if (Node* next = (direction == TraversalDirectionForward ? distribution->firstNode() : distribution->lastNode()))
     116        const InsertionPoint* insertionPoint = toInsertionPoint(node);
     117        if (Node* next = (direction == TraversalDirectionForward ? insertionPoint->first() : insertionPoint->last()))
    118118            return traverseNode(next, direction);
    119119        return traverseLightChildren(node, direction);
     
    142142    if (!shadow)
    143143        return traverseSiblingInCurrentTree(node, direction);
    144     ContentDistribution::Item* item = shadow->distributionItemFor(node);
    145     if (!item)
     144    InsertionPoint* insertionPoint = shadow->insertionPointFor(node);
     145    if (!insertionPoint)
    146146        return traverseSiblingInCurrentTree(node, direction);
    147     if (ContentDistribution::Item* nextItem = (direction == TraversalDirectionForward ? item->next() : item->previous()))
    148         return traverseNode(nextItem->node(), direction);
    149     return traverseSiblingOrBackToInsertionPoint(item->insertionPoint(), direction);
     147    if (Node* next = (direction == TraversalDirectionForward ? insertionPoint->nextTo(node) : insertionPoint->previousTo(node)))
     148        return traverseNode(next, direction);
     149    return traverseSiblingOrBackToInsertionPoint(insertionPoint, direction);
    150150}
    151151
  • trunk/Source/WebCore/dom/ElementShadow.cpp

    r116974 r117033  
    168168}
    169169
    170 ContentDistribution::Item* ElementShadow::distributionItemFor(const Node* node) const
    171 {
    172     return m_distributor.findFor(node);
    173 }
    174 
    175170void ElementShadow::reattach()
    176171{
  • trunk/Source/WebCore/dom/ElementShadow.h

    r116974 r117033  
    7171
    7272    InsertionPoint* insertionPointFor(const Node*) const;
    73     ContentDistribution::Item* distributionItemFor(const Node*) const;
    7473
    7574    ContentDistributor& distributor();
  • trunk/Source/WebCore/dom/NodeRenderingContext.cpp

    r116974 r117033  
    143143static inline RenderObject* nextRendererOfInsertionPoint(InsertionPoint* parent, Node* current)
    144144{
    145     ContentDistribution::Item* currentItem = parent->distribution()->find(current);
    146     if (!currentItem)
     145    size_t start = parent->indexOf(current);
     146    if (notFound == start)
    147147        return 0;
    148148
    149     for (ContentDistribution::Item* item = currentItem->next(); item; item = item->next()) {
    150         if (RenderObject* renderer = item->node()->renderer())
     149    for (size_t i = start + 1; i < parent->size(); ++i) {
     150        if (RenderObject* renderer = parent->at(i)->renderer())
    151151            return renderer;
    152152    }
     
    159159    RenderObject* lastRenderer = 0;
    160160
    161     for (ContentDistribution::Item* item = parent->distribution()->first(); item; item = item->next()) {
    162         if (item->node() == current)
     161    for (size_t i = 0; i < parent->size(); ++i) {
     162        if (parent->at(i) == current)
    163163            break;
    164         if (RenderObject* renderer = item->node()->renderer())
     164        if (RenderObject* renderer = parent->at(i)->renderer())
    165165            lastRenderer = renderer;
    166166    }
     
    171171static inline RenderObject* firstRendererOfInsertionPoint(InsertionPoint* parent)
    172172{
    173     if (parent->hasDistribution()) {
    174         for (ContentDistribution::Item* item = parent->distribution()->first(); item; item = item->next()) {
    175             if (RenderObject* renderer = item->node()->renderer())
    176                 return renderer;
    177         }
    178 
    179         return 0;
     173    size_t size = parent->size();
     174    for (size_t i = 0; i < size; ++i) {
     175        if (RenderObject* renderer = parent->at(i)->renderer())
     176            return renderer;
    180177    }
    181178
     
    185182static inline RenderObject* lastRendererOfInsertionPoint(InsertionPoint* parent)
    186183{
    187     if (parent->hasDistribution()) {
    188         for (ContentDistribution::Item* item = parent->distribution()->last(); item; item = item->previous()) {
    189             if (RenderObject* renderer = item->node()->renderer())
    190                 return renderer;
    191         }
    192 
    193         return 0;
     184    size_t size = parent->size();
     185    for (size_t i = 0; i < size; ++i) {
     186        if (RenderObject* renderer = parent->at(size - 1 - i)->renderer())
     187            return renderer;
    194188    }
    195189
  • trunk/Source/WebCore/html/shadow/ContentDistributor.cpp

    r116974 r117033  
    3535namespace WebCore {
    3636
    37 ContentDistribution::ContentDistribution()
    38 {
    39 }
    40 
    41 ContentDistribution::~ContentDistribution()
    42 {
    43     ASSERT(isEmpty());
    44 }
    45 
    46 ContentDistribution::Item* ContentDistribution::find(Node* node) const
    47 {
    48     for (ContentDistribution::Item* item = first(); item; item = item->next()) {
    49         if (node == item->node())
    50             return item;
    51     }
    52    
    53     return 0;
    54 }
    55 
    56 void ContentDistribution::clear()
    57 {
    58     if (isEmpty()) {
    59         ASSERT(!m_last);
    60         return;
    61     }
    62 
    63     RefPtr<ContentDistribution::Item> item = m_first;
    64     while (item) {
    65         ASSERT(!item->previous());
    66         RefPtr<ContentDistribution::Item> nextItem = item->m_next;
    67         item->m_next.clear();
    68         if (nextItem)
    69             nextItem->m_previous.clear();
    70         item = nextItem;
    71     }
    72 
    73     m_first.clear();
    74     m_last.clear();
    75 }
    76 
    77 void ContentDistribution::append(InsertionPoint* insertionPoint, Node* node)
    78 {
    79     RefPtr<Item> child = Item::create(insertionPoint, node);
    80 
    81     if (isEmpty()) {
    82         ASSERT(!m_last);
    83         m_first = m_last = child;
    84         return;
    85     }
    86 
    87     ASSERT(!m_last->next());
    88     ASSERT(!child->previous());
    89     m_last->m_next = child;
    90     child->m_previous = m_last;
    91     m_last = m_last->next();
    92 }
    93 
    9437ContentDistributor::ContentDistributor()
    9538    : m_phase(Prevented)
     
    11659            continue;
    11760
    118         distribution->append(insertionPoint, child);
    119         m_nodeToInsertionPoint.add(distribution->last());
     61        distribution->append(child);
     62        m_nodeToInsertionPoint.add(child, insertionPoint);
    12063        m_pool[i] = 0;
    12164    }
     
    12467void ContentDistributor::clearDistribution(ContentDistribution* list)
    12568{
    126     for (ContentDistribution::Item* item = list->first(); item; item = item->next())
    127         m_nodeToInsertionPoint.remove(item);
    128 
     69    for (size_t i = 0; i < list->size(); ++i)
     70        m_nodeToInsertionPoint.remove(list->at(i).get());
    12971    list->clear();
    130 }
    131 
    132 ContentDistribution::Item* ContentDistributor::findFor(const Node* key) const
    133 {
    134     InvertedTable::iterator found = m_nodeToInsertionPoint.find<const Node*, Translator>(key);
    135     return found != m_nodeToInsertionPoint.end() ? *found : 0;
    13672}
    13773
    13874InsertionPoint* ContentDistributor::findInsertionPointFor(const Node* key) const
    13975{
    140     InvertedTable::iterator found = m_nodeToInsertionPoint.find<const Node*, Translator>(key);
    141     return found != m_nodeToInsertionPoint.end() ? (*found)->insertionPoint() : 0;
     76    return m_nodeToInsertionPoint.get(key);
    14277}
    14378
  • trunk/Source/WebCore/html/shadow/ContentDistributor.h

    r116974 r117033  
    3333
    3434#include <wtf/Forward.h>
    35 #include <wtf/HashSet.h>
     35#include <wtf/HashMap.h>
    3636#include <wtf/RefCounted.h>
    3737#include <wtf/Vector.h>
     
    4444class ShadowRoot;
    4545
    46 class ContentDistribution {
    47 public:
    48     // TODO: The class should be reduced into simple Node* which is kept in a vector: https://bugs.webkit.org/show_bug.cgi?id=86350
    49     class Item : public RefCounted<Item> {
    50     public:
    51         friend class ContentDistribution;
    52 
    53         InsertionPoint* insertionPoint() const { return m_insertionPoint; }
    54         Node* node() const { return m_node.get(); }
    55         Item* next() const { return m_next.get(); }
    56         Item* previous() const { return m_previous.get(); }
    57 
    58     private:
    59         static PassRefPtr<Item> create(InsertionPoint* insertionPoint, Node* node) {  return adoptRef(new Item(insertionPoint, node)); }
    60 
    61         Item(InsertionPoint* insertionPoint, Node* node)
    62             : m_insertionPoint(insertionPoint)
    63             , m_node(node)
    64         { }
    65 
    66         InsertionPoint* m_insertionPoint;
    67         RefPtr<Node> m_node;
    68         RefPtr<Item> m_next;
    69         RefPtr<Item> m_previous;
    70     };
    71    
    72     ContentDistribution();
    73     ~ContentDistribution();
    74 
    75     Item* first() const { return m_first.get(); }
    76     Item* last() const { return m_last.get(); }
    77     Node* firstNode() const { return m_first ? m_first->node() : 0; }
    78     Node* lastNode() const { return m_first ? m_last->node() : 0; }
    79    
    80     Item* find(Node*) const;
    81     bool isEmpty() const { return !m_first; }
    82 
    83     void clear();
    84     void append(InsertionPoint*, Node*);
    85 
    86 private:
    87 
    88     RefPtr<Item> m_first;
    89     RefPtr<Item> m_last;
    90 };
     46typedef Vector<RefPtr<Node> > ContentDistribution;
    9147
    9248class ContentDistributor {
     
    9854    void distribute(InsertionPoint*, ContentDistribution*);
    9955    void clearDistribution(ContentDistribution*);
    100     ContentDistribution::Item* findFor(const Node* key) const;
    10156    InsertionPoint* findInsertionPointFor(const Node* key) const;
    10257
     
    10964
    11065private:
    111     struct Translator {
    112     public:
    113         static unsigned hash(const Node* key) { return PtrHash<const Node*>::hash(key); }
    114         static bool equal(const ContentDistribution::Item* item, const Node* node) { return item->node() == node; }
    115     };
    116 
    117     struct Hash {
    118         static unsigned hash(ContentDistribution::Item* key) { return PtrHash<const Node*>::hash(key->node()); }
    119         static bool equal(ContentDistribution::Item* a, ContentDistribution::Item* b) { return a->node() == b->node(); }
    120         static const bool safeToCompareToEmptyOrDeleted = false;
    121     };
    122 
    123     // Used as a table from Node to InseretionPoint
    124     typedef HashSet<ContentDistribution::Item*, Hash> InvertedTable;
    125 
    12666    enum DistributionPhase {
    12767        Prevented,
     
    13272    Vector<RefPtr<Node> > m_pool;
    13373    DistributionPhase m_phase;
    134     InvertedTable m_nodeToInsertionPoint;
     74    HashMap<const Node*, InsertionPoint*> m_nodeToInsertionPoint;
    13575};
    13676
  • trunk/Source/WebCore/html/shadow/InsertionPoint.cpp

    r116974 r117033  
    3939InsertionPoint::InsertionPoint(const QualifiedName& tagName, Document* document)
    4040    : HTMLElement(tagName, document)
    41     , m_distribution()
    4241{
    4342}
     
    139138inline void InsertionPoint::attachDistributedNode()
    140139{
    141     for (ContentDistribution::Item* item = m_distribution.first(); item; item = item->next())
    142         item->node()->attach();
     140    for (size_t i = 0; i < m_distribution.size(); ++i)
     141        m_distribution.at(i)->attach();
    143142}
    144143
     
    148147    m_distribution.clear();
    149148    for (Node* node = shadowRoot->firstChild(); node; node = node->nextSibling())
    150         m_distribution.append(this, node);
     149        m_distribution.append(node);
    151150}
    152151
     
    157156}
    158157
     158Node* InsertionPoint::nextTo(const Node* node) const
     159{
     160    size_t index = m_distribution.find(node);
     161    if (index == notFound || index + 1 == m_distribution.size())
     162        return 0;
     163    return m_distribution.at(index + 1).get();
     164}
     165
     166Node* InsertionPoint::previousTo(const Node* node) const
     167{
     168    size_t index = m_distribution.find(node);
     169    if (index == notFound || !index)
     170        return 0;
     171    return m_distribution.at(index - 1).get();
     172}
     173
     174
    159175} // namespace WebCore
  • trunk/Source/WebCore/html/shadow/InsertionPoint.h

    r116974 r117033  
    4343    virtual ~InsertionPoint();
    4444
    45     const ContentDistribution* distribution() const { return &m_distribution; }
    46     bool hasDistribution() const { return m_distribution.first(); }
     45    bool hasDistribution() const { return !m_distribution.isEmpty(); }
    4746    bool isShadowBoundary() const;
    4847    bool isActive() const;
     
    5756    virtual bool isInsertionPoint() const OVERRIDE { return true; }
    5857    ShadowRoot* assignedFrom() const;
     58
     59    size_t indexOf(Node* node) const { return m_distribution.find(node); }
     60    size_t size() const { return m_distribution.size(); }
     61    Node* at(size_t index)  const { return m_distribution.at(index).get(); }
     62    Node* first() const { return m_distribution.isEmpty() ? 0 : m_distribution.first().get(); }
     63    Node* last() const { return m_distribution.isEmpty() ? 0 : m_distribution.last().get(); }
     64    Node* nextTo(const Node*) const;
     65    Node* previousTo(const Node*) const;
    5966
    6067protected:
Note: See TracChangeset for help on using the changeset viewer.