Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

#356 - new lint rule to disallow .col-*-12 classes alone #368

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 23 additions & 0 deletions src/bootlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,29 @@ var LocationIndex = _location.LocationIndex;
reporter('Found one or more `<input>`s missing a `type` attribute.', inputsMissingTypeAttr);
}
});
addLinter('W018', function lintUselessFullColumns($, reporter) {
var selector = SCREENS.map(function (size) {
return '.col-' + size + '-' + NUM_COLS;
}).join(',');
var fullRows = $(selector);
if (fullRows.length) {
var fullColRegex = new RegExp('^col-(' + SCREENS.join('|') + ')-' + NUM_COLS + '$');
fullRows.each(function (_index, row) {
var $row = $(row);
var gridClasses = $row.attr('class').match(COL_REGEX_G);
var fullWidthColCount = 0;
for (var i = 0; i < gridClasses.length; i++) {
if (fullColRegex.test(gridClasses[i])) {
fullWidthColCount++;
}
}
if (fullWidthColCount === gridClasses.length) {
//The ONLY grid classes found here were full column classes.
reporter('`.col-*-' + NUM_COLS + '` classes used alone are not useful. This element does not need to be in a row/column since it will render the same way without using the grid here at all', $row);
}
});
}
});

addLinter('E001', (function () {
var MISSING_DOCTYPE = 'Document is missing a DOCTYPE declaration';
Expand Down
12 changes: 12 additions & 0 deletions test/bootlint_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,5 +977,17 @@ exports.bootlint = {
'should complain about a `.pull-right/.pull-left` classes on `.row` AND manual `style="float:left;"/style="float:right;"` on a `.row`'
);
test.done();
},
'.col-*-12 columns by themselves are not useful': function (test) {
test.expect(1);
test.deepEqual(lintHtml(utf8Fixture('grid/col-no-full-width.html')),
[
'Since grid classes apply to devices with screen widths greater than or equal to the breakpoint sizes (unless overridden by grid classes targeting larger screens), `class="col-xs-12 col-sm-12"` is redundant and can be simplified to `class="col-xs-12"`',
'`.col-*-12` classes used alone are not useful. This element does not need to be in a row/column since it will render the same way without using the grid here at all',
'`.col-*-12` classes used alone are not useful. This element does not need to be in a row/column since it will render the same way without using the grid here at all'
],
'should complain about a `.col-*-12` class used with no other grid classes AND about multiple `.col-*-12` classes appearing with no other grid classes'
);
test.done();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="col-md-8">
<div class="alert alert-danger alert-dismissible">
<ul>
<li>This list should come after the button</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="col-md-8">
<div class="alert alert-danger alert-dismissible"><button type="button" class="close">Close</button>This text should be after the button.</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="col-md-8">
<div class="alert alert-danger alert-dismissible">
This text should be after the button.
<button type="button" class="close">Close</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="col-md-8">
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">
<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/alert-dismiss-close/valid.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="col-md-8">
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert">
<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/containers/columns.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div class="row">
<div class="col-xs-6">
<div class="row">
<div class="col-xs-12">
<div class="col-xs-6">
We can nest rows within columns.
</div>
</div>
Expand Down
45 changes: 45 additions & 0 deletions test/fixtures/grid/col-no-full-width.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Test</title>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="../../lib/jquery.min.js"></script>

<link rel="stylesheet" href="../../lib/qunit.css">
<script src="../../lib/qunit.js"></script>
<script src="../../../dist/browser/bootlint.js"></script>
<script src="../generic-qunit.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12">Useless</div>
</div>

<div class="row">
<div class="col-xs-12 col-sm-12">Useless</div>
</div>

<div class="row">
<div class="col-xs-12 col-sm-6 col-md-12">This is fine</div>
</div>

<div class="row">
<div class="col-sm-6 col-md-12">This is fine</div>
</div>
</div>

<div id="qunit"></div>
<ol id="bootlint">
<li data-lint='Since grid classes apply to devices with screen widths greater than or equal to the breakpoint sizes (unless overridden by grid classes targeting larger screens), `class="col-xs-12 col-sm-12"` is redundant and can be simplified to `class="col-xs-12"`'></li>
<li data-lint="`.col-*-12` classes used alone are not useful. This element does not need to be in a row/column since it will render the same way without using the grid here at all"></li>
<li data-lint="`.col-*-12` classes used alone are not useful. This element does not need to be in a row/column since it will render the same way without using the grid here at all"></li>
</ol>
</body>
</html>
2 changes: 1 addition & 1 deletion test/fixtures/input-group/mixed-with-grid-col.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<body>
<div class="container">
<div class="row">
<div class="input-group col-sm-12">
<div class="input-group col-sm-8">
<span class="input-group-addon">@</span>
<input type="text" class="form-control" placeholder="Username">
</div>
Expand Down