2015-05-03 22:32:37 +03: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 15:12:37 +04: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/. */
|
2011-05-20 21:23:49 +04:00
|
|
|
|
|
|
|
#include "nsDOMStringMap.h"
|
|
|
|
|
2013-08-22 09:28:12 +04:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsError.h"
|
2011-05-20 21:23:49 +04:00
|
|
|
#include "nsGenericHTMLElement.h"
|
|
|
|
#include "nsContentUtils.h"
|
2012-11-05 20:58:03 +04:00
|
|
|
#include "mozilla/dom/DOMStringMapBinding.h"
|
2018-02-09 19:17:10 +03:00
|
|
|
#include "mozilla/dom/MutationEventBinding.h"
|
2011-05-20 21:23:49 +04:00
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2011-05-20 21:23:49 +04:00
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMStringMap)
|
|
|
|
|
2011-05-20 21:23:49 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMStringMap)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mElement)
|
2011-05-20 21:23:49 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-08-02 05:29:05 +04:00
|
|
|
|
2011-05-20 21:23:49 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMStringMap)
|
2012-10-17 05:09:42 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2011-07-25 23:24:59 +04:00
|
|
|
// Check that mElement exists in case the unlink code is run more than once.
|
|
|
|
if (tmp->mElement) {
|
|
|
|
// Call back to element to null out weak reference to this object.
|
|
|
|
tmp->mElement->ClearDataset();
|
2013-06-18 00:31:03 +04:00
|
|
|
tmp->mElement->RemoveMutationObserver(tmp);
|
2012-07-30 18:20:58 +04:00
|
|
|
tmp->mElement = nullptr;
|
2011-07-25 23:24:59 +04:00
|
|
|
}
|
2016-07-22 23:19:52 +03:00
|
|
|
tmp->mExpandoAndGeneration.OwnerUnlinked();
|
2011-05-20 21:23:49 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2016-07-22 23:19:52 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsDOMStringMap)
|
2012-10-17 05:09:42 +04:00
|
|
|
|
2011-05-20 21:23:49 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMStringMap)
|
2012-10-17 05:09:42 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2013-06-18 00:31:03 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
2011-05-20 21:23:49 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMStringMap)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMStringMap)
|
|
|
|
|
2016-08-11 23:56:34 +03:00
|
|
|
nsDOMStringMap::nsDOMStringMap(Element* aElement)
|
2011-05-20 21:23:49 +04:00
|
|
|
: mElement(aElement), mRemovingProp(false) {
|
2013-06-18 00:31:03 +04:00
|
|
|
mElement->AddMutationObserver(this);
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMStringMap::~nsDOMStringMap() {
|
|
|
|
// Check if element still exists, may have been unlinked by cycle collector.
|
|
|
|
if (mElement) {
|
|
|
|
// Call back to element to null out weak reference to this object.
|
|
|
|
mElement->ClearDataset();
|
2013-06-18 00:31:03 +04:00
|
|
|
mElement->RemoveMutationObserver(this);
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:10:27 +03:00
|
|
|
DocGroup* nsDOMStringMap::GetDocGroup() const {
|
|
|
|
return mElement ? mElement->GetDocGroup() : nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
/* virtual */
|
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 17:13:33 +03:00
|
|
|
JSObject* nsDOMStringMap::WrapObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return DOMStringMap_Binding::Wrap(cx, this, aGivenProto);
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
void nsDOMStringMap::NamedGetter(const nsAString& aProp, bool& found,
|
2013-05-11 02:57:58 +04:00
|
|
|
DOMString& aResult) const {
|
2011-05-20 21:23:49 +04:00
|
|
|
nsAutoString attr;
|
|
|
|
|
|
|
|
if (!DataPropToAttr(aProp, attr)) {
|
2012-11-05 20:58:03 +04:00
|
|
|
found = false;
|
|
|
|
return;
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
2013-05-11 02:57:58 +04:00
|
|
|
found = mElement->GetAttr(attr, aResult);
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
void nsDOMStringMap::NamedSetter(const nsAString& aProp,
|
|
|
|
const nsAString& aValue, ErrorResult& rv) {
|
2011-05-20 21:23:49 +04:00
|
|
|
nsAutoString attr;
|
2012-11-05 20:58:03 +04:00
|
|
|
if (!DataPropToAttr(aProp, attr)) {
|
|
|
|
rv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2011-05-20 21:23:49 +04:00
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
nsresult res = nsContentUtils::CheckQName(attr, false);
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
rv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2011-05-20 21:23:49 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> attrAtom = NS_Atomize(attr);
|
2012-11-05 20:58:03 +04:00
|
|
|
MOZ_ASSERT(attrAtom, "Should be infallible");
|
2011-05-20 21:23:49 +04:00
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
res = mElement->SetAttr(kNameSpaceID_None, attrAtom, aValue, true);
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
rv.Throw(res);
|
|
|
|
}
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
void nsDOMStringMap::NamedDeleter(const nsAString& aProp, bool& found) {
|
2011-05-20 21:23:49 +04:00
|
|
|
// Currently removing property, attribute is already removed.
|
|
|
|
if (mRemovingProp) {
|
2014-11-04 12:11:08 +03:00
|
|
|
found = false;
|
2011-05-20 21:23:49 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-05-20 21:23:49 +04:00
|
|
|
nsAutoString attr;
|
|
|
|
if (!DataPropToAttr(aProp, attr)) {
|
2014-11-04 12:11:08 +03:00
|
|
|
found = false;
|
2011-05-20 21:23:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> attrAtom = NS_Atomize(attr);
|
2012-11-05 20:58:03 +04:00
|
|
|
MOZ_ASSERT(attrAtom, "Should be infallible");
|
2011-05-20 21:23:49 +04:00
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
found = mElement->HasAttr(kNameSpaceID_None, attrAtom);
|
2011-05-20 21:23:49 +04:00
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
if (found) {
|
|
|
|
mRemovingProp = true;
|
|
|
|
mElement->UnsetAttr(kNameSpaceID_None, attrAtom, true);
|
|
|
|
mRemovingProp = false;
|
|
|
|
}
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
2016-05-10 05:25:40 +03:00
|
|
|
void nsDOMStringMap::GetSupportedNames(nsTArray<nsString>& aNames) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t attrCount = mElement->GetAttrCount();
|
2011-05-20 21:23:49 +04:00
|
|
|
|
|
|
|
// Iterate through all the attributes and add property
|
|
|
|
// names corresponding to data attributes to return array.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < attrCount; ++i) {
|
2011-05-20 21:23:49 +04:00
|
|
|
const nsAttrName* attrName = mElement->GetAttrNameAt(i);
|
2012-11-05 20:58:03 +04:00
|
|
|
// Skip the ones that are not in the null namespace
|
|
|
|
if (attrName->NamespaceID() != kNameSpaceID_None) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-20 21:23:49 +04:00
|
|
|
|
|
|
|
nsAutoString prop;
|
2013-05-11 02:57:58 +04:00
|
|
|
if (!AttrToDataProp(nsDependentAtomString(attrName->LocalName()), prop)) {
|
2011-05-20 21:23:49 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
aNames.AppendElement(prop);
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a dataset property name to the corresponding data attribute name.
|
|
|
|
* (ex. aBigFish to data-a-big-fish).
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsDOMStringMap::DataPropToAttr(const nsAString& aProp,
|
2013-05-11 02:57:58 +04:00
|
|
|
nsAutoString& aResult) {
|
|
|
|
// aResult is an autostring, so don't worry about setting its capacity:
|
|
|
|
// SetCapacity is slow even when it's a no-op and we already have enough
|
|
|
|
// storage there for most cases, probably.
|
|
|
|
aResult.AppendLiteral("data-");
|
2011-05-20 21:23:49 +04:00
|
|
|
|
|
|
|
// Iterate property by character to form attribute name.
|
|
|
|
// Return syntax error if there is a sequence of "-" followed by a character
|
|
|
|
// in the range "a" to "z".
|
|
|
|
// Replace capital characters with "-" followed by lower case character.
|
|
|
|
// Otherwise, simply append character to attribute name.
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* start = aProp.BeginReading();
|
|
|
|
const char16_t* end = aProp.EndReading();
|
|
|
|
const char16_t* cur = start;
|
2011-05-20 21:23:49 +04:00
|
|
|
for (; cur < end; ++cur) {
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* next = cur + 1;
|
|
|
|
if (char16_t('-') == *cur && next < end && char16_t('a') <= *next &&
|
|
|
|
*next <= char16_t('z')) {
|
2011-05-20 21:23:49 +04:00
|
|
|
// Syntax error if character following "-" is in range "a" to "z".
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
if (char16_t('A') <= *cur && *cur <= char16_t('Z')) {
|
2013-05-11 02:57:58 +04:00
|
|
|
// Append the characters in the range [start, cur)
|
|
|
|
aResult.Append(start, cur - start);
|
2011-05-20 21:23:49 +04:00
|
|
|
// Uncamel-case characters in the range of "A" to "Z".
|
2014-01-04 19:02:17 +04:00
|
|
|
aResult.Append(char16_t('-'));
|
2013-05-11 02:57:58 +04:00
|
|
|
aResult.Append(*cur - 'A' + 'a');
|
|
|
|
start = next; // We've already appended the thing at *cur
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 02:57:58 +04:00
|
|
|
aResult.Append(start, cur - start);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a data attribute name to the corresponding dataset property name.
|
|
|
|
* (ex. data-a-big-fish to aBigFish).
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsDOMStringMap::AttrToDataProp(const nsAString& aAttr,
|
2013-05-11 02:57:58 +04:00
|
|
|
nsAutoString& aResult) {
|
2011-05-20 21:23:49 +04:00
|
|
|
// If the attribute name does not begin with "data-" then it can not be
|
|
|
|
// a data attribute.
|
2020-07-01 11:29:29 +03:00
|
|
|
if (!StringBeginsWith(aAttr, u"data-"_ns)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start reading attribute from first character after "data-".
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* cur = aAttr.BeginReading() + 5;
|
|
|
|
const char16_t* end = aAttr.EndReading();
|
2011-05-20 21:23:49 +04:00
|
|
|
|
2013-05-11 02:57:58 +04:00
|
|
|
// Don't try to mess with aResult's capacity: the probably-no-op SetCapacity()
|
|
|
|
// call is not that fast.
|
2011-05-20 21:23:49 +04:00
|
|
|
|
|
|
|
// Iterate through attrName by character to form property name.
|
|
|
|
// If there is a sequence of "-" followed by a character in the range "a" to
|
|
|
|
// "z" then replace with upper case letter.
|
|
|
|
// Otherwise append character to property name.
|
|
|
|
for (; cur < end; ++cur) {
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* next = cur + 1;
|
|
|
|
if (char16_t('-') == *cur && next < end && char16_t('a') <= *next &&
|
|
|
|
*next <= char16_t('z')) {
|
2011-05-20 21:23:49 +04:00
|
|
|
// Upper case the lower case letters that follow a "-".
|
2013-05-11 02:57:58 +04:00
|
|
|
aResult.Append(*next - 'a' + 'A');
|
2011-05-20 21:23:49 +04:00
|
|
|
// Consume character to account for "-" character.
|
|
|
|
++cur;
|
|
|
|
} else {
|
|
|
|
// Simply append character if camel case is not necessary.
|
2013-05-11 02:57:58 +04:00
|
|
|
aResult.Append(*cur);
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-05-20 21:23:49 +04:00
|
|
|
}
|
2013-06-18 00:31:03 +04:00
|
|
|
|
2018-03-01 14:36:58 +03:00
|
|
|
void nsDOMStringMap::AttributeChanged(Element* aElement, int32_t aNameSpaceID,
|
|
|
|
nsAtom* aAttribute, int32_t aModType,
|
2015-07-25 09:01:19 +03:00
|
|
|
const nsAttrValue* aOldValue) {
|
2018-06-26 00:20:54 +03:00
|
|
|
if ((aModType == MutationEvent_Binding::ADDITION ||
|
|
|
|
aModType == MutationEvent_Binding::REMOVAL) &&
|
2013-06-18 00:31:03 +04:00
|
|
|
aNameSpaceID == kNameSpaceID_None &&
|
2020-07-01 11:29:29 +03:00
|
|
|
StringBeginsWith(nsDependentAtomString(aAttribute), u"data-"_ns)) {
|
2013-06-18 00:31:03 +04:00
|
|
|
++mExpandoAndGeneration.generation;
|
|
|
|
}
|
|
|
|
}
|