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