我有很多HTML文档在项目的根目录下。让我们拿一个简单的骨架HTML文档举例:
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<!-- Place favicon.ico in the root directory -->
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!--[if lt IE 8]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<a href="#">hello</a>
<a href="">hello</a>
<a href="#">hello</a>
<a href="">hello</a>
<a href="#">hello</a>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="js/scripts.js"></script>
</body>
</html>
现在在将这些文件发送给开发团队之前,我被指定任务检查是否存在没有href链接、空href或者带有空片段作为href的链接。也就是说,基本上不能有以下这样的链接:
<a href="">
或者
<a href="#">
或者
<a>
我找到了这个gulp插件,但是我有一些问题。首先让我们看一下gulp文件:
gulp.task("checkDev", function(callback) {
var options = {
pageUrls: [
'http://localhost:8080/Gulp-Test/index.html'
],
checkLinks: true,
summary: true
};
checkPages(console, options, callback);
});
请注意,当您传递选项
checkLinks: true
时,不仅对于a
标签,对于此页面上提到的所有标签都是如此。如果<a>
标签为空或只有#或根本不存在,则插件没有问题。看看我运行gulp任务时会发生什么: 因此,我希望仅检查
a
链接,如果<a>
标签没有href、空值或仅有#,则应引发错误或在摘要报告中显示。最后,请参阅关于如何传递pageUrl(即要检查的页面)的示例gulp文件:
pageUrls: [
'http://localhost:8080/Gulp-Test/index.html'
],
我该如何让这个插件检查
Gulp-Test
目录下的所有 .html
文件呢?总结一下我的问题:我该如何让这个插件在发现没有
href
属性、href
属性为空或者为 # 时抛出错误(即在摘要报告中显示),并且告诉它检查一个目录下的所有 .html 文件。
noEmptyFragments: true
,只有<a href="#"></a>会失败... 但是<a></a>和<a href=""></a>会通过。 - Alexander Solonik