parser/html/nsHtml5HtmlAttributes.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /*
michael@0 2 * Copyright (c) 2007 Henri Sivonen
michael@0 3 * Copyright (c) 2008-2011 Mozilla Foundation
michael@0 4 *
michael@0 5 * Permission is hereby granted, free of charge, to any person obtaining a
michael@0 6 * copy of this software and associated documentation files (the "Software"),
michael@0 7 * to deal in the Software without restriction, including without limitation
michael@0 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
michael@0 9 * and/or sell copies of the Software, and to permit persons to whom the
michael@0 10 * Software is furnished to do so, subject to the following conditions:
michael@0 11 *
michael@0 12 * The above copyright notice and this permission notice shall be included in
michael@0 13 * all copies or substantial portions of the Software.
michael@0 14 *
michael@0 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
michael@0 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
michael@0 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
michael@0 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
michael@0 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
michael@0 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
michael@0 21 * DEALINGS IN THE SOFTWARE.
michael@0 22 */
michael@0 23
michael@0 24 /*
michael@0 25 * THIS IS A GENERATED FILE. PLEASE DO NOT EDIT.
michael@0 26 * Please edit HtmlAttributes.java instead and regenerate.
michael@0 27 */
michael@0 28
michael@0 29 #define nsHtml5HtmlAttributes_cpp__
michael@0 30
michael@0 31 #include "nsIAtom.h"
michael@0 32 #include "nsHtml5AtomTable.h"
michael@0 33 #include "nsString.h"
michael@0 34 #include "nsNameSpaceManager.h"
michael@0 35 #include "nsIContent.h"
michael@0 36 #include "nsTraceRefcnt.h"
michael@0 37 #include "jArray.h"
michael@0 38 #include "nsHtml5ArrayCopy.h"
michael@0 39 #include "nsAHtml5TreeBuilderState.h"
michael@0 40 #include "nsHtml5Atoms.h"
michael@0 41 #include "nsHtml5ByteReadable.h"
michael@0 42 #include "nsIUnicodeDecoder.h"
michael@0 43 #include "nsHtml5Macros.h"
michael@0 44 #include "nsIContentHandle.h"
michael@0 45
michael@0 46 #include "nsHtml5Tokenizer.h"
michael@0 47 #include "nsHtml5TreeBuilder.h"
michael@0 48 #include "nsHtml5MetaScanner.h"
michael@0 49 #include "nsHtml5AttributeName.h"
michael@0 50 #include "nsHtml5ElementName.h"
michael@0 51 #include "nsHtml5StackNode.h"
michael@0 52 #include "nsHtml5UTF16Buffer.h"
michael@0 53 #include "nsHtml5StateSnapshot.h"
michael@0 54 #include "nsHtml5Portability.h"
michael@0 55
michael@0 56 #include "nsHtml5HtmlAttributes.h"
michael@0 57
michael@0 58 nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES = nullptr;
michael@0 59
michael@0 60 nsHtml5HtmlAttributes::nsHtml5HtmlAttributes(int32_t mode)
michael@0 61 : mode(mode),
michael@0 62 length(0),
michael@0 63 names(jArray<nsHtml5AttributeName*,int32_t>::newJArray(5)),
michael@0 64 values(jArray<nsString*,int32_t>::newJArray(5))
michael@0 65 {
michael@0 66 MOZ_COUNT_CTOR(nsHtml5HtmlAttributes);
michael@0 67 }
michael@0 68
michael@0 69
michael@0 70 nsHtml5HtmlAttributes::~nsHtml5HtmlAttributes()
michael@0 71 {
michael@0 72 MOZ_COUNT_DTOR(nsHtml5HtmlAttributes);
michael@0 73 clear(0);
michael@0 74 }
michael@0 75
michael@0 76 int32_t
michael@0 77 nsHtml5HtmlAttributes::getIndex(nsHtml5AttributeName* name)
michael@0 78 {
michael@0 79 for (int32_t i = 0; i < length; i++) {
michael@0 80 if (names[i] == name) {
michael@0 81 return i;
michael@0 82 }
michael@0 83 }
michael@0 84 return -1;
michael@0 85 }
michael@0 86
michael@0 87 nsString*
michael@0 88 nsHtml5HtmlAttributes::getValue(nsHtml5AttributeName* name)
michael@0 89 {
michael@0 90 int32_t index = getIndex(name);
michael@0 91 if (index == -1) {
michael@0 92 return nullptr;
michael@0 93 } else {
michael@0 94 return getValueNoBoundsCheck(index);
michael@0 95 }
michael@0 96 }
michael@0 97
michael@0 98 int32_t
michael@0 99 nsHtml5HtmlAttributes::getLength()
michael@0 100 {
michael@0 101 return length;
michael@0 102 }
michael@0 103
michael@0 104 nsIAtom*
michael@0 105 nsHtml5HtmlAttributes::getLocalNameNoBoundsCheck(int32_t index)
michael@0 106 {
michael@0 107 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
michael@0 108 return names[index]->getLocal(mode);
michael@0 109 }
michael@0 110
michael@0 111 int32_t
michael@0 112 nsHtml5HtmlAttributes::getURINoBoundsCheck(int32_t index)
michael@0 113 {
michael@0 114 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
michael@0 115 return names[index]->getUri(mode);
michael@0 116 }
michael@0 117
michael@0 118 nsIAtom*
michael@0 119 nsHtml5HtmlAttributes::getPrefixNoBoundsCheck(int32_t index)
michael@0 120 {
michael@0 121 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
michael@0 122 return names[index]->getPrefix(mode);
michael@0 123 }
michael@0 124
michael@0 125 nsString*
michael@0 126 nsHtml5HtmlAttributes::getValueNoBoundsCheck(int32_t index)
michael@0 127 {
michael@0 128 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
michael@0 129 return values[index];
michael@0 130 }
michael@0 131
michael@0 132 nsHtml5AttributeName*
michael@0 133 nsHtml5HtmlAttributes::getAttributeNameNoBoundsCheck(int32_t index)
michael@0 134 {
michael@0 135 MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
michael@0 136 return names[index];
michael@0 137 }
michael@0 138
michael@0 139 void
michael@0 140 nsHtml5HtmlAttributes::addAttribute(nsHtml5AttributeName* name, nsString* value)
michael@0 141 {
michael@0 142 if (names.length == length) {
michael@0 143 int32_t newLen = length << 1;
michael@0 144 jArray<nsHtml5AttributeName*,int32_t> newNames = jArray<nsHtml5AttributeName*,int32_t>::newJArray(newLen);
michael@0 145 nsHtml5ArrayCopy::arraycopy(names, newNames, names.length);
michael@0 146 names = newNames;
michael@0 147 jArray<nsString*,int32_t> newValues = jArray<nsString*,int32_t>::newJArray(newLen);
michael@0 148 nsHtml5ArrayCopy::arraycopy(values, newValues, values.length);
michael@0 149 values = newValues;
michael@0 150 }
michael@0 151 names[length] = name;
michael@0 152 values[length] = value;
michael@0 153 length++;
michael@0 154 }
michael@0 155
michael@0 156 void
michael@0 157 nsHtml5HtmlAttributes::clear(int32_t m)
michael@0 158 {
michael@0 159 for (int32_t i = 0; i < length; i++) {
michael@0 160 names[i]->release();
michael@0 161 names[i] = nullptr;
michael@0 162 nsHtml5Portability::releaseString(values[i]);
michael@0 163 values[i] = nullptr;
michael@0 164 }
michael@0 165 length = 0;
michael@0 166 mode = m;
michael@0 167 }
michael@0 168
michael@0 169 void
michael@0 170 nsHtml5HtmlAttributes::releaseValue(int32_t i)
michael@0 171 {
michael@0 172 nsHtml5Portability::releaseString(values[i]);
michael@0 173 }
michael@0 174
michael@0 175 void
michael@0 176 nsHtml5HtmlAttributes::clearWithoutReleasingContents()
michael@0 177 {
michael@0 178 for (int32_t i = 0; i < length; i++) {
michael@0 179 names[i] = nullptr;
michael@0 180 values[i] = nullptr;
michael@0 181 }
michael@0 182 length = 0;
michael@0 183 }
michael@0 184
michael@0 185 bool
michael@0 186 nsHtml5HtmlAttributes::contains(nsHtml5AttributeName* name)
michael@0 187 {
michael@0 188 for (int32_t i = 0; i < length; i++) {
michael@0 189 if (name->equalsAnother(names[i])) {
michael@0 190 return true;
michael@0 191 }
michael@0 192 }
michael@0 193 return false;
michael@0 194 }
michael@0 195
michael@0 196 void
michael@0 197 nsHtml5HtmlAttributes::adjustForMath()
michael@0 198 {
michael@0 199 mode = NS_HTML5ATTRIBUTE_NAME_MATHML;
michael@0 200 }
michael@0 201
michael@0 202 void
michael@0 203 nsHtml5HtmlAttributes::adjustForSvg()
michael@0 204 {
michael@0 205 mode = NS_HTML5ATTRIBUTE_NAME_SVG;
michael@0 206 }
michael@0 207
michael@0 208 nsHtml5HtmlAttributes*
michael@0 209 nsHtml5HtmlAttributes::cloneAttributes(nsHtml5AtomTable* interner)
michael@0 210 {
michael@0 211 MOZ_ASSERT((!length) || !mode || mode == 3);
michael@0 212 nsHtml5HtmlAttributes* clone = new nsHtml5HtmlAttributes(0);
michael@0 213 for (int32_t i = 0; i < length; i++) {
michael@0 214 clone->addAttribute(names[i]->cloneAttributeName(interner), nsHtml5Portability::newStringFromString(values[i]));
michael@0 215 }
michael@0 216 return clone;
michael@0 217 }
michael@0 218
michael@0 219 bool
michael@0 220 nsHtml5HtmlAttributes::equalsAnother(nsHtml5HtmlAttributes* other)
michael@0 221 {
michael@0 222 MOZ_ASSERT(!mode || mode == 3, "Trying to compare attributes in foreign content.");
michael@0 223 int32_t otherLength = other->getLength();
michael@0 224 if (length != otherLength) {
michael@0 225 return false;
michael@0 226 }
michael@0 227 for (int32_t i = 0; i < length; i++) {
michael@0 228 bool found = false;
michael@0 229 nsIAtom* ownLocal = names[i]->getLocal(NS_HTML5ATTRIBUTE_NAME_HTML);
michael@0 230 for (int32_t j = 0; j < otherLength; j++) {
michael@0 231 if (ownLocal == other->names[j]->getLocal(NS_HTML5ATTRIBUTE_NAME_HTML)) {
michael@0 232 found = true;
michael@0 233 if (!nsHtml5Portability::stringEqualsString(values[i], other->values[j])) {
michael@0 234 return false;
michael@0 235 }
michael@0 236 }
michael@0 237 }
michael@0 238 if (!found) {
michael@0 239 return false;
michael@0 240 }
michael@0 241 }
michael@0 242 return true;
michael@0 243 }
michael@0 244
michael@0 245 void
michael@0 246 nsHtml5HtmlAttributes::initializeStatics()
michael@0 247 {
michael@0 248 EMPTY_ATTRIBUTES = new nsHtml5HtmlAttributes(NS_HTML5ATTRIBUTE_NAME_HTML);
michael@0 249 }
michael@0 250
michael@0 251 void
michael@0 252 nsHtml5HtmlAttributes::releaseStatics()
michael@0 253 {
michael@0 254 delete EMPTY_ATTRIBUTES;
michael@0 255 }
michael@0 256
michael@0 257

mercurial