SSL nsDOMTokenList.cpp
Interaktion und PortierbarkeitC
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* * Implementation of DOMTokenList specified by HTML5.
*/
nsDOMTokenList::nsDOMTokenList(
Element* aElement, nsAtom* aAttrAtom, const DOMTokenListSupportedTokenArray aSupportedTokens)
: mElement(aElement),
mAttrAtom(aAttrAtom),
mSupportedTokens(aSupportedTokens) { // We don't add a reference to our element. If it goes away, // we'll be told to drop our reference
}
void nsDOMTokenList::CheckToken(const nsAString& aToken, ErrorResult& aRv) { if (aToken.IsEmpty()) { return aRv.ThrowSyntaxError("The empty string is not a valid token.");
}
while (iter != end) { if (nsContentUtils::IsHTMLWhitespace(*iter)) { return aRv.ThrowInvalidCharacterError( "The token can not contain whitespace.");
}
++iter;
}
}
void nsDOMTokenList::CheckTokens(const nsTArray<nsString>& aTokens,
ErrorResult& aRv) { for (uint32_t i = 0, l = aTokens.Length(); i < l; ++i) {
CheckToken(aTokens[i], aRv); if (aRv.Failed()) { return;
}
}
}
if (aAttr) {
RemoveDuplicates(aAttr); for (uint32_t i = 0; i < aAttr->GetAtomCount(); i++) { if (i != 0) {
resultStr.AppendLiteral(" ");
}
resultStr.Append(nsDependentAtomString(aAttr->AtomAt(i)));
}
}
AutoTArray<nsString, 10> addedClasses;
for (uint32_t i = 0, l = aTokens.Length(); i < l; ++i) { const nsString& aToken = aTokens[i];
if ((aAttr && aAttr->Contains(aToken)) || addedClasses.Contains(aToken)) { continue;
}
if (!resultStr.IsEmpty()) {
resultStr.Append(' ');
}
resultStr.Append(aToken);
nsAutoString resultStr; for (uint32_t i = 0; i < aAttr->GetAtomCount(); i++) { if (aTokens.Contains(nsDependentAtomString(aAttr->AtomAt(i)))) { continue;
} if (!resultStr.IsEmpty()) {
resultStr.AppendLiteral(" ");
}
resultStr.Append(nsDependentAtomString(aAttr->AtomAt(i)));
}
if (isPresent) { if (!forceOn) {
RemoveInternal(attr, tokens);
isPresent = false;
}
} else { if (!forceOff) {
AddInternal(attr, tokens);
isPresent = true;
}
}
return isPresent;
}
bool nsDOMTokenList::Replace(const nsAString& aToken, const nsAString& aNewToken, ErrorResult& aError) { // Doing this here instead of using `CheckToken` because if aToken had invalid // characters, and aNewToken is empty, the returned error should be a // SyntaxError, not an InvalidCharacterError. if (aNewToken.IsEmpty()) {
aError.ThrowSyntaxError("The empty string is not a valid token."); returnfalse;
}
CheckToken(aToken, aError); if (aError.Failed()) { returnfalse;
}
CheckToken(aNewToken, aError); if (aError.Failed()) { returnfalse;
}
const nsAttrValue* attr = GetParsedAttr(); if (!attr) { returnfalse;
}
// Trying to do a single pass here leads to really complicated code. Just do // the simple thing. bool haveOld = false; for (uint32_t i = 0; i < aAttr->GetAtomCount(); ++i) { if (aAttr->AtomAt(i)->Equals(aToken)) {
haveOld = true; break;
}
} if (!haveOld) { // Make sure to not touch the attribute value in this case. returnfalse;
}
bool sawIt = false;
nsAutoString resultStr; for (uint32_t i = 0; i < aAttr->GetAtomCount(); i++) { if (aAttr->AtomAt(i)->Equals(aToken) ||
aAttr->AtomAt(i)->Equals(aNewToken)) { if (sawIt) { // We keep only the first continue;
}
sawIt = true; if (!resultStr.IsEmpty()) {
resultStr.AppendLiteral(" ");
}
resultStr.Append(aNewToken); continue;
} if (!resultStr.IsEmpty()) {
resultStr.AppendLiteral(" ");
}
resultStr.Append(nsDependentAtomString(aAttr->AtomAt(i)));
}
MOZ_ASSERT(sawIt, "How could we not have found our token this time?");
mElement->SetAttr(kNameSpaceID_None, mAttrAtom, resultStr, true); returntrue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.