Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: Source/core/dom/AttributeCollection.h

Issue 354023008: Move attributes-related methods from ElementData to AttributeCollection (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Take feedback into consideration Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/core.gypi ('k') | Source/core/dom/AttributeCollection.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifndef AttributeCollection_h
34 #define AttributeCollection_h
35
36 #include "core/dom/Attribute.h"
37
38 namespace WebCore {
39
40 class Attr;
41
42 class AttributeCollection {
43 public:
44 typedef const Attribute* const_iterator;
45
46 AttributeCollection(const Attribute* array, unsigned size)
47 : m_array(array)
48 , m_size(size)
49 { }
50
51 const Attribute& operator[](unsigned index) const { return at(index); }
52 const Attribute& at(unsigned index) const
53 {
54 RELEASE_ASSERT(index < m_size);
55 return m_array[index];
56 }
57
58 const Attribute* find(const QualifiedName&) const;
59 const Attribute* find(const AtomicString& name, bool shouldIgnoreCase) const ;
60 size_t findIndex(const QualifiedName&, bool shouldIgnoreCase = false) const;
61 size_t findIndex(const AtomicString& name, bool shouldIgnoreCase) const;
62 size_t findIndex(Attr*) const;
63
64 const_iterator begin() const { return m_array; }
65 const_iterator end() const { return m_array + m_size; }
66
67 unsigned size() const { return m_size; }
68 bool isEmpty() const { return !m_size; }
69
70 private:
71 size_t findSlowCase(const AtomicString& name, bool shouldIgnoreAttributeCase ) const;
72
73 const Attribute* m_array;
74 unsigned m_size;
75 };
76
77 inline const Attribute* AttributeCollection::find(const AtomicString& name, bool shouldIgnoreCase) const
78 {
79 size_t index = findIndex(name, shouldIgnoreCase);
80 return index != kNotFound ? &at(index) : 0;
81 }
82
83 inline size_t AttributeCollection::findIndex(const QualifiedName& name, bool sho uldIgnoreCase) const
84 {
85 const_iterator end = this->end();
86 unsigned index = 0;
87 for (const_iterator it = begin(); it != end; ++it, ++index) {
88 if (it->name().matchesPossiblyIgnoringCase(name, shouldIgnoreCase))
89 return index;
90 }
91 return kNotFound;
92 }
93
94 // We use a boolean parameter instead of calling shouldIgnoreAttributeCase so th at the caller
95 // can tune the behavior (hasAttribute is case sensitive whereas getAttribute is not).
96 inline size_t AttributeCollection::findIndex(const AtomicString& name, bool shou ldIgnoreCase) const
97 {
98 bool doSlowCheck = shouldIgnoreCase;
99
100 // Optimize for the case where the attribute exists and its name exactly mat ches.
101 const_iterator end = this->end();
102 unsigned index = 0;
103 for (const_iterator it = begin(); it != end; ++it, ++index) {
104 // FIXME: Why check the prefix? Namespaces should be all that matter.
105 // Most attributes (all of HTML and CSS) have no namespace.
106 if (!it->name().hasPrefix()) {
107 if (name == it->localName())
108 return index;
109 } else {
110 doSlowCheck = true;
111 }
112 }
113
114 if (doSlowCheck)
115 return findSlowCase(name, shouldIgnoreCase);
116 return kNotFound;
117 }
118
119 inline const Attribute* AttributeCollection::find(const QualifiedName& name) con st
120 {
121 const_iterator end = this->end();
122 for (const_iterator it = begin(); it != end; ++it) {
123 if (it->name().matches(name))
124 return it;
125 }
126 return 0;
127 }
128
129 } // namespace WebCore
130
131 #endif // AttributeCollection_h
OLDNEW
« no previous file with comments | « Source/core/core.gypi ('k') | Source/core/dom/AttributeCollection.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698