1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
19
* Portions created by the Initial Developer are Copyright (C) 2003
20
* IBM Corporation. All Rights Reserved.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#include "nsAttrValue.h"
40
#include "nsHTMLValue.h"
42
#include "nsUnicharUtils.h"
43
#include "nsICSSStyleRule.h"
44
#include "nsCSSDeclaration.h"
45
#include "nsIHTMLDocument.h"
46
#include "nsIDocument.h"
49
#include "nsISVGValue.h"
52
nsAttrValue::nsAttrValue()
57
nsAttrValue::nsAttrValue(const nsAttrValue& aOther)
63
nsAttrValue::nsAttrValue(const nsAString& aValue)
69
nsAttrValue::nsAttrValue(nsICSSStyleRule* aValue)
76
nsAttrValue::nsAttrValue(nsISVGValue* aValue)
83
nsAttrValue::~nsAttrValue()
88
nsAttrValue::ValueType
89
nsAttrValue::Type() const
94
return NS_STATIC_CAST(ValueType, mBits & NS_ATTRVALUE_INTEGERTYPE_MASK);
98
return GetMiscContainer()->mType;
102
return NS_STATIC_CAST(ValueType, NS_STATIC_CAST(PRUint16, BaseType()));
113
PRUnichar* str = NS_STATIC_CAST(PRUnichar*, GetPtr());
115
nsCheapStringBufferUtils::Free(str);
122
EnsureEmptyMiscContainer();
123
delete GetMiscContainer();
129
nsIAtom* atom = GetAtomValue();
144
nsAttrValue::SetTo(const nsAttrValue& aOther)
146
switch (aOther.BaseType()) {
149
SetTo(aOther.GetStringValue());
159
nsIAtom* atom = aOther.GetAtomValue();
161
SetPtrValueAndType(atom, eAtomBase);
167
mBits = aOther.mBits;
172
MiscContainer* otherCont = aOther.GetMiscContainer();
173
switch (otherCont->mType) {
176
if (EnsureEmptyMiscContainer()) {
177
MiscContainer* cont = GetMiscContainer();
178
cont->mColor = otherCont->mColor;
179
cont->mType = eColor;
185
SetTo(otherCont->mCSSStyleRule);
190
if (!EnsureEmptyAtomArray() ||
191
!GetAtomArrayValue()->AppendObjects(*otherCont->mAtomArray)) {
199
SetTo(otherCont->mSVGValue);
204
NS_NOTREACHED("unknown type stored in MiscContainer");
211
nsAttrValue::SetTo(const nsAString& aValue)
213
PRUnichar* str = nsnull;
214
PRBool empty = aValue.IsEmpty();
216
if (BaseType() == eStringBase && (ptr = GetPtr())) {
218
nsCheapStringBufferUtils::
219
CopyToExistingBuffer(str, NS_STATIC_CAST(PRUnichar*, ptr), aValue);
222
nsCheapStringBufferUtils::Free(NS_STATIC_CAST(PRUnichar*, ptr));
228
nsCheapStringBufferUtils::CopyToBuffer(str, aValue);
231
SetPtrValueAndType(str, eStringBase);
235
nsAttrValue::SetTo(PRInt16 aInt, ValueType aType)
238
SetIntValueAndType(aInt, aType);
242
nsAttrValue::SetTo(nsICSSStyleRule* aValue)
244
if (EnsureEmptyMiscContainer()) {
245
MiscContainer* cont = GetMiscContainer();
246
NS_ADDREF(cont->mCSSStyleRule = aValue);
247
cont->mType = eCSSStyleRule;
253
nsAttrValue::SetTo(nsISVGValue* aValue)
255
if (EnsureEmptyMiscContainer()) {
256
MiscContainer* cont = GetMiscContainer();
257
NS_ADDREF(cont->mSVGValue = aValue);
258
cont->mType = eSVGValue;
264
nsAttrValue::SwapValueWith(nsAttrValue& aOther)
266
PtrBits tmp = aOther.mBits;
267
aOther.mBits = mBits;
272
nsAttrValue::ToString(nsAString& aResult) const
277
PRUnichar* str = NS_STATIC_CAST(PRUnichar*, GetPtr());
279
aResult = nsCheapStringBufferUtils::GetDependentString(str);
288
NS_STATIC_CAST(nsIAtom*, GetPtr())->ToString(aResult);
295
intStr.AppendInt(GetIntInternal());
304
NS_RGBToHex(v, aResult);
311
intStr.AppendInt(GetIntInternal());
312
aResult = intStr + NS_LITERAL_STRING("*");
318
NS_NOTREACHED("trying to convert enum to string");
325
intStr.AppendInt(GetIntInternal());
326
aResult = intStr + NS_LITERAL_STRING("%");
333
nsCSSDeclaration* decl =
334
GetMiscContainer()->mCSSStyleRule->GetDeclaration();
336
decl->ToString(aResult);
343
MiscContainer* cont = GetMiscContainer();
344
PRInt32 count = cont->mAtomArray->Count();
346
cont->mAtomArray->ObjectAt(0)->ToString(aResult);
349
for (i = 1; i < count; ++i) {
350
cont->mAtomArray->ObjectAt(i)->ToString(tmp);
351
aResult.Append(NS_LITERAL_STRING(" ") + tmp);
362
GetMiscContainer()->mSVGValue->GetValueString(aResult);
369
nsAttrValue::ToHTMLValue(nsHTMLValue& aResult) const
374
aResult.SetStringValue(GetStringValue());
380
GetAtomValue()->ToString(tmp);
381
aResult.SetStringValue(tmp);
386
aResult.SetIntValue(GetIntInternal(), eHTMLUnit_Integer);
393
aResult.SetColorValue(v);
398
aResult.SetIntValue(GetProportionalValue(), eHTMLUnit_Proportional);
403
aResult.SetIntValue(GetEnumValue(), eHTMLUnit_Enumerated);
408
aResult.SetPercentValue(GetPercentValue());
413
aResult.SetCSSStyleRuleValue(GetCSSStyleRuleValue());
418
nsCOMArray<nsIAtom>* array = new nsCOMArray<nsIAtom>(*GetAtomArrayValue());
419
aResult.SetAtomArrayValue(array);
426
GetSVGValue()->GetValueString(tmp);
427
aResult.SetStringValue(tmp);
433
const nsDependentSubstring
434
nsAttrValue::GetStringValue() const
436
NS_PRECONDITION(Type() == eString, "wrong type");
438
static const PRUnichar blankStr[] = { '\0' };
439
void* ptr = GetPtr();
441
? nsCheapStringBufferUtils::GetDependentString(NS_STATIC_CAST(PRUnichar*, ptr))
442
: Substring(blankStr, blankStr);
446
nsAttrValue::GetColorValue(nscolor& aColor) const
448
NS_PRECONDITION(Type() == eColor || Type() == eString, "wrong type");
449
switch (BaseType()) {
452
return GetPtr() && NS_ColorNameToRGB(GetStringValue(), &aColor);
456
aColor = GetMiscContainer()->mColor;
462
aColor = NS_STATIC_CAST(nscolor, GetIntInternal());
468
NS_NOTREACHED("unexpected basetype");
478
nsAttrValue::GetAtomCount() const
480
ValueType type = Type();
486
if (type == eAtomArray) {
487
return GetAtomArrayValue()->Count();
494
nsAttrValue::AtomAt(PRInt32 aIndex) const
496
NS_PRECONDITION(aIndex >= 0, "Index must not be negative");
497
NS_PRECONDITION(GetAtomCount() > aIndex, "aIndex out of range");
499
if (BaseType() == eAtomBase) {
500
return GetAtomValue();
503
NS_ASSERTION(Type() == eAtomArray, "GetAtomCount must be confused");
505
return GetAtomArrayValue()->ObjectAt(aIndex);
509
nsAttrValue::HashValue() const
514
PRUnichar* str = NS_STATIC_CAST(PRUnichar*, GetPtr());
515
return str ? nsCheapStringBufferUtils::HashCode(str) : 0;
524
// mBits and PRUint32 might have different size. This should silence
525
// any warnings or compile-errors. This is what the implementation of
526
// NS_PTR_TO_INT32 does to take care of the same problem.
531
MiscContainer* cont = GetMiscContainer();
532
switch (cont->mType) {
539
return NS_PTR_TO_INT32(cont->mCSSStyleRule);
544
PRInt32 i, count = cont->mAtomArray->Count();
545
for (i = 0; i < count; ++i) {
546
retval ^= NS_PTR_TO_INT32(cont->mAtomArray->ObjectAt(i));
553
return NS_PTR_TO_INT32(cont->mSVGValue);
558
NS_NOTREACHED("unknown type stored in MiscContainer");
565
nsAttrValue::Equals(const nsAttrValue& aOther) const
567
if (BaseType() != aOther.BaseType()) {
574
return GetStringValue().Equals(aOther.GetStringValue());
583
return mBits == aOther.mBits;
587
MiscContainer* thisCont = GetMiscContainer();
588
MiscContainer* otherCont = aOther.GetMiscContainer();
589
if (thisCont->mType != otherCont->mType) {
593
switch (thisCont->mType) {
596
return thisCont->mColor == otherCont->mColor;
600
return thisCont->mCSSStyleRule == otherCont->mCSSStyleRule;
604
// For classlists we could be insensitive to order, however
605
// classlists are never mapped attributes so they are never compared.
607
PRInt32 count = thisCont->mAtomArray->Count();
608
if (count != otherCont->mAtomArray->Count()) {
613
for (i = 0; i < count; ++i) {
614
if (thisCont->mAtomArray->ObjectAt(i) !=
615
otherCont->mAtomArray->ObjectAt(i)) {
624
return thisCont->mSVGValue == otherCont->mSVGValue;
629
NS_NOTREACHED("unknown type stored in MiscContainer");
636
nsAttrValue::ParseAtom(const nsAString& aValue)
640
nsIAtom* atom = NS_NewAtom(aValue);
642
SetPtrValueAndType(atom, eAtomBase);
647
nsAttrValue::ParseAtomArray(const nsAString& aValue)
649
nsAString::const_iterator iter, end;
650
aValue.BeginReading(iter);
651
aValue.EndReading(end);
653
// skip initial whitespace
654
while (iter != end && nsCRT::IsAsciiSpace(*iter)) {
663
nsAString::const_iterator start(iter);
665
// get first - and often only - atom
668
} while (iter != end && !nsCRT::IsAsciiSpace(*iter));
670
nsCOMPtr<nsIAtom> classAtom = do_GetAtom(Substring(start, iter));
677
while (iter != end && nsCRT::IsAsciiSpace(*iter)) {
682
// we only found one classname so don't bother storing a list
684
nsIAtom* atom = nsnull;
685
classAtom.swap(atom);
686
SetPtrValueAndType(atom, eAtomBase);
690
if (!EnsureEmptyAtomArray()) {
694
nsCOMArray<nsIAtom>* array = GetAtomArrayValue();
696
if (!array->AppendObject(classAtom)) {
701
// parse the rest of the classnames
707
} while (iter != end && !nsCRT::IsAsciiSpace(*iter));
709
classAtom = do_GetAtom(Substring(start, iter));
711
if (!array->AppendObject(classAtom)) {
717
while (iter != end && nsCRT::IsAsciiSpace(*iter)) {
720
} while (iter != end);
726
nsAttrValue::ParseStringOrAtom(const nsAString& aValue)
728
PRUint32 len = aValue.Length();
729
// Don't bother with atoms if it's an empty string since
730
// we can store those efficently anyway.
731
if (len && len <= NS_ATTRVALUE_MAX_STRINGLENGTH_ATOM) {
740
nsAttrValue::ParseEnumValue(const nsAString& aValue,
741
const nsHTMLValue::EnumTable* aTable,
742
PRBool aCaseSensitive)
746
nsAutoString val(aValue);
747
while (aTable->tag) {
748
if (aCaseSensitive ? val.EqualsWithConversion(aTable->tag) :
749
val.EqualsIgnoreCase(aTable->tag)) {
750
SetIntValueAndType(aTable->value, eEnum);
761
nsAttrValue::ParseSpecialIntValue(const nsAString& aString,
762
PRBool aCanBePercent,
763
PRBool aCanBeProportional)
768
nsAutoString tmp(aString);
769
PRInt32 val = tmp.ToInteger(&ec);
772
if (aCanBeProportional) {
773
// Even if the integer could not be parsed, it might just be "*"
774
tmp.CompressWhitespace(PR_TRUE, PR_TRUE);
775
if (tmp.Length() == 1 && tmp.Last() == '*') {
776
// special case: HTML spec says a value '*' == '1*'
777
// see http://www.w3.org/TR/html4/types.html#type-multi-length
779
SetIntValueAndType(1, eProportional);
786
val = PR_MAX(val, 0);
787
val = PR_MIN(val, NS_ATTRVALUE_INTEGERTYPE_MAXVALUE);
790
// XXX RFindChar means that 5%x will be parsed!
791
if (aCanBePercent && tmp.RFindChar('%') >= 0) {
795
SetIntValueAndType(val, ePercent);
800
// XXX RFindChar means that 5*x will be parsed!
801
if (aCanBeProportional && tmp.RFindChar('*') >= 0) {
802
SetIntValueAndType(val, eProportional);
806
// Straight number is interpreted as integer
807
SetIntValueAndType(val, eInteger);
812
nsAttrValue::ParseIntWithBounds(const nsAString& aString,
813
PRInt32 aMin, PRInt32 aMax)
815
NS_PRECONDITION(aMin < aMax &&
816
aMin >= NS_ATTRVALUE_INTEGERTYPE_MINVALUE &&
817
aMax <= NS_ATTRVALUE_INTEGERTYPE_MAXVALUE, "bad boundaries");
822
PRInt32 val = PromiseFlatString(aString).ToInteger(&ec);
827
val = PR_MAX(val, aMin);
828
val = PR_MIN(val, aMax);
829
SetIntValueAndType(val, eInteger);
835
nsAttrValue::ParseColor(const nsAString& aString, nsIDocument* aDocument)
837
nsAutoString colorStr(aString);
838
colorStr.CompressWhitespace(PR_TRUE, PR_TRUE);
839
if (colorStr.IsEmpty()) {
845
// No color names begin with a '#', but numerical colors do so
846
// it is a very common first char
847
if ((colorStr.CharAt(0) != '#') && NS_ColorNameToRGB(colorStr, &color)) {
852
// Check if we are in compatibility mode
853
// XXX evil NS_HexToRGB and NS_LooseHexToRGB take nsString as argument!
854
nsCOMPtr<nsIHTMLDocument> doc(do_QueryInterface(aDocument));
855
if (doc && doc->GetCompatibilityMode() == eCompatibility_NavQuirks) {
856
NS_LooseHexToRGB(colorStr, &color);
859
if (colorStr.First() != '#') {
864
if (!NS_HexToRGB(colorStr, &color)) {
870
PRInt32 colAsInt = NS_STATIC_CAST(PRInt32, color);
871
PRInt32 tmp = colAsInt * NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER;
872
if (tmp / NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER == colAsInt) {
874
SetIntValueAndType(colAsInt, eColor);
876
else if (EnsureEmptyMiscContainer()) {
877
MiscContainer* cont = GetMiscContainer();
878
cont->mColor = color;
879
cont->mType = eColor;
886
nsAttrValue::EnsureEmptyMiscContainer()
889
if (BaseType() == eOtherBase) {
890
cont = GetMiscContainer();
891
switch (cont->mType) {
894
NS_RELEASE(cont->mCSSStyleRule);
899
delete cont->mAtomArray;
911
cont = new MiscContainer;
912
NS_ENSURE_TRUE(cont, PR_FALSE);
914
SetPtrValueAndType(cont, eOtherBase);
917
cont->mType = eColor;
924
nsAttrValue::EnsureEmptyAtomArray()
926
if (Type() == eAtomArray) {
927
GetAtomArrayValue()->Clear();
931
if (!EnsureEmptyMiscContainer()) {
932
// should already be reset
936
nsCOMArray<nsIAtom>* array = new nsCOMArray<nsIAtom>;
942
MiscContainer* cont = GetMiscContainer();
943
cont->mAtomArray = array;
944
cont->mType = eAtomArray;