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 "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
);
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.
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
.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
,
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);
140 void nsHtml5HtmlAttributes::releaseValue(int32_t aIndex
) {
141 MOZ_ASSERT(aIndex
< int32_t(mStorage
.Length()) && aIndex
>= 0,
142 "Index out of bounds");
143 mStorage
.Elements()[aIndex
].ReleaseValue();
146 void nsHtml5HtmlAttributes::clearWithoutReleasingContents() {
147 mStorage
.TruncateLength(0);
150 bool nsHtml5HtmlAttributes::contains(nsHtml5AttributeName
* aName
) {
151 size_t len
= mStorage
.Length();
152 for (size_t i
= 0; i
< len
; i
++) {
153 if (mStorage
.Elements()[i
].GetLocal(nsHtml5AttributeName::HTML
) ==
154 aName
->getLocal(nsHtml5AttributeName::HTML
)) {
161 void nsHtml5HtmlAttributes::adjustForMath() {
162 mMode
= nsHtml5AttributeName::MATHML
;
165 void nsHtml5HtmlAttributes::adjustForSvg() {
166 mMode
= nsHtml5AttributeName::SVG
;
169 nsHtml5HtmlAttributes
* nsHtml5HtmlAttributes::cloneAttributes() {
170 MOZ_ASSERT(mStorage
.IsEmpty() || !mMode
);
171 nsHtml5HtmlAttributes
* clone
=
172 new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML
);
173 size_t len
= mStorage
.Length();
174 for (size_t i
= 0; i
< len
; ++i
) {
175 nsHtml5AttributeEntry
& entry
= mStorage
.Elements()[i
];
176 clone
->AddEntry(entry
.Clone());
181 bool nsHtml5HtmlAttributes::equalsAnother(nsHtml5HtmlAttributes
* aOther
) {
182 MOZ_ASSERT(!mMode
, "Trying to compare attributes in foreign content.");
183 size_t len
= mStorage
.Length();
184 size_t lenOther
= aOther
->mStorage
.Length();
185 if (mStorage
.Length() != aOther
->mStorage
.Length()) {
188 for (size_t i
= 0; i
< len
; ++i
) {
189 nsHtml5AttributeEntry
& entry
= mStorage
.Elements()[i
];
191 nsAtom
* ownLocal
= entry
.GetLocal(nsHtml5AttributeName::HTML
);
192 for (size_t j
= 0; j
< lenOther
; ++j
) {
193 nsHtml5AttributeEntry
& otherEntry
= aOther
->mStorage
.Elements()[j
];
194 if (ownLocal
== otherEntry
.GetLocal(nsHtml5AttributeName::HTML
)) {
196 if (!entry
.GetValue().Equals(otherEntry
.GetValue())) {
209 void nsHtml5HtmlAttributes::AddEntry(nsHtml5AttributeEntry
&& aEntry
) {
210 mStorage
.AppendElement(aEntry
);
213 void nsHtml5HtmlAttributes::initializeStatics() {
214 EMPTY_ATTRIBUTES
= new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML
);
217 void nsHtml5HtmlAttributes::releaseStatics() { delete EMPTY_ATTRIBUTES
; }