1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2010, 2011, 2012 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "HTMLFormControlsCollection.h"
#include "HTMLFieldSetElement.h"
#include "HTMLFormElement.h"
#include "HTMLImageElement.h"
#include "HTMLNames.h"
namespace WebCore {
using namespace HTMLNames;
// Since the collections are to be "live", we have to do the
// calculation every time if anything has changed.
HTMLFormControlsCollection::HTMLFormControlsCollection(ContainerNode& ownerNode)
: CachedHTMLCollection<HTMLFormControlsCollection, CollectionTypeTraits<FormControls>::traversalType>(ownerNode, FormControls)
, m_cachedElement(nullptr)
, m_cachedElementOffsetInArray(0)
{
ASSERT(is<HTMLFormElement>(ownerNode) || is<HTMLFieldSetElement>(ownerNode));
}
Ref<HTMLFormControlsCollection> HTMLFormControlsCollection::create(ContainerNode& ownerNode, CollectionType)
{
return adoptRef(*new HTMLFormControlsCollection(ownerNode));
}
HTMLFormControlsCollection::~HTMLFormControlsCollection()
{
}
const Vector<FormAssociatedElement*>& HTMLFormControlsCollection::formControlElements() const
{
ASSERT(is<HTMLFormElement>(ownerNode()) || is<HTMLFieldSetElement>(ownerNode()));
if (is<HTMLFormElement>(ownerNode()))
return downcast<HTMLFormElement>(ownerNode()).associatedElements();
return downcast<HTMLFieldSetElement>(ownerNode()).associatedElements();
}
const Vector<HTMLImageElement*>& HTMLFormControlsCollection::formImageElements() const
{
ASSERT(is<HTMLFormElement>(ownerNode()));
return downcast<HTMLFormElement>(ownerNode()).imageElements();
}
static unsigned findFormAssociatedElement(const Vector<FormAssociatedElement*>& elements, const Element& element)
{
for (unsigned i = 0; i < elements.size(); ++i) {
auto& associatedElement = *elements[i];
if (associatedElement.isEnumeratable() && &associatedElement.asHTMLElement() == &element)
return i;
}
return elements.size();
}
HTMLElement* HTMLFormControlsCollection::customElementAfter(Element* current) const
{
const Vector<FormAssociatedElement*>& elements = formControlElements();
unsigned start;
if (!current)
start = 0;
else if (m_cachedElement == current)
start = m_cachedElementOffsetInArray + 1;
else
start = findFormAssociatedElement(elements, *current) + 1;
for (unsigned i = start; i < elements.size(); ++i) {
FormAssociatedElement& element = *elements[i];
if (element.isEnumeratable()) {
m_cachedElement = &element.asHTMLElement();
m_cachedElementOffsetInArray = i;
return &element.asHTMLElement();
}
}
return nullptr;
}
void HTMLFormControlsCollection::updateNamedElementCache() const
{
if (hasNamedElementCache())
return;
auto cache = std::make_unique<CollectionNamedElementCache>();
bool ownerIsFormElement = is<HTMLFormElement>(ownerNode());
HashSet<AtomicStringImpl*> foundInputElements;
for (auto& elementPtr : formControlElements()) {
FormAssociatedElement& associatedElement = *elementPtr;
if (associatedElement.isEnumeratable()) {
HTMLElement& element = associatedElement.asHTMLElement();
const AtomicString& id = element.getIdAttribute();
if (!id.isEmpty()) {
cache->appendToIdCache(id, element);
if (ownerIsFormElement)
foundInputElements.add(id.impl());
}
const AtomicString& name = element.getNameAttribute();
if (!name.isEmpty() && id != name) {
cache->appendToNameCache(name, element);
if (ownerIsFormElement)
foundInputElements.add(name.impl());
}
}
}
if (ownerIsFormElement) {
for (auto* elementPtr : formImageElements()) {
HTMLImageElement& element = *elementPtr;
const AtomicString& id = element.getIdAttribute();
if (!id.isEmpty() && !foundInputElements.contains(id.impl()))
cache->appendToIdCache(id, element);
const AtomicString& name = element.getNameAttribute();
if (!name.isEmpty() && id != name && !foundInputElements.contains(name.impl()))
cache->appendToNameCache(name, element);
}
}
setNamedItemCache(WTFMove(cache));
}
void HTMLFormControlsCollection::invalidateCache(Document& document)
{
CachedHTMLCollection<HTMLFormControlsCollection, CollectionTypeTraits<FormControls>::traversalType>::invalidateCache(document);
m_cachedElement = nullptr;
m_cachedElementOffsetInArray = 0;
}
}
|