|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* keywords used within CSS property values */ |
|
7 |
|
8 #include "nsCSSKeywords.h" |
|
9 #include "nsString.h" |
|
10 #include "nsStaticNameTable.h" |
|
11 |
|
12 // required to make the symbol external, so that TestCSSPropertyLookup.cpp can link with it |
|
13 extern const char* const kCSSRawKeywords[]; |
|
14 |
|
15 // define an array of all CSS keywords |
|
16 #define CSS_KEY(_name,_id) #_name, |
|
17 const char* const kCSSRawKeywords[] = { |
|
18 #include "nsCSSKeywordList.h" |
|
19 }; |
|
20 #undef CSS_KEY |
|
21 |
|
22 static int32_t gKeywordTableRefCount; |
|
23 static nsStaticCaseInsensitiveNameTable* gKeywordTable; |
|
24 |
|
25 void |
|
26 nsCSSKeywords::AddRefTable(void) |
|
27 { |
|
28 if (0 == gKeywordTableRefCount++) { |
|
29 NS_ASSERTION(!gKeywordTable, "pre existing array!"); |
|
30 gKeywordTable = new nsStaticCaseInsensitiveNameTable(); |
|
31 if (gKeywordTable) { |
|
32 #ifdef DEBUG |
|
33 { |
|
34 // let's verify the table... |
|
35 int32_t index = 0; |
|
36 for (; index < eCSSKeyword_COUNT && kCSSRawKeywords[index]; ++index) { |
|
37 nsAutoCString temp1(kCSSRawKeywords[index]); |
|
38 nsAutoCString temp2(kCSSRawKeywords[index]); |
|
39 ToLowerCase(temp1); |
|
40 NS_ASSERTION(temp1.Equals(temp2), "upper case char in table"); |
|
41 NS_ASSERTION(-1 == temp1.FindChar('_'), "underscore char in table"); |
|
42 } |
|
43 NS_ASSERTION(index == eCSSKeyword_COUNT, "kCSSRawKeywords and eCSSKeyword_COUNT are out of sync"); |
|
44 } |
|
45 #endif |
|
46 gKeywordTable->Init(kCSSRawKeywords, eCSSKeyword_COUNT); |
|
47 } |
|
48 } |
|
49 } |
|
50 |
|
51 void |
|
52 nsCSSKeywords::ReleaseTable(void) |
|
53 { |
|
54 if (0 == --gKeywordTableRefCount) { |
|
55 if (gKeywordTable) { |
|
56 delete gKeywordTable; |
|
57 gKeywordTable = nullptr; |
|
58 } |
|
59 } |
|
60 } |
|
61 |
|
62 nsCSSKeyword |
|
63 nsCSSKeywords::LookupKeyword(const nsACString& aKeyword) |
|
64 { |
|
65 NS_ASSERTION(gKeywordTable, "no lookup table, needs addref"); |
|
66 if (gKeywordTable) { |
|
67 return nsCSSKeyword(gKeywordTable->Lookup(aKeyword)); |
|
68 } |
|
69 return eCSSKeyword_UNKNOWN; |
|
70 } |
|
71 |
|
72 nsCSSKeyword |
|
73 nsCSSKeywords::LookupKeyword(const nsAString& aKeyword) |
|
74 { |
|
75 NS_ASSERTION(gKeywordTable, "no lookup table, needs addref"); |
|
76 if (gKeywordTable) { |
|
77 return nsCSSKeyword(gKeywordTable->Lookup(aKeyword)); |
|
78 } |
|
79 return eCSSKeyword_UNKNOWN; |
|
80 } |
|
81 |
|
82 const nsAFlatCString& |
|
83 nsCSSKeywords::GetStringValue(nsCSSKeyword aKeyword) |
|
84 { |
|
85 NS_ASSERTION(gKeywordTable, "no lookup table, needs addref"); |
|
86 NS_ASSERTION(0 <= aKeyword && aKeyword < eCSSKeyword_COUNT, "out of range"); |
|
87 if (gKeywordTable) { |
|
88 return gKeywordTable->GetStringValue(int32_t(aKeyword)); |
|
89 } else { |
|
90 static nsDependentCString kNullStr(""); |
|
91 return kNullStr; |
|
92 } |
|
93 } |
|
94 |