How to use TNPL (simple example)
Posted: 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.
Example 2:
This example generates a warning message if an "a" element is used without any 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.
Example 4:
This example checks that the content of certain meta data is meaningful (it's not empty or only whitespace).
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).
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.
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));
}
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.');
}
}
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"));
}
}
}
}
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));
}
}
}
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
}
}
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));
}
}