From 8d5ff2c5141ae10b85c26277f4d3ef56d37b26da Mon Sep 17 00:00:00 2001 From: Geoff McLane Date: Tue, 27 Mar 2018 15:36:15 +0200 Subject: [PATCH] Is #692 - Add a 'CleanHead' service --- src/clean.c | 31 +++++++++++++++++++++++++++++++ src/clean.h | 3 +++ src/tidylib.c | 2 ++ 3 files changed, 36 insertions(+) diff --git a/src/clean.c b/src/clean.c index e96dd3f..61c9596 100644 --- a/src/clean.c +++ b/src/clean.c @@ -2780,6 +2780,37 @@ void TY_(CleanStyle)(TidyDocImpl* doc, Node *html) /* ================================================== */ +/* + * CleanHead - clean the head node, if it exists, and we + * are going to show it in the output. + * Issue #692 - Remove multiple title elements + */ +void TY_(CleanHead)(TidyDocImpl* doc) +{ + Node *head, *node, *next; + uint titles = 0; + if (cfgAutoBool(doc, TidyBodyOnly) == TidyYesState) + return; /* not going to show head, so forget it */ + head = TY_(FindHEAD)(doc); + if (!head) + return; + node = head->content; + while (node) + { + next = node->next; /* get any 'next' */ + if (nodeIsTITLE(node)) + { + titles++; + if (titles > 1) + { + TY_(Report)(doc, head, node, DISCARDING_UNEXPECTED); + TY_(DiscardElement)(doc, node); /* delete this node */ + } + } + node = next; + } +} + /* * local variables: * mode: c diff --git a/src/clean.h b/src/clean.h index f0ef7a6..c765e81 100644 --- a/src/clean.h +++ b/src/clean.h @@ -75,5 +75,8 @@ void TY_(FixLanguageInformation)(TidyDocImpl* doc, Node* node, Bool wantXmlLang, /* Issue #567 - move style elements from body to head */ void TY_(CleanStyle)(TidyDocImpl* doc, Node *html); +/* Issue #692 - discard multiple titles */ +void TY_(CleanHead)(TidyDocImpl* doc); + #endif /* __CLEAN_H__ */ diff --git a/src/tidylib.c b/src/tidylib.c index 67d7a37..7396c74 100644 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -2153,6 +2153,8 @@ int tidyDocCleanAndRepair( TidyDocImpl* doc ) } } + TY_(CleanHead)(doc); /* Is #692 - discard multiple tags */ + #if defined(ENABLE_DEBUG_LOG) SPRTF("All nodes AFTER clean and repair\n"); dbg_show_all_nodes( doc, &doc->root, 0 );