Skip to content

Commit 50859e8

Browse files
committed
Issue #567 - add option, messages, and fix node iteration.
Add option TidyStyleTags, --fix-style-tags, Bool, to turn off this action. Add warning messages MOVED_STYLE_TO_HEAD, and FOUND_STYLE_IN_BODY. Fully iterate ALL nodes in the body, in search of style tags... Changes to be committed: modified: include/tidyenum.h modified: src/clean.c modified: src/config.c modified: src/language_en.h modified: src/message.c
1 parent d4ca02a commit 50859e8

File tree

5 files changed

+63
-42
lines changed

5 files changed

+63
-42
lines changed

include/tidyenum.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ extern "C" {
270270
FN(COERCE_TO_ENDTAG) \
271271
FN(ELEMENT_NOT_EMPTY) \
272272
FN(UNEXPECTED_END_OF_FILE) \
273-
FN(UNEXPECTED_ENDTAG)
273+
FN(UNEXPECTED_ENDTAG) \
274+
FN(MOVED_STYLE_TO_HEAD) \
275+
FN(FOUND_STYLE_IN_BODY)
274276

275277

276278
/** These are report messages added by Tidy's accessibility module. */
@@ -648,6 +650,7 @@ typedef enum
648650
TidyXmlPIs, /**< If set to yes PIs must end with ?> */
649651
TidyXmlSpace, /**< If set to yes adds xml:space attr as needed */
650652
TidyXmlTags, /**< Treat input as XML */
653+
TidyStyleTags, /**< Move sytle to head */
651654
N_TIDY_OPTIONS /**< Must be last */
652655
} TidyOptionId;
653656

src/clean.c

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,57 +2647,56 @@ void TY_(FixAnchors)(TidyDocImpl* doc, Node *node, Bool wantName, Bool wantId)
26472647
}
26482648
}
26492649

2650-
/* Issue #567 - move style elements from body to head */
2650+
/* Issue #567 - move style elements from body to head
2651+
* ==================================================
2652+
*/
2653+
static void StyleToHead(TidyDocImpl* doc, Node *head, Node *node, Bool fix, int indent)
2654+
{
2655+
Node *next;
2656+
while (node)
2657+
{
2658+
next = node->next; /* get 'next' now , in case the node is moved */
2659+
/* dbg_show_node(doc, node, 0, indent); */
2660+
if (nodeIsSTYLE(node))
2661+
{
2662+
if (fix)
2663+
{
2664+
TY_(RemoveNode)(node); /* unhook style node from body */
2665+
TY_(InsertNodeAtEnd)(head, node); /* add to end of head */
2666+
TY_(ReportNotice)(doc, node, head, MOVED_STYLE_TO_HEAD); /* report move */
2667+
}
2668+
else
2669+
{
2670+
TY_(ReportNotice)(doc, node, head, FOUND_STYLE_IN_BODY);
2671+
}
2672+
}
2673+
else if (node->content)
2674+
{
2675+
StyleToHead(doc, head, node->content, fix, indent + 1);
2676+
}
2677+
node = next; /* process the 'next', if any */
2678+
}
2679+
}
2680+
2681+
26512682
void TY_(CleanStyle)(TidyDocImpl* doc, Node *html)
26522683
{
2653-
Node *node, *next, *head = NULL, *body = NULL;
2654-
Node *child;
2684+
Node *head = NULL, *body = NULL;
2685+
Bool fix = cfgBool(doc, TidyStyleTags);
2686+
26552687
if (!html)
26562688
return; /* oops, not given a start node */
26572689

2658-
#if 0 /* this failed??? */
2659-
for (node = html->content; node != NULL; node = node->next)
2660-
{
2661-
if (nodeIsHEAD(node))
2662-
head = node;
2663-
2664-
if (nodeIsBODY(node))
2665-
body = node;
2666-
}
2667-
#endif /* 0000000000000000000000 */
26682690
head = TY_(FindHEAD)( doc );
26692691
body = TY_(FindBody)( doc );
26702692

2671-
if (head != NULL && body != NULL)
2693+
if ((head != NULL) && (body != NULL))
26722694
{
2673-
/* found head and body */
2674-
for (node = body->content; node != NULL; node = next)
2675-
{
2676-
next = node->next;
2677-
if (nodeIsSTYLE(node))
2678-
{
2679-
TY_(RemoveNode)(node); /* unhool style node from body */
2680-
TY_(InsertNodeAtEnd)(head, node); /* add to end of head */
2681-
/* TODO: Add warning */
2682-
}
2683-
else if (node->content)
2684-
{
2685-
for (child = node->content; child != NULL; child = child->content)
2686-
{
2687-
if (nodeIsSTYLE(child))
2688-
{
2689-
TY_(RemoveNode)(child); /* unhool style node from body */
2690-
TY_(InsertNodeAtEnd)(head, child); /* add to end of head */
2691-
/* TODO: Add warning */
2692-
break;
2693-
}
2694-
2695-
}
2696-
2697-
}
2698-
}
2695+
StyleToHead(doc, head, body, fix, 0); /* found head and body */
26992696
}
27002697
}
2698+
/* ==================================================
2699+
*/
27012700

