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
AddRefTable(void)26 nsCSSKeywords::AddRefTable(void)
27 {
28   if (0 == gKeywordTableRefCount++) {
29     NS_ASSERTION(!gKeywordTable, "pre existing array!");
30     gKeywordTable =
31       new nsStaticCaseInsensitiveNameTable(kCSSRawKeywords, eCSSKeyword_COUNT);
32 #ifdef DEBUG
33     // Partially verify the entries.
34     int32_t index = 0;
35     for (; index < eCSSKeyword_COUNT && kCSSRawKeywords[index]; ++index) {
36       nsAutoCString temp(kCSSRawKeywords[index]);
37       NS_ASSERTION(-1 == temp.FindChar('_'), "underscore char in table");
38     }
39     NS_ASSERTION(index == eCSSKeyword_COUNT, "kCSSRawKeywords and eCSSKeyword_COUNT are out of sync");
40 #endif
41   }
42 }
43 
44 void
ReleaseTable(void)45 nsCSSKeywords::ReleaseTable(void)
46 {
47   if (0 == --gKeywordTableRefCount) {
48     if (gKeywordTable) {
49       delete gKeywordTable;
50       gKeywordTable = nullptr;
51     }
52   }
53 }
54 
55 nsCSSKeyword
LookupKeyword(const nsACString & aKeyword)56 nsCSSKeywords::LookupKeyword(const nsACString& aKeyword)
57 {
58   NS_ASSERTION(gKeywordTable, "no lookup table, needs addref");
59   if (gKeywordTable) {
60     return nsCSSKeyword(gKeywordTable->Lookup(aKeyword));
61   }
62   return eCSSKeyword_UNKNOWN;
63 }
64 
65 nsCSSKeyword
LookupKeyword(const nsAString & aKeyword)66 nsCSSKeywords::LookupKeyword(const nsAString& aKeyword)
67 {
68   NS_ASSERTION(gKeywordTable, "no lookup table, needs addref");
69   if (gKeywordTable) {
70     return nsCSSKeyword(gKeywordTable->Lookup(aKeyword));
71   }
72   return eCSSKeyword_UNKNOWN;
73 }
74 
75 const nsAFlatCString&
GetStringValue(nsCSSKeyword aKeyword)76 nsCSSKeywords::GetStringValue(nsCSSKeyword aKeyword)
77 {
78   NS_ASSERTION(gKeywordTable, "no lookup table, needs addref");
79   NS_ASSERTION(0 <= aKeyword && aKeyword < eCSSKeyword_COUNT, "out of range");
80   if (gKeywordTable) {
81     return gKeywordTable->GetStringValue(int32_t(aKeyword));
82   } else {
83     static nsDependentCString kNullStr("");
84     return kNullStr;
85   }
86 }
87 
88