1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2011, 2012 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "HTMLNameCollection.h"
25
26#include "Element.h"
27#include "HTMLAppletElement.h"
28#include "HTMLEmbedElement.h"
29#include "HTMLFormElement.h"
30#include "HTMLIFrameElement.h"
31#include "HTMLImageElement.h"
32#include "HTMLNames.h"
33#include "HTMLObjectElement.h"
34#include "NodeRareData.h"
35#include "NodeTraversal.h"
36#include <wtf/IsoMallocInlines.h>
37
38namespace WebCore {
39
40using namespace HTMLNames;
41
42WTF_MAKE_ISO_ALLOCATED_IMPL(WindowNameCollection);
43WTF_MAKE_ISO_ALLOCATED_IMPL(DocumentNameCollection);
44
45bool WindowNameCollection::elementMatchesIfNameAttributeMatch(const Element& element)
46{
47 return is<HTMLAppletElement>(element)
48 || is<HTMLEmbedElement>(element)
49 || is<HTMLFormElement>(element)
50 || is<HTMLImageElement>(element)
51 || is<HTMLObjectElement>(element);
52}
53
54bool WindowNameCollection::elementMatches(const Element& element, const AtomicStringImpl* name)
55{
56 // Find only images, forms, applets, embeds and objects by name, but anything by id.
57 return (elementMatchesIfNameAttributeMatch(element) && element.getNameAttribute().impl() == name)
58 || element.getIdAttribute() == name;
59}
60
61static inline bool isObjectElementForDocumentNameCollection(const Element& element)
62{
63 return is<HTMLObjectElement>(element) && downcast<HTMLObjectElement>(element).isExposed();
64}
65
66bool DocumentNameCollection::elementMatchesIfIdAttributeMatch(const Element& element)
67{
68 // FIXME: We need to fix HTMLImageElement to update the hash map for us when the name attribute is removed.
69 return isObjectElementForDocumentNameCollection(element)
70 || is<HTMLAppletElement>(element)
71 || (is<HTMLImageElement>(element) && element.hasName());
72}
73
74bool DocumentNameCollection::elementMatchesIfNameAttributeMatch(const Element& element)
75{
76 return isObjectElementForDocumentNameCollection(element)
77 || is<HTMLAppletElement>(element)
78 || is<HTMLEmbedElement>(element)
79 || is<HTMLFormElement>(element)
80 || is<HTMLIFrameElement>(element)
81 || is<HTMLImageElement>(element);
82}
83
84bool DocumentNameCollection::elementMatches(const Element& element, const AtomicStringImpl* name)
85{
86 // Find images, forms, applets, embeds, objects and iframes by name, applets and object by id, and images by id
87 // but only if they have a name attribute (this very strange rule matches IE).
88 return (elementMatchesIfNameAttributeMatch(element) && element.getNameAttribute().impl() == name)
89 || (elementMatchesIfIdAttributeMatch(element) && element.getIdAttribute().impl() == name);
90}
91
92}
93