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 "HTMLDocument.h"
28#include "HTMLNames.h"
29#include "HTMLObjectElement.h"
30#include "NodeRareData.h"
31#include "NodeTraversal.h"
32
33namespace WebCore {
34
35using namespace HTMLNames;
36
37HTMLNameCollection::HTMLNameCollection(Node* document, CollectionType type, const AtomicString& name)
38    : HTMLCollection(document, type, DoesNotOverrideItemAfter)
39    , m_name(name)
40{
41}
42
43HTMLNameCollection::~HTMLNameCollection()
44{
45    ASSERT(ownerNode());
46    ASSERT(ownerNode()->isDocumentNode());
47    ASSERT(type() == WindowNamedItems || type() == DocumentNamedItems);
48
49    ownerNode()->nodeLists()->removeCacheWithAtomicName(this, type(), m_name);
50}
51
52bool WindowNameCollection::nodeMatchesIfNameAttributeMatch(Element* element)
53{
54    return element->hasTagName(imgTag) || element->hasTagName(formTag) || element->hasTagName(appletTag)
55        || element->hasTagName(embedTag) || element->hasTagName(objectTag);
56}
57
58bool WindowNameCollection::nodeMatches(Element* element, const AtomicString& name)
59{
60    // Find only images, forms, applets, embeds and objects by name, but anything by id
61    if (nodeMatchesIfNameAttributeMatch(element) && element->getNameAttribute() == name)
62        return true;
63    return element->getIdAttribute() == name;
64}
65
66bool DocumentNameCollection::nodeMatchesIfIdAttributeMatch(Element* element)
67{
68    // FIXME: we need to fix HTMLImageElement to update the hash map for us when name attribute has been removed.
69    return element->hasTagName(appletTag) || (element->hasTagName(objectTag) && toHTMLObjectElement(element)->isDocNamedItem())
70        || (element->hasTagName(imgTag) && element->hasName());
71}
72
73bool DocumentNameCollection::nodeMatchesIfNameAttributeMatch(Element* element)
74{
75    return element->hasTagName(formTag) || element->hasTagName(embedTag) || element->hasTagName(iframeTag)
76        || element->hasTagName(appletTag) || (element->hasTagName(objectTag) && toHTMLObjectElement(element)->isDocNamedItem())
77        || element->hasTagName(imgTag);
78}
79
80bool DocumentNameCollection::nodeMatches(Element* element, const AtomicString& name)
81{
82    // Find images, forms, applets, embeds, objects and iframes by name, applets and object by id, and images by id
83    // but only if they have a name attribute (this very strange rule matches IE)
84    if (element->hasTagName(formTag) || element->hasTagName(embedTag) || element->hasTagName(iframeTag))
85        return element->getNameAttribute() == name;
86    if (element->hasTagName(appletTag))
87        return element->getNameAttribute() == name || element->getIdAttribute() == name;
88    if (element->hasTagName(objectTag))
89        return (element->getNameAttribute() == name || element->getIdAttribute() == name) && toHTMLObjectElement(element)->isDocNamedItem();
90    if (element->hasTagName(imgTag))
91        return element->getNameAttribute() == name || (element->getIdAttribute() == name && element->hasName());
92    return false;
93}
94
95}
96