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