forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsAttrValue.cpp
1942 lines (1718 loc) · 47.8 KB
/
nsAttrValue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
/*
* A struct that represents the value (type and actual data) of an
* attribute.
*/
#include "mozilla/DebugOnly.h"
#include "mozilla/HashFunctions.h"
#include "nsAttrValue.h"
#include "nsAttrValueInlines.h"
#include "nsIAtom.h"
#include "nsUnicharUtils.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/css/StyleRule.h"
#include "mozilla/css/Declaration.h"
#include "nsContentUtils.h"
#include "nsReadableUtils.h"
#include "prprf.h"
#include "nsHTMLCSSStyleSheet.h"
#include "nsCSSParser.h"
#include "nsStyledElement.h"
#include "nsIURI.h"
#include "nsIDocument.h"
#include <algorithm>
using namespace mozilla;
#define MISC_STR_PTR(_cont) \
reinterpret_cast<void*>((_cont)->mStringBits & NS_ATTRVALUE_POINTERVALUE_MASK)
bool
MiscContainer::GetString(nsAString& aString) const
{
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;
}
buffer->ToString(buffer->StorageSize() / sizeof(char16_t) - 1, aString);
return true;
}
nsIAtom* atom = static_cast<nsIAtom*>(ptr);
if (!atom) {
return false;
}
atom->ToString(aString);
return true;
}
void
MiscContainer::Cache()
{
// Not implemented for anything else yet.
MOZ_ASSERT(mType == nsAttrValue::eCSSStyleRule);
MOZ_ASSERT(IsRefCounted());
MOZ_ASSERT(mValue.mRefCount > 0);
MOZ_ASSERT(!mValue.mCached);
css::StyleRule* rule = mValue.mCSSStyleRule;
nsHTMLCSSStyleSheet* sheet = rule->GetHTMLCSSStyleSheet();
if (!sheet) {
return;
}
nsString str;
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.
css::Declaration* decl = rule->GetDeclaration();
if (decl) {
decl->SetImmutable();
}
}
void
MiscContainer::Evict()
{
// Not implemented for anything else yet.
MOZ_ASSERT(mType == nsAttrValue::eCSSStyleRule);
MOZ_ASSERT(IsRefCounted());
MOZ_ASSERT(mValue.mRefCount == 0);
if (!mValue.mCached) {
return;
}
css::StyleRule* rule = mValue.mCSSStyleRule;
nsHTMLCSSStyleSheet* sheet = rule->GetHTMLCSSStyleSheet();
MOZ_ASSERT(sheet);
nsString str;
DebugOnly<bool> gotString = GetString(str);
MOZ_ASSERT(gotString);
sheet->EvictStyleAttr(str, this);
mValue.mCached = 0;
}
nsTArray<const nsAttrValue::EnumTable*>* nsAttrValue::sEnumTableArray = nullptr;
nsAttrValue::nsAttrValue()
: mBits(0)
{
}
nsAttrValue::nsAttrValue(const nsAttrValue& aOther)
: mBits(0)
{
SetTo(aOther);
}
nsAttrValue::nsAttrValue(const nsAString& aValue)
: mBits(0)
{
SetTo(aValue);
}
nsAttrValue::nsAttrValue(nsIAtom* aValue)
: mBits(0)
{
SetTo(aValue);
}
nsAttrValue::nsAttrValue(css::StyleRule* aValue, const nsAString* aSerialized)
: mBits(0)
{
SetTo(aValue, aSerialized);
}
nsAttrValue::nsAttrValue(const nsIntMargin& aValue)
: mBits(0)
{
SetTo(aValue);
}
nsAttrValue::~nsAttrValue()
{
ResetIfSet();
}
/* static */
nsresult
nsAttrValue::Init()
{
NS_ASSERTION(!sEnumTableArray, "nsAttrValue already initialized");
sEnumTableArray = new nsTArray<const EnumTable*>;
NS_ENSURE_TRUE(sEnumTableArray, NS_ERROR_OUT_OF_MEMORY);
return NS_OK;
}
/* static */
void
nsAttrValue::Shutdown()
{
delete sEnumTableArray;
sEnumTableArray = nullptr;
}
nsAttrValue::ValueType
nsAttrValue::Type() const
{
switch (BaseType()) {
case eIntegerBase:
{
return static_cast<ValueType>(mBits & NS_ATTRVALUE_INTEGERTYPE_MASK);
}
case eOtherBase:
{
return GetMiscContainer()->mType;
}
default:
{
return static_cast<ValueType>(static_cast<uint16_t>(BaseType()));
}
}
}
void
nsAttrValue::Reset()
{
switch(BaseType()) {
case eStringBase:
{
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
if (str) {
str->Release();
}
break;
}
case eOtherBase:
{
MiscContainer* cont = GetMiscContainer();
if (cont->IsRefCounted() && cont->mValue.mRefCount > 1) {
NS_RELEASE(cont);
break;
}
delete ClearMiscContainer();
break;
}
case eAtomBase:
{
nsIAtom* atom = GetAtomValue();
NS_RELEASE(atom);
break;
}
case eIntegerBase:
{
break;
}
}
mBits = 0;
}
void
nsAttrValue::SetTo(const nsAttrValue& aOther)
{
if (this == &aOther) {
return;
}
switch (aOther.BaseType()) {
case eStringBase:
{
ResetIfSet();
nsStringBuffer* str = static_cast<nsStringBuffer*>(aOther.GetPtr());
if (str) {
str->AddRef();
SetPtrValueAndType(str, eStringBase);
}
return;
}
case eOtherBase:
{
break;
}
case eAtomBase:
{
ResetIfSet();
nsIAtom* atom = aOther.GetAtomValue();
NS_ADDREF(atom);
SetPtrValueAndType(atom, eAtomBase);
return;
}
case eIntegerBase:
{
ResetIfSet();
mBits = aOther.mBits;
return;
}
}
MiscContainer* otherCont = aOther.GetMiscContainer();
if (otherCont->IsRefCounted()) {
delete ClearMiscContainer();
NS_ADDREF(otherCont);
SetPtrValueAndType(otherCont, eOtherBase);
return;
}
MiscContainer* cont = EnsureEmptyMiscContainer();
switch (otherCont->mType) {
case eInteger:
{
cont->mValue.mInteger = otherCont->mValue.mInteger;
break;
}
case eEnum:
{
cont->mValue.mEnumValue = otherCont->mValue.mEnumValue;
break;
}
case ePercent:
{
cont->mValue.mPercent = otherCont->mValue.mPercent;
break;
}
case eColor:
{
cont->mValue.mColor = otherCont->mValue.mColor;
break;
}
case eCSSStyleRule:
{
MOZ_CRASH("These should be refcounted!");
}
case eURL:
{
NS_ADDREF(cont->mValue.mURL = otherCont->mValue.mURL);
break;
}
case eImage:
{
NS_ADDREF(cont->mValue.mImage = otherCont->mValue.mImage);
break;
}
case eAtomArray:
{
if (!EnsureEmptyAtomArray() ||
!GetAtomArrayValue()->AppendElements(*otherCont->mValue.mAtomArray)) {
Reset();
return;
}
break;
}
case eDoubleValue:
{
cont->mDoubleValue = otherCont->mDoubleValue;
break;
}
case eIntMarginValue:
{
if (otherCont->mValue.mIntMargin)
cont->mValue.mIntMargin =
new nsIntMargin(*otherCont->mValue.mIntMargin);
break;
}
default:
{
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
cont->mValue.mSVGAngle = otherCont->mValue.mSVGAngle;
} else {
NS_NOTREACHED("unknown type stored in MiscContainer");
}
break;
}
}
void* otherPtr = MISC_STR_PTR(otherCont);
if (otherPtr) {
if (static_cast<ValueBaseType>(otherCont->mStringBits & NS_ATTRVALUE_BASETYPE_MASK) ==
eStringBase) {
static_cast<nsStringBuffer*>(otherPtr)->AddRef();
} else {
static_cast<nsIAtom*>(otherPtr)->AddRef();
}
cont->mStringBits = otherCont->mStringBits;
}
// Note, set mType after switch-case, otherwise EnsureEmptyAtomArray doesn't
// work correctly.
cont->mType = otherCont->mType;
}
void
nsAttrValue::SetTo(const nsAString& aValue)
{
ResetIfSet();
nsStringBuffer* buf = GetStringBuffer(aValue).take();
if (buf) {
SetPtrValueAndType(buf, eStringBase);
}
}
void
nsAttrValue::SetTo(nsIAtom* aValue)
{
ResetIfSet();
if (aValue) {
NS_ADDREF(aValue);
SetPtrValueAndType(aValue, eAtomBase);
}
}
void
nsAttrValue::SetTo(int16_t aInt)
{
ResetIfSet();
SetIntValueAndType(aInt, eInteger, nullptr);
}
void
nsAttrValue::SetTo(int32_t aInt, const nsAString* aSerialized)
{
ResetIfSet();
SetIntValueAndType(aInt, eInteger, aSerialized);
}
void
nsAttrValue::SetTo(double aValue, const nsAString* aSerialized)
{
MiscContainer* cont = EnsureEmptyMiscContainer();
cont->mDoubleValue = aValue;
cont->mType = eDoubleValue;
SetMiscAtomOrString(aSerialized);
}
void
nsAttrValue::SetTo(css::StyleRule* aValue, const nsAString* aSerialized)
{
MiscContainer* cont = EnsureEmptyMiscContainer();
MOZ_ASSERT(cont->mValue.mRefCount == 0);
NS_ADDREF(cont->mValue.mCSSStyleRule = aValue);
cont->mType = eCSSStyleRule;
NS_ADDREF(cont);
SetMiscAtomOrString(aSerialized);
MOZ_ASSERT(cont->mValue.mRefCount == 1);
}
void
nsAttrValue::SetTo(css::URLValue* aValue, const nsAString* aSerialized)
{
MiscContainer* cont = EnsureEmptyMiscContainer();
NS_ADDREF(cont->mValue.mURL = aValue);
cont->mType = eURL;
SetMiscAtomOrString(aSerialized);
}
void
nsAttrValue::SetTo(const nsIntMargin& aValue)
{
MiscContainer* cont = EnsureEmptyMiscContainer();
cont->mValue.mIntMargin = new nsIntMargin(aValue);
cont->mType = eIntMarginValue;
}
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);
}
}
void
nsAttrValue::SetTo(const nsSVGAngle& aValue, const nsAString* aSerialized)
{
SetSVGType(eSVGAngle, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const nsSVGIntegerPair& aValue, const nsAString* aSerialized)
{
SetSVGType(eSVGIntegerPair, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const nsSVGLength2& aValue, const nsAString* aSerialized)
{
SetSVGType(eSVGLength, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const SVGLengthList& aValue,
const nsAString* aSerialized)
{
// 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()) {
aSerialized = nullptr;
}
SetSVGType(eSVGLengthList, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const SVGNumberList& aValue,
const nsAString* aSerialized)
{
// 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()) {
aSerialized = nullptr;
}
SetSVGType(eSVGNumberList, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const nsSVGNumberPair& aValue, const nsAString* aSerialized)
{
SetSVGType(eSVGNumberPair, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const SVGPathData& aValue,
const nsAString* aSerialized)
{
// 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()) {
aSerialized = nullptr;
}
SetSVGType(eSVGPathData, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const SVGPointList& aValue,
const nsAString* aSerialized)
{
// 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()) {
aSerialized = nullptr;
}
SetSVGType(eSVGPointList, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const SVGAnimatedPreserveAspectRatio& aValue,
const nsAString* aSerialized)
{
SetSVGType(eSVGPreserveAspectRatio, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const SVGStringList& aValue,
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()) {
aSerialized = nullptr;
}
SetSVGType(eSVGStringList, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const SVGTransformList& aValue,
const nsAString* aSerialized)
{
// 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()) {
aSerialized = nullptr;
}
SetSVGType(eSVGTransformList, &aValue, aSerialized);
}
void
nsAttrValue::SetTo(const nsSVGViewBox& aValue, const nsAString* aSerialized)
{
SetSVGType(eSVGViewBox, &aValue, aSerialized);
}
void
nsAttrValue::SwapValueWith(nsAttrValue& aOther)
{
uintptr_t tmp = aOther.mBits;
aOther.mBits = mBits;
mBits = tmp;
}
void
nsAttrValue::ToString(nsAString& aResult) const
{
MiscContainer* cont = nullptr;
if (BaseType() == eOtherBase) {
cont = GetMiscContainer();
if (cont->GetString(aResult)) {
return;
}
}
switch(Type()) {
case eString:
{
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
if (str) {
str->ToString(str->StorageSize()/sizeof(char16_t) - 1, aResult);
}
else {
aResult.Truncate();
}
break;
}
case eAtom:
{
nsIAtom *atom = static_cast<nsIAtom*>(GetPtr());
atom->ToString(aResult);
break;
}
case eInteger:
{
nsAutoString intStr;
intStr.AppendInt(GetIntegerValue());
aResult = intStr;
break;
}
#ifdef DEBUG
case eColor:
{
NS_NOTREACHED("color attribute without string data");
aResult.Truncate();
break;
}
#endif
case eEnum:
{
GetEnumString(aResult, false);
break;
}
case ePercent:
{
nsAutoString intStr;
intStr.AppendInt(cont ? cont->mValue.mPercent : GetIntInternal());
aResult = intStr + NS_LITERAL_STRING("%");
break;
}
case eCSSStyleRule:
{
aResult.Truncate();
MiscContainer *container = GetMiscContainer();
css::Declaration *decl =
container->mValue.mCSSStyleRule->GetDeclaration();
if (decl) {
decl->ToString(aResult);
}
const_cast<nsAttrValue*>(this)->SetMiscAtomOrString(&aResult);
break;
}
case eDoubleValue:
{
aResult.Truncate();
aResult.AppendFloat(GetDoubleValue());
break;
}
case eSVGAngle:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGAngle,
aResult);
break;
}
case eSVGIntegerPair:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGIntegerPair,
aResult);
break;
}
case eSVGLength:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGLength,
aResult);
break;
}
case eSVGLengthList:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGLengthList,
aResult);
break;
}
case eSVGNumberList:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGNumberList,
aResult);
break;
}
case eSVGNumberPair:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGNumberPair,
aResult);
break;
}
case eSVGPathData:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGPathData,
aResult);
break;
}
case eSVGPointList:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGPointList,
aResult);
break;
}
case eSVGPreserveAspectRatio:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGPreserveAspectRatio,
aResult);
break;
}
case eSVGStringList:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGStringList,
aResult);
break;
}
case eSVGTransformList:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGTransformList,
aResult);
break;
}
case eSVGViewBox:
{
SVGAttrValueWrapper::ToString(GetMiscContainer()->mValue.mSVGViewBox,
aResult);
break;
}
default:
{
aResult.Truncate();
break;
}
}
}
already_AddRefed<nsIAtom>
nsAttrValue::GetAsAtom() const
{
switch (Type()) {
case eString:
return do_GetAtom(GetStringValue());
case eAtom:
{
nsCOMPtr<nsIAtom> atom = GetAtomValue();
return atom.forget();
}
default:
{
nsAutoString val;
ToString(val);
return do_GetAtom(val);
}
}
}
const nsCheapString
nsAttrValue::GetStringValue() const
{
NS_PRECONDITION(Type() == eString, "wrong type");
return nsCheapString(static_cast<nsStringBuffer*>(GetPtr()));
}
bool
nsAttrValue::GetColorValue(nscolor& aColor) const
{
if (Type() != eColor) {
// Unparseable value, treat as unset.
NS_ASSERTION(Type() == eString, "unexpected type for color-valued attr");
return false;
}
aColor = GetMiscContainer()->mValue.mColor;
return true;
}
void
nsAttrValue::GetEnumString(nsAString& aResult, bool aRealTag) const
{
NS_PRECONDITION(Type() == eEnum, "wrong type");
uint32_t allEnumBits =
(BaseType() == eIntegerBase) ? static_cast<uint32_t>(GetIntInternal())
: GetMiscContainer()->mValue.mEnumValue;
int16_t val = allEnumBits >> NS_ATTRVALUE_ENUMTABLEINDEX_BITS;
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) {
nsContentUtils::ASCIIToUpper(aResult);
}
return;
}
table++;
}
NS_NOTREACHED("couldn't find value in EnumTable");
}
uint32_t
nsAttrValue::GetAtomCount() const
{
ValueType type = Type();
if (type == eAtom) {
return 1;
}
if (type == eAtomArray) {
return GetAtomArrayValue()->Length();
}
return 0;
}
nsIAtom*
nsAttrValue::AtomAt(int32_t aIndex) const
{
NS_PRECONDITION(aIndex >= 0, "Index must not be negative");
NS_PRECONDITION(GetAtomCount() > uint32_t(aIndex), "aIndex out of range");
if (BaseType() == eAtomBase) {
return GetAtomValue();
}
NS_ASSERTION(Type() == eAtomArray, "GetAtomCount must be confused");
return GetAtomArrayValue()->ElementAt(aIndex);
}
uint32_t
nsAttrValue::HashValue() const
{
switch(BaseType()) {
case eStringBase:
{
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
if (str) {
uint32_t len = str->StorageSize()/sizeof(char16_t) - 1;
return HashString(static_cast<char16_t*>(str->Data()), len);
}
return 0;
}
case eOtherBase:
{
break;
}
case eAtomBase:
case eIntegerBase:
{
// mBits and uint32_t might have different size. This should silence
// 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;
}
}
MiscContainer* cont = GetMiscContainer();
if (static_cast<ValueBaseType>(cont->mStringBits & NS_ATTRVALUE_BASETYPE_MASK)
== eAtomBase) {
return cont->mStringBits - 0;
}
switch (cont->mType) {
case eInteger:
{
return cont->mValue.mInteger;
}
case eEnum:
{
return cont->mValue.mEnumValue;
}
case ePercent:
{
return cont->mValue.mPercent;
}
case eColor:
{
return cont->mValue.mColor;
}
case eCSSStyleRule:
{
return NS_PTR_TO_INT32(cont->mValue.mCSSStyleRule);
}
// Intentionally identical, so that loading the image does not change the
// hash code.
case eURL:
case eImage:
{
nsString str;
ToString(str);
return HashString(str);
}
case eAtomArray:
{
uint32_t hash = 0;
uint32_t count = cont->mValue.mAtomArray->Length();
for (nsCOMPtr<nsIAtom> *cur = cont->mValue.mAtomArray->Elements(),
*end = cur + count;
cur != end; ++cur) {
hash = AddToHash(hash, cur->get());
}
return hash;
}
case eDoubleValue:
{
// XXX this is crappy, but oh well
return cont->mDoubleValue;
}
case eIntMarginValue:
{
return NS_PTR_TO_INT32(cont->mValue.mIntMargin);
}
default:
{
if (IsSVGType(cont->mType)) {
// All SVG types are just pointers to classes so we can treat them alike
return NS_PTR_TO_INT32(cont->mValue.mSVGAngle);
}
NS_NOTREACHED("unknown type stored in MiscContainer");
return 0;
}
}
}
bool
nsAttrValue::Equals(const nsAttrValue& aOther) const
{
if (BaseType() != aOther.BaseType()) {
return false;
}
switch(BaseType()) {
case eStringBase:
{
return GetStringValue().Equals(aOther.GetStringValue());
}
case eOtherBase:
{
break;
}
case eAtomBase:
case eIntegerBase:
{
return mBits == aOther.mBits;
}
}
MiscContainer* thisCont = GetMiscContainer();
MiscContainer* otherCont = aOther.GetMiscContainer();
if (thisCont == otherCont) {
return true;
}
if (thisCont->mType != otherCont->mType) {
return false;
}
bool needsStringComparison = false;
switch (thisCont->mType) {
case eInteger:
{
if (thisCont->mValue.mInteger == otherCont->mValue.mInteger) {
needsStringComparison = true;
}
break;
}
case eEnum:
{
if (thisCont->mValue.mEnumValue == otherCont->mValue.mEnumValue) {
needsStringComparison = true;
}
break;
}
case ePercent:
{
if (thisCont->mValue.mPercent == otherCont->mValue.mPercent) {
needsStringComparison = true;
}
break;
}
case eColor:
{
if (thisCont->mValue.mColor == otherCont->mValue.mColor) {
needsStringComparison = true;
}
break;
}
case eCSSStyleRule:
{
return thisCont->mValue.mCSSStyleRule == otherCont->mValue.mCSSStyleRule;
}
case eURL:
{