OSDN Git Service

am 3c54ece0: am 5dc34a85: activeDocumentLoader() causes crash in WebCoreFrameBridge.cpp
[android-x86/external-webkit.git] / WebCore / css / CSSSelectorList.cpp
1 /*
2  * Copyright (C) 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2009 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #include "config.h"
28 #include "CSSSelectorList.h"
29
30 namespace WebCore {
31
32 CSSSelectorList::~CSSSelectorList() 
33 {
34     deleteSelectors();
35 }
36     
37 void CSSSelectorList::adopt(CSSSelectorList& list)
38 {
39     deleteSelectors();
40     m_selectorArray = list.m_selectorArray;
41     list.m_selectorArray = 0;
42 }
43
44 void CSSSelectorList::adoptSelectorVector(Vector<CSSSelector*>& selectorVector)
45 {
46     deleteSelectors();
47     const size_t size = selectorVector.size();
48     ASSERT(size);
49     if (size == 1) {
50         m_selectorArray = selectorVector[0];
51         m_selectorArray->setLastInSelectorList();
52         selectorVector.shrink(0);
53         return;
54     }
55     m_selectorArray = reinterpret_cast<CSSSelector*>(fastMalloc(sizeof(CSSSelector) * selectorVector.size()));
56     for (size_t i = 0; i < size; ++i) {
57         memcpy(&m_selectorArray[i], selectorVector[i], sizeof(CSSSelector));
58         // We want to free the memory (which was allocated with fastNew), but we
59         // don't want the destructor to run since it will affect the copy we've just made.
60         fastDeleteSkippingDestructor(selectorVector[i]);
61         ASSERT(!m_selectorArray[i].isLastInSelectorList());
62     }
63     m_selectorArray[size - 1].setLastInSelectorList();
64     selectorVector.shrink(0);
65 }
66
67 void CSSSelectorList::deleteSelectors()
68 {
69     if (!m_selectorArray)
70         return;
71
72     // We had two cases in adoptSelectVector. The fast case of a 1 element
73     // vector took the CSSSelector directly, which was allocated with new.
74     // The second case we allocated a new fastMalloc buffer, which should be
75     // freed with fastFree, and the destructors called manually.
76     CSSSelector* s = m_selectorArray;
77     bool done = s->isLastInSelectorList();
78     if (done)
79         delete s;
80     else {
81         while (1) {
82             s->~CSSSelector();
83             if (done)
84                 break;
85             ++s;
86             done = s->isLastInSelectorList();
87         }
88         fastFree(m_selectorArray);
89     }
90 }
91
92
93 template <typename Functor>
94 static bool forEachTagSelector(Functor& functor, CSSSelector* selector)
95 {
96     ASSERT(selector);
97
98     do {
99         if (functor(selector))
100             return true;
101         if (CSSSelector* simpleSelector = selector->simpleSelector()) {
102             if (forEachTagSelector(functor, simpleSelector))
103                 return true;
104         }
105     } while ((selector = selector->tagHistory()));
106
107     return false;
108 }
109
110 template <typename Functor>
111 static bool forEachSelector(Functor& functor, const CSSSelectorList* selectorList)
112 {
113     for (CSSSelector* selector = selectorList->first(); selector; selector = CSSSelectorList::next(selector)) {
114         if (forEachTagSelector(functor, selector))
115             return true;
116     }
117
118     return false;
119 }
120
121 class SelectorNeedsNamespaceResolutionFunctor {
122 public:
123     bool operator()(CSSSelector* selector)
124     {
125         if (selector->hasTag() && selector->m_tag.prefix() != nullAtom && selector->m_tag.prefix() != starAtom)
126             return true;
127         if (selector->hasAttribute() && selector->attribute().prefix() != nullAtom && selector->attribute().prefix() != starAtom)
128             return true;
129         return false;
130     }
131 };
132
133 bool CSSSelectorList::selectorsNeedNamespaceResolution()
134 {
135     SelectorNeedsNamespaceResolutionFunctor functor;
136     return forEachSelector(functor, this);
137 }
138
139 } // namespace WebCore