Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 103 additions & 13 deletions Contrib/PhantomJS/HTMLCS_Run.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,89 @@
var page = require('webpage').create(),
system = require('system'),
address, standard;
address, standard, reportType;
var messages = {
'ERROR': [],
'WARNING': [],
'NOTICE': []
};

if (system.args.length < 3 || system.args.length > 3) {
console.log('Usage: phantomjs HTMLCS_Run.js URL standard');
console.log(' available standards: "WCAG2A", "WCAG2AA", "WCAG2AAA"');
if (system.args.length < 3 || system.args.length > 4) {
console.log('Usage: phantomjs HTMLCS_Run.js URL standard [report]');
console.log(' available standards: "WCAG2A", "WCAG2AA", "WCAG2AAA", "Section508"');
console.log(' available reports: "default" (default if omitted), "table"');
phantom.exit();
} else {
address = system.args[1];
standard = system.args[2];
address = system.args[1];
standard = system.args[2];
reportType = 'default';
if (system.args.length > 3) {
reportType = system.args[3];
}

// Default reporter.
var reportDefaultFn = function(cb) {
var levels = ['ERROR', 'WARNING', 'NOTICE'];
for (var lvl = 0; lvl < levels.length; lvl++) {
for (var i = 0; i < messages[levels[lvl]].length; i++) {
var thisMsg = messages[levels[lvl]][i];
var line = thisMsg.join('|');
console.log(line);
}
}

cb();
}

// Table reporter.
var reportTableFn = function(cb) {
console.log('LEVEL | MESSAGE CODE | NODE NAME | ELEMENT ID');
console.log('--------+------------------------------------------+------------+---------------------');
var levels = ['ERROR', 'WARNING', 'NOTICE'];
for (var lvl = 0; lvl < levels.length; lvl++) {
for (var i = 0; i < messages[levels[lvl]].length; i++) {
var thisMsg = messages[levels[lvl]][i];
var line = '';
var stdMsg = thisMsg[1].split('.');
stdMsg.splice(0, 3);
stdMsg = stdMsg.join('.');
if (stdMsg.length > 40) {
stdMsg = '...' + stdMsg.substr(stdMsg.length - 37);
}

line += (thisMsg[0] + Array(8).join(' ')).substr(0, 7) + ' | ';
line += (stdMsg + Array(41).join(' ')).substr(0, 40) + ' | ';
line += (thisMsg[2] + Array(11).join(' ')).substr(0, 10) + ' | ';
line += (thisMsg[3] + Array(21).join(' ')).substr(0, 20);
console.log(line);

var remText = thisMsg[4];
while (remText.length > 0) {
line = '';
line += (Array(8).join(' ')).substr(0, 7) + ' | ';

if (remText.length < 75) {
line += remText;
console.log(line);
break;
} else {
var lastSpace = remText.substr(0, 75).lastIndexOf(' ');
line += remText.substr(0, lastSpace);
console.log(line);
remText = remText.substr(lastSpace + 1);
}
}

console.log('--------+------------------------------------------+------------+---------------------');

}
}

console.log('');
console.log('Errors: ' + messages['ERROR'].length + ', Warnings: ' + messages['WARNING'].length +
', Notices: ' + messages['NOTICE'].length);
cb();
}

page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
Expand All @@ -18,8 +93,26 @@ if (system.args.length < 3 || system.args.length > 3) {

// Override onConsoleMessage function for outputting.
page.onConsoleMessage = function (msg) {
if (msg === 'done') phantom.exit();
console.log(msg);
var thisMsg;
if (msg.indexOf('[HTMLCS] ') === 0) {
thisMsg = msg.substr(9, msg.length).split('|');
messages[thisMsg[0]].push(thisMsg);
} else if (msg === 'done') {
var cb = function() {
phantom.exit();
}
switch (reportType.toLowerCase()) {
case 'table':
reportTableFn(cb);
break;

default:
reportDefaultFn(cb);
break;
}
} else {
console.log(msg);
}
};

// Include all sniff files.
Expand Down Expand Up @@ -48,13 +141,10 @@ if (system.args.length < 3 || system.args.length > 3) {
// the loaded page's context. We can't pass any variable to it.
switch (standard) {
case 'WCAG2A':
page.evaluate(function() {HTMLCS_RUNNER.run('WCAG2A');});
break;
case 'WCAG2AA':
page.evaluate(function() {HTMLCS_RUNNER.run('WCAG2AA');});
break;
case 'WCAG2AAA':
page.evaluate(function() {HTMLCS_RUNNER.run('WCAG2AAA');});
case 'Section508':
page.evaluate(function(standard) {HTMLCS_RUNNER.run(standard);}, standard);
break;
default:
console.log('Unknown standard.');
Expand Down
13 changes: 10 additions & 3 deletions Contrib/PhantomJS/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ var HTMLCS_RUNNER = new function() {
self.output(messages[i]);
msgCount[messages[i].type]++;
}
console.log('Errors: ' + msgCount[HTMLCS.ERROR] + ', Warnings: ' + msgCount[HTMLCS.WARNING] +
', Notices: ' + msgCount[HTMLCS.NOTICE]);
console.log('done');
}, function() {
console.log('Something in HTML_CodeSniffer failed to parse. Cannot run.');
Expand All @@ -41,7 +39,16 @@ var HTMLCS_RUNNER = new function() {
break;
}//end switch

console.log(typeName + '|' + msg.code + '|' + msg.msg);
var nodeName = '';
if (msg.element) {
nodeName = msg.element.nodeName.toLowerCase();
}

var elementId = '';
if (msg.element.id && (msg.element.id !== '')) {
elementId = '#' + msg.element.id;
}
console.log('[HTMLCS] ' + typeName + '|' + msg.code + '|' + nodeName + '|' + elementId + '|' + msg.msg);
};

};
187 changes: 187 additions & 0 deletions Contrib/Testing/HTMLCS_Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
var page = require('webpage').create(),
system = require('system'),
testfile, address, standard, reportType,
fs = require('fs');

var messages = [];
var testData = {
name: '',
standard: '',
assertions: []
};

if (system.args.length < 2 || system.args.length > 2) {
console.log('Usage: phantomjs HTMLCS_Test.js test_name');
phantom.exit(255);
} else {
testfile = system.args[1];
if (testfile.substr(-5) !== '.html') {
testfile += '.html';
}
address = 'file://' + fs.absolute('../../') + 'Tests/' + testfile;
var content = fs.read('../../Tests/' + testfile);

if (!content) {
console.log('Test file ' + testfile + ' doesn\'t exist?');
phantom.exit(2);
} else {
var indexStart = content.indexOf('<!-- @HTMLCS_Test@\n');
if (indexStart === -1) {
console.log('No HTMLCS_Test banner?');
phantom.exit(2);
}
var indexEnd = content.indexOf('\n-->', indexStart + 1);

var testContent = content.substr(indexStart, indexEnd - indexStart);
var testContent = testContent.split("\n");
testContent.splice(0, 1);

for (var i = 0; i < testContent.length; i++) {
var words = testContent[i].split(' ');
switch (words[0]) {
case 'Name:':
testData.name = words.slice(1, words.length).join(' ');
break;

case 'Standard:':
testData.standard = words.slice(1, words.length).join(' ');
break;

case 'Assert:':
var assertion = {
line: null,
expected: true,
level: null,
code: null,
id: null,
triggered: false
};

// Get rid of "Assert";
words.shift();

assertion.line = words.join(' ');

if (words[0].toLowerCase() === 'no') {
words.shift();
assertion.expected = false;
}
assertion.level = words.shift().toUpperCase();
assertion.code = words.shift();
assertion.code = assertion.code.replace('.', '\\.');
assertion.code = assertion.code.replace('*', '.*');
assertion.code = new RegExp('^' + assertion.code + '$');

if (words[0].toLowerCase() === 'on') {
words.shift();
assertion.id = words.shift();
}

testData.assertions.push(assertion);
break;
}//end switch
}//end for
}//end if

// Default reporter.
var reportDefaultFn = function() {
var assertion, thisMsg;
for (var assert = 0; assert < testData.assertions.length; assert++) {
assertion = testData.assertions[assert];
for (var i = 0; i < messages.length; i++) {
thisMsg = messages[i];
if (assertion.level === thisMsg[0]) {
if (assertion.code.test(thisMsg[1]) === true) {
if (assertion.id === thisMsg[3]) {
assertion.triggered = true;
} else if (assertion.id === null) {
assertion.triggered = true;
}
}
}
}
}

var failures = 0;
console.info('Results for ' + testData.name + ' (' + testData.standard + '):');
for (var assert = 0; assert < testData.assertions.length; assert++) {
assertion = testData.assertions[assert];
if (assertion.triggered !== assertion.expected) {
console.info(' [FAIL] ' + assertion.line);
failures++;
} else {
console.info(' [ OK ] ' + assertion.line);
}
}

var retval = 0;
if (failures === 0) {
console.info('OK');
} else {
console.info('FAILURES!');
retval = 1;
}

console.info('Assertions: ' + testData.assertions.length + ', Passed: ' + (testData.assertions.length - failures) + ', Failed: ' + failures);
phantom.exit(retval);
};

page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address ' + address);
phantom.exit(2);
} else {
window.setTimeout(function () {

// Override onConsoleMessage function for outputting.
page.onConsoleMessage = function (msg) {
var thisMsg;
if (msg.indexOf('[HTMLCS] ') === 0) {
thisMsg = msg.substr(9, msg.length).split('|');
messages.push(thisMsg);
} else if (msg === 'done') {
reportDefaultFn();
} else {
console.log(msg);
}
};

// Include all sniff files.
var injectAllStandards = function(dir) {
var files = fs.list(dir),
filesLen = files.length,
absPath = '';
for (var i = 0; i < filesLen; i++) {
if (files[i] === '.' || files[i] === '..') continue;

absPath = fs.absolute(dir + '/' + files[i]);
if (fs.isDirectory(absPath) === true) {
injectAllStandards(absPath);
} else if (fs.isFile(absPath) === true) {
page.injectJs(absPath);
}
}
};

injectAllStandards('../../Standards');
page.injectJs('../../HTMLCS.js');
page.injectJs('../PhantomJS/runner.js');

// Now Run. Note that page.evaluate() function is sanboxed to
// the loaded page's context. We can't pass any variable to it.
switch (testData.standard) {
case 'WCAG2A':
case 'WCAG2AA':
case 'WCAG2AAA':
case 'Section508':
page.evaluate(function(standard) {HTMLCS_RUNNER.run(standard);}, testData.standard);
break;
default:
console.log('Unknown standard.');
phantom.exit(2);
break;
}
}, 200);
}//end if
});//end
}//end if
30 changes: 30 additions & 0 deletions Contrib/Testing/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

WD=$PWD
NUMFAIL=0
NUMPASS=0
NUMTESTS=0
cd ../../Tests
FILES=`find . -name *.html`
cd $WD
for FILE in $FILES; do
REMAIN=`expr $NUMTESTS % 40`
if [[ "x$REMAIN" == "x0" ]]; then
if [[ "x$NUMTESTS" != "x0" ]]; then
echo ''
fi
fi

NUMTESTS=`expr $NUMTESTS + 1`
phantomjs HTMLCS_Test.js $FILE > /dev/null
if [[ "x$?" == "x0" ]]; then
NUMPASS=`expr $NUMPASS + 1`
echo -n '.'
else
NUMFAIL=`expr $NUMFAIL + 1`
echo -n 'F'
fi
done

echo ''
echo "Tests: $NUMTESTS, Passed: $NUMPASS, Failed: $NUMFAIL"
Loading