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/. */
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2006-03-30 12:03:04 +04:00
|
|
|
/*
|
|
|
|
* A struct that represents the value (type and actual data) of an
|
|
|
|
* attribute.
|
|
|
|
*/
|
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "mozilla/HashFunctions.h"
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
#include "nsAttrValue.h"
|
2012-09-30 20:40:24 +04:00
|
|
|
#include "nsAttrValueInlines.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2004-01-26 22:22:05 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
2018-01-29 21:55:38 +03:00
|
|
|
#include "mozilla/CORSMode.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-10-24 12:16:46 +03:00
|
|
|
#include "mozilla/ServoBindingTypes.h"
|
2018-01-30 21:29:14 +03:00
|
|
|
#include "mozilla/ServoUtils.h"
|
2019-11-06 21:03:51 +03:00
|
|
|
#include "mozilla/ShadowParts.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/SVGAttrValueWrapper.h"
|
2018-05-30 19:15:25 +03:00
|
|
|
#include "mozilla/DeclarationBlock.h"
|
2020-09-21 12:57:46 +03:00
|
|
|
#include "mozilla/dom/CSSRuleBinding.h"
|
2008-06-11 04:27:00 +04:00
|
|
|
#include "nsContentUtils.h"
|
2008-09-26 14:39:18 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2012-09-30 20:40:24 +04:00
|
|
|
#include "nsHTMLCSSStyleSheet.h"
|
|
|
|
#include "nsStyledElement.h"
|
2013-09-07 17:01:08 +04:00
|
|
|
#include "nsIURI.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2019-11-05 23:22:40 +03:00
|
|
|
#include "ReferrerInfo.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2004-03-09 11:44:53 +03:00
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
using namespace mozilla;
|
2012-02-16 03:40:46 +04:00
|
|
|
|
2009-03-10 16:51:34 +03:00
|
|
|
#define MISC_STR_PTR(_cont) \
|
|
|
|
reinterpret_cast<void*>((_cont)->mStringBits & NS_ATTRVALUE_POINTERVALUE_MASK)
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
MiscContainer* nsAttrValue::AllocMiscContainer() {
|
2018-07-24 12:10:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MiscContainer* cont = nullptr;
|
2020-01-20 19:17:06 +03:00
|
|
|
std::swap(cont, sMiscContainerCache);
|
2018-07-24 12:10:00 +03:00
|
|
|
|
|
|
|
if (cont) {
|
|
|
|
return new (cont) MiscContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MiscContainer;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void nsAttrValue::DeallocMiscContainer(MiscContainer* aCont) {
|
2018-07-24 12:10:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!aCont) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sMiscContainerCache) {
|
|
|
|
aCont->~MiscContainer();
|
|
|
|
sMiscContainerCache = aCont;
|
|
|
|
} else {
|
|
|
|
delete aCont;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-05 00:31:50 +04:00
|
|
|
bool MiscContainer::GetString(nsAString& aString) const {
|
2012-09-30 20:40:24 +04:00
|
|
|
void* ptr = MISC_STR_PTR(this);
|
|
|
|
|
|
|
|
if (!ptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (static_cast<nsAttrValue::ValueBaseType>(mStringBits &
|
|
|
|
NS_ATTRVALUE_BASETYPE_MASK) ==
|
|
|
|
nsAttrValue::eStringBase) {
|
|
|
|
nsStringBuffer* buffer = static_cast<nsStringBuffer*>(ptr);
|
|
|
|
if (!buffer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
buffer->ToString(buffer->StorageSize() / sizeof(char16_t) - 1, aString);
|
2012-09-30 20:40:24 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* atom = static_cast<nsAtom*>(ptr);
|
2012-09-30 20:40:24 +04:00
|
|
|
if (!atom) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-05 00:31:50 +04:00
|
|
|
atom->ToString(aString);
|
2012-09-30 20:40:24 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MiscContainer::Cache() {
|
|
|
|
// Not implemented for anything else yet.
|
2016-10-18 07:29:03 +03:00
|
|
|
if (mType != nsAttrValue::eCSSDeclaration) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected cached nsAttrValue type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
MOZ_ASSERT(IsRefCounted());
|
|
|
|
MOZ_ASSERT(mValue.mRefCount > 0);
|
|
|
|
MOZ_ASSERT(!mValue.mCached);
|
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
nsHTMLCSSStyleSheet* sheet = mValue.mCSSDeclaration->GetHTMLCSSStyleSheet();
|
2012-09-30 20:40:24 +04:00
|
|
|
if (!sheet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-05 00:31:50 +04:00
|
|
|
nsString str;
|
2012-09-30 20:40:24 +04:00
|
|
|
bool gotString = GetString(str);
|
|
|
|
if (!gotString) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sheet->CacheStyleAttr(str, this);
|
|
|
|
mValue.mCached = 1;
|
|
|
|
|
|
|
|
// This has to be immutable once it goes into the cache.
|
2016-10-18 07:29:03 +03:00
|
|
|
mValue.mCSSDeclaration->SetImmutable();
|
2012-09-30 20:40:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MiscContainer::Evict() {
|
|
|
|
// Not implemented for anything else yet.
|
2016-10-18 07:29:03 +03:00
|
|
|
if (mType != nsAttrValue::eCSSDeclaration) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected cached nsAttrValue type");
|
|
|
|
return;
|
|
|
|
}
|
2012-09-30 20:40:24 +04:00
|
|
|
MOZ_ASSERT(IsRefCounted());
|
|
|
|
MOZ_ASSERT(mValue.mRefCount == 0);
|
|
|
|
|
|
|
|
if (!mValue.mCached) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
nsHTMLCSSStyleSheet* sheet = mValue.mCSSDeclaration->GetHTMLCSSStyleSheet();
|
2012-09-30 20:40:24 +04:00
|
|
|
MOZ_ASSERT(sheet);
|
|
|
|
|
2013-01-05 00:31:50 +04:00
|
|
|
nsString str;
|
2012-09-30 20:40:24 +04:00
|
|
|
DebugOnly<bool> gotString = GetString(str);
|
|
|
|
MOZ_ASSERT(gotString);
|
|
|
|
|
|
|
|
sheet->EvictStyleAttr(str, this);
|
|
|
|
mValue.mCached = 0;
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsTArray<const nsAttrValue::EnumTable*>* nsAttrValue::sEnumTableArray = nullptr;
|
2018-07-24 12:10:00 +03:00
|
|
|
MiscContainer* nsAttrValue::sMiscContainerCache = nullptr;
|
2005-01-25 03:02:58 +03:00
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
nsAttrValue::nsAttrValue() : mBits(0) {}
|
|
|
|
|
|
|
|
nsAttrValue::nsAttrValue(const nsAttrValue& aOther) : mBits(0) {
|
|
|
|
SetTo(aOther);
|
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
nsAttrValue::nsAttrValue(const nsAString& aValue) : mBits(0) { SetTo(aValue); }
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAttrValue::nsAttrValue(nsAtom* aValue) : mBits(0) { SetTo(aValue); }
|
2012-02-16 03:40:44 +04:00
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
nsAttrValue::nsAttrValue(already_AddRefed<DeclarationBlock> aValue,
|
|
|
|
const nsAString* aSerialized)
|
2004-01-15 20:07:27 +03:00
|
|
|
: mBits(0) {
|
2018-05-30 22:15:35 +03:00
|
|
|
SetTo(std::move(aValue), aSerialized);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2010-06-25 06:01:07 +04:00
|
|
|
nsAttrValue::nsAttrValue(const nsIntMargin& aValue) : mBits(0) {
|
|
|
|
SetTo(aValue);
|
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
nsAttrValue::~nsAttrValue() { ResetIfSet(); }
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2005-01-25 03:02:58 +03:00
|
|
|
/* static */
|
2019-11-27 13:57:24 +03:00
|
|
|
void nsAttrValue::Init() {
|
|
|
|
MOZ_ASSERT(!sEnumTableArray, "nsAttrValue already initialized");
|
2011-08-10 09:36:00 +04:00
|
|
|
sEnumTableArray = new nsTArray<const EnumTable*>;
|
2005-01-25 03:02:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void nsAttrValue::Shutdown() {
|
2018-07-24 12:10:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2005-01-25 03:02:58 +03:00
|
|
|
delete sEnumTableArray;
|
2012-07-30 18:20:58 +04:00
|
|
|
sEnumTableArray = nullptr;
|
2018-07-24 12:10:00 +03:00
|
|
|
// The MiscContainer pointed to by sMiscContainerCache has already
|
|
|
|
// be destructed so `delete sMiscContainerCache` is
|
|
|
|
// dangerous. Invoke `operator delete` to free the memory.
|
|
|
|
::operator delete(sMiscContainerCache);
|
|
|
|
sMiscContainerCache = nullptr;
|
2005-01-25 03:02:58 +03:00
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
void nsAttrValue::Reset() {
|
2004-03-04 05:06:28 +03:00
|
|
|
switch (BaseType()) {
|
|
|
|
case eStringBase: {
|
2007-07-08 11:08:04 +04:00
|
|
|
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
|
2004-01-15 20:07:27 +03:00
|
|
|
if (str) {
|
2005-02-04 02:49:13 +03:00
|
|
|
str->Release();
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
break;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eOtherBase: {
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = GetMiscContainer();
|
|
|
|
if (cont->IsRefCounted() && cont->mValue.mRefCount > 1) {
|
|
|
|
NS_RELEASE(cont);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-07-24 12:10:00 +03:00
|
|
|
DeallocMiscContainer(ClearMiscContainer());
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
break;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eAtomBase: {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* atom = GetAtomValue();
|
2004-01-26 22:22:05 +03:00
|
|
|
NS_RELEASE(atom);
|
2004-03-04 05:06:28 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eIntegerBase: {
|
2004-01-26 22:22:05 +03:00
|
|
|
break;
|
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mBits = 0;
|
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
void nsAttrValue::SetTo(const nsAttrValue& aOther) {
|
2012-02-14 06:00:57 +04:00
|
|
|
if (this == &aOther) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
switch (aOther.BaseType()) {
|
|
|
|
case eStringBase: {
|
2005-02-04 02:49:13 +03:00
|
|
|
ResetIfSet();
|
2007-07-08 11:08:04 +04:00
|
|
|
nsStringBuffer* str = static_cast<nsStringBuffer*>(aOther.GetPtr());
|
2005-02-04 02:49:13 +03:00
|
|
|
if (str) {
|
|
|
|
str->AddRef();
|
|
|
|
SetPtrValueAndType(str, eStringBase);
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case eOtherBase: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eAtomBase: {
|
|
|
|
ResetIfSet();
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* atom = aOther.GetAtomValue();
|
2004-03-04 05:06:28 +03:00
|
|
|
NS_ADDREF(atom);
|
|
|
|
SetPtrValueAndType(atom, eAtomBase);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case eIntegerBase: {
|
|
|
|
ResetIfSet();
|
|
|
|
mBits = aOther.mBits;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MiscContainer* otherCont = aOther.GetMiscContainer();
|
2012-09-30 20:40:24 +04:00
|
|
|
if (otherCont->IsRefCounted()) {
|
2018-07-24 12:10:00 +03:00
|
|
|
DeallocMiscContainer(ClearMiscContainer());
|
2012-09-30 20:40:24 +04:00
|
|
|
NS_ADDREF(otherCont);
|
|
|
|
SetPtrValueAndType(otherCont, eOtherBase);
|
2008-09-26 14:39:18 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
2004-03-04 05:06:28 +03:00
|
|
|
switch (otherCont->mType) {
|
2008-09-26 14:39:18 +04:00
|
|
|
case eInteger: {
|
2012-09-30 20:40:24 +04:00
|
|
|
cont->mValue.mInteger = otherCont->mValue.mInteger;
|
2008-09-26 14:39:18 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eEnum: {
|
2012-09-30 20:40:24 +04:00
|
|
|
cont->mValue.mEnumValue = otherCont->mValue.mEnumValue;
|
2008-09-26 14:39:18 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ePercent: {
|
2019-06-29 02:26:09 +03:00
|
|
|
cont->mDoubleValue = otherCont->mDoubleValue;
|
2008-09-26 14:39:18 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eColor: {
|
2012-09-30 20:40:24 +04:00
|
|
|
cont->mValue.mColor = otherCont->mValue.mColor;
|
2004-01-26 22:22:05 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-11-06 21:03:51 +03:00
|
|
|
case eShadowParts:
|
2016-10-18 07:29:03 +03:00
|
|
|
case eCSSDeclaration: {
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("These should be refcounted!");
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
2012-08-24 21:50:49 +04:00
|
|
|
case eURL: {
|
2012-09-30 20:40:24 +04:00
|
|
|
NS_ADDREF(cont->mValue.mURL = otherCont->mValue.mURL);
|
2012-08-24 21:50:49 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eAtomArray: {
|
2020-04-24 16:31:14 +03:00
|
|
|
if (!EnsureEmptyAtomArray()) {
|
2004-03-04 05:06:28 +03:00
|
|
|
Reset();
|
2008-09-26 14:39:18 +04:00
|
|
|
return;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2020-04-24 16:31:14 +03:00
|
|
|
// XXX(Bug 1631371) Check if this should use a fallible operation as it
|
|
|
|
// pretended earlier.
|
|
|
|
GetAtomArrayValue()->AppendElements(*otherCont->mValue.mAtomArray);
|
2004-03-04 05:06:28 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-03-25 17:43:53 +03:00
|
|
|
case eDoubleValue: {
|
|
|
|
cont->mDoubleValue = otherCont->mDoubleValue;
|
2009-03-10 16:51:34 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-06-25 06:01:07 +04:00
|
|
|
case eIntMarginValue: {
|
2019-11-06 21:03:51 +03:00
|
|
|
if (otherCont->mValue.mIntMargin) {
|
2012-09-30 20:40:24 +04:00
|
|
|
cont->mValue.mIntMargin =
|
|
|
|
new nsIntMargin(*otherCont->mValue.mIntMargin);
|
2019-11-06 21:03:51 +03:00
|
|
|
}
|
2010-06-25 06:01:07 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
default: {
|
2012-02-16 03:40:46 +04:00
|
|
|
if (IsSVGType(otherCont->mType)) {
|
|
|
|
// All SVG types are just pointers to classes and will therefore have
|
|
|
|
// the same size so it doesn't really matter which one we assign
|
2019-02-15 11:41:37 +03:00
|
|
|
cont->mValue.mSVGLength = otherCont->mValue.mSVGLength;
|
2012-02-16 03:40:46 +04:00
|
|
|
} else {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("unknown type stored in MiscContainer");
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-09-26 14:39:18 +04:00
|
|
|
|
2009-03-10 16:51:34 +03:00
|
|
|
void* otherPtr = MISC_STR_PTR(otherCont);
|
2008-09-26 14:39:18 +04:00
|
|
|
if (otherPtr) {
|
|
|
|
if (static_cast<ValueBaseType>(otherCont->mStringBits &
|
|
|
|
NS_ATTRVALUE_BASETYPE_MASK) == eStringBase) {
|
|
|
|
static_cast<nsStringBuffer*>(otherPtr)->AddRef();
|
|
|
|
} else {
|
2017-10-03 01:05:19 +03:00
|
|
|
static_cast<nsAtom*>(otherPtr)->AddRef();
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
2018-01-30 21:29:14 +03:00
|
|
|
cont->SetStringBitsMainThread(otherCont->mStringBits);
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
// Note, set mType after switch-case, otherwise EnsureEmptyAtomArray doesn't
|
|
|
|
// work correctly.
|
|
|
|
cont->mType = otherCont->mType;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
void nsAttrValue::SetTo(const nsAString& aValue) {
|
2005-02-04 02:49:13 +03:00
|
|
|
ResetIfSet();
|
2014-03-15 23:00:15 +04:00
|
|
|
nsStringBuffer* buf = GetStringBuffer(aValue).take();
|
2008-09-26 14:39:18 +04:00
|
|
|
if (buf) {
|
2005-02-04 02:49:13 +03:00
|
|
|
SetPtrValueAndType(buf, eStringBase);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
void nsAttrValue::SetTo(nsAtom* aValue) {
|
2012-02-16 03:40:44 +04:00
|
|
|
ResetIfSet();
|
|
|
|
if (aValue) {
|
|
|
|
NS_ADDREF(aValue);
|
|
|
|
SetPtrValueAndType(aValue, eAtomBase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void nsAttrValue::SetTo(int16_t aInt) {
|
2004-03-04 05:06:28 +03:00
|
|
|
ResetIfSet();
|
2012-07-30 18:20:58 +04:00
|
|
|
SetIntValueAndType(aInt, eInteger, nullptr);
|
2004-02-10 12:08:06 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void nsAttrValue::SetTo(int32_t aInt, const nsAString* aSerialized) {
|
2012-02-16 03:40:45 +04:00
|
|
|
ResetIfSet();
|
|
|
|
SetIntValueAndType(aInt, eInteger, aSerialized);
|
|
|
|
}
|
|
|
|
|
2012-02-16 03:40:45 +04:00
|
|
|
void nsAttrValue::SetTo(double aValue, const nsAString* aSerialized) {
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
cont->mDoubleValue = aValue;
|
|
|
|
cont->mType = eDoubleValue;
|
|
|
|
SetMiscAtomOrString(aSerialized);
|
2012-02-16 03:40:45 +04:00
|
|
|
}
|
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
void nsAttrValue::SetTo(already_AddRefed<DeclarationBlock> aValue,
|
2016-06-24 06:35:12 +03:00
|
|
|
const nsAString* aSerialized) {
|
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
MOZ_ASSERT(cont->mValue.mRefCount == 0);
|
2016-10-18 07:29:03 +03:00
|
|
|
cont->mValue.mCSSDeclaration = aValue.take();
|
|
|
|
cont->mType = eCSSDeclaration;
|
2016-06-24 06:35:12 +03:00
|
|
|
NS_ADDREF(cont);
|
|
|
|
SetMiscAtomOrString(aSerialized);
|
|
|
|
MOZ_ASSERT(cont->mValue.mRefCount == 1);
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:54:57 +03:00
|
|
|
void nsAttrValue::SetTo(nsIURI* aValue, const nsAString* aSerialized) {
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
NS_ADDREF(cont->mValue.mURL = aValue);
|
|
|
|
cont->mType = eURL;
|
|
|
|
SetMiscAtomOrString(aSerialized);
|
2012-08-24 21:50:49 +04:00
|
|
|
}
|
|
|
|
|
2010-06-25 06:01:07 +04:00
|
|
|
void nsAttrValue::SetTo(const nsIntMargin& aValue) {
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
cont->mValue.mIntMargin = new nsIntMargin(aValue);
|
|
|
|
cont->mType = eIntMarginValue;
|
2010-06-25 06:01:07 +04:00
|
|
|
}
|
|
|
|
|
2012-02-14 06:00:56 +04:00
|
|
|
void nsAttrValue::SetToSerialized(const nsAttrValue& aOther) {
|
|
|
|
if (aOther.Type() != nsAttrValue::eString &&
|
|
|
|
aOther.Type() != nsAttrValue::eAtom) {
|
|
|
|
nsAutoString val;
|
|
|
|
aOther.ToString(val);
|
|
|
|
SetTo(val);
|
|
|
|
} else {
|
|
|
|
SetTo(aOther);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 20:40:56 +03:00
|
|
|
void nsAttrValue::SetTo(const SVGAnimatedOrient& aValue,
|
|
|
|
const nsAString* aSerialized) {
|
2019-02-15 11:41:37 +03:00
|
|
|
SetSVGType(eSVGOrient, &aValue, aSerialized);
|
2012-02-16 03:40:45 +04:00
|
|
|
}
|
|
|
|
|
2019-04-04 20:40:56 +03:00
|
|
|
void nsAttrValue::SetTo(const SVGAnimatedIntegerPair& aValue,
|
2012-02-16 03:40:45 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGIntegerPair, &aValue, aSerialized);
|
2012-02-16 03:40:45 +04:00
|
|
|
}
|
|
|
|
|
2019-04-09 23:04:33 +03:00
|
|
|
void nsAttrValue::SetTo(const SVGAnimatedLength& aValue,
|
2012-02-16 03:40:45 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGLength, &aValue, aSerialized);
|
2012-02-16 03:40:45 +04:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
void nsAttrValue::SetTo(const SVGLengthList& aValue,
|
2012-02-16 03:40:45 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
// While an empty string will parse as a length list, there's no need to store
|
|
|
|
// it (and SetMiscAtomOrString will assert if we try)
|
|
|
|
if (aSerialized && aSerialized->IsEmpty()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
aSerialized = nullptr;
|
2012-02-16 03:40:45 +04:00
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGLengthList, &aValue, aSerialized);
|
2012-02-16 03:40:45 +04:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
void nsAttrValue::SetTo(const SVGNumberList& aValue,
|
2012-02-16 03:40:46 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
// While an empty string will parse as a number list, there's no need to store
|
|
|
|
// it (and SetMiscAtomOrString will assert if we try)
|
|
|
|
if (aSerialized && aSerialized->IsEmpty()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
aSerialized = nullptr;
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGNumberList, &aValue, aSerialized);
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
|
|
|
|
2019-04-04 20:40:56 +03:00
|
|
|
void nsAttrValue::SetTo(const SVGAnimatedNumberPair& aValue,
|
2012-02-16 03:40:45 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGNumberPair, &aValue, aSerialized);
|
2012-02-16 03:40:45 +04:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
void nsAttrValue::SetTo(const SVGPathData& aValue,
|
2012-02-16 03:40:46 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
// While an empty string will parse as path data, there's no need to store it
|
|
|
|
// (and SetMiscAtomOrString will assert if we try)
|
|
|
|
if (aSerialized && aSerialized->IsEmpty()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
aSerialized = nullptr;
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGPathData, &aValue, aSerialized);
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
void nsAttrValue::SetTo(const SVGPointList& aValue,
|
2012-02-16 03:40:46 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
// While an empty string will parse as a point list, there's no need to store
|
|
|
|
// it (and SetMiscAtomOrString will assert if we try)
|
|
|
|
if (aSerialized && aSerialized->IsEmpty()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
aSerialized = nullptr;
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGPointList, &aValue, aSerialized);
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
void nsAttrValue::SetTo(const SVGAnimatedPreserveAspectRatio& aValue,
|
2012-02-16 03:40:46 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGPreserveAspectRatio, &aValue, aSerialized);
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
void nsAttrValue::SetTo(const SVGStringList& aValue,
|
2012-02-16 03:40:46 +04:00
|
|
|
const nsAString* aSerialized) {
|
|
|
|
// While an empty string will parse as a string list, there's no need to store
|
|
|
|
// it (and SetMiscAtomOrString will assert if we try)
|
|
|
|
if (aSerialized && aSerialized->IsEmpty()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
aSerialized = nullptr;
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
|
|
|
SetSVGType(eSVGStringList, &aValue, aSerialized);
|
|
|
|
}
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
void nsAttrValue::SetTo(const SVGTransformList& aValue,
|
2012-02-16 03:40:46 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
// While an empty string will parse as a transform list, there's no need to
|
|
|
|
// store it (and SetMiscAtomOrString will assert if we try)
|
|
|
|
if (aSerialized && aSerialized->IsEmpty()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
aSerialized = nullptr;
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGTransformList, &aValue, aSerialized);
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
|
|
|
|
2019-04-04 20:40:56 +03:00
|
|
|
void nsAttrValue::SetTo(const SVGAnimatedViewBox& aValue,
|
2012-02-16 03:40:46 +04:00
|
|
|
const nsAString* aSerialized) {
|
2012-02-16 03:40:46 +04:00
|
|
|
SetSVGType(eSVGViewBox, &aValue, aSerialized);
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
void nsAttrValue::SwapValueWith(nsAttrValue& aOther) {
|
2012-10-02 12:24:11 +04:00
|
|
|
uintptr_t tmp = aOther.mBits;
|
2004-01-26 22:22:05 +03:00
|
|
|
aOther.mBits = mBits;
|
|
|
|
mBits = tmp;
|
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
void nsAttrValue::ToString(nsAString& aResult) const {
|
2012-07-30 18:20:58 +04:00
|
|
|
MiscContainer* cont = nullptr;
|
2008-09-26 14:39:18 +04:00
|
|
|
if (BaseType() == eOtherBase) {
|
|
|
|
cont = GetMiscContainer();
|
2012-09-30 20:40:24 +04:00
|
|
|
|
2013-01-05 00:31:50 +04:00
|
|
|
if (cont->GetString(aResult)) {
|
2012-09-30 20:40:24 +04:00
|
|
|
return;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
switch (Type()) {
|
2004-01-15 20:07:27 +03:00
|
|
|
case eString: {
|
2007-07-08 11:08:04 +04:00
|
|
|
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
|
2004-01-15 20:07:27 +03:00
|
|
|
if (str) {
|
2014-01-04 19:02:17 +04:00
|
|
|
str->ToString(str->StorageSize() / sizeof(char16_t) - 1, aResult);
|
2004-01-26 22:22:05 +03:00
|
|
|
} else {
|
|
|
|
aResult.Truncate();
|
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
break;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eAtom: {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* atom = static_cast<nsAtom*>(GetPtr());
|
2005-11-22 00:05:32 +03:00
|
|
|
atom->ToString(aResult);
|
2004-03-04 05:06:28 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eInteger: {
|
|
|
|
nsAutoString intStr;
|
2008-09-26 14:39:18 +04:00
|
|
|
intStr.AppendInt(GetIntegerValue());
|
2004-03-04 05:06:28 +03:00
|
|
|
aResult = intStr;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2009-08-19 19:05:14 +04:00
|
|
|
#ifdef DEBUG
|
2004-03-04 05:06:28 +03:00
|
|
|
case eColor: {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("color attribute without string data");
|
2009-08-19 19:05:14 +04:00
|
|
|
aResult.Truncate();
|
2004-03-04 05:06:28 +03:00
|
|
|
break;
|
|
|
|
}
|
2009-08-19 19:05:14 +04:00
|
|
|
#endif
|
2004-03-04 05:06:28 +03:00
|
|
|
case eEnum: {
|
2011-10-17 18:59:28 +04:00
|
|
|
GetEnumString(aResult, false);
|
2004-03-04 05:06:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ePercent: {
|
2019-06-29 02:26:09 +03:00
|
|
|
nsAutoString str;
|
|
|
|
if (cont) {
|
|
|
|
str.AppendFloat(cont->mDoubleValue);
|
|
|
|
} else {
|
|
|
|
str.AppendInt(GetIntInternal());
|
|
|
|
}
|
|
|
|
aResult = str + u"%"_ns;
|
2004-03-04 05:06:28 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2016-10-18 07:29:03 +03:00
|
|
|
case eCSSDeclaration: {
|
2004-03-09 03:51:56 +03:00
|
|
|
aResult.Truncate();
|
2005-11-22 00:05:32 +03:00
|
|
|
MiscContainer* container = GetMiscContainer();
|
2016-11-07 07:28:19 +03:00
|
|
|
if (DeclarationBlock* decl = container->mValue.mCSSDeclaration) {
|
|
|
|
decl->ToString(aResult);
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2017-01-31 08:19:19 +03:00
|
|
|
|
2018-01-30 21:29:14 +03:00
|
|
|
// This can be reached during parallel selector matching with attribute
|
|
|
|
// selectors on the style attribute. SetMiscAtomOrString handles this
|
|
|
|
// case, and as of this writing this is the only consumer that needs it.
|
|
|
|
const_cast<nsAttrValue*>(this)->SetMiscAtomOrString(&aResult);
|
2004-03-04 05:06:28 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-03-25 17:43:53 +03:00
|
|
|
case eDoubleValue: {
|
2010-11-30 21:18:15 +03:00
|
|
|
aResult.Truncate();
|
2011-03-25 17:43:53 +03:00
|
|
|
aResult.AppendFloat(GetDoubleValue());
|
2008-09-26 14:39:18 +04:00
|
|
|
break;
|
|
|
|
}
|
2019-02-15 11:41:37 +03:00
|
|
|
case eSVGIntegerPair: {
|
2019-04-04 20:40:56 +03:00
|
|
|
SVGAttrValueWrapper::ToString(
|
|
|
|
GetMiscContainer()->mValue.mSVGAnimatedIntegerPair, aResult);
|
2012-02-16 03:40:45 +04:00
|
|
|
break;
|
|
|
|
}
|
2019-02-15 11:41:37 +03:00
|
|
|
case eSVGOrient: {
|
2019-04-04 20:40:56 +03:00
|
|
|
SVGAttrValueWrapper::ToString(
|
|
|
|
GetMiscContainer()->mValue.mSVGAnimatedOrient, aResult);
|
2012-02-16 03:40:45 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:45 +04:00
|
|
|
case eSVGLength: {
|
2012-09-30 20:40:24 +04:00
|
|
|
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGLength,
|
|
|
|
aResult);
|
2012-02-16 03:40:45 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:45 +04:00
|
|
|
case eSVGLengthList: {
|
2012-09-30 20:40:24 +04:00
|
|
|
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGLengthList,
|
2012-02-16 03:40:46 +04:00
|
|
|
aResult);
|
2012-02-16 03:40:45 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
case eSVGNumberList: {
|
2012-09-30 20:40:24 +04:00
|
|
|
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGNumberList,
|
2012-02-16 03:40:46 +04:00
|
|
|
aResult);
|
2012-02-16 03:40:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:45 +04:00
|
|
|
case eSVGNumberPair: {
|
2019-04-04 20:40:56 +03:00
|
|
|
SVGAttrValueWrapper::ToString(
|
|
|
|
GetMiscContainer()->mValue.mSVGAnimatedNumberPair, aResult);
|
2012-02-16 03:40:45 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
case eSVGPathData: {
|
2012-09-30 20:40:24 +04:00
|
|
|
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGPathData,
|
|
|
|
aResult);
|
2012-02-16 03:40:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
case eSVGPointList: {
|
2012-09-30 20:40:24 +04:00
|
|
|
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGPointList,
|
|
|
|
aResult);
|
2012-02-16 03:40:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
case eSVGPreserveAspectRatio: {
|
2012-09-30 20:40:24 +04:00
|
|
|
SVGAttrValueWrapper::ToString(
|
2019-04-04 20:40:56 +03:00
|
|
|
GetMiscContainer()->mValue.mSVGAnimatedPreserveAspectRatio, aResult);
|
2012-02-16 03:40:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
case eSVGStringList: {
|
2012-09-30 20:40:24 +04:00
|
|
|
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGStringList,
|
2012-02-16 03:40:46 +04:00
|
|
|
aResult);
|
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
case eSVGTransformList: {
|
2012-09-30 20:40:24 +04:00
|
|
|
SVGAttrValueWrapper::ToString(
|
|
|
|
GetMiscContainer()->mValue.mSVGTransformList, aResult);
|
2012-02-16 03:40:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-16 03:40:46 +04:00
|
|
|
case eSVGViewBox: {
|
2019-04-04 20:40:56 +03:00
|
|
|
SVGAttrValueWrapper::ToString(
|
|
|
|
GetMiscContainer()->mValue.mSVGAnimatedViewBox, aResult);
|
2012-02-16 03:40:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-09-26 14:39:18 +04:00
|
|
|
default: {
|
|
|
|
aResult.Truncate();
|
2008-07-09 12:22:20 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 06:00:56 +04:00
|
|
|
already_AddRefed<nsAtom> nsAttrValue::GetAsAtom() const {
|
|
|
|
switch (Type()) {
|
|
|
|
case eString:
|
2017-04-03 23:13:18 +03:00
|
|
|
return NS_AtomizeMainThread(GetStringValue());
|
2012-02-14 06:00:56 +04:00
|
|
|
|
|
|
|
case eAtom: {
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> atom = GetAtomValue();
|
2013-04-22 15:15:59 +04:00
|
|
|
return atom.forget();
|
2012-02-14 06:00:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
nsAutoString val;
|
|
|
|
ToString(val);
|
2017-04-03 23:13:18 +03:00
|
|
|
return NS_AtomizeMainThread(val);
|
2012-02-14 06:00:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
const nsCheapString nsAttrValue::GetStringValue() const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eString, "wrong type");
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
return nsCheapString(static_cast<nsStringBuffer*>(GetPtr()));
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
bool nsAttrValue::GetColorValue(nscolor& aColor) const {
|
2009-08-19 19:05:14 +04:00
|
|
|
if (Type() != eColor) {
|
|
|
|
// Unparseable value, treat as unset.
|
|
|
|
NS_ASSERTION(Type() == eString, "unexpected type for color-valued attr");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
aColor = GetMiscContainer()->mValue.mColor;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsAttrValue::GetEnumString(nsAString& aResult, bool aRealTag) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eEnum, "wrong type");
|
2010-04-15 15:03:00 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t allEnumBits = (BaseType() == eIntegerBase)
|
|
|
|
? static_cast<uint32_t>(GetIntInternal())
|
2012-09-30 20:40:24 +04:00
|
|
|
: GetMiscContainer()->mValue.mEnumValue;
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t val = allEnumBits >> NS_ATTRVALUE_ENUMTABLEINDEX_BITS;
|
2010-04-15 15:03:00 +04:00
|
|
|
const EnumTable* table = sEnumTableArray->ElementAt(
|
|
|
|
allEnumBits & NS_ATTRVALUE_ENUMTABLEINDEX_MASK);
|
|
|
|
|
|
|
|
while (table->tag) {
|
|
|
|
if (table->value == val) {
|
|
|
|
aResult.AssignASCII(table->tag);
|
|
|
|
if (!aRealTag &&
|
|
|
|
allEnumBits & NS_ATTRVALUE_ENUMTABLE_VALUE_NEEDS_TO_UPPER) {
|
2012-03-10 09:50:34 +04:00
|
|
|
nsContentUtils::ASCIIToUpper(aResult);
|
2010-04-15 15:03:00 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
table++;
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("couldn't find value in EnumTable");
|
2010-04-15 15:03:00 +04:00
|
|
|
}
|
|
|
|
|
2004-03-10 02:22:02 +03:00
|
|
|
uint32_t nsAttrValue::GetAtomCount() const {
|
|
|
|
ValueType type = Type();
|
|
|
|
|
|
|
|
if (type == eAtom) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eAtomArray) {
|
2010-08-12 01:05:28 +04:00
|
|
|
return GetAtomArrayValue()->Length();
|
2004-03-10 02:22:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAtom* nsAttrValue::AtomAt(int32_t aIndex) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aIndex >= 0, "Index must not be negative");
|
|
|
|
MOZ_ASSERT(GetAtomCount() > uint32_t(aIndex), "aIndex out of range");
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2004-03-10 02:22:02 +03:00
|
|
|
if (BaseType() == eAtomBase) {
|
|
|
|
return GetAtomValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(Type() == eAtomArray, "GetAtomCount must be confused");
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2010-08-12 01:05:28 +04:00
|
|
|
return GetAtomArrayValue()->ElementAt(aIndex);
|
2004-03-10 02:22:02 +03:00
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
uint32_t nsAttrValue::HashValue() const {
|
2004-03-04 05:06:28 +03:00
|
|
|
switch (BaseType()) {
|
|
|
|
case eStringBase: {
|
2007-07-08 11:08:04 +04:00
|
|
|
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
|
2005-02-04 02:49:13 +03:00
|
|
|
if (str) {
|
2014-01-04 19:02:17 +04:00
|
|
|
uint32_t len = str->StorageSize() / sizeof(char16_t) - 1;
|
|
|
|
return HashString(static_cast<char16_t*>(str->Data()), len);
|
2005-02-04 02:49:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eOtherBase: {
|
|
|
|
break;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eAtomBase:
|
|
|
|
case eIntegerBase: {
|
2012-08-22 19:56:38 +04:00
|
|
|
// mBits and uint32_t might have different size. This should silence
|
2004-03-04 05:06:28 +03:00
|
|
|
// any warnings or compile-errors. This is what the implementation of
|
|
|
|
// NS_PTR_TO_INT32 does to take care of the same problem.
|
|
|
|
return mBits - 0;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
MiscContainer* cont = GetMiscContainer();
|
2008-09-26 14:39:18 +04:00
|
|
|
if (static_cast<ValueBaseType>(cont->mStringBits &
|
|
|
|
NS_ATTRVALUE_BASETYPE_MASK) == eAtomBase) {
|
|
|
|
return cont->mStringBits - 0;
|
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
switch (cont->mType) {
|
2008-09-26 14:39:18 +04:00
|
|
|
case eInteger: {
|
2012-09-30 20:40:24 +04:00
|
|
|
return cont->mValue.mInteger;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
case eEnum: {
|
2012-09-30 20:40:24 +04:00
|
|
|
return cont->mValue.mEnumValue;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
case ePercent: {
|
2019-06-29 02:26:09 +03:00
|
|
|
return cont->mDoubleValue;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eColor: {
|
2012-09-30 20:40:24 +04:00
|
|
|
return cont->mValue.mColor;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2016-10-18 07:29:03 +03:00
|
|
|
case eCSSDeclaration: {
|
|
|
|
return NS_PTR_TO_INT32(cont->mValue.mCSSDeclaration);
|
2016-06-24 06:35:12 +03:00
|
|
|
}
|
2012-08-24 21:50:49 +04:00
|
|
|
case eURL: {
|
|
|
|
nsString str;
|
|
|
|
ToString(str);
|
|
|
|
return HashString(str);
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eAtomArray: {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t hash = 0;
|
2012-09-30 20:40:24 +04:00
|
|
|
uint32_t count = cont->mValue.mAtomArray->Length();
|
2017-10-03 01:05:19 +03:00
|
|
|
for (RefPtr<nsAtom>*cur = cont->mValue.mAtomArray->Elements(),
|
2010-08-12 01:05:28 +04:00
|
|
|
*end = cur + count;
|
|
|
|
cur != end; ++cur) {
|
2012-03-13 02:53:18 +04:00
|
|
|
hash = AddToHash(hash, cur->get());
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2012-03-13 02:53:18 +04:00
|
|
|
return hash;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2011-03-25 17:43:53 +03:00
|
|
|
case eDoubleValue: {
|
2009-03-10 16:51:34 +03:00
|
|
|
// XXX this is crappy, but oh well
|
2011-03-25 17:43:53 +03:00
|
|
|
return cont->mDoubleValue;
|
2009-03-10 16:51:34 +03:00
|
|
|
}
|
2010-06-25 06:01:07 +04:00
|
|
|
case eIntMarginValue: {
|
2012-09-30 20:40:24 +04:00
|
|
|
return NS_PTR_TO_INT32(cont->mValue.mIntMargin);
|
2010-06-25 06:01:07 +04:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
default: {
|
2012-02-16 03:40:46 +04:00
|
|
|
if (IsSVGType(cont->mType)) {
|
|
|
|
// All SVG types are just pointers to classes so we can treat them alike
|
2019-02-15 11:41:37 +03:00
|
|
|
return NS_PTR_TO_INT32(cont->mValue.mSVGLength);
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("unknown type stored in MiscContainer");
|
2004-03-04 05:06:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2004-02-03 01:00:36 +03:00
|
|
|
bool nsAttrValue::Equals(const nsAttrValue& aOther) const {
|
2004-03-04 05:06:28 +03:00
|
|
|
if (BaseType() != aOther.BaseType()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
switch (BaseType()) {
|
|
|
|
case eStringBase: {
|
2004-02-03 01:00:36 +03:00
|
|
|
return GetStringValue().Equals(aOther.GetStringValue());
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eOtherBase: {
|
|
|
|
break;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eAtomBase:
|
|
|
|
case eIntegerBase: {
|
|
|
|
return mBits == aOther.mBits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MiscContainer* thisCont = GetMiscContainer();
|
|
|
|
MiscContainer* otherCont = aOther.GetMiscContainer();
|
2012-09-30 20:40:24 +04:00
|
|
|
if (thisCont == otherCont) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
if (thisCont->mType != otherCont->mType) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool needsStringComparison = false;
|
2009-08-19 19:05:14 +04:00
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
switch (thisCont->mType) {
|
2008-09-26 14:39:18 +04:00
|
|
|
case eInteger: {
|
2012-09-30 20:40:24 +04:00
|
|
|
if (thisCont->mValue.mInteger == otherCont->mValue.mInteger) {
|
2011-10-17 18:59:28 +04:00
|
|
|
needsStringComparison = true;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eEnum: {
|
2012-09-30 20:40:24 +04:00
|
|
|
if (thisCont->mValue.mEnumValue == otherCont->mValue.mEnumValue) {
|
2011-10-17 18:59:28 +04:00
|
|
|
needsStringComparison = true;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ePercent: {
|
2019-06-29 02:26:09 +03:00
|
|
|
if (thisCont->mDoubleValue == otherCont->mDoubleValue) {
|
2011-10-17 18:59:28 +04:00
|
|
|
needsStringComparison = true;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eColor: {
|
2012-09-30 20:40:24 +04:00
|
|
|
if (thisCont->mValue.mColor == otherCont->mValue.mColor) {
|
2011-10-17 18:59:28 +04:00
|
|
|
needsStringComparison = true;
|
2009-08-19 19:05:14 +04:00
|
|
|
}
|
|
|
|
break;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2016-10-18 07:29:03 +03:00
|
|
|
case eCSSDeclaration: {
|
|
|
|
return thisCont->mValue.mCSSDeclaration ==
|
|
|
|
otherCont->mValue.mCSSDeclaration;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2012-08-24 21:50:49 +04:00
|
|
|
case eURL: {
|
2012-09-30 20:40:24 +04:00
|
|
|
return thisCont->mValue.mURL == otherCont->mValue.mURL;
|
2012-08-24 21:50:49 +04:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
case eAtomArray: {
|
|
|
|
// For classlists we could be insensitive to order, however
|
|
|
|
// classlists are never mapped attributes so they are never compared.
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
if (!(*thisCont->mValue.mAtomArray == *otherCont->mValue.mAtomArray)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
needsStringComparison = true;
|
2008-09-26 14:39:18 +04:00
|
|
|
break;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2011-03-25 17:43:53 +03:00
|
|
|
case eDoubleValue: {
|
|
|
|
return thisCont->mDoubleValue == otherCont->mDoubleValue;
|
2009-03-10 16:51:34 +03:00
|
|
|
}
|
2010-06-25 06:01:07 +04:00
|
|
|
case eIntMarginValue: {
|
2012-09-30 20:40:24 +04:00
|
|
|
return thisCont->mValue.mIntMargin == otherCont->mValue.mIntMargin;
|
2010-06-25 06:01:07 +04:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
default: {
|
2012-02-16 03:40:46 +04:00
|
|
|
if (IsSVGType(thisCont->mType)) {
|
|
|
|
// Currently this method is never called for nsAttrValue objects that
|
|
|
|
// point to SVG data types.
|
|
|
|
// If that changes then we probably want to add methods to the
|
|
|
|
// corresponding SVG types to compare their base values.
|
|
|
|
// As a shortcut, however, we can begin by comparing the pointers.
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(false, "Comparing nsAttrValues that point to SVG data");
|
2012-02-16 03:40:46 +04:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("unknown type stored in MiscContainer");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
}
|
2008-09-26 14:39:18 +04:00
|
|
|
if (needsStringComparison) {
|
|
|
|
if (thisCont->mStringBits == otherCont->mStringBits) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
if ((static_cast<ValueBaseType>(thisCont->mStringBits &
|
|
|
|
NS_ATTRVALUE_BASETYPE_MASK) ==
|
|
|
|
eStringBase) &&
|
|
|
|
(static_cast<ValueBaseType>(otherCont->mStringBits &
|
|
|
|
NS_ATTRVALUE_BASETYPE_MASK) ==
|
|
|
|
eStringBase)) {
|
2018-01-30 21:29:14 +03:00
|
|
|
return nsCheapString(reinterpret_cast<nsStringBuffer*>(
|
|
|
|
static_cast<uintptr_t>(thisCont->mStringBits)))
|
|
|
|
.Equals(nsCheapString(reinterpret_cast<nsStringBuffer*>(
|
|
|
|
static_cast<uintptr_t>(otherCont->mStringBits))));
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
2005-09-11 23:20:08 +04:00
|
|
|
bool nsAttrValue::Equals(const nsAString& aValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) const {
|
|
|
|
switch (BaseType()) {
|
|
|
|
case eStringBase: {
|
2007-07-08 11:08:04 +04:00
|
|
|
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
|
2005-09-11 23:20:08 +04:00
|
|
|
if (str) {
|
2014-01-04 19:02:17 +04:00
|
|
|
nsDependentString dep(static_cast<char16_t*>(str->Data()),
|
|
|
|
str->StorageSize() / sizeof(char16_t) - 1);
|
2005-09-11 23:20:08 +04:00
|
|
|
return aCaseSensitive == eCaseMatters
|
|
|
|
? aValue.Equals(dep)
|
2012-12-14 02:08:44 +04:00
|
|
|
: nsContentUtils::EqualsIgnoreASCIICase(aValue, dep);
|
2005-09-11 23:20:08 +04:00
|
|
|
}
|
|
|
|
return aValue.IsEmpty();
|
|
|
|
}
|
|
|
|
case eAtomBase:
|
|
|
|
if (aCaseSensitive == eCaseMatters) {
|
2017-10-03 01:05:19 +03:00
|
|
|
return static_cast<nsAtom*>(GetPtr())->Equals(aValue);
|
2005-09-11 23:20:08 +04:00
|
|
|
}
|
2012-12-14 02:08:44 +04:00
|
|
|
return nsContentUtils::EqualsIgnoreASCIICase(
|
2017-10-03 01:05:19 +03:00
|
|
|
nsDependentAtomString(static_cast<nsAtom*>(GetPtr())), aValue);
|
2005-09-11 23:20:08 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString val;
|
|
|
|
ToString(val);
|
|
|
|
return aCaseSensitive == eCaseMatters
|
|
|
|
? val.Equals(aValue)
|
2012-12-14 02:08:44 +04:00
|
|
|
: nsContentUtils::EqualsIgnoreASCIICase(val, aValue);
|
2005-09-11 23:20:08 +04:00
|
|
|
}
|
|
|
|
|
2018-11-13 15:48:21 +03:00
|
|
|
bool nsAttrValue::Equals(const nsAtom* aValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) const {
|
2005-09-11 23:20:08 +04:00
|
|
|
if (aCaseSensitive != eCaseMatters) {
|
|
|
|
// Need a better way to handle this!
|
|
|
|
nsAutoString value;
|
|
|
|
aValue->ToString(value);
|
|
|
|
return Equals(value, aCaseSensitive);
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2005-09-11 23:20:08 +04:00
|
|
|
switch (BaseType()) {
|
|
|
|
case eStringBase: {
|
2007-07-08 11:08:04 +04:00
|
|
|
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
|
2005-09-11 23:20:08 +04:00
|
|
|
if (str) {
|
2014-01-04 19:02:17 +04:00
|
|
|
nsDependentString dep(static_cast<char16_t*>(str->Data()),
|
|
|
|
str->StorageSize() / sizeof(char16_t) - 1);
|
2005-09-11 23:20:08 +04:00
|
|
|
return aValue->Equals(dep);
|
|
|
|
}
|
2010-03-08 18:45:00 +03:00
|
|
|
return aValue == nsGkAtoms::_empty;
|
2005-09-11 23:20:08 +04:00
|
|
|
}
|
|
|
|
case eAtomBase: {
|
2017-10-03 01:05:19 +03:00
|
|
|
return static_cast<nsAtom*>(GetPtr()) == aValue;
|
2005-09-11 23:20:08 +04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString val;
|
|
|
|
ToString(val);
|
|
|
|
return aValue->Equals(val);
|
|
|
|
}
|
|
|
|
|
2020-05-26 01:26:09 +03:00
|
|
|
struct HasPrefixFn {
|
|
|
|
static bool Check(const char16_t* aAttrValue, size_t aAttrLen,
|
|
|
|
const nsAString& aSearchValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) {
|
|
|
|
if (aCaseSensitive == eCaseMatters) {
|
|
|
|
if (aSearchValue.Length() > aAttrLen) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !memcmp(aAttrValue, aSearchValue.BeginReading(),
|
|
|
|
aSearchValue.Length() * sizeof(char16_t));
|
|
|
|
}
|
|
|
|
return StringBeginsWith(nsDependentString(aAttrValue, aAttrLen),
|
|
|
|
aSearchValue,
|
2020-05-27 21:11:12 +03:00
|
|
|
nsASCIICaseInsensitiveStringComparator);
|
2020-05-26 01:26:09 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HasSuffixFn {
|
|
|
|
static bool Check(const char16_t* aAttrValue, size_t aAttrLen,
|
|
|
|
const nsAString& aSearchValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) {
|
|
|
|
if (aCaseSensitive == eCaseMatters) {
|
|
|
|
if (aSearchValue.Length() > aAttrLen) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !memcmp(aAttrValue + aAttrLen - aSearchValue.Length(),
|
|
|
|
aSearchValue.BeginReading(),
|
|
|
|
aSearchValue.Length() * sizeof(char16_t));
|
|
|
|
}
|
|
|
|
return StringEndsWith(nsDependentString(aAttrValue, aAttrLen), aSearchValue,
|
2020-05-27 21:11:12 +03:00
|
|
|
nsASCIICaseInsensitiveStringComparator);
|
2020-05-26 01:26:09 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HasSubstringFn {
|
|
|
|
static bool Check(const char16_t* aAttrValue, size_t aAttrLen,
|
|
|
|
const nsAString& aSearchValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) {
|
|
|
|
if (aCaseSensitive == eCaseMatters) {
|
|
|
|
if (aSearchValue.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const char16_t* end = aAttrValue + aAttrLen;
|
|
|
|
return std::search(aAttrValue, end, aSearchValue.BeginReading(),
|
|
|
|
aSearchValue.EndReading()) != end;
|
|
|
|
}
|
|
|
|
return FindInReadable(aSearchValue, nsDependentString(aAttrValue, aAttrLen),
|
2020-05-27 21:11:12 +03:00
|
|
|
nsASCIICaseInsensitiveStringComparator);
|
2020-05-26 01:26:09 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
bool nsAttrValue::SubstringCheck(const nsAString& aValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) const {
|
|
|
|
switch (BaseType()) {
|
|
|
|
case eStringBase: {
|
|
|
|
auto str = static_cast<nsStringBuffer*>(GetPtr());
|
|
|
|
if (str) {
|
|
|
|
return F::Check(static_cast<char16_t*>(str->Data()),
|
|
|
|
str->StorageSize() / sizeof(char16_t) - 1, aValue,
|
|
|
|
aCaseSensitive);
|
|
|
|
}
|
|
|
|
return aValue.IsEmpty();
|
|
|
|
}
|
|
|
|
case eAtomBase: {
|
|
|
|
auto atom = static_cast<nsAtom*>(GetPtr());
|
|
|
|
return F::Check(atom->GetUTF16String(), atom->GetLength(), aValue,
|
|
|
|
aCaseSensitive);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString val;
|
|
|
|
ToString(val);
|
|
|
|
return F::Check(val.BeginReading(), val.Length(), aValue, aCaseSensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsAttrValue::HasPrefix(const nsAString& aValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) const {
|
|
|
|
return SubstringCheck<HasPrefixFn>(aValue, aCaseSensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsAttrValue::HasSuffix(const nsAString& aValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) const {
|
|
|
|
return SubstringCheck<HasSuffixFn>(aValue, aCaseSensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsAttrValue::HasSubstring(const nsAString& aValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) const {
|
|
|
|
return SubstringCheck<HasSubstringFn>(aValue, aCaseSensitive);
|
|
|
|
}
|
|
|
|
|
2012-02-14 06:00:56 +04:00
|
|
|
bool nsAttrValue::EqualsAsStrings(const nsAttrValue& aOther) const {
|
|
|
|
if (Type() == aOther.Type()) {
|
|
|
|
return Equals(aOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to serialize at least one nsAttrValue before passing to
|
|
|
|
// Equals(const nsAString&), but we can avoid unnecessarily serializing both
|
|
|
|
// by checking if one is already of a string type.
|
|
|
|
bool thisIsString = (BaseType() == eStringBase || BaseType() == eAtomBase);
|
|
|
|
const nsAttrValue& lhs = thisIsString ? *this : aOther;
|
|
|
|
const nsAttrValue& rhs = thisIsString ? aOther : *this;
|
|
|
|
|
|
|
|
switch (rhs.BaseType()) {
|
|
|
|
case eAtomBase:
|
|
|
|
return lhs.Equals(rhs.GetAtomValue(), eCaseMatters);
|
|
|
|
|
|
|
|
case eStringBase:
|
|
|
|
return lhs.Equals(rhs.GetStringValue(), eCaseMatters);
|
|
|
|
|
|
|
|
default: {
|
|
|
|
nsAutoString val;
|
|
|
|
rhs.ToString(val);
|
|
|
|
return lhs.Equals(val, eCaseMatters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
bool nsAttrValue::Contains(nsAtom* aValue,
|
|
|
|
nsCaseTreatment aCaseSensitive) const {
|
2005-10-09 03:06:27 +04:00
|
|
|
switch (BaseType()) {
|
|
|
|
case eAtomBase: {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* atom = GetAtomValue();
|
2005-10-09 03:06:27 +04:00
|
|
|
if (aCaseSensitive == eCaseMatters) {
|
|
|
|
return aValue == atom;
|
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
// For performance reasons, don't do a full on unicode case insensitive
|
|
|
|
// string comparison. This is only used for quirks mode anyway.
|
2018-11-13 15:47:40 +03:00
|
|
|
return nsContentUtils::EqualsIgnoreASCIICase(aValue, atom);
|
2005-10-09 03:06:27 +04:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
if (Type() == eAtomArray) {
|
2010-08-12 01:05:28 +04:00
|
|
|
AtomArray* array = GetAtomArrayValue();
|
2005-10-09 03:06:27 +04:00
|
|
|
if (aCaseSensitive == eCaseMatters) {
|
2011-10-28 21:06:39 +04:00
|
|
|
return array->Contains(aValue);
|
2005-10-09 03:06:27 +04:00
|
|
|
}
|
|
|
|
|
2018-11-13 15:47:40 +03:00
|
|
|
for (RefPtr<nsAtom>& cur : *array) {
|
2010-03-08 18:45:00 +03:00
|
|
|
// For performance reasons, don't do a full on unicode case
|
|
|
|
// insensitive string comparison. This is only used for quirks mode
|
|
|
|
// anyway.
|
2018-11-13 15:47:40 +03:00
|
|
|
if (nsContentUtils::EqualsIgnoreASCIICase(aValue, cur)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-10-09 03:06:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-10-09 03:06:27 +04:00
|
|
|
}
|
|
|
|
|
2011-10-28 21:06:39 +04:00
|
|
|
struct AtomArrayStringComparator {
|
2017-10-03 01:05:19 +03:00
|
|
|
bool Equals(nsAtom* atom, const nsAString& string) const {
|
2011-10-28 21:06:39 +04:00
|
|
|
return atom->Equals(string);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool nsAttrValue::Contains(const nsAString& aValue) const {
|
|
|
|
switch (BaseType()) {
|
|
|
|
case eAtomBase: {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* atom = GetAtomValue();
|
2011-10-28 21:06:39 +04:00
|
|
|
return atom->Equals(aValue);
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
if (Type() == eAtomArray) {
|
|
|
|
AtomArray* array = GetAtomArrayValue();
|
|
|
|
return array->Contains(aValue, AtomArrayStringComparator());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
void nsAttrValue::ParseAtom(const nsAString& aValue) {
|
|
|
|
ResetIfSet();
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> atom = NS_Atomize(aValue);
|
2004-03-04 05:06:28 +03:00
|
|
|
if (atom) {
|
2014-03-15 23:00:15 +04:00
|
|
|
SetPtrValueAndType(atom.forget().take(), eAtomBase);
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsAttrValue::ParseAtomArray(const nsAString& aValue) {
|
|
|
|
nsAString::const_iterator iter, end;
|
|
|
|
aValue.BeginReading(iter);
|
|
|
|
aValue.EndReading(end);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasSpace = false;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
// skip initial whitespace
|
2008-06-11 04:27:00 +04:00
|
|
|
while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
hasSpace = true;
|
2004-03-04 05:06:28 +03:00
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter == end) {
|
2008-09-26 14:39:18 +04:00
|
|
|
SetTo(aValue);
|
2004-03-04 05:06:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAString::const_iterator start(iter);
|
|
|
|
|
|
|
|
// get first - and often only - atom
|
|
|
|
do {
|
|
|
|
++iter;
|
2008-06-11 04:27:00 +04:00
|
|
|
} while (iter != end && !nsContentUtils::IsHTMLWhitespace(*iter));
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> classAtom = NS_AtomizeMainThread(Substring(start, iter));
|
2004-03-04 05:06:28 +03:00
|
|
|
if (!classAtom) {
|
|
|
|
Reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip whitespace
|
2008-06-11 04:27:00 +04:00
|
|
|
while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
hasSpace = true;
|
2004-03-04 05:06:28 +03:00
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
2008-09-26 14:39:18 +04:00
|
|
|
if (iter == end && !hasSpace) {
|
|
|
|
// we only found one classname and there was no whitespace so
|
|
|
|
// don't bother storing a list
|
2004-03-04 05:06:28 +03:00
|
|
|
ResetIfSet();
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* atom = nullptr;
|
2004-03-04 05:06:28 +03:00
|
|
|
classAtom.swap(atom);
|
|
|
|
SetPtrValueAndType(atom, eAtomBase);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureEmptyAtomArray()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-12 01:05:28 +04:00
|
|
|
AtomArray* array = GetAtomArrayValue();
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2020-04-24 16:31:14 +03:00
|
|
|
// XXX(Bug 1631371) Check if this should use a fallible operation as it
|
|
|
|
// pretended earlier.
|
|
|
|
array->AppendElement(std::move(classAtom));
|
2004-03-04 05:06:28 +03:00
|
|
|
|
|
|
|
// parse the rest of the classnames
|
2008-09-26 14:39:18 +04:00
|
|
|
while (iter != end) {
|
2004-03-04 05:06:28 +03:00
|
|
|
start = iter;
|
|
|
|
|
|
|
|
do {
|
|
|
|
++iter;
|
2008-06-11 04:27:00 +04:00
|
|
|
} while (iter != end && !nsContentUtils::IsHTMLWhitespace(*iter));
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2017-04-03 23:13:18 +03:00
|
|
|
classAtom = NS_AtomizeMainThread(Substring(start, iter));
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2020-04-24 16:31:14 +03:00
|
|
|
// XXX(Bug 1631371) Check if this should use a fallible operation as it
|
|
|
|
// pretended earlier.
|
|
|
|
array->AppendElement(std::move(classAtom));
|
2004-03-04 05:06:28 +03:00
|
|
|
|
|
|
|
// skip whitespace
|
2008-06-11 04:27:00 +04:00
|
|
|
while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) {
|
2004-03-04 05:06:28 +03:00
|
|
|
++iter;
|
|
|
|
}
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2008-09-26 14:39:18 +04:00
|
|
|
SetMiscAtomOrString(&aValue);
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsAttrValue::ParseStringOrAtom(const nsAString& aValue) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = aValue.Length();
|
2004-03-04 05:06:28 +03:00
|
|
|
// Don't bother with atoms if it's an empty string since
|
2019-11-06 21:03:51 +03:00
|
|
|
// we can store those efficiently anyway.
|
2004-03-04 05:06:28 +03:00
|
|
|
if (len && len <= NS_ATTRVALUE_MAX_STRINGLENGTH_ATOM) {
|
|
|
|
ParseAtom(aValue);
|
|
|
|
} else {
|
|
|
|
SetTo(aValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 21:03:51 +03:00
|
|
|
void nsAttrValue::ParsePartMapping(const nsAString& aValue) {
|
|
|
|
ResetIfSet();
|
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
|
|
|
|
cont->mType = eShadowParts;
|
|
|
|
cont->mValue.mShadowParts = new ShadowParts(ShadowParts::Parse(aValue));
|
|
|
|
NS_ADDREF(cont);
|
|
|
|
SetMiscAtomOrString(&aValue);
|
|
|
|
MOZ_ASSERT(cont->mValue.mRefCount == 1);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void nsAttrValue::SetIntValueAndType(int32_t aValue, ValueType aType,
|
2008-09-26 14:39:18 +04:00
|
|
|
const nsAString* aStringValue) {
|
|
|
|
if (aStringValue || aValue > NS_ATTRVALUE_INTEGERTYPE_MAXVALUE ||
|
|
|
|
aValue < NS_ATTRVALUE_INTEGERTYPE_MINVALUE) {
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
switch (aType) {
|
|
|
|
case eInteger: {
|
|
|
|
cont->mValue.mInteger = aValue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ePercent: {
|
2019-06-29 02:26:09 +03:00
|
|
|
cont->mDoubleValue = aValue;
|
2012-09-30 20:40:24 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eEnum: {
|
|
|
|
cont->mValue.mEnumValue = aValue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("unknown integer type");
|
2012-09-30 20:40:24 +04:00
|
|
|
break;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
}
|
2012-09-30 20:40:24 +04:00
|
|
|
cont->mType = aType;
|
|
|
|
SetMiscAtomOrString(aStringValue);
|
2008-09-26 14:39:18 +04:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(!mBits, "Reset before calling SetIntValueAndType!");
|
|
|
|
mBits = (aValue * NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER) | aType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 02:26:09 +03:00
|
|
|
void nsAttrValue::SetDoubleValueAndType(double aValue, ValueType aType,
|
|
|
|
const nsAString* aStringValue) {
|
|
|
|
MOZ_ASSERT(aType == eDoubleValue || aType == ePercent, "Unexpected type");
|
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
cont->mDoubleValue = aValue;
|
|
|
|
cont->mType = aType;
|
|
|
|
SetMiscAtomOrString(aStringValue);
|
|
|
|
}
|
|
|
|
|
2011-08-11 02:25:22 +04:00
|
|
|
int16_t nsAttrValue::GetEnumTableIndex(const EnumTable* aTable) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t index = sEnumTableArray->IndexOf(aTable);
|
2010-04-15 15:03:00 +04:00
|
|
|
if (index < 0) {
|
|
|
|
index = sEnumTableArray->Length();
|
|
|
|
NS_ASSERTION(index <= NS_ATTRVALUE_ENUMTABLEINDEX_MAXVALUE,
|
|
|
|
"too many enum tables");
|
2011-08-11 02:25:22 +04:00
|
|
|
sEnumTableArray->AppendElement(aTable);
|
2010-04-15 15:03:00 +04:00
|
|
|
}
|
|
|
|
|
2011-08-11 02:25:22 +04:00
|
|
|
return index;
|
|
|
|
}
|
2010-04-15 15:03:00 +04:00
|
|
|
|
2011-08-11 02:25:22 +04:00
|
|
|
int32_t nsAttrValue::EnumTableEntryToValue(const EnumTable* aEnumTable,
|
|
|
|
const EnumTable* aTableEntry) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t index = GetEnumTableIndex(aEnumTable);
|
|
|
|
int32_t value =
|
|
|
|
(aTableEntry->value << NS_ATTRVALUE_ENUMTABLEINDEX_BITS) + index;
|
2011-08-11 02:25:22 +04:00
|
|
|
return value;
|
2010-04-15 15:03:00 +04:00
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
bool nsAttrValue::ParseEnumValue(const nsAString& aValue,
|
2005-01-25 03:02:58 +03:00
|
|
|
const EnumTable* aTable, bool aCaseSensitive,
|
2011-08-11 02:25:22 +04:00
|
|
|
const EnumTable* aDefaultValue) {
|
2004-05-01 02:03:09 +04:00
|
|
|
ResetIfSet();
|
2010-04-15 15:03:00 +04:00
|
|
|
const EnumTable* tableEntry = aTable;
|
2004-05-01 02:03:09 +04:00
|
|
|
|
2010-04-15 15:03:00 +04:00
|
|
|
while (tableEntry->tag) {
|
|
|
|
if (aCaseSensitive ? aValue.EqualsASCII(tableEntry->tag)
|
|
|
|
: aValue.LowerCaseEqualsASCII(tableEntry->tag)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t value = EnumTableEntryToValue(aTable, tableEntry);
|
2005-01-25 03:02:58 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equals = aCaseSensitive || aValue.EqualsASCII(tableEntry->tag);
|
2008-09-26 14:39:18 +04:00
|
|
|
if (!equals) {
|
|
|
|
nsAutoString tag;
|
2010-04-15 15:03:00 +04:00
|
|
|
tag.AssignASCII(tableEntry->tag);
|
2012-03-10 09:50:34 +04:00
|
|
|
nsContentUtils::ASCIIToUpper(tag);
|
2008-09-26 14:39:18 +04:00
|
|
|
if ((equals = tag.Equals(aValue))) {
|
|
|
|
value |= NS_ATTRVALUE_ENUMTABLE_VALUE_NEEDS_TO_UPPER;
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
SetIntValueAndType(value, eEnum, equals ? nullptr : &aValue);
|
2010-04-15 15:03:00 +04:00
|
|
|
NS_ASSERTION(GetEnumValue() == tableEntry->value,
|
2005-01-25 03:02:58 +03:00
|
|
|
"failed to store enum properly");
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
2010-04-15 15:03:00 +04:00
|
|
|
tableEntry++;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2011-08-11 02:25:22 +04:00
|
|
|
if (aDefaultValue) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aTable <= aDefaultValue && aDefaultValue < tableEntry,
|
|
|
|
"aDefaultValue not inside aTable?");
|
2011-08-11 02:25:22 +04:00
|
|
|
SetIntValueAndType(EnumTableEntryToValue(aTable, aDefaultValue), eEnum,
|
|
|
|
&aValue);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-08-11 02:25:22 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2019-06-28 23:49:48 +03:00
|
|
|
bool nsAttrValue::DoParseHTMLDimension(const nsAString& aInput,
|
|
|
|
bool aEnsureNonzero) {
|
|
|
|
ResetIfSet();
|
|
|
|
|
|
|
|
// We don't use nsContentUtils::ParseHTMLInteger here because we
|
|
|
|
// need a bunch of behavioral differences from it. We _could_ try to
|
|
|
|
// use it, but it would not be a great fit.
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/#rules-for-parsing-dimension-values
|
|
|
|
|
2019-07-03 09:36:17 +03:00
|
|
|
// Steps 1 and 2.
|
2019-06-28 23:49:48 +03:00
|
|
|
const char16_t* position = aInput.BeginReading();
|
|
|
|
const char16_t* end = aInput.EndReading();
|
|
|
|
|
|
|
|
// We will need to keep track of whether this was a canonical representation
|
|
|
|
// or not. It's non-canonical if it has leading whitespace, leading '+',
|
|
|
|
// leading '0' characters, or trailing garbage.
|
|
|
|
bool canonical = true;
|
|
|
|
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 3.
|
2019-06-28 23:49:48 +03:00
|
|
|
while (position != end && nsContentUtils::IsHTMLWhitespace(*position)) {
|
|
|
|
canonical = false; // Leading whitespace
|
|
|
|
++position;
|
|
|
|
}
|
|
|
|
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 4.
|
|
|
|
if (position == end || *position < char16_t('0') ||
|
|
|
|
*position > char16_t('9')) {
|
2019-06-28 23:49:48 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 5.
|
2019-06-28 23:49:48 +03:00
|
|
|
CheckedInt32 value = 0;
|
|
|
|
|
|
|
|
// Collect up leading '0' first to avoid extra branching in the main
|
|
|
|
// loop to set 'canonical' properly.
|
|
|
|
while (position != end && *position == char16_t('0')) {
|
|
|
|
canonical = false; // Leading '0'
|
|
|
|
++position;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now collect up other digits.
|
|
|
|
while (position != end && *position >= char16_t('0') &&
|
|
|
|
*position <= char16_t('9')) {
|
|
|
|
value = value * 10 + (*position - char16_t('0'));
|
|
|
|
if (!value.isValid()) {
|
|
|
|
// The spec assumes we can deal with arbitrary-size integers here, but we
|
|
|
|
// really can't. If someone sets something too big, just bail out and
|
|
|
|
// ignore it.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++position;
|
|
|
|
}
|
|
|
|
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 6 is implemented implicitly via the various "position != end" guards
|
2019-06-28 23:49:48 +03:00
|
|
|
// from this point on.
|
|
|
|
|
2019-06-29 02:31:15 +03:00
|
|
|
Maybe<double> doubleValue;
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 7. The return in step 7.2 is handled by just falling through to the
|
|
|
|
// code below this block when we reach end of input or a non-digit, because
|
|
|
|
// the while loop will terminate at that point.
|
2019-06-28 23:49:48 +03:00
|
|
|
if (position != end && *position == char16_t('.')) {
|
2019-06-29 02:31:15 +03:00
|
|
|
canonical = false; // Let's not rely on double serialization reproducing
|
|
|
|
// the string we started with.
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 7.1.
|
2019-06-28 23:49:48 +03:00
|
|
|
++position;
|
2019-06-29 02:31:15 +03:00
|
|
|
// If we have a '.' _not_ followed by digits, this is not as efficient as it
|
|
|
|
// could be, because we will store as a double while we could have stored as
|
|
|
|
// an int. But that seems like a pretty rare case.
|
|
|
|
doubleValue.emplace(value.value());
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 7.3.
|
2019-06-29 02:31:15 +03:00
|
|
|
double divisor = 1.0f;
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 7.4.
|
2019-06-28 23:49:48 +03:00
|
|
|
while (position != end && *position >= char16_t('0') &&
|
|
|
|
*position <= char16_t('9')) {
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 7.4.1.
|
2019-06-29 02:31:15 +03:00
|
|
|
divisor = divisor * 10.0f;
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 7.4.2.
|
2019-06-29 02:31:15 +03:00
|
|
|
doubleValue.ref() += (*position - char16_t('0')) / divisor;
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 7.4.3.
|
2019-06-28 23:49:48 +03:00
|
|
|
++position;
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 7.4.4 and 7.4.5 are captured in the while loop condition and the
|
|
|
|
// "position != end" checks below.
|
2019-06-28 23:49:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 02:31:15 +03:00
|
|
|
if (aEnsureNonzero && value.value() == 0 &&
|
|
|
|
(!doubleValue || *doubleValue == 0.0f)) {
|
2019-06-28 23:49:48 +03:00
|
|
|
// Not valid. Just drop it.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-03 09:36:17 +03:00
|
|
|
// Step 8 and the spec's early return from step 7.2.
|
2019-06-28 23:49:48 +03:00
|
|
|
ValueType type;
|
|
|
|
if (position != end && *position == char16_t('%')) {
|
|
|
|
type = ePercent;
|
|
|
|
++position;
|
2019-06-29 02:31:15 +03:00
|
|
|
} else if (doubleValue) {
|
|
|
|
type = eDoubleValue;
|
2019-06-28 23:49:48 +03:00
|
|
|
} else {
|
|
|
|
type = eInteger;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position != end) {
|
|
|
|
canonical = false;
|
|
|
|
}
|
|
|
|
|
2019-06-29 02:31:15 +03:00
|
|
|
if (doubleValue) {
|
|
|
|
MOZ_ASSERT(!canonical, "We set it false above!");
|
|
|
|
SetDoubleValueAndType(*doubleValue, type, &aInput);
|
|
|
|
} else {
|
|
|
|
SetIntValueAndType(value.value(), type, canonical ? nullptr : &aInput);
|
|
|
|
}
|
2019-06-28 23:49:48 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoString str;
|
|
|
|
ToString(str);
|
|
|
|
MOZ_ASSERT(str == aInput, "We messed up our 'canonical' boolean!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-03-04 05:06:28 +03:00
|
|
|
bool nsAttrValue::ParseIntWithBounds(const nsAString& aString, int32_t aMin,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aMax) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aMin < aMax, "bad boundaries");
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2004-05-01 02:03:09 +04:00
|
|
|
ResetIfSet();
|
|
|
|
|
2014-05-01 03:07:48 +04:00
|
|
|
nsContentUtils::ParseHTMLIntegerResultFlags result;
|
|
|
|
int32_t originalVal = nsContentUtils::ParseHTMLInteger(aString, &result);
|
|
|
|
if (result & nsContentUtils::eParseHTMLInteger_Error) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
int32_t val = std::max(originalVal, aMin);
|
|
|
|
val = std::min(val, aMax);
|
2014-05-01 03:07:48 +04:00
|
|
|
bool nonStrict =
|
|
|
|
(val != originalVal) ||
|
|
|
|
(result & nsContentUtils::eParseHTMLInteger_NonStandard) ||
|
|
|
|
(result & nsContentUtils::eParseHTMLInteger_DidNotConsumeAllInput);
|
|
|
|
|
|
|
|
SetIntValueAndType(val, eInteger, nonStrict ? &aString : nullptr);
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
2016-08-21 18:09:20 +03:00
|
|
|
void nsAttrValue::ParseIntWithFallback(const nsAString& aString,
|
|
|
|
int32_t aDefault, int32_t aMax) {
|
|
|
|
ResetIfSet();
|
|
|
|
|
|
|
|
nsContentUtils::ParseHTMLIntegerResultFlags result;
|
|
|
|
int32_t val = nsContentUtils::ParseHTMLInteger(aString, &result);
|
|
|
|
bool nonStrict = false;
|
|
|
|
if ((result & nsContentUtils::eParseHTMLInteger_Error) || val < 1) {
|
|
|
|
val = aDefault;
|
|
|
|
nonStrict = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val > aMax) {
|
|
|
|
val = aMax;
|
|
|
|
nonStrict = true;
|
|
|
|
}
|
|
|
|
|
2019-06-28 20:39:52 +03:00
|
|
|
if ((result & nsContentUtils::eParseHTMLInteger_NonStandard) ||
|
2016-08-21 18:09:20 +03:00
|
|
|
(result & nsContentUtils::eParseHTMLInteger_DidNotConsumeAllInput)) {
|
|
|
|
nonStrict = true;
|
2017-05-18 00:24:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SetIntValueAndType(val, eInteger, nonStrict ? &aString : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsAttrValue::ParseClampedNonNegativeInt(const nsAString& aString,
|
|
|
|
int32_t aDefault, int32_t aMin,
|
|
|
|
int32_t aMax) {
|
|
|
|
ResetIfSet();
|
|
|
|
|
|
|
|
nsContentUtils::ParseHTMLIntegerResultFlags result;
|
|
|
|
int32_t val = nsContentUtils::ParseHTMLInteger(aString, &result);
|
|
|
|
bool nonStrict =
|
|
|
|
(result & nsContentUtils::eParseHTMLInteger_NonStandard) ||
|
|
|
|
(result & nsContentUtils::eParseHTMLInteger_DidNotConsumeAllInput);
|
|
|
|
|
|
|
|
if (result & nsContentUtils::eParseHTMLInteger_ErrorOverflow) {
|
|
|
|
if (result & nsContentUtils::eParseHTMLInteger_Negative) {
|
|
|
|
val = aDefault;
|
|
|
|
} else {
|
|
|
|
val = aMax;
|
|
|
|
}
|
|
|
|
nonStrict = true;
|
|
|
|
} else if ((result & nsContentUtils::eParseHTMLInteger_Error) || val < 0) {
|
|
|
|
val = aDefault;
|
|
|
|
nonStrict = true;
|
|
|
|
} else if (val < aMin) {
|
|
|
|
val = aMin;
|
|
|
|
nonStrict = true;
|
|
|
|
} else if (val > aMax) {
|
|
|
|
val = aMax;
|
|
|
|
nonStrict = true;
|
2016-08-21 18:09:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SetIntValueAndType(val, eInteger, nonStrict ? &aString : nullptr);
|
|
|
|
}
|
|
|
|
|
2010-03-05 22:38:26 +03:00
|
|
|
bool nsAttrValue::ParseNonNegativeIntValue(const nsAString& aString) {
|
|
|
|
ResetIfSet();
|
|
|
|
|
2014-05-01 03:07:48 +04:00
|
|
|
nsContentUtils::ParseHTMLIntegerResultFlags result;
|
|
|
|
int32_t originalVal = nsContentUtils::ParseHTMLInteger(aString, &result);
|
|
|
|
if ((result & nsContentUtils::eParseHTMLInteger_Error) || originalVal < 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-03-05 22:38:26 +03:00
|
|
|
}
|
|
|
|
|
2014-05-01 03:07:48 +04:00
|
|
|
bool nonStrict =
|
|
|
|
(result & nsContentUtils::eParseHTMLInteger_NonStandard) ||
|
|
|
|
(result & nsContentUtils::eParseHTMLInteger_DidNotConsumeAllInput);
|
|
|
|
|
|
|
|
SetIntValueAndType(originalVal, eInteger, nonStrict ? &aString : nullptr);
|
2010-03-05 22:38:26 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-03-05 22:38:26 +03:00
|
|
|
}
|
|
|
|
|
2010-05-24 01:36:49 +04:00
|
|
|
bool nsAttrValue::ParsePositiveIntValue(const nsAString& aString) {
|
|
|
|
ResetIfSet();
|
|
|
|
|
2014-05-01 03:07:48 +04:00
|
|
|
nsContentUtils::ParseHTMLIntegerResultFlags result;
|
|
|
|
int32_t originalVal = nsContentUtils::ParseHTMLInteger(aString, &result);
|
|
|
|
if ((result & nsContentUtils::eParseHTMLInteger_Error) || originalVal <= 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-24 01:36:49 +04:00
|
|
|
}
|
|
|
|
|
2014-05-01 03:07:48 +04:00
|
|
|
bool nonStrict =
|
|
|
|
(result & nsContentUtils::eParseHTMLInteger_NonStandard) ||
|
|
|
|
(result & nsContentUtils::eParseHTMLInteger_DidNotConsumeAllInput);
|
|
|
|
|
|
|
|
SetIntValueAndType(originalVal, eInteger, nonStrict ? &aString : nullptr);
|
2010-05-24 01:36:49 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-05-24 01:36:49 +04:00
|
|
|
}
|
|
|
|
|
2009-08-19 19:05:14 +04:00
|
|
|
void nsAttrValue::SetColorValue(nscolor aColor, const nsAString& aString) {
|
2014-03-15 23:00:15 +04:00
|
|
|
nsStringBuffer* buf = GetStringBuffer(aString).take();
|
2009-08-19 19:05:14 +04:00
|
|
|
if (!buf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
cont->mValue.mColor = aColor;
|
2009-08-19 19:05:14 +04:00
|
|
|
cont->mType = eColor;
|
|
|
|
|
|
|
|
// Save the literal string we were passed for round-tripping.
|
2018-01-30 21:29:14 +03:00
|
|
|
cont->SetStringBitsMainThread(reinterpret_cast<uintptr_t>(buf) | eStringBase);
|
2009-08-19 19:05:14 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 12:09:14 +04:00
|
|
|
bool nsAttrValue::ParseColor(const nsAString& aString) {
|
2009-08-19 19:05:14 +04:00
|
|
|
ResetIfSet();
|
|
|
|
|
2010-06-04 00:11:35 +04:00
|
|
|
// FIXME (partially, at least): HTML5's algorithm says we shouldn't do
|
|
|
|
// the whitespace compression, trimming, or the test for emptiness.
|
|
|
|
// (I'm a little skeptical that we shouldn't do the whitespace
|
|
|
|
// trimming; WebKit also does it.)
|
2004-03-04 05:06:28 +03:00
|
|
|
nsAutoString colorStr(aString);
|
2011-10-17 18:59:28 +04:00
|
|
|
colorStr.CompressWhitespace(true, true);
|
2004-03-04 05:06:28 +03:00
|
|
|
if (colorStr.IsEmpty()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nscolor color;
|
2009-08-19 19:05:14 +04:00
|
|
|
// No color names begin with a '#'; in standards mode, all acceptable
|
|
|
|
// numeric colors do.
|
|
|
|
if (colorStr.First() == '#') {
|
2010-06-04 00:11:34 +04:00
|
|
|
nsDependentString withoutHash(colorStr.get() + 1, colorStr.Length() - 1);
|
2016-05-09 08:16:41 +03:00
|
|
|
if (NS_HexToRGBA(withoutHash, nsHexColorType::NoAlpha, &color)) {
|
2009-08-19 19:05:14 +04:00
|
|
|
SetColorValue(color, aString);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-08-19 19:05:14 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (NS_ColorNameToRGB(colorStr, &color)) {
|
|
|
|
SetColorValue(color, aString);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-04 00:11:34 +04:00
|
|
|
// FIXME (maybe): HTML5 says we should handle system colors. This
|
|
|
|
// means we probably need another storage type, since we'd need to
|
|
|
|
// handle dynamic changes. However, I think this is a bad idea:
|
|
|
|
// http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2010-May/026449.html
|
|
|
|
|
2010-06-04 00:11:34 +04:00
|
|
|
// Use NS_LooseHexToRGB as a fallback if nothing above worked.
|
2010-06-04 00:11:34 +04:00
|
|
|
if (NS_LooseHexToRGB(colorStr, &color)) {
|
|
|
|
SetColorValue(color, aString);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-04 00:11:34 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsAttrValue::ParseDoubleValue(const nsAString& aString) {
|
2008-07-09 12:22:20 +04:00
|
|
|
ResetIfSet();
|
|
|
|
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult ec;
|
2011-03-25 17:43:40 +03:00
|
|
|
double val = PromiseFlatString(aString).ToDouble(&ec);
|
2008-07-09 12:22:20 +04:00
|
|
|
if (NS_FAILED(ec)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-07-09 12:22:20 +04:00
|
|
|
}
|
2008-09-26 14:39:18 +04:00
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
cont->mDoubleValue = val;
|
|
|
|
cont->mType = eDoubleValue;
|
|
|
|
nsAutoString serializedFloat;
|
|
|
|
serializedFloat.AppendFloat(val);
|
|
|
|
SetMiscAtomOrString(serializedFloat.Equals(aString) ? nullptr : &aString);
|
|
|
|
return true;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
|
2010-06-25 06:01:07 +04:00
|
|
|
bool nsAttrValue::ParseIntMarginValue(const nsAString& aString) {
|
|
|
|
ResetIfSet();
|
|
|
|
|
|
|
|
nsIntMargin margins;
|
|
|
|
if (!nsContentUtils::ParseIntMarginValue(aString, margins)) return false;
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
cont->mValue.mIntMargin = new nsIntMargin(margins);
|
|
|
|
cont->mType = eIntMarginValue;
|
|
|
|
SetMiscAtomOrString(&aString);
|
|
|
|
return true;
|
2010-06-25 06:01:07 +04:00
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
bool nsAttrValue::ParseStyleAttribute(const nsAString& aString,
|
2017-11-02 23:36:14 +03:00
|
|
|
nsIPrincipal* aMaybeScriptedPrincipal,
|
2016-08-02 21:05:38 +03:00
|
|
|
nsStyledElement* aElement) {
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* ownerDoc = aElement->OwnerDoc();
|
2012-09-30 20:40:24 +04:00
|
|
|
nsHTMLCSSStyleSheet* sheet = ownerDoc->GetInlineStyleSheet();
|
2018-08-23 01:27:12 +03:00
|
|
|
nsIURI* baseURI = aElement->GetBaseURIForStyleAttr();
|
2012-09-30 20:40:24 +04:00
|
|
|
nsIURI* docURI = ownerDoc->GetDocumentURI();
|
|
|
|
|
|
|
|
NS_ASSERTION(aElement->NodePrincipal() == ownerDoc->NodePrincipal(),
|
|
|
|
"This is unexpected");
|
|
|
|
|
2018-08-23 01:27:12 +03:00
|
|
|
nsIPrincipal* principal = aMaybeScriptedPrincipal ? aMaybeScriptedPrincipal
|
|
|
|
: aElement->NodePrincipal();
|
2017-11-02 23:36:14 +03:00
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
// If the (immutable) document URI does not match the element's base URI
|
|
|
|
// (the common case is that they do match) do not cache the rule. This is
|
|
|
|
// because the results of the CSS parser are dependent on these URIs, and we
|
|
|
|
// do not want to have to account for the URIs in the hash lookup.
|
2017-11-02 23:36:14 +03:00
|
|
|
// Similarly, if the triggering principal does not match the node principal,
|
|
|
|
// do not cache the rule, since the principal will be encoded in any parsed
|
|
|
|
// URLs in the rule.
|
2018-08-23 01:27:12 +03:00
|
|
|
const bool cachingAllowed =
|
|
|
|
sheet && baseURI == docURI && principal == aElement->NodePrincipal();
|
2012-09-30 20:40:24 +04:00
|
|
|
if (cachingAllowed) {
|
|
|
|
MiscContainer* cont = sheet->LookupStyleAttr(aString);
|
|
|
|
if (cont) {
|
|
|
|
// Set our MiscContainer to the cached one.
|
|
|
|
NS_ADDREF(cont);
|
|
|
|
SetPtrValueAndType(cont, eOtherBase);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 14:43:56 +03:00
|
|
|
nsCOMPtr<nsIReferrerInfo> referrerInfo =
|
2019-11-05 23:22:40 +03:00
|
|
|
dom::ReferrerInfo::CreateForInternalCSSResources(ownerDoc);
|
2020-09-21 12:57:46 +03:00
|
|
|
auto data = MakeRefPtr<URLExtraData>(baseURI, referrerInfo, principal);
|
2018-09-17 08:36:45 +03:00
|
|
|
RefPtr<DeclarationBlock> decl = DeclarationBlock::FromCssText(
|
2020-09-21 12:57:46 +03:00
|
|
|
aString, data, ownerDoc->GetCompatibilityMode(), ownerDoc->CSSLoader(),
|
|
|
|
dom::CSSRule_Binding::STYLE_RULE);
|
2016-10-18 07:29:03 +03:00
|
|
|
if (!decl) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
decl->SetHTMLCSSStyleSheet(sheet);
|
|
|
|
SetTo(decl.forget(), &aString);
|
2012-09-30 20:40:24 +04:00
|
|
|
|
2016-06-24 06:35:12 +03:00
|
|
|
if (cachingAllowed) {
|
|
|
|
MiscContainer* cont = GetMiscContainer();
|
|
|
|
cont->Cache();
|
2012-09-30 20:40:24 +04:00
|
|
|
}
|
|
|
|
|
2016-06-24 06:35:12 +03:00
|
|
|
return true;
|
2012-09-30 20:40:24 +04:00
|
|
|
}
|
|
|
|
|
2008-09-26 14:39:18 +04:00
|
|
|
void nsAttrValue::SetMiscAtomOrString(const nsAString* aValue) {
|
|
|
|
NS_ASSERTION(GetMiscContainer(), "Must have MiscContainer!");
|
2018-01-30 21:29:14 +03:00
|
|
|
NS_ASSERTION(!GetMiscContainer()->mStringBits || IsInServoTraversal(),
|
2008-09-26 14:39:18 +04:00
|
|
|
"Trying to re-set atom or string!");
|
|
|
|
if (aValue) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = aValue->Length();
|
2016-10-18 07:29:03 +03:00
|
|
|
// * We're allowing eCSSDeclaration attributes to store empty
|
|
|
|
// strings as it can be beneficial to store an empty style
|
|
|
|
// attribute as a parsed rule.
|
2011-09-01 02:09:58 +04:00
|
|
|
// * We're allowing enumerated values because sometimes the empty
|
|
|
|
// string corresponds to a particular enumerated value, especially
|
|
|
|
// for enumerated values that are not limited enumerated.
|
2010-08-13 10:03:23 +04:00
|
|
|
// Add other types as needed.
|
2016-10-18 07:29:03 +03:00
|
|
|
NS_ASSERTION(len || Type() == eCSSDeclaration || Type() == eEnum,
|
2011-09-01 02:09:58 +04:00
|
|
|
"Empty string?");
|
2008-09-26 14:39:18 +04:00
|
|
|
MiscContainer* cont = GetMiscContainer();
|
2018-01-30 21:29:14 +03:00
|
|
|
|
2008-09-26 14:39:18 +04:00
|
|
|
if (len <= NS_ATTRVALUE_MAX_STRINGLENGTH_ATOM) {
|
2018-01-30 21:29:14 +03:00
|
|
|
nsAtom* atom = MOZ_LIKELY(!IsInServoTraversal())
|
|
|
|
? NS_AtomizeMainThread(*aValue).take()
|
|
|
|
: NS_Atomize(*aValue).take();
|
|
|
|
NS_ENSURE_TRUE_VOID(atom);
|
|
|
|
uintptr_t bits = reinterpret_cast<uintptr_t>(atom) | eAtomBase;
|
|
|
|
|
|
|
|
// In the common case we're not in the servo traversal, and we can just
|
|
|
|
// set the bits normally. The parallel case requires more care.
|
|
|
|
if (MOZ_LIKELY(!IsInServoTraversal())) {
|
|
|
|
cont->SetStringBitsMainThread(bits);
|
|
|
|
} else if (!cont->mStringBits.compareExchange(0, bits)) {
|
|
|
|
// We raced with somebody else setting the bits. Release our copy.
|
|
|
|
atom->Release();
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-30 21:29:14 +03:00
|
|
|
nsStringBuffer* buffer = GetStringBuffer(*aValue).take();
|
|
|
|
NS_ENSURE_TRUE_VOID(buffer);
|
|
|
|
uintptr_t bits = reinterpret_cast<uintptr_t>(buffer) | eStringBase;
|
|
|
|
|
|
|
|
// In the common case we're not in the servo traversal, and we can just
|
|
|
|
// set the bits normally. The parallel case requires more care.
|
|
|
|
if (MOZ_LIKELY(!IsInServoTraversal())) {
|
|
|
|
cont->SetStringBitsMainThread(bits);
|
|
|
|
} else if (!cont->mStringBits.compareExchange(0, bits)) {
|
|
|
|
// We raced with somebody else setting the bits. Release our copy.
|
|
|
|
buffer->Release();
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsAttrValue::ResetMiscAtomOrString() {
|
|
|
|
MiscContainer* cont = GetMiscContainer();
|
2009-03-10 16:51:34 +03:00
|
|
|
void* ptr = MISC_STR_PTR(cont);
|
2008-09-26 14:39:18 +04:00
|
|
|
if (ptr) {
|
|
|
|
if (static_cast<ValueBaseType>(cont->mStringBits &
|
|
|
|
NS_ATTRVALUE_BASETYPE_MASK) == eStringBase) {
|
|
|
|
static_cast<nsStringBuffer*>(ptr)->Release();
|
|
|
|
} else {
|
2017-10-03 01:05:19 +03:00
|
|
|
static_cast<nsAtom*>(ptr)->Release();
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
2018-01-30 21:29:14 +03:00
|
|
|
cont->SetStringBitsMainThread(0);
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
2008-07-09 12:22:20 +04:00
|
|
|
}
|
|
|
|
|
2012-02-16 03:40:46 +04:00
|
|
|
void nsAttrValue::SetSVGType(ValueType aType, const void* aValue,
|
|
|
|
const nsAString* aSerialized) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(IsSVGType(aType), "Not an SVG type");
|
2012-09-30 20:40:24 +04:00
|
|
|
|
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
|
|
|
// All SVG types are just pointers to classes so just setting any of them
|
|
|
|
// will do. We'll lose type-safety but the signature of the calling
|
|
|
|
// function should ensure we don't get anything unexpected, and once we
|
|
|
|
// stick aValue in a union we lose type information anyway.
|
2019-04-09 23:04:33 +03:00
|
|
|
cont->mValue.mSVGLength = static_cast<const SVGAnimatedLength*>(aValue);
|
2012-09-30 20:40:24 +04:00
|
|
|
cont->mType = aType;
|
|
|
|
SetMiscAtomOrString(aSerialized);
|
2012-02-16 03:40:46 +04:00
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* nsAttrValue::ClearMiscContainer() {
|
|
|
|
MiscContainer* cont = nullptr;
|
2004-03-04 05:06:28 +03:00
|
|
|
if (BaseType() == eOtherBase) {
|
|
|
|
cont = GetMiscContainer();
|
2012-09-30 20:40:24 +04:00
|
|
|
if (cont->IsRefCounted() && cont->mValue.mRefCount > 1) {
|
|
|
|
// This MiscContainer is shared, we need a new one.
|
|
|
|
NS_RELEASE(cont);
|
|
|
|
|
2018-07-24 12:10:00 +03:00
|
|
|
cont = AllocMiscContainer();
|
2012-09-30 20:40:24 +04:00
|
|
|
SetPtrValueAndType(cont, eOtherBase);
|
|
|
|
} else {
|
|
|
|
switch (cont->mType) {
|
2016-10-18 07:29:03 +03:00
|
|
|
case eCSSDeclaration: {
|
2012-09-30 20:40:24 +04:00
|
|
|
MOZ_ASSERT(cont->mValue.mRefCount == 1);
|
|
|
|
cont->Release();
|
|
|
|
cont->Evict();
|
2016-10-18 07:29:03 +03:00
|
|
|
NS_RELEASE(cont->mValue.mCSSDeclaration);
|
2012-09-30 20:40:24 +04:00
|
|
|
break;
|
|
|
|
}
|
2019-11-06 21:03:51 +03:00
|
|
|
case eShadowParts: {
|
|
|
|
MOZ_ASSERT(cont->mValue.mRefCount == 1);
|
|
|
|
cont->Release();
|
|
|
|
delete cont->mValue.mShadowParts;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-30 20:40:24 +04:00
|
|
|
case eURL: {
|
|
|
|
NS_RELEASE(cont->mValue.mURL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eAtomArray: {
|
|
|
|
delete cont->mValue.mAtomArray;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eIntMarginValue: {
|
|
|
|
delete cont->mValue.mIntMargin;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
}
|
2012-09-30 20:40:24 +04:00
|
|
|
ResetMiscAtomOrString();
|
2004-03-04 05:06:28 +03:00
|
|
|
} else {
|
|
|
|
ResetIfSet();
|
2012-09-30 20:40:24 +04:00
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
return cont;
|
|
|
|
}
|
2004-03-04 05:06:28 +03:00
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* nsAttrValue::EnsureEmptyMiscContainer() {
|
|
|
|
MiscContainer* cont = ClearMiscContainer();
|
|
|
|
if (cont) {
|
|
|
|
MOZ_ASSERT(BaseType() == eOtherBase);
|
|
|
|
ResetMiscAtomOrString();
|
|
|
|
cont = GetMiscContainer();
|
|
|
|
} else {
|
2018-07-24 12:10:00 +03:00
|
|
|
cont = AllocMiscContainer();
|
2004-03-04 05:06:28 +03:00
|
|
|
SetPtrValueAndType(cont, eOtherBase);
|
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
return cont;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nsAttrValue::EnsureEmptyAtomArray() {
|
|
|
|
if (Type() == eAtomArray) {
|
2008-09-26 14:39:18 +04:00
|
|
|
ResetMiscAtomOrString();
|
2004-03-04 05:06:28 +03:00
|
|
|
GetAtomArrayValue()->Clear();
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
MiscContainer* cont = EnsureEmptyMiscContainer();
|
2015-11-19 10:13:49 +03:00
|
|
|
cont->mValue.mAtomArray = new AtomArray;
|
2004-03-04 05:06:28 +03:00
|
|
|
cont->mType = eAtomArray;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-03-04 05:06:28 +03:00
|
|
|
}
|
2008-09-26 14:39:18 +04:00
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
already_AddRefed<nsStringBuffer> nsAttrValue::GetStringBuffer(
|
2008-09-26 14:39:18 +04:00
|
|
|
const nsAString& aValue) const {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = aValue.Length();
|
2008-09-26 14:39:18 +04:00
|
|
|
if (!len) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsStringBuffer> buf = nsStringBuffer::FromString(aValue);
|
2014-01-04 19:02:17 +04:00
|
|
|
if (buf && (buf->StorageSize() / sizeof(char16_t) - 1) == len) {
|
2013-04-22 15:13:22 +04:00
|
|
|
return buf.forget();
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
buf = nsStringBuffer::Alloc((len + 1) * sizeof(char16_t));
|
2008-09-26 14:39:18 +04:00
|
|
|
if (!buf) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* data = static_cast<char16_t*>(buf->Data());
|
2008-09-26 14:39:18 +04:00
|
|
|
CopyUnicodeTo(aValue, 0, data, len);
|
2014-01-04 19:02:17 +04:00
|
|
|
data[len] = char16_t(0);
|
2013-04-22 15:13:22 +04:00
|
|
|
return buf.forget();
|
2008-09-26 14:39:18 +04:00
|
|
|
}
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t nsAttrValue::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
2012-02-02 01:58:01 +04:00
|
|
|
size_t n = 0;
|
2011-08-11 02:54:19 +04:00
|
|
|
|
|
|
|
switch (BaseType()) {
|
|
|
|
case eStringBase: {
|
|
|
|
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
|
2012-02-02 01:58:01 +04:00
|
|
|
n += str ? str->SizeOfIncludingThisIfUnshared(aMallocSizeOf) : 0;
|
2011-08-11 02:54:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eOtherBase: {
|
|
|
|
MiscContainer* container = GetMiscContainer();
|
|
|
|
if (!container) {
|
|
|
|
break;
|
|
|
|
}
|
2014-06-19 13:14:41 +04:00
|
|
|
if (container->IsRefCounted() && container->mValue.mRefCount > 1) {
|
|
|
|
// We don't report this MiscContainer at all in order to avoid
|
|
|
|
// twice-reporting it.
|
|
|
|
// TODO DMD, bug 1027551 - figure out how to report this ref-counted
|
|
|
|
// object just once.
|
|
|
|
break;
|
|
|
|
}
|
2012-02-02 01:58:01 +04:00
|
|
|
n += aMallocSizeOf(container);
|
2011-08-11 02:54:19 +04:00
|
|
|
|
|
|
|
void* otherPtr = MISC_STR_PTR(container);
|
|
|
|
// We only count the size of the object pointed by otherPtr if it's a
|
|
|
|
// string. When it's an atom, it's counted separatly.
|
|
|
|
if (otherPtr && static_cast<ValueBaseType>(container->mStringBits &
|
|
|
|
NS_ATTRVALUE_BASETYPE_MASK) ==
|
|
|
|
eStringBase) {
|
|
|
|
nsStringBuffer* str = static_cast<nsStringBuffer*>(otherPtr);
|
2012-02-02 01:58:01 +04:00
|
|
|
n += str ? str->SizeOfIncludingThisIfUnshared(aMallocSizeOf) : 0;
|
2011-08-11 02:54:19 +04:00
|
|
|
}
|
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
if (Type() == eCSSDeclaration && container->mValue.mCSSDeclaration) {
|
|
|
|
// TODO: mCSSDeclaration might be owned by another object which
|
2016-06-24 06:35:12 +03:00
|
|
|
// would make us count them twice, bug 677493.
|
2018-05-30 19:15:25 +03:00
|
|
|
// Bug 1281964: For DeclarationBlock if we do measure we'll
|
2016-10-18 07:29:03 +03:00
|
|
|
// need a way to call the Servo heap_size_of function.
|
|
|
|
// n += container->mCSSDeclaration->SizeOfIncludingThis(aMallocSizeOf);
|
2012-09-30 20:40:24 +04:00
|
|
|
} else if (Type() == eAtomArray && container->mValue.mAtomArray) {
|
2017-10-03 01:05:19 +03:00
|
|
|
// Don't measure each nsAtom, they are measured separatly.
|
2015-07-29 09:24:24 +03:00
|
|
|
n += container->mValue.mAtomArray->ShallowSizeOfIncludingThis(
|
|
|
|
aMallocSizeOf);
|
2011-08-11 02:54:19 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-02-02 01:58:01 +04:00
|
|
|
case eAtomBase: // Atoms are counted separately.
|
2011-08-11 02:54:19 +04:00
|
|
|
case eIntegerBase: // The value is in mBits, nothing to do.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-02 01:58:01 +04:00
|
|
|
return n;
|
2011-08-11 02:54:19 +04:00
|
|
|
}
|