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__
27 #include "nsAHtml5TreeBuilderState.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 "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"
49 #include "nsHtml5HtmlAttributes.h"
51 nsHtml5HtmlAttributes
* nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES
= nullptr;
53 nsHtml5HtmlAttributes::nsHtml5HtmlAttributes(int32_t aMode
) : mMode(aMode
) {
54 MOZ_COUNT_CTOR(nsHtml5HtmlAttributes
);
57 nsHtml5HtmlAttributes::~nsHtml5HtmlAttributes() {
58 MOZ_COUNT_DTOR(nsHtml5HtmlAttributes
);
62 int32_t nsHtml5HtmlAttributes::getIndex(nsHtml5AttributeName
* aName
) {
63 for (size_t i
= 0; i
< mStorage
.Length(); i
++) {
64 if (mStorage
[i
].GetLocal(nsHtml5AttributeName::HTML
) ==
65 aName
->getLocal(nsHtml5AttributeName::HTML
)) {
66 // It's release asserted elsewhere that i can't be too large.
73 nsHtml5String
nsHtml5HtmlAttributes::getValue(nsHtml5AttributeName
* aName
) {
74 int32_t index
= getIndex(aName
);
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
[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
[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
[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
[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
[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
,
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 for (nsHtml5AttributeEntry
& entry
: mStorage
) {
132 entry
.ReleaseValue();
134 mStorage
.TruncateLength(0);
138 void nsHtml5HtmlAttributes::releaseValue(int32_t aIndex
) {
139 mStorage
[aIndex
].ReleaseValue();
142 void nsHtml5HtmlAttributes::clearWithoutReleasingContents() {
143 mStorage
.TruncateLength(0);
146 bool nsHtml5HtmlAttributes::contains(nsHtml5AttributeName
* aName
) {
147 for (size_t i
= 0; i
< mStorage
.Length(); i
++) {
148 if (mStorage
[i
].GetLocal(nsHtml5AttributeName::HTML
) ==
149 aName
->getLocal(nsHtml5AttributeName::HTML
)) {
156 void nsHtml5HtmlAttributes::adjustForMath() {
157 mMode
= nsHtml5AttributeName::MATHML
;
160 void nsHtml5HtmlAttributes::adjustForSvg() {
161 mMode
= nsHtml5AttributeName::SVG
;
164 nsHtml5HtmlAttributes
* nsHtml5HtmlAttributes::cloneAttributes() {
165 MOZ_ASSERT(mStorage
.IsEmpty() || !mMode
);
166 nsHtml5HtmlAttributes
* clone
=
167 new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML
);
168 for (nsHtml5AttributeEntry
& entry
: mStorage
) {
169 clone
->AddEntry(entry
.Clone());
174 bool nsHtml5HtmlAttributes::equalsAnother(nsHtml5HtmlAttributes
* aOther
) {
175 MOZ_ASSERT(!mMode
, "Trying to compare attributes in foreign content.");
176 if (mStorage
.Length() != aOther
->mStorage
.Length()) {
179 for (nsHtml5AttributeEntry
& entry
: mStorage
) {
181 nsAtom
* ownLocal
= entry
.GetLocal(nsHtml5AttributeName::HTML
);
182 for (nsHtml5AttributeEntry
& otherEntry
: aOther
->mStorage
) {
183 if (ownLocal
== otherEntry
.GetLocal(nsHtml5AttributeName::HTML
)) {
185 if (!entry
.GetValue().Equals(otherEntry
.GetValue())) {
198 void nsHtml5HtmlAttributes::AddEntry(nsHtml5AttributeEntry
&& aEntry
) {
199 mStorage
.AppendElement(aEntry
);
202 void nsHtml5HtmlAttributes::initializeStatics() {
203 EMPTY_ATTRIBUTES
= new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML
);
206 void nsHtml5HtmlAttributes::releaseStatics() { delete EMPTY_ATTRIBUTES
; }