nsHTMLTags.h (2644B)
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 #ifndef nsHTMLTags_h___ 7 #define nsHTMLTags_h___ 8 9 #include "nsAtomHashKeys.h" 10 #include "nsString.h" 11 #include "nsTHashMap.h" 12 #include "nsHashKeys.h" 13 14 /* 15 Declare the enum list using the magic of preprocessing 16 enum values are "eHTMLTag_foo" (where foo is the tag) 17 18 To change the list of tags, see nsHTMLTagList.h 19 20 These enum values are used as the index of array in various places. 21 If we change the structure of the enum by adding entries to it or removing 22 entries from it _directly_, not via nsHTMLTagList.h, don't forget to update 23 dom/bindings/BindingUtils.cpp and dom/html/HTMLElementFactory.cpp as well. 24 */ 25 #define HTML_TAG(_tag, _classname, _interfacename) eHTMLTag_##_tag, 26 #define HTML_OTHER(_tag) eHTMLTag_##_tag, 27 enum nsHTMLTag { 28 /* this enum must be first and must be zero */ 29 eHTMLTag_unknown = 0, 30 #include "nsHTMLTagList.h" 31 32 /* can't be moved into nsHTMLTagList since gcc3.4 doesn't like a 33 comma at the end of enum list*/ 34 eHTMLTag_userdefined 35 }; 36 #undef HTML_TAG 37 #undef HTML_OTHER 38 39 // All tags before eHTMLTag_text are HTML tags 40 #define NS_HTML_TAG_MAX int32_t(eHTMLTag_text - 1) 41 42 class nsHTMLTags { 43 public: 44 using TagStringHash = nsTHashMap<nsStringHashKey, nsHTMLTag>; 45 // Can be weak, because we know these are always static, see AddRefTable. 46 using TagAtomHash = nsTHashMap<nsAtom*, nsHTMLTag>; 47 48 static nsresult AddRefTable(void); 49 static void ReleaseTable(void); 50 51 // Functions for converting string or atom to id 52 static nsHTMLTag StringTagToId(const nsAString& aTagName); 53 static nsHTMLTag AtomTagToId(nsAtom* aTagName) { 54 return StringTagToId(nsDependentAtomString(aTagName)); 55 } 56 57 static nsHTMLTag CaseSensitiveStringTagToId(const nsAString& aTagName) { 58 NS_ASSERTION(gTagTable, "no lookup table, needs addref"); 59 60 return gTagTable->MaybeGet(aTagName).valueOr(eHTMLTag_userdefined); 61 } 62 static nsHTMLTag CaseSensitiveAtomTagToId(nsAtom* aTagName) { 63 NS_ASSERTION(gTagAtomTable, "no lookup table, needs addref"); 64 NS_ASSERTION(aTagName, "null tagname!"); 65 66 return gTagAtomTable->MaybeGet(aTagName).valueOr(eHTMLTag_userdefined); 67 } 68 69 #ifdef DEBUG 70 static void TestTagTable(); 71 #endif 72 73 private: 74 static const char16_t* const sTagNames[]; 75 76 static int32_t gTableRefCount; 77 static TagStringHash* gTagTable; 78 static TagAtomHash* gTagAtomTable; 79 }; 80 81 #endif /* nsHTMLTags_h___ */