nsCSSKeywords.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /* keywords used within CSS property values */
  6. #include "nsCSSKeywords.h"
  7. #include "nsString.h"
  8. #include "nsStaticNameTable.h"
  9. // required to make the symbol external, so that TestCSSPropertyLookup.cpp can link with it
  10. extern const char* const kCSSRawKeywords[];
  11. // define an array of all CSS keywords
  12. #define CSS_KEY(_name,_id) #_name,
  13. const char* const kCSSRawKeywords[] = {
  14. #include "nsCSSKeywordList.h"
  15. };
  16. #undef CSS_KEY
  17. static int32_t gKeywordTableRefCount;
  18. static nsStaticCaseInsensitiveNameTable* gKeywordTable;
  19. void
  20. nsCSSKeywords::AddRefTable(void)
  21. {
  22. if (0 == gKeywordTableRefCount++) {
  23. NS_ASSERTION(!gKeywordTable, "pre existing array!");
  24. gKeywordTable =
  25. new nsStaticCaseInsensitiveNameTable(kCSSRawKeywords, eCSSKeyword_COUNT);
  26. #ifdef DEBUG
  27. // Partially verify the entries.
  28. int32_t index = 0;
  29. for (; index < eCSSKeyword_COUNT && kCSSRawKeywords[index]; ++index) {
  30. nsAutoCString temp(kCSSRawKeywords[index]);
  31. NS_ASSERTION(-1 == temp.FindChar('_'), "underscore char in table");
  32. }
  33. NS_ASSERTION(index == eCSSKeyword_COUNT, "kCSSRawKeywords and eCSSKeyword_COUNT are out of sync");
  34. #endif
  35. }
  36. }
  37. void
  38. nsCSSKeywords::ReleaseTable(void)
  39. {
  40. if (0 == --gKeywordTableRefCount) {
  41. if (gKeywordTable) {
  42. delete gKeywordTable;
  43. gKeywordTable = nullptr;
  44. }
  45. }
  46. }
  47. nsCSSKeyword
  48. nsCSSKeywords::LookupKeyword(const nsACString& aKeyword)
  49. {
  50. NS_ASSERTION(gKeywordTable, "no lookup table, needs addref");
  51. if (gKeywordTable) {
  52. return nsCSSKeyword(gKeywordTable->Lookup(aKeyword));
  53. }
  54. return eCSSKeyword_UNKNOWN;
  55. }
  56. nsCSSKeyword
  57. nsCSSKeywords::LookupKeyword(const nsAString& aKeyword)
  58. {
  59. NS_ASSERTION(gKeywordTable, "no lookup table, needs addref");
  60. if (gKeywordTable) {
  61. return nsCSSKeyword(gKeywordTable->Lookup(aKeyword));
  62. }
  63. return eCSSKeyword_UNKNOWN;
  64. }
  65. const nsAFlatCString&
  66. nsCSSKeywords::GetStringValue(nsCSSKeyword aKeyword)
  67. {
  68. NS_ASSERTION(gKeywordTable, "no lookup table, needs addref");
  69. NS_ASSERTION(0 <= aKeyword && aKeyword < eCSSKeyword_COUNT, "out of range");
  70. if (gKeywordTable) {
  71. return gKeywordTable->GetStringValue(int32_t(aKeyword));
  72. } else {
  73. static nsDependentCString kNullStr("");
  74. return kNullStr;
  75. }
  76. }