How to use TNPL (simple example)

Post here if your topic is about CSS HTML Validator but doesn't fit in another forum.
Post Reply
User avatar
Albert Wiersch
Site Admin
Site Admin
Posts: 3221
Joined: Sat Dec 11, 2004 9:23 am
Location: Near Dallas, TX
Contact:

How to use TNPL (simple example)

Post by Albert Wiersch » Tue Apr 18, 2017 3:09 pm

Did you know that CSE HTML Validator is extremely customizable? You can create your own "user functions" in a text file (with a '*.cfg' or '*.txt' extension), and specify that file in the Validator Engine Options in the Config File page.

Note that this functionality is only available in the professional edition and higher.

Here are some simple examples using the onStartTag_(tagname)() function. These are taken from the documentation page here:
https://www.htmlvalidator.com/v170/docs ... agname.htm

Example 6 is the most newest edition. I added this because I wanted to quickly see what stylesheets where being linked to in some documents I was validating.

Example 1:

This example generates a message with the current count of "h1" start tags every time an "h1" start tag is encountered.

Code: Select all

function onStartTag_h1() {
 $h1tagcounter++;
 Message(1,MSG_MESSAGE,'This is "h1" start tag #'+toString($h1tagcounter));
}
Example 2:

This example generates a warning message if an "a" element is used without any attributes.

Code: Select all

function onStartTag_a() {
 if !numAttributes {
  Message(1,MSG_WARNING,'This "'+CurrentTagName+'" element has no attributes.');
 }
}
Example 3:

This example checks to see if a meta tag has a name of "pwgsc.contact.email", and if so, then it makes sure that the content is a valid email address (syntax check only). If it's not, then it generates an error message.

Code: Select all

function onStartTag_meta() {
 if hasAttWithStringValue("name","pwgsc.contact.email") {
  if hasAtt("content") {
   if !checkStringEx(0,39,getAttValueEx("content",12)) {
    Message(1,MSG_ERROR,'Bad email address: '+$checkstringex39details,getAttValueLocation("content"));
   }
  }
 }
}
Example 4:

This example checks that the content of certain meta data is meaningful (it's not empty or only whitespace).

Code: Select all

function onStartTag_meta() {
 if hasAtt("name") {
  $namevalue=getAttValue("name");
  if $namevalue=="dcterms.title" { checkMetaDataNonWhitespace(); }
  elseif $namevalue=="dcterms.subject" { checkMetaDataNonWhitespace(); }
  elseif $namevalue=="dcterms.language" { checkMetaDataNonWhitespace(); }
  elseif $namevalue=="dcterms.creator" { checkMetaDataNonWhitespace(); }
 }
}

function checkMetaDataNonWhitespace() {
 if hasAtt("content") {
  $contentindex=getAttIndex("content");
  if checkStringEx(0,33,getAttValue($contentindex)) {
   Message(1,MSG_ERROR,'The "content" value is empty or contains only whitespace characters. Meaningful text is required.',getAttValueLocation($contentindex));
  }
 }
}
Example 5:

Store a link from "link" element in a variable and append it to a log file with writeFile(). Note that writeFile() must be enabled (see its documentation page for more information).

Code: Select all

function onStartTag_link() {
 if getAttValueEx('rel',12)=='amphtml' {
  $amphtmlurl=getAttValueEx('href',12);
  writeFile('ampurls.txt',$amphtmlurl,5); // append to log file
 }
}
Example 6:

Generate a new message every time a stylesheet link is encountered. The MessageEx() function is used and a message ID of 2017041800 is assigned to any generated messages.

Code: Select all

function onStartTag_link() {
 if getAttValueEx('rel',12)=='stylesheet' {
  MessageEx(5,2017041800,MSG_MESSAGE,'Stylesheets','Stylesheet found: '+getAttValueEx('href',12));
 }
}
Image
Albert Wiersch

Post Reply