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 "nsAtom.h"
27 #include "nsHtml5AtomTable.h"
28 #include "nsHtml5String.h"
29 #include "nsNameSpaceManager.h"
30 #include "nsIContent.h"
31 #include "nsTraceRefcnt.h"
32 #include "jArray.h"
33 #include "nsHtml5ArrayCopy.h"
34 #include "nsAHtml5TreeBuilderState.h"
35 #include "nsHtml5ByteReadable.h"
36 #include "nsHtml5Macros.h"
37 #include "nsIContentHandle.h"
38 
39 #include "nsHtml5Tokenizer.h"
40 #include "nsHtml5TreeBuilder.h"
41 #include "nsHtml5MetaScanner.h"
42 #include "nsHtml5AttributeName.h"
43 #include "nsHtml5ElementName.h"
44 #include "nsHtml5StackNode.h"
45 #include "nsHtml5UTF16Buffer.h"
46 #include "nsHtml5StateSnapshot.h"
47 #include "nsHtml5Portability.h"
48 
49 #include "nsHtml5HtmlAttributes.h"
50 
51 nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES = nullptr;
52 
nsHtml5HtmlAttributes(int32_t aMode)53 nsHtml5HtmlAttributes::nsHtml5HtmlAttributes(int32_t aMode) : mMode(aMode) {
54   MOZ_COUNT_CTOR(nsHtml5HtmlAttributes);
55 }
56 
~nsHtml5HtmlAttributes()57 nsHtml5HtmlAttributes::~nsHtml5HtmlAttributes() {
58   MOZ_COUNT_DTOR(nsHtml5HtmlAttributes);
59   clear(0);
60 }
61 
getIndex(nsHtml5AttributeName * aName)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.
67       return i;
68     }
69   }
70   return -1;
71 }
72 
getValue(nsHtml5AttributeName * aName)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);
79   }
80 }
81 
getLength()82 int32_t nsHtml5HtmlAttributes::getLength() { return mStorage.Length(); }
83 
getLocalNameNoBoundsCheck(int32_t aIndex)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);
88 }
89 
getURINoBoundsCheck(int32_t aIndex)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);
94 }
95 
getPrefixNoBoundsCheck(int32_t aIndex)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);
100 }
101 
getValueNoBoundsCheck(int32_t aIndex)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();
106 }
107 
getLineNoBoundsCheck(int32_t aIndex)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();
112 }
113 
addAttribute(nsHtml5AttributeName * aName,nsHtml5String aValue,int32_t aLine)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.");
119 }
120 
121 // Isindex-only, so doesn't need to deal with SVG and MathML
AddAttributeWithLocal(nsAtom * aName,nsHtml5String aValue,int32_t aLine)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.");
128 }
129 
clear(int32_t aMode)130 void nsHtml5HtmlAttributes::clear(int32_t aMode) {
131   for (nsHtml5AttributeEntry& entry : mStorage) {
132     entry.ReleaseValue();
133   }
134   mStorage.TruncateLength(0);
135   mMode = aMode;
136 }
137 
releaseValue(int32_t aIndex)138 void nsHtml5HtmlAttributes::releaseValue(int32_t aIndex) {
139   mStorage[aIndex].ReleaseValue();
140 }
141 
clearWithoutReleasingContents()142 void nsHtml5HtmlAttributes::clearWithoutReleasingContents() {
143   mStorage.TruncateLength(0);
144 }
145 
contains(nsHtml5AttributeName * aName)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)) {
150       return true;
151     }
152   }
153   return false;
154 }
155 
adjustForMath()156 void nsHtml5HtmlAttributes::adjustForMath() {
157   mMode = nsHtml5AttributeName::MATHML;
158 }
159 
adjustForSvg()160 void nsHtml5HtmlAttributes::adjustForSvg() {
161   mMode = nsHtml5AttributeName::SVG;
162 }
163 
cloneAttributes(nsHtml5AtomTable * aInterner)164 nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::cloneAttributes(
165     nsHtml5AtomTable* aInterner) {
166   MOZ_ASSERT(mStorage.IsEmpty() || !mMode);
167   nsHtml5HtmlAttributes* clone =
168       new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML);
169   for (nsHtml5AttributeEntry& entry : mStorage) {
170     clone->AddEntry(entry.Clone(aInterner));
171   }
172   return clone;
173 }
174 
equalsAnother(nsHtml5HtmlAttributes * aOther)175 bool nsHtml5HtmlAttributes::equalsAnother(nsHtml5HtmlAttributes* aOther) {
176   MOZ_ASSERT(!mMode, "Trying to compare attributes in foreign content.");
177   if (mStorage.Length() != aOther->mStorage.Length()) {
178     return false;
179   }
180   for (nsHtml5AttributeEntry& entry : mStorage) {
181     bool found = false;
182     nsAtom* ownLocal = entry.GetLocal(nsHtml5AttributeName::HTML);
183     for (nsHtml5AttributeEntry& otherEntry : aOther->mStorage) {
184       if (ownLocal == otherEntry.GetLocal(nsHtml5AttributeName::HTML)) {
185         found = true;
186         if (!entry.GetValue().Equals(otherEntry.GetValue())) {
187           return false;
188         }
189         break;
190       }
191     }
192     if (!found) {
193       return false;
194     }
195   }
196   return true;
197 }
198 
AddEntry(nsHtml5AttributeEntry && aEntry)199 void nsHtml5HtmlAttributes::AddEntry(nsHtml5AttributeEntry&& aEntry) {
200   mStorage.AppendElement(aEntry);
201 }
202 
initializeStatics()203 void nsHtml5HtmlAttributes::initializeStatics() {
204   EMPTY_ATTRIBUTES = new nsHtml5HtmlAttributes(nsHtml5AttributeName::HTML);
205 }
206 
releaseStatics()207 void nsHtml5HtmlAttributes::releaseStatics() { delete EMPTY_ATTRIBUTES; }
208