27022701
/*
27032702
* local variables:

src/config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ static const TidyOptionImpl option_defs[] =
312312
{ TidyXmlPIs, MU, "assume-xml-procins", BL, no, ParsePickList, &boolPicks },
313313
{ TidyXmlSpace, MU, "add-xml-space", BL, no, ParsePickList, &boolPicks },
314314
{ TidyXmlTags, MU, "input-xml", BL, no, ParsePickList, &boolPicks },
315+
{ TidyStyleTags, MU, "fix-style-tags", BL, yes, ParsePickList, &boolPicks },
315316
{ N_TIDY_OPTIONS, XX, NULL, XY, 0, NULL, NULL }
316317
};
317318

src/language_en.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,18 @@ static languageDefinition language_en = { whichPluralForm_en, {
15121512
"This option specifies if Tidy should use the XML parser rather than the "
15131513
"error correcting HTML parser. "
15141514
},
1515+
{/* Important notes for translators:
1516+
- Use only <code></code>, <var></var>, <em></em>, <strong></strong>, and
1517+
<br/>.
1518+
- Entities, tags, attributes, etc., should be enclosed in <code></code>.
1519+
- Option values should be enclosed in <var></var>.
1520+
- It's very important that <br/> be self-closing!
1521+
- The strings "Tidy" and "HTML Tidy" are the program name and must not
1522+
be translated. */
1523+
TidyStyleTags, 0,
1524+
"This option specifies if Tidy should move all style tags to the "
1525+
"head of the document. "
1526+
},
15151527

15161528

15171529
/********************************************
@@ -1890,8 +1902,9 @@ static languageDefinition language_en = { whichPluralForm_en, {
18901902
{ ELEMENT_NOT_EMPTY, 0, "%s element not empty or not closed" }, /* ReportError, ReportAttrError */
18911903
{ UNEXPECTED_END_OF_FILE, 0, "unexpected end of file %s" }, /* ReportError, ReportAttrError */
18921904
{ UNEXPECTED_ENDTAG, 0, "unexpected </%s>" }, /* ReportError, ReportFatal */
1905+
{ MOVED_STYLE_TO_HEAD, 0, "moved <style> tag to <head>! fix-style-tags: no to avoid." }, /* ReportWarning */
1906+
{ FOUND_STYLE_IN_BODY, 0, "found <style> tag in <body>! fix-style-tags: yes to move." }, /* ReportWarning */
18931907

1894-
18951908
#if SUPPORT_ACCESSIBILITY_CHECKS
18961909

18971910
/***************************************

src/message.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ void TY_(ReportNotice)(TidyDocImpl* doc, Node *element, Node *node, uint code)
249249
}
250250
message = TY_(tidyMessageCreateWithNode)(doc, element, code, TidyInfo, elemdesc, tagtype );
251251
break;
252+
case MOVED_STYLE_TO_HEAD:
253+
case FOUND_STYLE_IN_BODY:
254+
TagToString(element, elemdesc, sizeof(elemdesc));
255+
message = TY_(tidyMessageCreateWithNode)(doc, element, code, TidyWarning, elemdesc);
256+
break;
252257
}
253258

254259
messageOut( message );

0 commit comments

Comments
 (0)