New config option now drives the list; memory deallocated.

This commit is contained in:
Jim Derry 2017-09-23 07:20:31 -04:00
parent 23c28e5b82
commit 70681131d6
3 changed files with 46 additions and 33 deletions

View file

@ -930,15 +930,31 @@ AttVal* TY_(RepairAttrValue)(TidyDocImpl* doc, Node* node, ctmbstr name, ctmbstr
} }
void TY_(FreeAttrPriorityList)( TidyDocImpl* doc )
{
PriorityAttribs *priorities = &(doc->attribs.priorityAttribs);
if ( priorities->list )
{
uint i = 0;
while ( priorities->list[i] != NULL )
{
TidyFree( doc->allocator, priorities->list[i] );
i++;
}
TidyFree( doc->allocator, priorities->list );
}
}
void TY_(DefinePriorityAttribute)(TidyDocImpl* doc, ctmbstr name) void TY_(DefinePriorityAttribute)(TidyDocImpl* doc, ctmbstr name)
{ {
enum { capacity = 10 }; enum { capacity = 10 };
PriorityAttribs *priorities = &(doc->attribs.priorityAttribs); PriorityAttribs *priorities = &(doc->attribs.priorityAttribs);
/* @TODO: Don't forget to free this stuff */
if ( !priorities->list ) if ( !priorities->list )
{ {
priorities->list = malloc( sizeof(ctmbstr) * capacity ); priorities->list = TidyAlloc(doc->allocator, sizeof(ctmbstr) * capacity );
priorities->list[0] = NULL; priorities->list[0] = NULL;
priorities->capacity = capacity; priorities->capacity = capacity;
priorities->count = 0; priorities->count = 0;
@ -953,15 +969,6 @@ void TY_(DefinePriorityAttribute)(TidyDocImpl* doc, ctmbstr name)
priorities->list[priorities->count] = TY_(tmbstrdup)( doc->allocator, name); priorities->list[priorities->count] = TY_(tmbstrdup)( doc->allocator, name);
priorities->count++; priorities->count++;
priorities->list[priorities->count] = NULL; priorities->list[priorities->count] = NULL;
uint i = 0;
while ( priorities->list[i] != NULL )
{
printf("Array contains %s.\n", priorities->list[i]);
i++;
}
printf("Adding %s to list.\n", name);
} }
@ -2210,15 +2217,15 @@ void CheckType( TidyDocImpl* doc, Node *node, AttVal *attval)
} }
static static
AttVal *SortAttVal( AttVal* list, TidyAttrSortStrategy strat ); AttVal *SortAttVal( TidyDocImpl* doc, AttVal* list, TidyAttrSortStrategy strat );
void TY_(SortAttributes)(Node* node, TidyAttrSortStrategy strat) void TY_(SortAttributes)(TidyDocImpl* doc, Node* node, TidyAttrSortStrategy strat)
{ {
while (node) while (node)
{ {
node->attributes = SortAttVal( node->attributes, strat ); node->attributes = SortAttVal( doc, node->attributes, strat );
if (node->content) if (node->content)
TY_(SortAttributes)(node->content, strat); TY_(SortAttributes)(doc, node->content, strat);
node = node->next; node = node->next;
} }
} }
@ -2261,11 +2268,14 @@ typedef int(*ptAttValComparator)(AttVal *one, AttVal *two, ctmbstr *list);
static static
int indexof( ctmbstr item, ctmbstr *list ) int indexof( ctmbstr item, ctmbstr *list )
{ {
uint i = 0; if ( list )
while ( list[i] != NULL ) { {
if ( TY_(tmbstrcasecmp)(item, list[i]) == 0 ) uint i = 0;
return i; while ( list[i] != NULL ) {
i++; if ( TY_(tmbstrcasecmp)(item, list[i]) == 0 )
return i;
i++;
}
} }
return -1; return -1;
@ -2332,7 +2342,7 @@ ptAttValComparator GetAttValComparator(TidyAttrSortStrategy strat, ctmbstr *list
case TidySortAttrAlpha: case TidySortAttrAlpha:
return AlphaComparator; return AlphaComparator;
case TidySortAttrNone: case TidySortAttrNone:
if ( list[0] ) if ( list && list[0] )
return PriorityComparator; return PriorityComparator;
break; break;
} }
@ -2341,15 +2351,14 @@ ptAttValComparator GetAttValComparator(TidyAttrSortStrategy strat, ctmbstr *list
/* The sort routine */ /* The sort routine */
static static
AttVal *SortAttVal( AttVal *list, TidyAttrSortStrategy strat) AttVal *SortAttVal( TidyDocImpl* doc, AttVal *list, TidyAttrSortStrategy strat)
{ {
/* Get the list from the pass-in tidyDoc, which is a to-do. /* Get the list from the passed-in tidyDoc. */
We'll use this static list temporarily. // ctmbstr* priorityList = (ctmbstr*)doc->attribs.priorityAttribs.list;
*/ // ctmbstr priorityList[] = { "id", NULL };
// ctmbstr doc[] = { NULL }; ctmbstr* priorityList = (ctmbstr*)doc->attribs.priorityAttribs.list;
ctmbstr temp_list[] = { "id", "name", "class", NULL }; /* temp until option */
ptAttValComparator ptComparator = GetAttValComparator(strat, temp_list); ptAttValComparator ptComparator = GetAttValComparator(strat, priorityList);
AttVal *p, *q, *e, *tail; AttVal *p, *q, *e, *tail;
int insize, nmerges, psize, qsize, i; int insize, nmerges, psize, qsize, i;
@ -2360,7 +2369,7 @@ AttVal *SortAttVal( AttVal *list, TidyAttrSortStrategy strat)
if (!list) if (!list)
return NULL; return NULL;
/* If no comparator, return the list as is */ /* If no comparator, return the list as-is */
if (ptComparator == 0) if (ptComparator == 0)
return list; return list;
@ -2397,7 +2406,7 @@ AttVal *SortAttVal( AttVal *list, TidyAttrSortStrategy strat)
} else if (qsize == 0 || !q) { } else if (qsize == 0 || !q) {
/* q is empty; e must come from p. */ /* q is empty; e must come from p. */
e = p; p = p->next; psize--; e = p; p = p->next; psize--;
} else if (ptComparator(p,q, temp_list) <= 0) { } else if (ptComparator(p,q, priorityList) <= 0) {
/* First element of p is lower (or same); /* First element of p is lower (or same);
* e must come from p. */ * e must come from p. */
e = p; p = p->next; psize--; e = p; p = p->next; psize--;

View file

@ -60,8 +60,9 @@ enum
ANCHOR_HASH_SIZE=1021u ANCHOR_HASH_SIZE=1021u
}; };
/* Keeps a list of attributes that are sorted ahead of the others. */
typedef struct _priorityAttribs { typedef struct _priorityAttribs {
ctmbstr* list; tmbstr* list;
uint count; uint count;
uint capacity; uint capacity;
} PriorityAttribs; } PriorityAttribs;
@ -144,13 +145,15 @@ void TY_(FreeAnchors)( TidyDocImpl* doc );
void TY_(InitAttrs)( TidyDocImpl* doc ); void TY_(InitAttrs)( TidyDocImpl* doc );
void TY_(FreeAttrTable)( TidyDocImpl* doc ); void TY_(FreeAttrTable)( TidyDocImpl* doc );
void TY_(FreeAttrPriorityList)( TidyDocImpl* doc );
void TY_(AppendToClassAttr)( TidyDocImpl* doc, AttVal *classattr, ctmbstr classname ); void TY_(AppendToClassAttr)( TidyDocImpl* doc, AttVal *classattr, ctmbstr classname );
/* /*
the same attribute name can't be used the same attribute name can't be used
more than once in each element more than once in each element
*/ */
void TY_(RepairDuplicateAttributes)( TidyDocImpl* doc, Node* node, Bool isXml ); void TY_(RepairDuplicateAttributes)( TidyDocImpl* doc, Node* node, Bool isXml );
void TY_(SortAttributes)(Node* node, TidyAttrSortStrategy strat); void TY_(SortAttributes)(TidyDocImpl* doc, Node* node, TidyAttrSortStrategy strat);
Bool TY_(IsBoolAttribute)( AttVal* attval ); Bool TY_(IsBoolAttribute)( AttVal* attval );
Bool TY_(attrIsEvent)( AttVal* attval ); Bool TY_(attrIsEvent)( AttVal* attval );

View file

@ -181,6 +181,7 @@ void tidyDocRelease( TidyDocImpl* doc )
TY_(FreeConfig)( doc ); TY_(FreeConfig)( doc );
TY_(FreeAttrTable)( doc ); TY_(FreeAttrTable)( doc );
TY_(FreeAttrPriorityList)( doc );
TY_(FreeTags)( doc ); TY_(FreeTags)( doc );
/*\ /*\
* Issue #186 - Now FreeNode depend on the doctype, so the lexer is needed * Issue #186 - Now FreeNode depend on the doctype, so the lexer is needed
@ -2215,7 +2216,7 @@ int tidyDocSaveStream( TidyDocImpl* doc, StreamOut* out )
else else
TY_(ReplacePreformattedSpaces)(doc, &doc->root); TY_(ReplacePreformattedSpaces)(doc, &doc->root);
TY_(SortAttributes)(&doc->root, sortAttrStrat); TY_(SortAttributes)(doc, &doc->root, sortAttrStrat);
if ( showMarkup && (doc->errors == 0 || forceOutput) ) if ( showMarkup && (doc->errors == 0 || forceOutput) )
{ {