Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
1 /*
2 * Copyright (c) 2007 Henri Sivonen
3 * Copyright (c) 2008-2011 Mozilla Foundation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
24 /*
25 * THIS IS A GENERATED FILE. PLEASE DO NOT EDIT.
26 * Please edit HtmlAttributes.java instead and regenerate.
27 */
29 #define nsHtml5HtmlAttributes_cpp__
31 #include "nsIAtom.h"
32 #include "nsHtml5AtomTable.h"
33 #include "nsString.h"
34 #include "nsNameSpaceManager.h"
35 #include "nsIContent.h"
36 #include "nsTraceRefcnt.h"
37 #include "jArray.h"
38 #include "nsHtml5ArrayCopy.h"
39 #include "nsAHtml5TreeBuilderState.h"
40 #include "nsHtml5Atoms.h"
41 #include "nsHtml5ByteReadable.h"
42 #include "nsIUnicodeDecoder.h"
43 #include "nsHtml5Macros.h"
44 #include "nsIContentHandle.h"
46 #include "nsHtml5Tokenizer.h"
47 #include "nsHtml5TreeBuilder.h"
48 #include "nsHtml5MetaScanner.h"
49 #include "nsHtml5AttributeName.h"
50 #include "nsHtml5ElementName.h"
51 #include "nsHtml5StackNode.h"
52 #include "nsHtml5UTF16Buffer.h"
53 #include "nsHtml5StateSnapshot.h"
54 #include "nsHtml5Portability.h"
56 #include "nsHtml5HtmlAttributes.h"
58 nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES = nullptr;
60 nsHtml5HtmlAttributes::nsHtml5HtmlAttributes(int32_t mode)
61 : mode(mode),
62 length(0),
63 names(jArray<nsHtml5AttributeName*,int32_t>::newJArray(5)),
64 values(jArray<nsString*,int32_t>::newJArray(5))
65 {
66 MOZ_COUNT_CTOR(nsHtml5HtmlAttributes);
67 }
70 nsHtml5HtmlAttributes::~nsHtml5HtmlAttributes()
71 {
72 MOZ_COUNT_DTOR(nsHtml5HtmlAttributes);
73 clear(0);
74 }
76 int32_t
77 nsHtml5HtmlAttributes::getIndex(nsHtml5AttributeName* name)
78 {
79 for (int32_t i = 0; i < length; i++) {
80 if (names[i] == name) {
81 return i;
82 }
83 }
84 return -1;
85 }
87 nsString*
88 nsHtml5HtmlAttributes::getValue(nsHtml5AttributeName* name)
89 {
90 int32_t index = getIndex(name);
91 if (index == -1) {
92 return nullptr;
93 } else {
94 return getValueNoBoundsCheck(index);
95 }
96 }
98 int32_t
99 nsHtml5HtmlAttributes::getLength()
100 {
101 return length;
102 }
104 nsIAtom*
105 nsHtml5HtmlAttributes::getLocalNameNoBoundsCheck(int32_t index)
106 {
107 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
108 return names[index]->getLocal(mode);
109 }
111 int32_t
112 nsHtml5HtmlAttributes::getURINoBoundsCheck(int32_t index)
113 {
114 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
115 return names[index]->getUri(mode);
116 }
118 nsIAtom*
119 nsHtml5HtmlAttributes::getPrefixNoBoundsCheck(int32_t index)
120 {
121 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
122 return names[index]->getPrefix(mode);
123 }
125 nsString*
126 nsHtml5HtmlAttributes::getValueNoBoundsCheck(int32_t index)
127 {
128 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
129 return values[index];
130 }
132 nsHtml5AttributeName*
133 nsHtml5HtmlAttributes::getAttributeNameNoBoundsCheck(int32_t index)
134 {
135 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
136 return names[index];
137 }
139 void
140 nsHtml5HtmlAttributes::addAttribute(nsHtml5AttributeName* name, nsString* value)
141 {
142 if (names.length == length) {
143 int32_t newLen = length << 1;
144 jArray<nsHtml5AttributeName*,int32_t> newNames = jArray<nsHtml5AttributeName*,int32_t>::newJArray(newLen);
145 nsHtml5ArrayCopy::arraycopy(names, newNames, names.length);
146 names = newNames;
147 jArray<nsString*,int32_t> newValues = jArray<nsString*,int32_t>::newJArray(newLen);
148 nsHtml5ArrayCopy::arraycopy(values, newValues, values.length);
149 values = newValues;
150 }
151 names[length] = name;
152 values[length] = value;
153 length++;
154 }
156 void
157 nsHtml5HtmlAttributes::clear(int32_t m)
158 {
159 for (int32_t i = 0; i < length; i++) {
160 names[i]->release();
161 names[i] = nullptr;
162 nsHtml5Portability::releaseString(values[i]);
163 values[i] = nullptr;
164 }
165 length = 0;
166 mode = m;
167 }
169 void
170 nsHtml5HtmlAttributes::releaseValue(int32_t i)
171 {
172 nsHtml5Portability::releaseString(values[i]);
173 }
175 void
176 nsHtml5HtmlAttributes::clearWithoutReleasingContents()
177 {
178 for (int32_t i = 0; i < length; i++) {
179 names[i] = nullptr;
180 values[i] = nullptr;
181 }
182 length = 0;
183 }
185 bool
186 nsHtml5HtmlAttributes::contains(nsHtml5AttributeName* name)
187 {
188 for (int32_t i = 0; i < length; i++) {
189 if (name->equalsAnother(names[i])) {
190 return true;
191 }
192 }
193 return false;
194 }
196 void
197 nsHtml5HtmlAttributes::adjustForMath()
198 {
199 mode = NS_HTML5ATTRIBUTE_NAME_MATHML;
200 }
202 void
203 nsHtml5HtmlAttributes::adjustForSvg()
204 {
205 mode = NS_HTML5ATTRIBUTE_NAME_SVG;
206 }
208 nsHtml5HtmlAttributes*
209 nsHtml5HtmlAttributes::cloneAttributes(nsHtml5AtomTable* interner)
210 {
211 MOZ_ASSERT((!length) || !mode || mode == 3);
212 nsHtml5HtmlAttributes* clone = new nsHtml5HtmlAttributes(0);
213 for (int32_t i = 0; i < length; i++) {
214 clone->addAttribute(names[i]->cloneAttributeName(interner), nsHtml5Portability::newStringFromString(values[i]));
215 }
216 return clone;
217 }
219 bool
220 nsHtml5HtmlAttributes::equalsAnother(nsHtml5HtmlAttributes* other)
221 {
222 MOZ_ASSERT(!mode || mode == 3, "Trying to compare attributes in foreign content.");
223 int32_t otherLength = other->getLength();
224 if (length != otherLength) {
225 return false;
226 }
227 for (int32_t i = 0; i < length; i++) {
228 bool found = false;
229 nsIAtom* ownLocal = names[i]->getLocal(NS_HTML5ATTRIBUTE_NAME_HTML);
230 for (int32_t j = 0; j < otherLength; j++) {
231 if (ownLocal == other->names[j]->getLocal(NS_HTML5ATTRIBUTE_NAME_HTML)) {
232 found = true;
233 if (!nsHtml5Portability::stringEqualsString(values[i], other->values[j])) {
234 return false;
235 }
236 }
237 }
238 if (!found) {
239 return false;
240 }
241 }
242 return true;
243 }
245 void
246 nsHtml5HtmlAttributes::initializeStatics()
247 {
248 EMPTY_ATTRIBUTES = new nsHtml5HtmlAttributes(NS_HTML5ATTRIBUTE_NAME_HTML);
249 }
251 void
252 nsHtml5HtmlAttributes::releaseStatics()
253 {
254 delete EMPTY_ATTRIBUTES;
255 }