Skip to content

Add new warning or error message

Geoff McLane edited this page Jun 6, 2015 · 1 revision

Having recently added a new warning message, decided it might be a good thing to add to the wiki.

To add a new message there are several steps -

  1. message.h - define a new message name and value
  2. localize.c - define the text associated with this message
  3. localize.c - add the case to determine the message type
  4. Add the code to use the message, in this case in tags.c

Now in detail for the BAD_SUMMARY_HTML5 warning message added, through git diff -u

1. Define a new message value

Note carefully that this involved also bumping the last value.

diff --git a/src/message.h b/src/message.h
--- a/src/message.h
+++ b/src/message.h
@@ -157,8 +157,9 @@ void TY_(ReportFatal)(TidyDocImpl* doc, Node* element, Node* node, uint code);
 #define REMOVED_HTML5                88 /* this element removed from HTML5 */
 #define BAD_BODY_HTML5               89 /* attr on body removed from HTML5 */
 #define BAD_ALIGN_HTML5              90 /* use of align attr removed from HTML5 */
+#define BAD_SUMMARY_HTML5            91 /* use of summary attr removed from HTML5 */
 
-#define PREVIOUS_LOCATION            91 /* last */
+#define PREVIOUS_LOCATION            92 /* last */
 
 /* character encoding errors */
2. Define the text

This should be relatively short and succinct, and note that the element name can be supplied through a vararg.

diff --git a/src/localize.c b/src/localize.c
index f1c35ab..63e43b8 100755
--- a/src/localize.c
+++ b/src/localize.c
@@ -113,7 +113,8 @@ static struct _msgfmt
   /* HTML5 */
   { REMOVED_HTML5,                "%s element removed from HTML5"                                           }, /* Warning */
   { BAD_BODY_HTML5,               "Found attribute on body that is obsolete in HTML5. Use CSS"              }, /* Warning */
-  { BAD_ALIGN_HTML5,              "The align attribute on the %s element is obsolete, Use CSS"              }, /* Wanring */
+  { BAD_ALIGN_HTML5,              "The align attribute on the %s element is obsolete, Use CSS"              }, /* Warning */
+  { BAD_SUMMARY_HTML5,            "The summary attribute on the %s element is obsolete in HTML5"            }, /* Warning */
 
 /* ReportNotice */
   { TRIM_EMPTY_ELEMENT,           "trimming empty %s"                                                       }, /* Notice */
3. Add to case to set the type

This will establish whether it be a warning, error, note, etc, and the format used.

diff --git a/src/localize.c b/src/localize.c
index f1c35ab..63e43b8 100755
--- a/src/localize.c
+++ b/src/localize.c
@@ -1499,6 +1500,7 @@ void TY_(ReportWarning)(TidyDocImpl* doc, Node *element, Node *node, uint code)
     case REMOVED_HTML5:
     case BAD_BODY_HTML5:
     case BAD_ALIGN_HTML5:
+    case BAD_SUMMARY_HTML5:
         messageNode(doc, TidyWarning, rpt, fmt, nodedesc);
         break;
     case COERCE_TO_ENDTAG_WARN:
4. Add code to use the message

This can be anywhere in the code. In this case it is in tags.c, when doing a CheckTABLE().

diff --git a/src/tags.c b/src/tags.c
index a1a54d9..5470831 100644
--- a/src/tags.c
+++ b/src/tags.c
@@ -873,9 +873,29 @@ void CheckAREA( TidyDocImpl* doc, Node *node )
 void CheckTABLE( TidyDocImpl* doc, Node *node )
 {
     AttVal* attval;
+    Bool HasSummary = (TY_(AttrGetById)(node, TidyAttr_SUMMARY) != NULL) ? yes : no;
+    Bool isHTML5 = (TY_(HTMLVersion)(doc) == HT50) ? yes : no;
 
     TY_(CheckAttributes)(doc, node);
 
+    /* Issue #210 - a missing summary attribute is bad accessibility, no matter
+       what HTML version is involved; a document without is valid 
+       EXCEPT for HTML5, when to have a summary is wrong */
+    if (cfg(doc, TidyAccessibilityCheckLevel) == 0)
+    {
+        if (HasSummary && isHTML5)
+        {
+            /* #210 - has summary, and is HTML5, then report obsolete */
+            TY_(ReportWarning)(doc, node, node, BAD_SUMMARY_HTML5);
+        } 
+        else if (!HasSummary && !isHTML5) 
+        {
+            /* #210 - No summary, and NOT HTML5, then report as before */
+            doc->badAccess |= BA_MISSING_SUMMARY;
+            TY_(ReportMissingAttr)( doc, node, "summary");
+        }
+    }
+
     /* convert <table border> to <table border="1"> */
     if ( cfgBool(doc, TidyXmlOut) && (attval = TY_(AttrGetById)(node, TidyAttr_BORDER)) )
     {

And of course do not forget to bump the version

diff --git a/version.txt b/version.txt
index 361e469..1da6541 100644
--- a/version.txt
+++ b/version.txt
@@ -1 +1 @@
-4.9.32
\ No newline at end of file
+4.9.33
\ No newline at end of file

That's it folks!