2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "CSSSelectorList.h"
32 CSSSelectorList::~CSSSelectorList()
37 void CSSSelectorList::adopt(CSSSelectorList& list)
40 m_selectorArray = list.m_selectorArray;
41 list.m_selectorArray = 0;
44 void CSSSelectorList::adoptSelectorVector(Vector<CSSSelector*>& selectorVector)
47 const size_t size = selectorVector.size();
50 m_selectorArray = selectorVector[0];
51 m_selectorArray->setLastInSelectorList();
52 selectorVector.shrink(0);
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());
63 m_selectorArray[size - 1].setLastInSelectorList();
64 selectorVector.shrink(0);
67 void CSSSelectorList::deleteSelectors()
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();
86 done = s->isLastInSelectorList();
88 fastFree(m_selectorArray);
93 template <typename Functor>
94 static bool forEachTagSelector(Functor& functor, CSSSelector* selector)
99 if (functor(selector))
101 if (CSSSelector* simpleSelector = selector->simpleSelector()) {
102 if (forEachTagSelector(functor, simpleSelector))
105 } while ((selector = selector->tagHistory()));
110 template <typename Functor>
111 static bool forEachSelector(Functor& functor, const CSSSelectorList* selectorList)
113 for (CSSSelector* selector = selectorList->first(); selector; selector = CSSSelectorList::next(selector)) {
114 if (forEachTagSelector(functor, selector))
121 class SelectorNeedsNamespaceResolutionFunctor {
123 bool operator()(CSSSelector* selector)
125 if (selector->hasTag() && selector->m_tag.prefix() != nullAtom && selector->m_tag.prefix() != starAtom)
127 if (selector->hasAttribute() && selector->attribute().prefix() != nullAtom && selector->attribute().prefix() != starAtom)
133 bool CSSSelectorList::selectorsNeedNamespaceResolution()
135 SelectorNeedsNamespaceResolutionFunctor functor;
136 return forEachSelector(functor, this);
139 } // namespace WebCore