2011-05-20 10:23:49 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=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/. */
|
2011-05-20 10:23:49 -07:00
|
|
|
|
|
|
|
#include "nsDOMStringMap.h"
|
|
|
|
|
2013-08-21 22:28:12 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsError.h"
|
2011-05-20 10:23:49 -07:00
|
|
|
#include "nsGenericHTMLElement.h"
|
|
|
|
#include "nsContentUtils.h"
|
2012-11-05 08:58:03 -08:00
|
|
|
#include "mozilla/dom/DOMStringMapBinding.h"
|
2013-06-17 13:31:03 -07:00
|
|
|
#include "nsIDOMMutationEvent.h"
|
2011-05-20 10:23:49 -07:00
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2011-05-20 10:23:49 -07:00
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMStringMap)
|
|
|
|
|
2011-05-20 10:23:49 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMStringMap)
|
2012-10-16 18:09:42 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mElement)
|
2011-05-20 10:23:49 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-08-01 18:29:05 -07:00
|
|
|
|
2011-05-20 10:23:49 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMStringMap)
|
2012-10-16 18:09:42 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2011-07-25 12:24:59 -07: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-17 13:31:03 -07:00
|
|
|
tmp->mElement->RemoveMutationObserver(tmp);
|
2012-07-30 07:20:58 -07:00
|
|
|
tmp->mElement = nullptr;
|
2011-07-25 12:24:59 -07:00
|
|
|
}
|
2013-06-19 11:48:44 -07:00
|
|
|
tmp->mExpandoAndGeneration.Unlink();
|
2011-05-20 10:23:49 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2012-10-16 18:09:42 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsDOMStringMap)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
2013-06-17 13:31:03 -07:00
|
|
|
if (tmp->PreservingWrapper()) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mExpandoAndGeneration.expando);
|
|
|
|
}
|
2012-10-16 18:09:42 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2011-05-20 10:23:49 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMStringMap)
|
2012-10-16 18:09:42 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2013-06-17 13:31:03 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
2011-05-20 10:23:49 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMStringMap)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMStringMap)
|
|
|
|
|
|
|
|
nsDOMStringMap::nsDOMStringMap(nsGenericHTMLElement* aElement)
|
|
|
|
: mElement(aElement),
|
2011-10-17 07:59:28 -07:00
|
|
|
mRemovingProp(false)
|
2011-05-20 10:23:49 -07:00
|
|
|
{
|
2013-06-17 13:31:03 -07:00
|
|
|
mElement->AddMutationObserver(this);
|
2011-05-20 10:23:49 -07: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-17 13:31:03 -07:00
|
|
|
mElement->RemoveMutationObserver(this);
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
/* virtual */
|
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
nsDOMStringMap::WrapObject(JSContext *cx)
|
2011-05-20 10:23:49 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return DOMStringMapBinding::Wrap(cx, this);
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
void
|
|
|
|
nsDOMStringMap::NamedGetter(const nsAString& aProp, bool& found,
|
2013-05-10 15:57:58 -07:00
|
|
|
DOMString& aResult) const
|
2011-05-20 10:23:49 -07:00
|
|
|
{
|
|
|
|
nsAutoString attr;
|
|
|
|
|
|
|
|
if (!DataPropToAttr(aProp, attr)) {
|
2012-11-05 08:58:03 -08:00
|
|
|
found = false;
|
|
|
|
return;
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
|
2013-05-10 15:57:58 -07:00
|
|
|
found = mElement->GetAttr(attr, aResult);
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
|
2014-04-15 19:58:44 -07:00
|
|
|
bool
|
|
|
|
nsDOMStringMap::NameIsEnumerable(const nsAString& aName)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
void
|
|
|
|
nsDOMStringMap::NamedSetter(const nsAString& aProp,
|
|
|
|
const nsAString& aValue,
|
|
|
|
ErrorResult& rv)
|
2011-05-20 10:23:49 -07:00
|
|
|
{
|
|
|
|
nsAutoString attr;
|
2012-11-05 08:58:03 -08:00
|
|
|
if (!DataPropToAttr(aProp, attr)) {
|
|
|
|
rv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2011-05-20 10:23:49 -07:00
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
nsresult res = nsContentUtils::CheckQName(attr, false);
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
rv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2011-05-20 10:23:49 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> attrAtom = do_GetAtom(attr);
|
2012-11-05 08:58:03 -08:00
|
|
|
MOZ_ASSERT(attrAtom, "Should be infallible");
|
2011-05-20 10:23:49 -07:00
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
res = mElement->SetAttr(kNameSpaceID_None, attrAtom, aValue, true);
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
rv.Throw(res);
|
|
|
|
}
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
void
|
|
|
|
nsDOMStringMap::NamedDeleter(const nsAString& aProp, bool& found)
|
2011-05-20 10:23:49 -07:00
|
|
|
{
|
|
|
|
// Currently removing property, attribute is already removed.
|
|
|
|
if (mRemovingProp) {
|
2014-11-04 01:11:08 -08:00
|
|
|
found = false;
|
2011-05-20 10:23:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString attr;
|
|
|
|
if (!DataPropToAttr(aProp, attr)) {
|
2014-11-04 01:11:08 -08:00
|
|
|
found = false;
|
2011-05-20 10:23:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> attrAtom = do_GetAtom(attr);
|
2012-11-05 08:58:03 -08:00
|
|
|
MOZ_ASSERT(attrAtom, "Should be infallible");
|
2011-05-20 10:23:49 -07:00
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
found = mElement->HasAttr(kNameSpaceID_None, attrAtom);
|
2011-05-20 10:23:49 -07:00
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
if (found) {
|
|
|
|
mRemovingProp = true;
|
|
|
|
mElement->UnsetAttr(kNameSpaceID_None, attrAtom, true);
|
|
|
|
mRemovingProp = false;
|
|
|
|
}
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
void
|
2014-04-15 19:58:44 -07:00
|
|
|
nsDOMStringMap::GetSupportedNames(unsigned, nsTArray<nsString>& aNames)
|
2011-05-20 10:23:49 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t attrCount = mElement->GetAttrCount();
|
2011-05-20 10:23:49 -07:00
|
|
|
|
|
|
|
// Iterate through all the attributes and add property
|
|
|
|
// names corresponding to data attributes to return array.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < attrCount; ++i) {
|
2011-05-20 10:23:49 -07:00
|
|
|
const nsAttrName* attrName = mElement->GetAttrNameAt(i);
|
2012-11-05 08:58:03 -08:00
|
|
|
// Skip the ones that are not in the null namespace
|
|
|
|
if (attrName->NamespaceID() != kNameSpaceID_None) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-20 10:23:49 -07:00
|
|
|
|
|
|
|
nsAutoString prop;
|
2013-05-10 15:57:58 -07:00
|
|
|
if (!AttrToDataProp(nsDependentAtomString(attrName->LocalName()),
|
|
|
|
prop)) {
|
2011-05-20 10:23:49 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
aNames.AppendElement(prop);
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a dataset property name to the corresponding data attribute name.
|
|
|
|
* (ex. aBigFish to data-a-big-fish).
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsDOMStringMap::DataPropToAttr(const nsAString& aProp,
|
2013-05-10 15:57:58 -07:00
|
|
|
nsAutoString& aResult)
|
2011-05-20 10:23:49 -07:00
|
|
|
{
|
2013-05-10 15:57:58 -07:00
|
|
|
// 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 10:23:49 -07: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 07:02:17 -08:00
|
|
|
const char16_t* start = aProp.BeginReading();
|
|
|
|
const char16_t* end = aProp.EndReading();
|
|
|
|
const char16_t* cur = start;
|
2011-05-20 10:23:49 -07:00
|
|
|
for (; cur < end; ++cur) {
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* next = cur + 1;
|
|
|
|
if (char16_t('-') == *cur && next < end &&
|
|
|
|
char16_t('a') <= *next && *next <= char16_t('z')) {
|
2011-05-20 10:23:49 -07:00
|
|
|
// Syntax error if character following "-" is in range "a" to "z".
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
if (char16_t('A') <= *cur && *cur <= char16_t('Z')) {
|
2013-05-10 15:57:58 -07:00
|
|
|
// Append the characters in the range [start, cur)
|
|
|
|
aResult.Append(start, cur - start);
|
2011-05-20 10:23:49 -07:00
|
|
|
// Uncamel-case characters in the range of "A" to "Z".
|
2014-01-04 07:02:17 -08:00
|
|
|
aResult.Append(char16_t('-'));
|
2013-05-10 15:57:58 -07:00
|
|
|
aResult.Append(*cur - 'A' + 'a');
|
|
|
|
start = next; // We've already appended the thing at *cur
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 15:57:58 -07:00
|
|
|
aResult.Append(start, cur - start);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a data attribute name to the corresponding dataset property name.
|
|
|
|
* (ex. data-a-big-fish to aBigFish).
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsDOMStringMap::AttrToDataProp(const nsAString& aAttr,
|
2013-05-10 15:57:58 -07:00
|
|
|
nsAutoString& aResult)
|
2011-05-20 10:23:49 -07:00
|
|
|
{
|
|
|
|
// If the attribute name does not begin with "data-" then it can not be
|
|
|
|
// a data attribute.
|
|
|
|
if (!StringBeginsWith(aAttr, NS_LITERAL_STRING("data-"))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start reading attribute from first character after "data-".
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* cur = aAttr.BeginReading() + 5;
|
|
|
|
const char16_t* end = aAttr.EndReading();
|
2011-05-20 10:23:49 -07:00
|
|
|
|
2013-05-10 15:57:58 -07:00
|
|
|
// Don't try to mess with aResult's capacity: the probably-no-op SetCapacity()
|
|
|
|
// call is not that fast.
|
2011-05-20 10:23:49 -07: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 07:02:17 -08:00
|
|
|
const char16_t* next = cur + 1;
|
|
|
|
if (char16_t('-') == *cur && next < end &&
|
|
|
|
char16_t('a') <= *next && *next <= char16_t('z')) {
|
2011-05-20 10:23:49 -07:00
|
|
|
// Upper case the lower case letters that follow a "-".
|
2013-05-10 15:57:58 -07:00
|
|
|
aResult.Append(*next - 'a' + 'A');
|
2011-05-20 10:23:49 -07:00
|
|
|
// Consume character to account for "-" character.
|
|
|
|
++cur;
|
|
|
|
} else {
|
|
|
|
// Simply append character if camel case is not necessary.
|
2013-05-10 15:57:58 -07:00
|
|
|
aResult.Append(*cur);
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2011-05-20 10:23:49 -07:00
|
|
|
}
|
2013-06-17 13:31:03 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMStringMap::AttributeChanged(nsIDocument *aDocument, Element* aElement,
|
|
|
|
int32_t aNameSpaceID, nsIAtom* aAttribute,
|
|
|
|
int32_t aModType)
|
|
|
|
{
|
|
|
|
if ((aModType == nsIDOMMutationEvent::ADDITION ||
|
|
|
|
aModType == nsIDOMMutationEvent::REMOVAL) &&
|
|
|
|
aNameSpaceID == kNameSpaceID_None &&
|
|
|
|
StringBeginsWith(nsDependentAtomString(aAttribute),
|
|
|
|
NS_LITERAL_STRING("data-"))) {
|
|
|
|
++mExpandoAndGeneration.generation;
|
|
|
|
}
|
|
|
|
}
|