2015-05-03 12:32:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
/*
|
2013-01-15 00:35:59 -08:00
|
|
|
* Implementation of DOMTokenList specified by HTML5.
|
2009-08-12 01:55:14 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsDOMTokenList.h"
|
|
|
|
|
|
|
|
#include "nsAttrValue.h"
|
|
|
|
#include "nsContentUtils.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2012-11-14 14:10:08 -08:00
|
|
|
#include "mozilla/dom/Element.h"
|
2012-06-13 08:18:30 -07:00
|
|
|
#include "mozilla/dom/DOMTokenListBinding.h"
|
2012-09-05 13:49:53 -07:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2009-08-12 01:55:14 -07:00
|
|
|
|
2012-06-27 06:01:55 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2009-08-12 01:55:14 -07:00
|
|
|
|
2012-11-14 14:10:08 -08:00
|
|
|
nsDOMTokenList::nsDOMTokenList(Element* aElement, nsIAtom* aAttrAtom)
|
2009-08-12 01:55:14 -07:00
|
|
|
: mElement(aElement),
|
|
|
|
mAttrAtom(aAttrAtom)
|
|
|
|
{
|
|
|
|
// We don't add a reference to our element. If it goes away,
|
|
|
|
// we'll be told to drop our reference
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMTokenList::~nsDOMTokenList() { }
|
|
|
|
|
2014-04-29 01:57:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsDOMTokenList, mElement)
|
2011-08-22 02:14:13 -07:00
|
|
|
|
2013-01-18 11:53:52 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDOMTokenList)
|
2011-08-22 02:14:13 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2013-01-18 11:53:52 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsDOMTokenList)
|
2009-08-12 01:55:14 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2011-08-22 02:14:13 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMTokenList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMTokenList)
|
2009-08-12 01:55:14 -07:00
|
|
|
|
2012-10-16 04:51:00 -07:00
|
|
|
const nsAttrValue*
|
|
|
|
nsDOMTokenList::GetParsedAttr()
|
|
|
|
{
|
|
|
|
if (!mElement) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mElement->GetAttrInfo(kNameSpaceID_None, mAttrAtom).mValue;
|
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
uint32_t
|
|
|
|
nsDOMTokenList::Length()
|
2009-08-12 01:55:14 -07:00
|
|
|
{
|
|
|
|
const nsAttrValue* attr = GetParsedAttr();
|
|
|
|
if (!attr) {
|
2012-09-05 13:49:53 -07:00
|
|
|
return 0;
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
return attr->GetAtomCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMTokenList::IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aResult)
|
2009-08-12 01:55:14 -07:00
|
|
|
{
|
|
|
|
const nsAttrValue* attr = GetParsedAttr();
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
if (attr && aIndex < static_cast<uint32_t>(attr->GetAtomCount())) {
|
|
|
|
aFound = true;
|
|
|
|
attr->AtomAt(aIndex)->ToString(aResult);
|
|
|
|
} else {
|
|
|
|
aFound = false;
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
2012-09-05 13:49:53 -07:00
|
|
|
}
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMTokenList::CheckToken(const nsAString& aStr)
|
|
|
|
{
|
|
|
|
if (aStr.IsEmpty()) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAString::const_iterator iter, end;
|
|
|
|
aStr.BeginReading(iter);
|
|
|
|
aStr.EndReading(end);
|
|
|
|
|
|
|
|
while (iter != end) {
|
|
|
|
if (nsContentUtils::IsHTMLWhitespace(*iter))
|
|
|
|
return NS_ERROR_DOM_INVALID_CHARACTER_ERR;
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:31:13 -07:00
|
|
|
nsresult
|
|
|
|
nsDOMTokenList::CheckTokens(const nsTArray<nsString>& aTokens)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0, l = aTokens.Length(); i < l; ++i) {
|
|
|
|
nsresult rv = CheckToken(aTokens[i]);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
bool
|
|
|
|
nsDOMTokenList::Contains(const nsAString& aToken, ErrorResult& aError)
|
2009-08-12 01:55:14 -07:00
|
|
|
{
|
2012-09-05 13:49:53 -07:00
|
|
|
aError = CheckToken(aToken);
|
|
|
|
if (aError.Failed()) {
|
|
|
|
return false;
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
const nsAttrValue* attr = GetParsedAttr();
|
|
|
|
return attr && attr->Contains(aToken);
|
|
|
|
}
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMTokenList::AddInternal(const nsAttrValue* aAttr,
|
2013-08-28 00:31:13 -07:00
|
|
|
const nsTArray<nsString>& aTokens)
|
2009-08-12 01:55:14 -07:00
|
|
|
{
|
2010-04-15 04:07:30 -07:00
|
|
|
if (!mElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-12 01:55:14 -07:00
|
|
|
nsAutoString resultStr;
|
|
|
|
|
|
|
|
if (aAttr) {
|
|
|
|
aAttr->ToString(resultStr);
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:31:13 -07:00
|
|
|
bool oneWasAdded = false;
|
|
|
|
nsAutoTArray<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 (oneWasAdded ||
|
|
|
|
(!resultStr.IsEmpty() &&
|
|
|
|
!nsContentUtils::IsHTMLWhitespace(resultStr.Last()))) {
|
2014-05-21 20:48:50 -07:00
|
|
|
resultStr.Append(' ');
|
|
|
|
resultStr.Append(aToken);
|
2013-08-28 00:31:13 -07:00
|
|
|
} else {
|
|
|
|
resultStr.Append(aToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
oneWasAdded = true;
|
|
|
|
addedClasses.AppendElement(aToken);
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
2013-08-28 00:31:13 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mElement->SetAttr(kNameSpaceID_None, mAttrAtom, resultStr, true);
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
void
|
2013-08-28 00:31:13 -07:00
|
|
|
nsDOMTokenList::Add(const nsTArray<nsString>& aTokens, ErrorResult& aError)
|
2009-08-12 01:55:14 -07:00
|
|
|
{
|
2013-08-28 00:31:13 -07:00
|
|
|
aError = CheckTokens(aTokens);
|
2012-09-05 13:49:53 -07:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
const nsAttrValue* attr = GetParsedAttr();
|
2013-08-28 00:31:13 -07:00
|
|
|
AddInternal(attr, aTokens);
|
|
|
|
}
|
2009-08-12 01:55:14 -07:00
|
|
|
|
2013-08-28 00:31:13 -07:00
|
|
|
void
|
|
|
|
nsDOMTokenList::Add(const nsAString& aToken, mozilla::ErrorResult& aError)
|
|
|
|
{
|
2013-09-04 13:43:12 -07:00
|
|
|
nsAutoTArray<nsString, 1> tokens;
|
|
|
|
tokens.AppendElement(aToken);
|
|
|
|
Add(tokens, aError);
|
2012-09-05 13:49:53 -07:00
|
|
|
}
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMTokenList::RemoveInternal(const nsAttrValue* aAttr,
|
2013-08-28 00:31:13 -07:00
|
|
|
const nsTArray<nsString>& aTokens)
|
2009-08-12 01:55:14 -07:00
|
|
|
{
|
2015-02-09 14:34:50 -08:00
|
|
|
MOZ_ASSERT(aAttr, "Need an attribute");
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
nsAutoString input;
|
|
|
|
aAttr->ToString(input);
|
|
|
|
|
|
|
|
nsAString::const_iterator copyStart, tokenStart, iter, end;
|
|
|
|
input.BeginReading(iter);
|
|
|
|
input.EndReading(end);
|
|
|
|
copyStart = iter;
|
|
|
|
|
|
|
|
nsAutoString output;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool lastTokenRemoved = false;
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
while (iter != end) {
|
|
|
|
// skip whitespace.
|
|
|
|
while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter == end) {
|
|
|
|
// At this point we're sure the last seen token (if any) wasn't to be
|
|
|
|
// removed. So the trailing spaces will need to be kept.
|
2015-02-09 14:34:50 -08:00
|
|
|
MOZ_ASSERT(!lastTokenRemoved, "How did this happen?");
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
output.Append(Substring(copyStart, end));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenStart = iter;
|
|
|
|
do {
|
|
|
|
++iter;
|
|
|
|
} while (iter != end && !nsContentUtils::IsHTMLWhitespace(*iter));
|
|
|
|
|
2013-08-28 00:31:13 -07:00
|
|
|
if (aTokens.Contains(Substring(tokenStart, iter))) {
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
// Skip whitespace after the token, it will be collapsed.
|
|
|
|
while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
copyStart = iter;
|
2011-10-17 07:59:28 -07:00
|
|
|
lastTokenRemoved = true;
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (lastTokenRemoved && !output.IsEmpty()) {
|
2015-02-09 14:34:50 -08:00
|
|
|
MOZ_ASSERT(!nsContentUtils::IsHTMLWhitespace(output.Last()),
|
|
|
|
"Invalid last output token");
|
2014-01-04 07:02:17 -08:00
|
|
|
output.Append(char16_t(' '));
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
lastTokenRemoved = false;
|
2009-08-12 01:55:14 -07:00
|
|
|
output.Append(Substring(copyStart, iter));
|
|
|
|
copyStart = iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mElement->SetAttr(kNameSpaceID_None, mAttrAtom, output, true);
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
void
|
2013-08-28 00:31:13 -07:00
|
|
|
nsDOMTokenList::Remove(const nsTArray<nsString>& aTokens, ErrorResult& aError)
|
2009-08-12 01:55:14 -07:00
|
|
|
{
|
2013-08-28 00:31:13 -07:00
|
|
|
aError = CheckTokens(aTokens);
|
2012-09-05 13:49:53 -07:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return;
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
const nsAttrValue* attr = GetParsedAttr();
|
2013-08-28 00:31:13 -07:00
|
|
|
if (!attr) {
|
2012-09-05 13:49:53 -07:00
|
|
|
return;
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
2013-08-28 00:31:13 -07:00
|
|
|
RemoveInternal(attr, aTokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMTokenList::Remove(const nsAString& aToken, mozilla::ErrorResult& aError)
|
|
|
|
{
|
2013-09-04 13:43:12 -07:00
|
|
|
nsAutoTArray<nsString, 1> tokens;
|
|
|
|
tokens.AppendElement(aToken);
|
|
|
|
Remove(tokens, aError);
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
bool
|
2013-06-14 14:22:17 -07:00
|
|
|
nsDOMTokenList::Toggle(const nsAString& aToken,
|
|
|
|
const Optional<bool>& aForce,
|
|
|
|
ErrorResult& aError)
|
2012-09-05 13:49:53 -07:00
|
|
|
{
|
|
|
|
aError = CheckToken(aToken);
|
|
|
|
if (aError.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-12 01:55:14 -07:00
|
|
|
|
|
|
|
const nsAttrValue* attr = GetParsedAttr();
|
2013-06-14 14:22:17 -07:00
|
|
|
const bool forceOn = aForce.WasPassed() && aForce.Value();
|
|
|
|
const bool forceOff = aForce.WasPassed() && !aForce.Value();
|
2009-08-12 01:55:14 -07:00
|
|
|
|
2013-06-14 14:22:17 -07:00
|
|
|
bool isPresent = attr && attr->Contains(aToken);
|
2013-09-04 13:43:12 -07:00
|
|
|
nsAutoTArray<nsString, 1> tokens;
|
|
|
|
(*tokens.AppendElement()).Rebind(aToken.Data(), aToken.Length());
|
2013-06-14 14:22:17 -07:00
|
|
|
|
|
|
|
if (isPresent) {
|
|
|
|
if (!forceOn) {
|
2013-09-04 13:43:12 -07:00
|
|
|
RemoveInternal(attr, tokens);
|
2013-06-14 14:22:17 -07:00
|
|
|
isPresent = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!forceOff) {
|
2013-09-04 13:43:12 -07:00
|
|
|
AddInternal(attr, tokens);
|
2013-06-14 14:22:17 -07:00
|
|
|
isPresent = true;
|
|
|
|
}
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
2013-06-14 14:22:17 -07:00
|
|
|
return isPresent;
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
void
|
|
|
|
nsDOMTokenList::Stringify(nsAString& aResult)
|
2009-08-12 01:55:14 -07:00
|
|
|
{
|
|
|
|
if (!mElement) {
|
|
|
|
aResult.Truncate();
|
2012-09-05 13:49:53 -07:00
|
|
|
return;
|
2009-08-12 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mElement->GetAttr(kNameSpaceID_None, mAttrAtom, aResult);
|
2012-09-05 13:49:53 -07:00
|
|
|
}
|
2009-08-12 01:55:14 -07:00
|
|
|
|
2011-08-22 02:14:13 -07:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
nsDOMTokenList::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto)
|
2011-08-22 02:14:13 -07:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
return DOMTokenListBinding::Wrap(cx, this, aGivenProto);
|
2011-08-22 02:14:13 -07:00
|
|
|
}
|
|
|
|
|