Merge pull request #579 from htacg/issue-567-2

Issue 567 2 - style tag to head
This commit is contained in:
Jim Derry 2017-08-28 10:02:25 -04:00 committed by GitHub
commit 561d43c7e5
7 changed files with 80 additions and 2 deletions

View file

@ -270,7 +270,9 @@ extern "C" {
FN(COERCE_TO_ENDTAG) \ FN(COERCE_TO_ENDTAG) \
FN(ELEMENT_NOT_EMPTY) \ FN(ELEMENT_NOT_EMPTY) \
FN(UNEXPECTED_END_OF_FILE) \ FN(UNEXPECTED_END_OF_FILE) \
FN(UNEXPECTED_ENDTAG) FN(UNEXPECTED_ENDTAG) \
FN(MOVED_STYLE_TO_HEAD) \
FN(FOUND_STYLE_IN_BODY)
/** These are report messages added by Tidy's accessibility module. */ /** These are report messages added by Tidy's accessibility module. */
@ -648,6 +650,7 @@ typedef enum
TidyXmlPIs, /**< If set to yes PIs must end with ?> */ TidyXmlPIs, /**< If set to yes PIs must end with ?> */
TidyXmlSpace, /**< If set to yes adds xml:space attr as needed */ TidyXmlSpace, /**< If set to yes adds xml:space attr as needed */
TidyXmlTags, /**< Treat input as XML */ TidyXmlTags, /**< Treat input as XML */
TidyStyleTags, /**< Move sytle to head */
N_TIDY_OPTIONS /**< Must be last */ N_TIDY_OPTIONS /**< Must be last */
} TidyOptionId; } TidyOptionId;

View file

@ -2647,6 +2647,57 @@ void TY_(FixAnchors)(TidyDocImpl* doc, Node *node, Bool wantName, Bool wantId)
} }
} }
/* Issue #567 - move style elements from body to head
* ==================================================
*/
static void StyleToHead(TidyDocImpl* doc, Node *head, Node *node, Bool fix, int indent)
{
Node *next;
while (node)
{
next = node->next; /* get 'next' now , in case the node is moved */
/* dbg_show_node(doc, node, 0, indent); */
if (nodeIsSTYLE(node))
{
if (fix)
{
TY_(RemoveNode)(node); /* unhook style node from body */
TY_(InsertNodeAtEnd)(head, node); /* add to end of head */
TY_(ReportNotice)(doc, node, head, MOVED_STYLE_TO_HEAD); /* report move */
}
else
{
TY_(ReportNotice)(doc, node, head, FOUND_STYLE_IN_BODY);
}
}
else if (node->content)
{
StyleToHead(doc, head, node->content, fix, indent + 1);
}
node = next; /* process the 'next', if any */
}
}
void TY_(CleanStyle)(TidyDocImpl* doc, Node *html)
{
Node *head = NULL, *body = NULL;
Bool fix = cfgBool(doc, TidyStyleTags);
if (!html)
return; /* oops, not given a start node */
head = TY_(FindHEAD)( doc );
body = TY_(FindBody)( doc );
if ((head != NULL) && (body != NULL))
{
StyleToHead(doc, head, body, fix, 0); /* found head and body */
}
}
/* ==================================================
*/
/* /*
* local variables: * local variables:
* mode: c * mode: c

View file

@ -78,5 +78,7 @@ void TY_(FixAnchors)(TidyDocImpl* doc, Node *node, Bool wantName, Bool wantId);
void TY_(FixXhtmlNamespace)(TidyDocImpl* doc, Bool wantXmlns); void TY_(FixXhtmlNamespace)(TidyDocImpl* doc, Bool wantXmlns);
void TY_(FixLanguageInformation)(TidyDocImpl* doc, Node* node, Bool wantXmlLang, Bool wantLang); void TY_(FixLanguageInformation)(TidyDocImpl* doc, Node* node, Bool wantXmlLang, Bool wantLang);
/* Issue #567 - move style elements from body to head */
void TY_(CleanStyle)(TidyDocImpl* doc, Node *html);
#endif /* __CLEAN_H__ */ #endif /* __CLEAN_H__ */

View file

