Line data Source code
1 : /*
2 : * Copyright (c) 2007 Henri Sivonen
3 : * Copyright (c) 2008-2017 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 : */
23 :
24 : #define nsHtml5HtmlAttributes_cpp__
25 :
26 : #include "nsIAtom.h"
27 : #include "nsHtml5AtomTable.h"
28 : #include "nsHtml5String.h"
29 : #include "nsNameSpaceManager.h"
30 : #include "nsIContent.h"
31 : #include "nsTraceRefcnt.h"
32 : #include "jArray.h"
33 : #include "nsHtml5ArrayCopy.h"
34 : #include "nsAHtml5TreeBuilderState.h"
35 : #include "nsHtml5ByteReadable.h"
36 : #include "nsHtml5Macros.h"
37 : #include "nsIContentHandle.h"
38 :
39 : #include "nsHtml5Tokenizer.h"
40 : #include "nsHtml5TreeBuilder.h"
41 : #include "nsHtml5MetaScanner.h"
42 : #include "nsHtml5AttributeName.h"
43 : #include "nsHtml5ElementName.h"
44 : #include "nsHtml5StackNode.h"
45 : #include "nsHtml5UTF16Buffer.h"
46 : #include "nsHtml5StateSnapshot.h"
47 : #include "nsHtml5Portability.h"
48 :
49 : #include "nsHtml5HtmlAttributes.h"
50 :
51 : nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES = nullptr;
52 :
53 10 : nsHtml5HtmlAttributes::nsHtml5HtmlAttributes(int32_t aMode)
54 10 : : mMode(aMode)
55 : {
56 10 : MOZ_COUNT_CTOR(nsHtml5HtmlAttributes);
57 10 : }
58 :
59 :
60 14 : nsHtml5HtmlAttributes::~nsHtml5HtmlAttributes()
61 : {
62 7 : MOZ_COUNT_DTOR(nsHtml5HtmlAttributes);
63 7 : clear(0);
64 7 : }
65 :
66 : int32_t
67 29 : nsHtml5HtmlAttributes::getIndex(nsHtml5AttributeName* aName)
68 : {
69 83 : for (size_t i = 0; i < mStorage.Length(); i++) {
70 128 : if (mStorage[i].GetLocal(nsHtml5AttributeName::HTML) ==
71 64 : aName->getLocal(nsHtml5AttributeName::HTML)) {
72 : // It's release asserted elsewhere that i can't be too large.
73 10 : return i;
74 : }
75 : }
76 19 : return -1;
77 : }
78 :
79 : nsHtml5String
80 29 : nsHtml5HtmlAttributes::getValue(nsHtml5AttributeName* aName)
81 : {
82 29 : int32_t index = getIndex(aName);
83 29 : if (index == -1) {
84 19 : return nullptr;
85 : } else {
86 10 : return getValueNoBoundsCheck(index);
87 : }
88 : }
89 :
90 : int32_t
91 9 : nsHtml5HtmlAttributes::getLength()
92 : {
93 9 : return mStorage.Length();
94 : }
95 :
96 : nsIAtom*
97 16 : nsHtml5HtmlAttributes::getLocalNameNoBoundsCheck(int32_t aIndex)
98 : {
99 16 : MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
100 : "Index out of bounds");
101 16 : return mStorage[aIndex].GetLocal(mMode);
102 : }
103 :
104 : int32_t
105 16 : nsHtml5HtmlAttributes::getURINoBoundsCheck(int32_t aIndex)
106 : {
107 16 : MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
108 : "Index out of bounds");
109 16 : return mStorage[aIndex].GetUri(mMode);
110 : }
111 :
112 : nsIAtom*
113 16 : nsHtml5HtmlAttributes::getPrefixNoBoundsCheck(int32_t aIndex)
114 : {
115 16 : MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
116 : "Index out of bounds");
117 16 : return mStorage[aIndex].GetPrefix(mMode);
118 : }
119 :
120 : nsHtml5String
121 26 : nsHtml5HtmlAttributes::getValueNoBoundsCheck(int32_t aIndex)
122 : {
123 26 : MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
124 : "Index out of bounds");
125 26 : return mStorage[aIndex].GetValue();
126 : }
127 :
128 : int32_t
129 0 : nsHtml5HtmlAttributes::getLineNoBoundsCheck(int32_t aIndex)
130 : {
131 0 : MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
132 : "Index out of bounds");
133 0 : return mStorage[aIndex].GetLine();
134 : }
135 :
136 : void
137 16 : nsHtml5HtmlAttributes::addAttribute(nsHtml5AttributeName* aName,
138 : nsHtml5String aValue,
139 : int32_t aLine)
140 : {
141 16 : mStorage.AppendElement(nsHtml5AttributeEntry(aName, aValue, aLine));
142 16 : MOZ_RELEASE_ASSERT(mStorage.Length() <= INT32_MAX,
143 : "Can't handle this many attributes.");
144 16 : }
145 :
146 : // Isindex-only, so doesn't need to deal with SVG and MathML
147 : void
148 0 : nsHtml5HtmlAttributes::AddAttributeWithLocal(nsIAtom* aName,
149 : nsHtml5String aValue,
150 : int32_t aLine)
151 : {
152 0 : mStorage.AppendElement(nsHtml5AttributeEntry(aName, aValue, aLine));
153 0 : MOZ_RELEASE_ASSERT(mStorage.Length() <= INT32_MAX,
154 : "Can't handle this many attributes.");
155 0 : }
156 :
157 : void
158 7 : nsHtml5HtmlAttributes::clear(int32_t aMode)
159 : {
160 23 : for (nsHtml5AttributeEntry& entry : mStorage) {
161 16 : entry.ReleaseValue();
162 : }
163 7 : mStorage.TruncateLength(0);
164 7 : mMode = aMode;
165 7 : }
166 :
167 : void
168 0 : nsHtml5HtmlAttributes::releaseValue(int32_t aIndex)
169 : {
170 0 : mStorage[aIndex].ReleaseValue();
171 0 : }
172 :
173 : void
174 0 : nsHtml5HtmlAttributes::clearWithoutReleasingContents()
175 : {
176 0 : mStorage.TruncateLength(0);
177 0 : }
178 :
179 : bool
180 24 : nsHtml5HtmlAttributes::contains(nsHtml5AttributeName* aName)
181 : {
182 61 : for (size_t i = 0; i < mStorage.Length(); i++) {
183 74 : if (mStorage[i].GetLocal(nsHtml5AttributeName::HTML) ==
184 37 : aName->getLocal(nsHtml5AttributeName::HTML)) {
185 0 : return true;
186 : }
187 : }
188 24 : return false;
189 : }
190 :
191 : void
192 0 : nsHtml5HtmlAttributes::adjustForMath()
193 : {
194 0 : mMode = nsHtml5AttributeName::MATHML;
195 0 : }
196 :
197 : void
198 0 : nsHtml5HtmlAttributes::adjustForSvg()
199 : {
200 0 : mMode = nsHtml5AttributeName::SVG;
201 0 : }
202 :
203 : nsHtml5HtmlAttributes*
204 0 : nsHtml5HtmlAttributes::cloneAttributes(nsHtml5AtomTable* aInterner)
205 : {
206 0 : MOZ_ASSERT(mStorage.IsEmpty() || !mMode);
207 : nsHtml5HtmlAttributes* clone =
208 0 : new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML);
209 0 : for (nsHtml5AttributeEntry& entry : mStorage) {
210 0 : clone->AddEntry(entry.Clone(aInterner));
211 : }
212 0 : return clone;
213 : }
214 :
215 : bool
216 0 : nsHtml5HtmlAttributes::equalsAnother(nsHtml5HtmlAttributes* aOther)
217 : {
218 0 : MOZ_ASSERT(!mMode, "Trying to compare attributes in foreign content.");
219 0 : if (mStorage.Length() != aOther->mStorage.Length()) {
220 0 : return false;
221 : }
222 0 : for (nsHtml5AttributeEntry& entry : mStorage) {
223 0 : bool found = false;
224 0 : nsIAtom* ownLocal = entry.GetLocal(nsHtml5AttributeName::HTML);
225 0 : for (nsHtml5AttributeEntry& otherEntry : aOther->mStorage) {
226 0 : if (ownLocal == otherEntry.GetLocal(nsHtml5AttributeName::HTML)) {
227 0 : found = true;
228 0 : if (!entry.GetValue().Equals(otherEntry.GetValue())) {
229 0 : return false;
230 : }
231 0 : break;
232 : }
233 : }
234 0 : if (!found) {
235 0 : return false;
236 : }
237 : }
238 0 : return true;
239 : }
240 :
241 : void
242 0 : nsHtml5HtmlAttributes::AddEntry(nsHtml5AttributeEntry&& aEntry)
243 : {
244 0 : mStorage.AppendElement(aEntry);
245 0 : }
246 :
247 : void
248 3 : nsHtml5HtmlAttributes::initializeStatics()
249 : {
250 3 : EMPTY_ATTRIBUTES = new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML);
251 3 : }
252 :
253 : void
254 0 : nsHtml5HtmlAttributes::releaseStatics()
255 : {
256 0 : delete EMPTY_ATTRIBUTES;
257 0 : }
258 :
259 :
|