michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #include "nsHTMLEntities.h" michael@0: michael@0: michael@0: michael@0: #include "nsString.h" michael@0: #include "nsCRT.h" michael@0: #include "pldhash.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: struct EntityNode { michael@0: const char* mStr; // never owns buffer michael@0: int32_t mUnicode; michael@0: }; michael@0: michael@0: struct EntityNodeEntry : public PLDHashEntryHdr michael@0: { michael@0: const EntityNode* node; michael@0: }; michael@0: michael@0: static bool michael@0: matchNodeString(PLDHashTable*, const PLDHashEntryHdr* aHdr, michael@0: const void* key) michael@0: { michael@0: const EntityNodeEntry* entry = static_cast(aHdr); michael@0: const char* str = static_cast(key); michael@0: return (nsCRT::strcmp(entry->node->mStr, str) == 0); michael@0: } michael@0: michael@0: static bool michael@0: matchNodeUnicode(PLDHashTable*, const PLDHashEntryHdr* aHdr, michael@0: const void* key) michael@0: { michael@0: const EntityNodeEntry* entry = static_cast(aHdr); michael@0: const int32_t ucode = NS_PTR_TO_INT32(key); michael@0: return (entry->node->mUnicode == ucode); michael@0: } michael@0: michael@0: static PLDHashNumber michael@0: hashUnicodeValue(PLDHashTable*, const void* key) michael@0: { michael@0: // key is actually the unicode value michael@0: return PLDHashNumber(NS_PTR_TO_INT32(key)); michael@0: } michael@0: michael@0: michael@0: static const PLDHashTableOps EntityToUnicodeOps = { michael@0: PL_DHashAllocTable, michael@0: PL_DHashFreeTable, michael@0: PL_DHashStringKey, michael@0: matchNodeString, michael@0: PL_DHashMoveEntryStub, michael@0: PL_DHashClearEntryStub, michael@0: PL_DHashFinalizeStub, michael@0: nullptr, michael@0: }; michael@0: michael@0: static const PLDHashTableOps UnicodeToEntityOps = { michael@0: PL_DHashAllocTable, michael@0: PL_DHashFreeTable, michael@0: hashUnicodeValue, michael@0: matchNodeUnicode, michael@0: PL_DHashMoveEntryStub, michael@0: PL_DHashClearEntryStub, michael@0: PL_DHashFinalizeStub, michael@0: nullptr, michael@0: }; michael@0: michael@0: static PLDHashTable gEntityToUnicode = { 0 }; michael@0: static PLDHashTable gUnicodeToEntity = { 0 }; michael@0: static nsrefcnt gTableRefCnt = 0; michael@0: michael@0: #define HTML_ENTITY(_name, _value) { #_name, _value }, michael@0: static const EntityNode gEntityArray[] = { michael@0: #include "nsHTMLEntityList.h" michael@0: }; michael@0: #undef HTML_ENTITY michael@0: michael@0: #define NS_HTML_ENTITY_COUNT ((int32_t)ArrayLength(gEntityArray)) michael@0: michael@0: nsresult michael@0: nsHTMLEntities::AddRefTable(void) michael@0: { michael@0: if (!gTableRefCnt) { michael@0: if (!PL_DHashTableInit(&gEntityToUnicode, &EntityToUnicodeOps, michael@0: nullptr, sizeof(EntityNodeEntry), michael@0: uint32_t(NS_HTML_ENTITY_COUNT / 0.75), michael@0: fallible_t())) { michael@0: gEntityToUnicode.ops = nullptr; michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: if (!PL_DHashTableInit(&gUnicodeToEntity, &UnicodeToEntityOps, michael@0: nullptr, sizeof(EntityNodeEntry), michael@0: uint32_t(NS_HTML_ENTITY_COUNT / 0.75), michael@0: fallible_t())) { michael@0: PL_DHashTableFinish(&gEntityToUnicode); michael@0: gEntityToUnicode.ops = gUnicodeToEntity.ops = nullptr; michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: for (const EntityNode *node = gEntityArray, michael@0: *node_end = ArrayEnd(gEntityArray); michael@0: node < node_end; ++node) { michael@0: michael@0: // add to Entity->Unicode table michael@0: EntityNodeEntry* entry = michael@0: static_cast michael@0: (PL_DHashTableOperate(&gEntityToUnicode, michael@0: node->mStr, michael@0: PL_DHASH_ADD)); michael@0: NS_ASSERTION(entry, "Error adding an entry"); michael@0: // Prefer earlier entries when we have duplication. michael@0: if (!entry->node) michael@0: entry->node = node; michael@0: michael@0: // add to Unicode->Entity table michael@0: entry = static_cast michael@0: (PL_DHashTableOperate(&gUnicodeToEntity, michael@0: NS_INT32_TO_PTR(node->mUnicode), michael@0: PL_DHASH_ADD)); michael@0: NS_ASSERTION(entry, "Error adding an entry"); michael@0: // Prefer earlier entries when we have duplication. michael@0: if (!entry->node) michael@0: entry->node = node; michael@0: } michael@0: #ifdef DEBUG michael@0: PL_DHashMarkTableImmutable(&gUnicodeToEntity); michael@0: PL_DHashMarkTableImmutable(&gEntityToUnicode); michael@0: #endif michael@0: } michael@0: ++gTableRefCnt; michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsHTMLEntities::ReleaseTable(void) michael@0: { michael@0: if (--gTableRefCnt != 0) michael@0: return; michael@0: michael@0: if (gEntityToUnicode.ops) { michael@0: PL_DHashTableFinish(&gEntityToUnicode); michael@0: gEntityToUnicode.ops = nullptr; michael@0: } michael@0: if (gUnicodeToEntity.ops) { michael@0: PL_DHashTableFinish(&gUnicodeToEntity); michael@0: gUnicodeToEntity.ops = nullptr; michael@0: } michael@0: michael@0: } michael@0: michael@0: int32_t michael@0: nsHTMLEntities::EntityToUnicode(const nsCString& aEntity) michael@0: { michael@0: NS_ASSERTION(gEntityToUnicode.ops, "no lookup table, needs addref"); michael@0: if (!gEntityToUnicode.ops) michael@0: return -1; michael@0: michael@0: //this little piece of code exists because entities may or may not have the terminating ';'. michael@0: //if we see it, strip if off for this test... michael@0: michael@0: if(';'==aEntity.Last()) { michael@0: nsAutoCString temp(aEntity); michael@0: temp.Truncate(aEntity.Length()-1); michael@0: return EntityToUnicode(temp); michael@0: } michael@0: michael@0: EntityNodeEntry* entry = michael@0: static_cast michael@0: (PL_DHashTableOperate(&gEntityToUnicode, aEntity.get(), PL_DHASH_LOOKUP)); michael@0: michael@0: if (!entry || PL_DHASH_ENTRY_IS_FREE(entry)) michael@0: return -1; michael@0: michael@0: return entry->node->mUnicode; michael@0: } michael@0: michael@0: michael@0: int32_t michael@0: nsHTMLEntities::EntityToUnicode(const nsAString& aEntity) { michael@0: nsAutoCString theEntity; theEntity.AssignWithConversion(aEntity); michael@0: if(';'==theEntity.Last()) { michael@0: theEntity.Truncate(theEntity.Length()-1); michael@0: } michael@0: michael@0: return EntityToUnicode(theEntity); michael@0: } michael@0: michael@0: michael@0: const char* michael@0: nsHTMLEntities::UnicodeToEntity(int32_t aUnicode) michael@0: { michael@0: NS_ASSERTION(gUnicodeToEntity.ops, "no lookup table, needs addref"); michael@0: EntityNodeEntry* entry = michael@0: static_cast michael@0: (PL_DHashTableOperate(&gUnicodeToEntity, NS_INT32_TO_PTR(aUnicode), PL_DHASH_LOOKUP)); michael@0: michael@0: if (!entry || PL_DHASH_ENTRY_IS_FREE(entry)) michael@0: return nullptr; michael@0: michael@0: return entry->node->mStr; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: #include michael@0: michael@0: class nsTestEntityTable { michael@0: public: michael@0: nsTestEntityTable() { michael@0: int32_t value; michael@0: nsHTMLEntities::AddRefTable(); michael@0: michael@0: // Make sure we can find everything we are supposed to michael@0: for (int i = 0; i < NS_HTML_ENTITY_COUNT; ++i) { michael@0: nsAutoString entity; entity.AssignWithConversion(gEntityArray[i].mStr); michael@0: michael@0: value = nsHTMLEntities::EntityToUnicode(entity); michael@0: NS_ASSERTION(value != -1, "can't find entity"); michael@0: NS_ASSERTION(value == gEntityArray[i].mUnicode, "bad unicode value"); michael@0: michael@0: entity.AssignWithConversion(nsHTMLEntities::UnicodeToEntity(value)); michael@0: NS_ASSERTION(entity.EqualsASCII(gEntityArray[i].mStr), "bad entity name"); michael@0: } michael@0: michael@0: // Make sure we don't find things that aren't there michael@0: value = nsHTMLEntities::EntityToUnicode(nsAutoCString("@")); michael@0: NS_ASSERTION(value == -1, "found @"); michael@0: value = nsHTMLEntities::EntityToUnicode(nsAutoCString("zzzzz")); michael@0: NS_ASSERTION(value == -1, "found zzzzz"); michael@0: nsHTMLEntities::ReleaseTable(); michael@0: } michael@0: }; michael@0: //nsTestEntityTable validateEntityTable; michael@0: #endif michael@0: