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 "nsHtml5Portability.h"
42 #include "nsHtml5StackNode.h"
43 #include "nsHtml5StateSnapshot.h"
44 #include "nsHtml5Tokenizer.h"
45 #include "nsHtml5TreeBuilder.h"
46 #include "nsHtml5UTF16Buffer.h"
47 
48 #include "nsHtml5HtmlAttributes.h"
49 
50 nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES = nullptr;
51 
nsHtml5HtmlAttributes(int32_t aMode)52 nsHtml5HtmlAttributes::nsHtml5HtmlAttributes(int32_t aMode) : mMode(aMode) {
53   MOZ_COUNT_CTOR(nsHtml5HtmlAttributes);
54 }
55 
~nsHtml5HtmlAttributes()56 nsHtml5HtmlAttributes::~nsHtml5HtmlAttributes() {
57   MOZ_COUNT_DTOR(nsHtml5HtmlAttributes);
58   clear(0);
59 }
60 
getIndex(nsHtml5AttributeName * aName)61 int32_t nsHtml5HtmlAttributes::getIndex(nsHtml5AttributeName* aName) {
62   for (size_t i = 0; i < mStorage.Length(); i++) {
63     if (mStorage[i].GetLocal(nsHtml5AttributeName::HTML) ==
64         aName->getLocal(nsHtml5AttributeName::HTML)) {
65       // It's release asserted elsewhere that i can't be too large.
66       return i;
67     }
68   }
69   return -1;
70 }
71 
getValue(nsHtml5AttributeName * aName)72 nsHtml5String nsHtml5HtmlAttributes::getValue(nsHtml5AttributeName* aName) {
73   int32_t index = getIndex(aName);
74   if (index == -1) {
75     return nullptr;
76   } else {
77     return getValueNoBoundsCheck(index);
78   }
79 }
80 
getLength()81 int32_t nsHtml5HtmlAttributes::getLength() { return mStorage.Length(); }
82 
getLocalNameNoBoundsCheck(int32_t aIndex)83 nsAtom* nsHtml5HtmlAttributes::getLocalNameNoBoundsCheck(int32_t aIndex) {
84   MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
85              "Index out of bounds");
86   return mStorage[aIndex].GetLocal(mMode);
87 }
88 
getURINoBoundsCheck(int32_t aIndex)89 int32_t nsHtml5HtmlAttributes::getURINoBoundsCheck(int32_t aIndex) {
90   MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
91              "Index out of bounds");
92   return mStorage[aIndex].GetUri(mMode);
93 }
94 
getPrefixNoBoundsCheck(int32_t aIndex)95 nsAtom* nsHtml5HtmlAttributes::getPrefixNoBoundsCheck(int32_t aIndex) {
96   MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
97              "Index out of bounds");
98   return mStorage[aIndex].GetPrefix(mMode);
99 }
100 
getValueNoBoundsCheck(int32_t aIndex)101 nsHtml5String nsHtml5HtmlAttributes::getValueNoBoundsCheck(int32_t aIndex) {
102   MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
103              "Index out of bounds");
104   return mStorage[aIndex].GetValue();
105 }
106 
getLineNoBoundsCheck(int32_t aIndex)107 int32_t nsHtml5HtmlAttributes::getLineNoBoundsCheck(int32_t aIndex) {
108   MOZ_ASSERT(aIndex < int32_t(mStorage.Length()) && aIndex >= 0,
109              "Index out of bounds");
110   return mStorage[aIndex].GetLine();
111 }
112 
addAttribute(nsHtml5AttributeName * aName,nsHtml5String aValue,int32_t aLine)113 void nsHtml5HtmlAttributes::addAttribute(nsHtml5AttributeName* aName,
114                                          nsHtml5String aValue, int32_t aLine) {
115   mStorage.AppendElement(nsHtml5AttributeEntry(aName, aValue, aLine));
116   MOZ_RELEASE_ASSERT(mStorage.Length() <= INT32_MAX,
117                      "Can't handle this many attributes.");
118 }
119 
120 // Isindex-only, so doesn't need to deal with SVG and MathML
AddAttributeWithLocal(nsAtom * aName,nsHtml5String aValue,int32_t aLine)121 void nsHtml5HtmlAttributes::AddAttributeWithLocal(nsAtom* aName,
122                                                   nsHtml5String aValue,
123                                                   int32_t aLine) {
124   mStorage.AppendElement(nsHtml5AttributeEntry(aName, aValue, aLine));
125   MOZ_RELEASE_ASSERT(mStorage.Length() <= INT32_MAX,
126                      "Can't handle this many attributes.");
127 }
128 
clear(int32_t aMode)129 void nsHtml5HtmlAttributes::clear(int32_t aMode) {
130   for (nsHtml5AttributeEntry& entry : mStorage) {
131     entry.ReleaseValue();
132   }
133   mStorage.TruncateLength(0);
134   mMode = aMode;
135 }
136 
releaseValue(int32_t aIndex)137 void nsHtml5HtmlAttributes::releaseValue(int32_t aIndex) {
138   mStorage[aIndex].ReleaseValue();
139 }
140 
clearWithoutReleasingContents()141 void nsHtml5HtmlAttributes::clearWithoutReleasingContents() {
142   mStorage.TruncateLength(0);
143 }
144 
contains(nsHtml5AttributeName * aName)145 bool nsHtml5HtmlAttributes::contains(nsHtml5AttributeName* aName) {
146   for (size_t i = 0; i < mStorage.Length(); i++) {
147     if (mStorage[i].GetLocal(nsHtml5AttributeName::HTML) ==
148         aName->getLocal(nsHtml5AttributeName::HTML)) {
149       return true;
150     }
151   }
152   return false;
153 }
154 
adjustForMath()155 void nsHtml5HtmlAttributes::adjustForMath() {
156   mMode = nsHtml5AttributeName::MATHML;
157 }
158 
adjustForSvg()159 void nsHtml5HtmlAttributes::adjustForSvg() {
160   mMode = nsHtml5AttributeName::SVG;
161 }
162 
cloneAttributes()163 nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::cloneAttributes() {
164   MOZ_ASSERT(mStorage.IsEmpty() || !mMode);
165   nsHtml5HtmlAttributes* clone =
166       new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML);
167   for (nsHtml5AttributeEntry& entry : mStorage) {
168     clone->AddEntry(entry.Clone());
169   }
170   return clone;
171 }
172 
equalsAnother(nsHtml5HtmlAttributes * aOther)173 bool nsHtml5HtmlAttributes::equalsAnother(nsHtml5HtmlAttributes* aOther) {
174   MOZ_ASSERT(!mMode, "Trying to compare attributes in foreign content.");
175   if (mStorage.Length() != aOther->mStorage.Length()) {
176     return false;
177   }
178   for (nsHtml5AttributeEntry& entry : mStorage) {
179     bool found = false;
180     nsAtom* ownLocal = entry.GetLocal(nsHtml5AttributeName::HTML);
181     for (nsHtml5AttributeEntry& otherEntry : aOther->mStorage) {
182       if (ownLocal == otherEntry.GetLocal(nsHtml5AttributeName::HTML)) {
183         found = true;
184         if (!entry.GetValue().Equals(otherEntry.GetValue())) {
185           return false;
186         }
187         break;
188       }
189     }
190     if (!found) {
191       return false;
192     }
193   }
194   return true;
195 }
196 
AddEntry(nsHtml5AttributeEntry && aEntry)197 void nsHtml5HtmlAttributes::AddEntry(nsHtml5AttributeEntry&& aEntry) {
198   mStorage.AppendElement(aEntry);
199 }
200 
initializeStatics()201 void nsHtml5HtmlAttributes::initializeStatics() {
202   EMPTY_ATTRIBUTES = new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML);
203 }
204 
releaseStatics()205 void nsHtml5HtmlAttributes::releaseStatics() { delete EMPTY_ATTRIBUTES; }
206