Skip to content

Commit 561d43c

Browse files
authored
Merge pull request #579 from htacg/issue-567-2
Issue 567 2 - style tag to head
2 parents d4a11b5 + 50859e8 commit 561d43c

File tree

7 files changed

+80
-2
lines changed

7 files changed

+80
-2
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,6 +2647,57 @@ void TY_(FixAnchors)(TidyDocImpl* doc, Node *node, Bool wantName, Bool wantId)
26472647
}
26482648
}
26492649

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+
2682+
void TY_(CleanStyle)(TidyDocImpl* doc, Node *html)
2683+
{
2684+
Node *head = NULL, *body = NULL;
2685+
Bool fix = cfgBool(doc, TidyStyleTags);
2686+
2687+
if (!html)
2688+
return; /* oops, not given a start node */
2689+
2690+
head = TY_(FindHEAD)( doc );
2691+
body = TY_(FindBody)( doc );
2692+
2693+
if ((head != NULL) && (body != NULL))
2694+
{
2695+
StyleToHead(doc, head, body, fix, 0); /* found head and body */
2696+
}
2697+
}
2698+
/* ==================================================
2699+
*/
2700+
26502701
/*
26512702
* local variables:
26522703
* mode: c

src/clean.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,7 @@ void TY_(FixAnchors)(TidyDocImpl* doc, Node *node, Bool wantName, Bool wantId);
7878
void TY_(FixXhtmlNamespace)(TidyDocImpl* doc, Bool wantXmlns);
7979
void TY_(FixLanguageInformation)(TidyDocImpl* doc, Node* node, Bool wantXmlLang, Bool wantLang);
8080

81+
/* Issue #567 - move style elements from body to head */
82+
void TY_(CleanStyle)(TidyDocImpl* doc, Node *html);
8183

8284
#endif /* __CLEAN_H__ */

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 );

src/tidylib.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,9 @@ int tidyDocCleanAndRepair( TidyDocImpl* doc )
20222022
if (tidyXmlTags)
20232023
return tidyDocStatus( doc );
20242024

2025+
/* Issue #567 - move style elements from body to head */
2026+
TY_(CleanStyle)(doc, &doc->root);
2027+
20252028
/* simplifies <b><b> ... </b> ...</b> etc. */
20262029
if ( mergeEmphasis )
20272030
TY_(NestedEmphasis)( doc, &doc->root );

0 commit comments

Comments
 (0)