diff --git a/lib/rules/max-line-length/README.md b/lib/rules/max-line-length/README.md index 27ca46a162..f8e0324471 100644 --- a/lib/rules/max-line-length/README.md +++ b/lib/rules/max-line-length/README.md @@ -10,7 +10,7 @@ a { color: red } Lines that exceed the maximum length but contain no whitespace (other than at the beginning of the line) are ignored. -When evaluating the line length, `url(...)` functions are collapsed into just `url()`, because typically you have no control over the length of its argument. This means that long `url()` functions should not contribute to warnings. +When evaluating the line length, the arguments of any `url(...)` functions are excluded from the calculation, because typically you have no control over the length of these arguments. This means that long `url()` functions should not contribute to warnings. ## Options @@ -103,7 +103,7 @@ a { color: pink; } ```css /* * comment that is too long the max length - * comment that is too long the max length + * comment that is too long the max length * */ a { color: pink; } diff --git a/lib/rules/max-line-length/index.js b/lib/rules/max-line-length/index.js index 49bd728108..dd99328494 100644 --- a/lib/rules/max-line-length/index.js +++ b/lib/rules/max-line-length/index.js @@ -1,5 +1,6 @@ "use strict" +const execall = require("execall") const optionsMatches = require("../../utils/optionsMatches") const report = require("../../utils/report") const ruleMessages = require("../../utils/ruleMessages") @@ -32,9 +33,7 @@ const rule = function (maxLength, options) { return } - // Collapse all urls into something nice and short, - // so they do not throw the game - const rootString = root.toString().replace(/url\(.*\)/ig, "url()") + const rootString = root.toString() const ignoreNonComments = optionsMatches(options, "ignore", "non-comments") const ignoreComments = optionsMatches(options, "ignore", "comments") @@ -66,9 +65,16 @@ const rule = function (maxLength, options) { nextNewlineIndex = rootString.length } + const rawLineLength = nextNewlineIndex - match.endIndex + const lineText = rootString.slice(match.endIndex, nextNewlineIndex) + const urlValueLengths = execall(/url\((.*)\)/ig, lineText).reduce((result, match) => { + return result + _.get(match, "sub[0].length", 0) + }, 0) + // If the line's length is less than or equal to the specified - // max, ignore it ... So anything below is liable to be complained about - if (nextNewlineIndex - match.endIndex <= maxLength) { + // max, ignore it ... So anything below is liable to be complained about. + // **Note that the length of any url values are excluded from the calculation.** + if (rawLineLength - urlValueLengths <= maxLength) { return }