2001-09-26 04:40:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1998-08-11 01:07:40 +04:00
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
1998-08-11 01:07:40 +04:00
|
|
|
#include "nsHTMLEntities.h"
|
|
|
|
|
1999-07-18 04:12:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include "nsString.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2003-04-07 02:24:35 +04:00
|
|
|
#include "pldhash.h"
|
2001-06-05 04:39:38 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
1999-07-18 04:12:32 +04:00
|
|
|
struct EntityNode {
|
2003-04-02 08:39:12 +04:00
|
|
|
const char* mStr; // never owns buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mUnicode;
|
1998-08-11 01:07:40 +04:00
|
|
|
};
|
1999-07-18 04:12:32 +04:00
|
|
|
|
2003-04-07 02:24:35 +04:00
|
|
|
struct EntityNodeEntry : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
const EntityNode* node;
|
1999-07-18 04:12:32 +04:00
|
|
|
};
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2003-04-07 02:24:35 +04:00
|
|
|
matchNodeString(PLDHashTable*, const PLDHashEntryHdr* aHdr,
|
|
|
|
const void* key)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
const EntityNodeEntry* entry = static_cast<const EntityNodeEntry*>(aHdr);
|
|
|
|
const char* str = static_cast<const char*>(key);
|
2003-04-07 02:24:35 +04:00
|
|
|
return (nsCRT::strcmp(entry->node->mStr, str) == 0);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2003-04-07 02:24:35 +04:00
|
|
|
matchNodeUnicode(PLDHashTable*, const PLDHashEntryHdr* aHdr,
|
|
|
|
const void* key)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
const EntityNodeEntry* entry = static_cast<const EntityNodeEntry*>(aHdr);
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t ucode = NS_PTR_TO_INT32(key);
|
2003-04-07 02:24:35 +04:00
|
|
|
return (entry->node->mUnicode == ucode);
|
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashNumber
|
2003-04-07 02:24:35 +04:00
|
|
|
hashUnicodeValue(PLDHashTable*, const void* key)
|
|
|
|
{
|
|
|
|
// key is actually the unicode value
|
2003-05-16 02:24:24 +04:00
|
|
|
return PLDHashNumber(NS_PTR_TO_INT32(key));
|
1999-07-18 04:12:32 +04:00
|
|
|
}
|
2003-04-07 02:24:35 +04:00
|
|
|
|
|
|
|
|
|
|
|
static const PLDHashTableOps EntityToUnicodeOps = {
|
|
|
|
PL_DHashStringKey,
|
|
|
|
matchNodeString,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
PL_DHashClearEntryStub,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
1999-07-18 04:12:32 +04:00
|
|
|
};
|
|
|
|
|
2003-04-07 02:24:35 +04:00
|
|
|
static const PLDHashTableOps UnicodeToEntityOps = {
|
|
|
|
hashUnicodeValue,
|
|
|
|
matchNodeUnicode,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
PL_DHashClearEntryStub,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2003-04-07 02:24:35 +04:00
|
|
|
};
|
1999-07-18 04:12:32 +04:00
|
|
|
|
2014-08-26 03:56:33 +04:00
|
|
|
static PLDHashTable gEntityToUnicode;
|
|
|
|
static PLDHashTable gUnicodeToEntity;
|
2003-05-21 00:53:21 +04:00
|
|
|
static nsrefcnt gTableRefCnt = 0;
|
1999-07-18 04:12:32 +04:00
|
|
|
|
2003-04-02 08:39:12 +04:00
|
|
|
#define HTML_ENTITY(_name, _value) { #_name, _value },
|
|
|
|
static const EntityNode gEntityArray[] = {
|
1999-07-18 04:12:32 +04:00
|
|
|
#include "nsHTMLEntityList.h"
|
|
|
|
};
|
|
|
|
#undef HTML_ENTITY
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
#define NS_HTML_ENTITY_COUNT ((int32_t)ArrayLength(gEntityArray))
|
1999-07-18 04:12:32 +04:00
|
|
|
|
2003-11-06 01:11:55 +03:00
|
|
|
nsresult
|
2014-08-06 17:31:21 +04:00
|
|
|
nsHTMLEntities::AddRefTable(void)
|
1999-07-18 04:12:32 +04:00
|
|
|
{
|
2003-11-06 01:11:55 +03:00
|
|
|
if (!gTableRefCnt) {
|
|
|
|
if (!PL_DHashTableInit(&gEntityToUnicode, &EntityToUnicodeOps,
|
2015-01-14 03:42:13 +03:00
|
|
|
sizeof(EntityNodeEntry),
|
2014-08-06 17:31:21 +04:00
|
|
|
fallible_t(), NS_HTML_ENTITY_COUNT)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
gEntityToUnicode.ops = nullptr;
|
2003-11-06 01:11:55 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
if (!PL_DHashTableInit(&gUnicodeToEntity, &UnicodeToEntityOps,
|
2015-01-14 03:42:13 +03:00
|
|
|
sizeof(EntityNodeEntry),
|
2014-08-06 17:31:21 +04:00
|
|
|
fallible_t(), NS_HTML_ENTITY_COUNT)) {
|
2003-11-06 01:11:55 +03:00
|
|
|
PL_DHashTableFinish(&gEntityToUnicode);
|
2012-07-30 18:20:58 +04:00
|
|
|
gEntityToUnicode.ops = gUnicodeToEntity.ops = nullptr;
|
2003-11-06 01:11:55 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
for (const EntityNode *node = gEntityArray,
|
2011-10-11 09:50:08 +04:00
|
|
|
*node_end = ArrayEnd(gEntityArray);
|
2003-11-06 01:11:55 +03:00
|
|
|
node < node_end; ++node) {
|
|
|
|
|
|
|
|
// add to Entity->Unicode table
|
|
|
|
EntityNodeEntry* entry =
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<EntityNodeEntry*>
|
2015-01-06 05:27:28 +03:00
|
|
|
(PL_DHashTableAdd(&gEntityToUnicode, node->mStr));
|
2003-11-06 01:11:55 +03:00
|
|
|
NS_ASSERTION(entry, "Error adding an entry");
|
|
|
|
// Prefer earlier entries when we have duplication.
|
|
|
|
if (!entry->node)
|
|
|
|
entry->node = node;
|
|
|
|
|
|
|
|
// add to Unicode->Entity table
|
2007-07-08 11:08:04 +04:00
|
|
|
entry = static_cast<EntityNodeEntry*>
|
2015-01-06 05:27:28 +03:00
|
|
|
(PL_DHashTableAdd(&gUnicodeToEntity,
|
|
|
|
NS_INT32_TO_PTR(node->mUnicode)));
|
2003-11-06 01:11:55 +03:00
|
|
|
NS_ASSERTION(entry, "Error adding an entry");
|
|
|
|
// Prefer earlier entries when we have duplication.
|
|
|
|
if (!entry->node)
|
|
|
|
entry->node = node;
|
|
|
|
}
|
2009-01-10 19:28:16 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
PL_DHashMarkTableImmutable(&gUnicodeToEntity);
|
|
|
|
PL_DHashMarkTableImmutable(&gEntityToUnicode);
|
|
|
|
#endif
|
1999-07-18 04:12:32 +04:00
|
|
|
}
|
2003-11-06 01:11:55 +03:00
|
|
|
++gTableRefCnt;
|
|
|
|
return NS_OK;
|
1999-07-18 04:12:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLEntities::ReleaseTable(void)
|
|
|
|
{
|
2003-05-21 00:53:21 +04:00
|
|
|
if (--gTableRefCnt != 0)
|
|
|
|
return;
|
|
|
|
|
2003-04-07 02:24:35 +04:00
|
|
|
if (gEntityToUnicode.ops) {
|
|
|
|
PL_DHashTableFinish(&gEntityToUnicode);
|
2012-07-30 18:20:58 +04:00
|
|
|
gEntityToUnicode.ops = nullptr;
|
1998-08-11 01:07:40 +04:00
|
|
|
}
|
2003-04-07 02:24:35 +04:00
|
|
|
if (gUnicodeToEntity.ops) {
|
|
|
|
PL_DHashTableFinish(&gUnicodeToEntity);
|
2012-07-30 18:20:58 +04:00
|
|
|
gUnicodeToEntity.ops = nullptr;
|
2003-04-07 02:24:35 +04:00
|
|
|
}
|
|
|
|
|
1998-08-11 01:07:40 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2000-03-12 13:01:10 +03:00
|
|
|
nsHTMLEntities::EntityToUnicode(const nsCString& aEntity)
|
1999-06-16 03:26:13 +04:00
|
|
|
{
|
2003-04-07 02:24:35 +04:00
|
|
|
NS_ASSERTION(gEntityToUnicode.ops, "no lookup table, needs addref");
|
|
|
|
if (!gEntityToUnicode.ops)
|
|
|
|
return -1;
|
1999-07-20 10:58:01 +04:00
|
|
|
|
|
|
|
//this little piece of code exists because entities may or may not have the terminating ';'.
|
|
|
|
//if we see it, strip if off for this test...
|
|
|
|
|
2000-03-12 13:52:02 +03:00
|
|
|
if(';'==aEntity.Last()) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString temp(aEntity);
|
2001-11-14 15:35:52 +03:00
|
|
|
temp.Truncate(aEntity.Length()-1);
|
1999-07-20 10:58:01 +04:00
|
|
|
return EntityToUnicode(temp);
|
|
|
|
}
|
|
|
|
|
2003-04-07 02:24:35 +04:00
|
|
|
EntityNodeEntry* entry =
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<EntityNodeEntry*>
|
2015-01-06 05:27:28 +03:00
|
|
|
(PL_DHashTableLookup(&gEntityToUnicode, aEntity.get()));
|
1999-07-20 10:58:01 +04:00
|
|
|
|
2003-04-07 02:24:35 +04:00
|
|
|
if (!entry || PL_DHASH_ENTRY_IS_FREE(entry))
|
1999-07-18 04:12:32 +04:00
|
|
|
return -1;
|
2003-04-07 02:24:35 +04:00
|
|
|
|
|
|
|
return entry->node->mUnicode;
|
1999-06-16 03:26:13 +04:00
|
|
|
}
|
|
|
|
|
2000-03-12 12:23:40 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2002-03-24 03:16:18 +03:00
|
|
|
nsHTMLEntities::EntityToUnicode(const nsAString& aEntity) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString theEntity; theEntity.AssignWithConversion(aEntity);
|
2000-03-12 13:52:02 +03:00
|
|
|
if(';'==theEntity.Last()) {
|
|
|
|
theEntity.Truncate(theEntity.Length()-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EntityToUnicode(theEntity);
|
2000-03-12 12:23:40 +03:00
|
|
|
}
|
1999-06-16 03:26:13 +04:00
|
|
|
|
2000-03-12 13:52:02 +03:00
|
|
|
|
2001-06-05 04:39:38 +04:00
|
|
|
const char*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHTMLEntities::UnicodeToEntity(int32_t aUnicode)
|
1999-07-18 04:12:32 +04:00
|
|
|
{
|
2003-04-07 02:24:35 +04:00
|
|
|
NS_ASSERTION(gUnicodeToEntity.ops, "no lookup table, needs addref");
|
|
|
|
EntityNodeEntry* entry =
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<EntityNodeEntry*>
|
2015-01-06 05:27:28 +03:00
|
|
|
(PL_DHashTableLookup(&gUnicodeToEntity, NS_INT32_TO_PTR(aUnicode)));
|
2003-04-07 02:24:35 +04:00
|
|
|
|
|
|
|
if (!entry || PL_DHASH_ENTRY_IS_FREE(entry))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2003-04-07 02:24:35 +04:00
|
|
|
|
|
|
|
return entry->node->mStr;
|
1999-07-18 04:12:32 +04:00
|
|
|
}
|
|
|
|
|
2012-06-25 23:59:42 +04:00
|
|
|
#ifdef DEBUG
|
1998-08-11 01:07:40 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
class nsTestEntityTable {
|
|
|
|
public:
|
|
|
|
nsTestEntityTable() {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t value;
|
1999-07-18 04:12:32 +04:00
|
|
|
nsHTMLEntities::AddRefTable();
|
1998-08-11 01:07:40 +04:00
|
|
|
|
|
|
|
// Make sure we can find everything we are supposed to
|
2002-09-04 02:23:22 +04:00
|
|
|
for (int i = 0; i < NS_HTML_ENTITY_COUNT; ++i) {
|
2000-04-26 05:13:55 +04:00
|
|
|
nsAutoString entity; entity.AssignWithConversion(gEntityArray[i].mStr);
|
1999-07-18 04:12:32 +04:00
|
|
|
|
|
|
|
value = nsHTMLEntities::EntityToUnicode(entity);
|
1998-08-11 01:07:40 +04:00
|
|
|
NS_ASSERTION(value != -1, "can't find entity");
|
1999-07-18 04:12:32 +04:00
|
|
|
NS_ASSERTION(value == gEntityArray[i].mUnicode, "bad unicode value");
|
|
|
|
|
2000-04-03 12:04:52 +04:00
|
|
|
entity.AssignWithConversion(nsHTMLEntities::UnicodeToEntity(value));
|
2004-07-22 00:18:39 +04:00
|
|
|
NS_ASSERTION(entity.EqualsASCII(gEntityArray[i].mStr), "bad entity name");
|
1998-08-11 01:07:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we don't find things that aren't there
|
2012-09-02 06:35:17 +04:00
|
|
|
value = nsHTMLEntities::EntityToUnicode(nsAutoCString("@"));
|
1998-08-11 01:07:40 +04:00
|
|
|
NS_ASSERTION(value == -1, "found @");
|
2012-09-02 06:35:17 +04:00
|
|
|
value = nsHTMLEntities::EntityToUnicode(nsAutoCString("zzzzz"));
|
1998-08-11 01:07:40 +04:00
|
|
|
NS_ASSERTION(value == -1, "found zzzzz");
|
1999-07-18 04:12:32 +04:00
|
|
|
nsHTMLEntities::ReleaseTable();
|
1998-08-11 01:07:40 +04:00
|
|
|
}
|
|
|
|
};
|
1999-07-18 04:12:32 +04:00
|
|
|
//nsTestEntityTable validateEntityTable;
|
1998-08-11 01:07:40 +04:00
|
|
|
#endif
|
|
|
|
|