1/*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "GenericCachedHTMLCollection.h"
28
29#include "HTMLAppletElement.h"
30#include "HTMLFieldSetElement.h"
31#include "HTMLNames.h"
32#include "HTMLObjectElement.h"
33#include "HTMLOptionElement.h"
34#include <wtf/IsoMallocInlines.h>
35
36namespace WebCore {
37
38using namespace HTMLNames;
39
40using GenericDescendantsCachedHTMLCollection = GenericCachedHTMLCollection<CollectionTraversalType::Descendants>;
41using GenericChildrenOnlyCachedHTMLCollection = GenericCachedHTMLCollection<CollectionTraversalType::ChildrenOnly>;
42
43WTF_MAKE_ISO_ALLOCATED_IMPL_TEMPLATE(GenericDescendantsCachedHTMLCollection);
44WTF_MAKE_ISO_ALLOCATED_IMPL_TEMPLATE(GenericChildrenOnlyCachedHTMLCollection);
45
46template <CollectionTraversalType traversalType>
47bool GenericCachedHTMLCollection<traversalType>::elementMatches(Element& element) const
48{
49 switch (this->type()) {
50 case NodeChildren:
51 return true;
52 case DocImages:
53 return element.hasTagName(imgTag);
54 case DocScripts:
55 return element.hasTagName(scriptTag);
56 case DocForms:
57 return element.hasTagName(formTag);
58 case TableTBodies:
59 return element.hasTagName(tbodyTag);
60 case TRCells:
61 return element.hasTagName(tdTag) || element.hasTagName(thTag);
62 case TSectionRows:
63 return element.hasTagName(trTag);
64 case SelectedOptions:
65 return is<HTMLOptionElement>(element) && downcast<HTMLOptionElement>(element).selected();
66 case DataListOptions:
67 if (is<HTMLOptionElement>(element)) {
68 HTMLOptionElement& option = downcast<HTMLOptionElement>(element);
69 if (!option.isDisabledFormControl() && !option.value().isEmpty())
70 return true;
71 }
72 return false;
73 case MapAreas:
74 return element.hasTagName(areaTag);
75 case DocApplets:
76 return is<HTMLAppletElement>(element) || (is<HTMLObjectElement>(element) && downcast<HTMLObjectElement>(element).containsJavaApplet());
77 case DocEmbeds:
78 return element.hasTagName(embedTag);
79 case DocLinks:
80 return (element.hasTagName(aTag) || element.hasTagName(areaTag)) && element.hasAttributeWithoutSynchronization(hrefAttr);
81 case DocAnchors:
82 return element.hasTagName(aTag) && element.hasAttributeWithoutSynchronization(nameAttr);
83 case FieldSetElements:
84 return is<HTMLObjectElement>(element) || is<HTMLFormControlElement>(element);
85 case ByClass:
86 case ByTag:
87 case ByHTMLTag:
88 case AllDescendants:
89 case DocAll:
90 case DocumentAllNamedItems:
91 case DocumentNamedItems:
92 case FormControls:
93 case SelectOptions:
94 case TableRows:
95 case WindowNamedItems:
96 break;
97 }
98 // Remaining collection types have their own CachedHTMLCollection subclasses and are not using GenericCachedHTMLCollection.
99 ASSERT_NOT_REACHED();
100 return false;
101}
102template bool GenericCachedHTMLCollection<CollectionTraversalType::Descendants>::elementMatches(Element&) const;
103template bool GenericCachedHTMLCollection<CollectionTraversalType::ChildrenOnly>::elementMatches(Element&) const;
104
105} // namespace WebCore
106