Bug 1890793: Assert CallArgs::newTarget is not gray. r=spidermonkey-reviewers,sfink...
[gecko.git] / parser / html / nsHtml5HtmlAttributes.cpp
blobdcae10e40c2f610eb870b9e5c63e5f291830733b
1 /*
2 * Copyright (c) 2007 Henri Sivonen
3 * Copyright (c) 2008-2017 Mozilla Foundation
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 #define nsHtml5HtmlAttributes_cpp__
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"
39 #include "nsHtml5AttributeName.h"
40 #include "nsHtml5ElementName.h"
41 #include "nsHtml5Portability.h"
42 #include "nsHtml5StackNode.h"
43 #include "nsHtml5StateSnapshot.h"
44 #include "nsHtml5Tokenizer.h"
45 #include "nsHtml5TreeBuilder.h"
46 #include "nsHtml5UTF16Buffer.h"
48 #include "nsHtml5HtmlAttributes.h"
50 nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES = nullptr;
52 nsHtml5HtmlAttributes::nsHtml5HtmlAttributes(int32_t aMode) : mMode(aMode) {
53 MOZ_COUNT_CTOR(nsHtml5HtmlAttributes);
56 nsHtml5HtmlAttributes::~nsHtml5HtmlAttributes() {
57 MOZ_COUNT_DTOR(nsHtml5HtmlAttributes);
58 clear(0);
61 int32_t nsHtml5HtmlAttributes::getIndex(nsHtml5AttributeName* aName) {
62 size_t len = mStorage.Length();
63 for (size_t i = 0; i < len; i++) {
64 if (mStorage.Elements()[i].GetLocal(nsHtml5AttributeName::HTML) ==
65 aName->getLocal(nsHtml5AttributeName::HTML)) {
66 // It's release asserted elsewhere that i can't be too large.
67 return i;
70 return -1;
73 nsHtml5String nsHtml5HtmlAttributes::getValue(nsHtml5AttributeName* aName) {
74 int32_t index = getIndex(aName);
75 if (index == -1) {
76 return nullptr;
77 } else {
78 return getValueNoBoundsCheck(index);
82 int32_t nsHtml5HtmlAttributes::getLength() { return mStorage.Length(); }
84 nsAtom* nsHtml5HtmlAttributes::getLocalNameNoBoundsCheck(int32_t aIndex) {
85 MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
86 "Index out of bounds");
87 return mStorage.Elements()[aIndex].GetLocal(mMode);
90 int32_t nsHtml5HtmlAttributes::getURINoBoundsCheck(int32_t aIndex) {
91 MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
92 "Index out of bounds");
93 return mStorage.Elements()[aIndex].GetUri(mMode);
96 nsAtom* nsHtml5HtmlAttributes::getPrefixNoBoundsCheck(int32_t aIndex) {
97 MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
98 "Index out of bounds");
99 return mStorage.Elements()[aIndex].GetPrefix(mMode);
102 nsHtml5String nsHtml5HtmlAttributes::getValueNoBoundsCheck(int32_t aIndex) {
103 MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
104 "Index out of bounds");
105 return mStorage.Elements()[aIndex].GetValue();
108 int32_t nsHtml5HtmlAttributes::getLineNoBoundsCheck(int32_t aIndex) {
109 MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
110 "Index out of bounds");
111 return mStorage.Elements()[aIndex].GetLine();
114 void nsHtml5HtmlAttributes::addAttribute(nsHtml5AttributeName* aName,
115 nsHtml5String aValue, int32_t aLine) {
116 mStorage.AppendElement(nsHtml5AttributeEntry(aName, aValue, aLine));
117 MOZ_RELEASE_ASSERT(mStorage.Length() <= INT32_MAX,
118 "Can't handle this many attributes.");
121 // Isindex-only, so doesn't need to deal with SVG and MathML
122 void nsHtml5HtmlAttributes::AddAttributeWithLocal(nsAtom* aName,
123 nsHtml5String aValue,
124 int32_t aLine) {
125 mStorage.AppendElement(nsHtml5AttributeEntry(aName, aValue, aLine));
126 MOZ_RELEASE_ASSERT(mStorage.Length() <= INT32_MAX,
127 "Can't handle this many attributes.");
130 void nsHtml5HtmlAttributes::clear(int32_t aMode) {
131 size_t len = mStorage.Length();
132 for (size_t i = 0; i < len; ++i) {
133 nsHtml5AttributeEntry& entry = mStorage.Elements()[i];
134 entry.ReleaseValue();
136 mStorage.TruncateLength(0);
137 mMode = aMode;
138 mDuplicateAttributeError = false;
141 void nsHtml5HtmlAttributes::releaseValue(int32_t aIndex) {
142 MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
143 "Index out of bounds");
144 mStorage.Elements()[aIndex].ReleaseValue();
147 void nsHtml5HtmlAttributes::clearWithoutReleasingContents() {
148 mStorage.TruncateLength(0);
151 bool nsHtml5HtmlAttributes::contains(nsHtml5AttributeName* aName) {
152 size_t len = mStorage.Length();
153 for (size_t i = 0; i < len; i++) {
154 if (mStorage.Elements()[i].GetLocal(nsHtml5AttributeName::HTML) ==
155 aName->getLocal(nsHtml5AttributeName::HTML)) {
156 return true;
159 return false;
162 void nsHtml5HtmlAttributes::adjustForMath() {
163 mMode = nsHtml5AttributeName::MATHML;
166 void nsHtml5HtmlAttributes::adjustForSvg() {
167 mMode = nsHtml5AttributeName::SVG;
170 nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::cloneAttributes() {
171 MOZ_ASSERT(mStorage.IsEmpty() || !mMode);
172 nsHtml5HtmlAttributes* clone =
173 new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML);
174 size_t len = mStorage.Length();
175 for (size_t i = 0; i < len; ++i) {
176 nsHtml5AttributeEntry& entry = mStorage.Elements()[i];
177 clone->AddEntry(entry.Clone());
179 return clone;
182 bool nsHtml5HtmlAttributes::equalsAnother(nsHtml5HtmlAttributes* aOther) {
183 MOZ_ASSERT(!mMode, "Trying to compare attributes in foreign content.");
184 size_t len = mStorage.Length();
185 size_t lenOther = aOther->mStorage.Length();
186 if (mStorage.Length() != aOther->mStorage.Length()) {
187 return false;
189 for (size_t i = 0; i < len; ++i) {
190 nsHtml5AttributeEntry& entry = mStorage.Elements()[i];
191 bool found = false;
192 nsAtom* ownLocal = entry.GetLocal(nsHtml5AttributeName::HTML);
193 for (size_t j = 0; j < lenOther; ++j) {
194 nsHtml5AttributeEntry& otherEntry = aOther->mStorage.Elements()[j];
195 if (ownLocal == otherEntry.GetLocal(nsHtml5AttributeName::HTML)) {
196 found = true;
197 if (!entry.GetValue().Equals(otherEntry.GetValue())) {
198 return false;
200 break;
203 if (!found) {
204 return false;
207 return true;
210 void nsHtml5HtmlAttributes::AddEntry(nsHtml5AttributeEntry&& aEntry) {
211 mStorage.AppendElement(aEntry);
214 void nsHtml5HtmlAttributes::initializeStatics() {
215 EMPTY_ATTRIBUTES = new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML);
218 void nsHtml5HtmlAttributes::releaseStatics() { delete EMPTY_ATTRIBUTES; }