@ -312,6 +312,7 @@ static const TidyOptionImpl option_defs[] =
{ TidyXmlPIs, MU, "assume-xml-procins", BL, no, ParsePickList, &boolPicks }, { TidyXmlPIs, MU, "assume-xml-procins", BL, no, ParsePickList, &boolPicks },
{ TidyXmlSpace, MU, "add-xml-space", BL, no, ParsePickList, &boolPicks }, { TidyXmlSpace, MU, "add-xml-space", BL, no, ParsePickList, &boolPicks },
{ TidyXmlTags, MU, "input-xml", BL, no, ParsePickList, &boolPicks }, { TidyXmlTags, MU, "input-xml", BL, no, ParsePickList, &boolPicks },
{ TidyStyleTags, MU, "fix-style-tags", BL, yes, ParsePickList, &boolPicks },
{ N_TIDY_OPTIONS, XX, NULL, XY, 0, NULL, NULL } { N_TIDY_OPTIONS, XX, NULL, XY, 0, NULL, NULL }
}; };

View file

@ -1512,6 +1512,18 @@ static languageDefinition language_en = { whichPluralForm_en, {
"This option specifies if Tidy should use the XML parser rather than the " "This option specifies if Tidy should use the XML parser rather than the "
"error correcting HTML parser. " "error correcting HTML parser. "
}, },
{/* Important notes for translators:
- Use only <code></code>, <var></var>, <em></em>, <strong></strong>, and
<br/>.
- Entities, tags, attributes, etc., should be enclosed in <code></code>.
- Option values should be enclosed in <var></var>.
- It's very important that <br/> be self-closing!
- The strings "Tidy" and "HTML Tidy" are the program name and must not
be translated. */
TidyStyleTags, 0,
"This option specifies if Tidy should move all style tags to the "
"head of the document. "
},
/******************************************** /********************************************
@ -1890,7 +1902,8 @@ static languageDefinition language_en = { whichPluralForm_en, {
{ ELEMENT_NOT_EMPTY, 0, "%s element not empty or not closed" }, /* ReportError, ReportAttrError */ { ELEMENT_NOT_EMPTY, 0, "%s element not empty or not closed" }, /* ReportError, ReportAttrError */
{ UNEXPECTED_END_OF_FILE, 0, "unexpected end of file %s" }, /* ReportError, ReportAttrError */ { UNEXPECTED_END_OF_FILE, 0, "unexpected end of file %s" }, /* ReportError, ReportAttrError */
{ UNEXPECTED_ENDTAG, 0, "unexpected </%s>" }, /* ReportError, ReportFatal */ { UNEXPECTED_ENDTAG, 0, "unexpected </%s>" }, /* ReportError, ReportFatal */
{ MOVED_STYLE_TO_HEAD, 0, "moved <style> tag to <head>! fix-style-tags: no to avoid." }, /* ReportWarning */
{ FOUND_STYLE_IN_BODY, 0, "found <style> tag in <body>! fix-style-tags: yes to move." }, /* ReportWarning */
#if SUPPORT_ACCESSIBILITY_CHECKS #if SUPPORT_ACCESSIBILITY_CHECKS

View file

@ -249,6 +249,11 @@ void TY_(ReportNotice)(TidyDocImpl* doc, Node *element, Node *node, uint code)
} }
message = TY_(tidyMessageCreateWithNode)(doc, element, code, TidyInfo, elemdesc, tagtype ); message = TY_(tidyMessageCreateWithNode)(doc, element, code, TidyInfo, elemdesc, tagtype );
break; break;
case MOVED_STYLE_TO_HEAD:
case FOUND_STYLE_IN_BODY:
TagToString(element, elemdesc, sizeof(elemdesc));
message = TY_(tidyMessageCreateWithNode)(doc, element, code, TidyWarning, elemdesc);
break;
} }
messageOut( message ); messageOut( message );

View file

@ -2022,6 +2022,9 @@ int tidyDocCleanAndRepair( TidyDocImpl* doc )
if (tidyXmlTags) if (tidyXmlTags)
return tidyDocStatus( doc ); return tidyDocStatus( doc );
/* Issue #567 - move style elements from body to head */
TY_(CleanStyle)(doc, &doc->root);
/* simplifies <b><b> ... </b> ...</b> etc. */ /* simplifies <b><b> ... </b> ...</b> etc. */
if ( mergeEmphasis ) if ( mergeEmphasis )
TY_(NestedEmphasis)( doc, &doc->root ); TY_(NestedEmphasis)( doc, &doc->root );