gecko-dev/dom/base/nsDOMTokenList.h
Aryeh Gregor 30a8dae775 Bug 869788 - Normalize DOMTokenList for whitespace/dupes; r=smaug
Some bits taken from a patch by Cameron McCormack.  This follows a
change to the DOM spec that has already been implemented by WebKit.

We do no checks for duplicates on initial attribute parsing, only when
the DOMTokenList is accessed.  We re-check for duplicates on every
DOMTokenList access, but optimized with a bloom filter, so it should be
fast.  It would be possible to add a flag to check if we've already
removed duplicates from the atom list, but it would require the
nsAttrValue to talk to the nsDOMTokenList somehow, and a spare bit would
be needed in nsAttrValue, and it would only help cases where
DOMTokenList is repeatedly accessed without the content attribute being
modified in between (e.g., .length) where the token list is extremely
long.

This patch assumes that no one other than nsDOMTokenList cares if
duplicates are removed from the nsAttrValue's atom array.  If anything
does, they will see inconsistent behavior depending on whether
nsDOMTokenList has removed duplicates yet.  Since we don't want to
check for duplicates on parse for performance reasons, the correct fix
is to update the code elsewhere to also remove duplicates.

MozReview-Commit-ID: 97KRVhPGwm8

--HG--
extra : rebase_source : 59fc76e26ea2055eb46fa6380c198c7a1e88ca61
2016-04-11 18:01:14 +03:00

108 lines
3.5 KiB
C++

/* -*- 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.
*/
#ifndef nsDOMTokenList_h___
#define nsDOMTokenList_h___
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsDOMString.h"
#include "nsWhitespaceTokenizer.h"
#include "nsWrapperCache.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/DOMTokenListSupportedTokens.h"
namespace mozilla {
class ErrorResult;
} // namespace mozilla
class nsAttrValue;
class nsIAtom;
// nsISupports must be on the primary inheritance chain
class nsDOMTokenList : public nsISupports,
public nsWrapperCache
{
protected:
typedef mozilla::dom::Element Element;
typedef nsWhitespaceTokenizerTemplate<nsContentUtils::IsHTMLWhitespace>
WhitespaceTokenizer;
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsDOMTokenList)
nsDOMTokenList(Element* aElement, nsIAtom* aAttrAtom,
const mozilla::dom::DOMTokenListSupportedTokenArray = nullptr);
virtual JSObject* WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;
Element* GetParentObject()
{
return mElement;
}
void RemoveDuplicates(const nsAttrValue* aAttr);
uint32_t Length();
void Item(uint32_t aIndex, nsAString& aResult)
{
bool found;
IndexedGetter(aIndex, found, aResult);
if (!found) {
SetDOMStringToNull(aResult);
}
}
void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aResult);
bool Contains(const nsAString& aToken);
void Add(const nsAString& aToken, mozilla::ErrorResult& aError);
void Add(const nsTArray<nsString>& aTokens,
mozilla::ErrorResult& aError);
void Remove(const nsAString& aToken, mozilla::ErrorResult& aError);
void Remove(const nsTArray<nsString>& aTokens,
mozilla::ErrorResult& aError);
void Replace(const nsAString& aToken,
const nsAString& aNewToken,
mozilla::ErrorResult& aError);
bool Toggle(const nsAString& aToken,
const mozilla::dom::Optional<bool>& force,
mozilla::ErrorResult& aError);
bool Supports(const nsAString& aToken,
mozilla::ErrorResult& aError);
void GetValue(nsAString& aResult) { Stringify(aResult); }
void SetValue(const nsAString& aValue, mozilla::ErrorResult& rv);
void Stringify(nsAString& aResult);
protected:
virtual ~nsDOMTokenList();
nsresult CheckToken(const nsAString& aStr);
nsresult CheckTokens(const nsTArray<nsString>& aStr);
void RemoveDuplicatesInternal(nsTArray<nsCOMPtr<nsIAtom>>* aArray,
uint32_t aStart);
void AddInternal(const nsAttrValue* aAttr,
const nsTArray<nsString>& aTokens);
void RemoveInternal(const nsAttrValue* aAttr,
const nsTArray<nsString>& aTokens);
void ReplaceInternal(const nsAttrValue* aAttr,
const nsAString& aToken,
const nsAString& aNewToken);
inline const nsAttrValue* GetParsedAttr();
nsCOMPtr<Element> mElement;
nsCOMPtr<nsIAtom> mAttrAtom;
const mozilla::dom::DOMTokenListSupportedTokenArray mSupportedTokens;
};
#endif // nsDOMTokenList_h___