File: HTMLCollection.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (268 lines) | stat: -rw-r--r-- 9,361 bytes parent folder | download | duplicates (7)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 * Copyright (C) 2003-2017 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 "HTMLCollection.h"

#include "CachedHTMLCollectionInlines.h"
#include "HTMLNames.h"
#include "NodeRareDataInlines.h"
#include <wtf/TZoneMallocInlines.h>

namespace WebCore {

using namespace HTMLNames;

WTF_MAKE_TZONE_ALLOCATED_IMPL(CollectionNamedElementCache);
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(HTMLCollection);

inline auto HTMLCollection::rootTypeFromCollectionType(CollectionType type) -> RootType
{
    switch (type) {
    case CollectionType::DocImages:
    case CollectionType::DocEmpty:
    case CollectionType::DocEmbeds:
    case CollectionType::DocForms:
    case CollectionType::DocLinks:
    case CollectionType::DocAnchors:
    case CollectionType::DocScripts:
    case CollectionType::DocAll:
    case CollectionType::WindowNamedItems:
    case CollectionType::DocumentNamedItems:
    case CollectionType::DocumentAllNamedItems:
    case CollectionType::FormControls:
        return HTMLCollection::RootType::AtTreeScope;
    case CollectionType::AllDescendants:
    case CollectionType::ByClass:
    case CollectionType::ByTag:
    case CollectionType::ByHTMLTag:
    case CollectionType::FieldSetElements:
    case CollectionType::NodeChildren:
    case CollectionType::TableTBodies:
    case CollectionType::TSectionRows:
    case CollectionType::TableRows:
    case CollectionType::TRCells:
    case CollectionType::SelectOptions:
    case CollectionType::SelectedOptions:
    case CollectionType::DataListOptions:
    case CollectionType::MapAreas:
        return HTMLCollection::RootType::AtNode;
    }
    ASSERT_NOT_REACHED();
    return HTMLCollection::RootType::AtNode;
}

static NodeListInvalidationType invalidationTypeExcludingIdAndNameAttributes(CollectionType type)
{
    switch (type) {
    case CollectionType::ByTag:
    case CollectionType::ByHTMLTag:
    case CollectionType::AllDescendants:
    case CollectionType::DocImages:
    case CollectionType::DocEmbeds:
    case CollectionType::DocForms:
    case CollectionType::DocScripts:
    case CollectionType::DocAll:
    case CollectionType::NodeChildren:
    case CollectionType::TableTBodies:
    case CollectionType::TSectionRows:
    case CollectionType::TableRows:
    case CollectionType::TRCells:
    case CollectionType::SelectOptions:
    case CollectionType::MapAreas:
    case CollectionType::DocEmpty:
        return NodeListInvalidationType::DoNotInvalidateOnAttributeChanges;
    case CollectionType::SelectedOptions:
    case CollectionType::DataListOptions:
        // FIXME: We can do better some day.
        return NodeListInvalidationType::InvalidateOnAnyAttrChange;
    case CollectionType::ByClass:
        return NodeListInvalidationType::InvalidateOnClassAttrChange;
    case CollectionType::DocAnchors:
        return NodeListInvalidationType::InvalidateOnNameAttrChange;
    case CollectionType::DocLinks:
        return NodeListInvalidationType::InvalidateOnHRefAttrChange;
    case CollectionType::WindowNamedItems:
    case CollectionType::DocumentNamedItems:
    case CollectionType::DocumentAllNamedItems:
        return NodeListInvalidationType::InvalidateOnIdNameAttrChange;
    case CollectionType::FieldSetElements:
    case CollectionType::FormControls:
        return NodeListInvalidationType::InvalidateForFormControls;
    }
    ASSERT_NOT_REACHED();
    return NodeListInvalidationType::DoNotInvalidateOnAttributeChanges;
}

HTMLCollection::HTMLCollection(ContainerNode& ownerNode, CollectionType type)
    : m_collectionType(static_cast<unsigned>(type))
    , m_invalidationType(static_cast<unsigned>(invalidationTypeExcludingIdAndNameAttributes(type)))
    , m_rootType(static_cast<unsigned>(rootTypeFromCollectionType(type)))
    , m_ownerNode(ownerNode)
{
    ASSERT(m_rootType == static_cast<unsigned>(rootTypeFromCollectionType(type)));
    ASSERT(m_invalidationType == static_cast<unsigned>(invalidationTypeExcludingIdAndNameAttributes(type)));
    ASSERT(m_collectionType == static_cast<unsigned>(type));
}

HTMLCollection::~HTMLCollection()
{
    if (hasNamedElementCache())
        document().collectionWillClearIdNameMap(*this);

    // HTMLNameCollection & ClassCollection remove cache by themselves.
    // FIXME: We need a cleaner way to handle this.
    switch (type()) {
    case CollectionType::ByClass:
    case CollectionType::ByTag:
    case CollectionType::ByHTMLTag:
    case CollectionType::WindowNamedItems:
    case CollectionType::DocumentNamedItems:
    case CollectionType::DocumentAllNamedItems:
        break;
    default:
        ownerNode().nodeLists()->removeCachedCollection(this);
    }
}

void HTMLCollection::invalidateCacheForDocument(Document& document)
{
    if (hasNamedElementCache())
        invalidateNamedElementCache(document);
}

void HTMLCollection::invalidateNamedElementCache(Document& document) const
{
    ASSERT(hasNamedElementCache());
    document.collectionWillClearIdNameMap(*this);
    {
        Locker locker { m_namedElementCacheAssignmentLock };
        m_namedElementCache = nullptr;
    }
}

Element* HTMLCollection::namedItemSlow(const AtomString& name) const
{
    // The pathological case. We need to walk the entire subtree.
    updateNamedElementCache();
    ASSERT(m_namedElementCache);

    if (auto* idResults = m_namedElementCache->findElementsWithId(name)) {
        if (idResults->size())
            return idResults->at(0).ptr();
    }

    if (auto* nameResults = m_namedElementCache->findElementsWithName(name)) {
        if (nameResults->size())
            return nameResults->at(0).ptr();
    }

    return nullptr;
}

// Documented in https://dom.spec.whatwg.org/#interface-htmlcollection.
const Vector<AtomString>& HTMLCollection::supportedPropertyNames()
{
    updateNamedElementCache();
    ASSERT(m_namedElementCache);

    return m_namedElementCache->propertyNames();
}

bool HTMLCollection::isSupportedPropertyName(const AtomString& name)
{
    updateNamedElementCache();
    ASSERT(m_namedElementCache);

    if (m_namedElementCache->findElementsWithId(name))
        return true;
    if (m_namedElementCache->findElementsWithName(name))
        return true;

    return false;
}

void HTMLCollection::updateNamedElementCache() const
{
    if (hasNamedElementCache())
        return;

    auto cache = makeUnique<CollectionNamedElementCache>();

    unsigned size = length();
    for (unsigned i = 0; i < size; ++i) {
        Element& element = *item(i);
        const AtomString& id = element.getIdAttribute();
        if (!id.isEmpty())
            cache->appendToIdCache(id, element);
        auto* htmlElement = dynamicDowncast<HTMLElement>(element);
        if (!htmlElement)
            continue;
        const AtomString& name = element.getNameAttribute();
        if (!name.isEmpty() && id != name && (type() != CollectionType::DocAll || nameShouldBeVisibleInDocumentAll(*htmlElement)))
            cache->appendToNameCache(name, element);
    }

    setNamedItemCache(WTFMove(cache));
}

Vector<Ref<Element>> HTMLCollection::namedItems(const AtomString& name) const
{
    // FIXME: This non-virtual function can't possibly be doing the correct thing for
    // any derived class that overrides the virtual namedItem function.

    Vector<Ref<Element>> elements;

    if (name.isEmpty())
        return elements;

    updateNamedElementCache();
    ASSERT(m_namedElementCache);

    auto* elementsWithId = m_namedElementCache->findElementsWithId(name);
    auto* elementsWithName = m_namedElementCache->findElementsWithName(name);

    elements.reserveInitialCapacity((elementsWithId ? elementsWithId->size() : 0) + (elementsWithName ? elementsWithName->size() : 0));

    if (elementsWithId) {
        elements.appendContainerWithMapping(*elementsWithId, [](auto& element) {
            return Ref { element.get() };
        });
    }
    if (elementsWithName) {
        elements.appendContainerWithMapping(*elementsWithName, [](auto& element) {
            return Ref { element.get() };
        });
    }

    return elements;
}

size_t HTMLCollection::memoryCost() const
{
    // memoryCost() may be invoked concurrently from a GC thread, and we need to be careful about what data we access here and how.
    // Hence, we need to guard m_namedElementCache from being replaced while accessing it.
    Locker locker { m_namedElementCacheAssignmentLock };
    return m_namedElementCache ? m_namedElementCache->memoryCost() : 0;
}

} // namespace WebCore