From b63f567945a669fdf3fa9f2af4d6846044e5985c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 13 Mar 2013 02:05:35 -0400 Subject: [PATCH] Add tpl-lint tool inspired by pull request #132 --- tools/scripts/tpl-lint | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 tools/scripts/tpl-lint diff --git a/tools/scripts/tpl-lint b/tools/scripts/tpl-lint new file mode 100755 index 0000000000..d2e8bcb19d --- /dev/null +++ b/tools/scripts/tpl-lint @@ -0,0 +1,92 @@ +#!/usr/bin/env php +find('a') as $a) { + check_a($a, $reporter); + } +} + +/** + * Scan an tag + * + * @param object $a + * @param callable $reporter + * @return void + */ +function check_a($a, $reporter) { + if (!$a->hasAttribute('href')) { + // anchor, don't care + return; + } + + $href = trim($a->getAttribute('href')); + if (preg_match('/javascript:/', $href)) { + $reporter('javascript-url', " has javascript url: $href"); + return; + } + if ($href == '#' && $a->hasAttribute('onclick')) { + $onclick = $a->getAttribute('onclick'); + if (!js_returns_false($onclick) && !js_returns_func($onclick)) { + $reporter('a-no-return', " has href=# but handler fails to return false: $onclick"); + return; + } + } + if ($href != '#' && $a->hasAttribute('onclick')) { + $onclick = $a->getAttribute('onclick'); + $reporter('a-double-action', " has both URL ($href) and onclick ($onclick)"); + return; + } +} + +/** + * Determine if snippet of JS returns strictly false + */ +function js_returns_false($js) { + return + // last in a series of statements + preg_match('/; *return +false *; *$/', $js) + || + // only statement + preg_match('/^ *return +false *;? *$/', $js); +} + +/** + * Determine if snippet of JS returns a function call + */ +function js_returns_func($js) { + return preg_match('/^ *return +[a-zA-Z0-9\._$]+\(/', $js); +} -- 2.25.1