Skip to content

Commit

Permalink
Merge 510c4a1 into 9d7785f
Browse files Browse the repository at this point in the history
  • Loading branch information
KamiKillertO committed Apr 22, 2020
2 parents 9d7785f + 510c4a1 commit 826a476
Show file tree
Hide file tree
Showing 52 changed files with 4,363 additions and 292 deletions.
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class Config {
if (shouldActiveRule(config[rule.name])) {
let ruleConfig = extractRuleConfig(config[rule.name], rule.name);
this.activatedRules[rule.name] = rule;
if (ruleConfig) {
if (ruleConfig !== null && ruleConfig !== undefined) {
ruleConfig = rule.configTransform ? rule.configTransform(ruleConfig) : ruleConfig;
if (rule.validateConfig) {
rule.validateConfig(ruleConfig);
Expand Down
101 changes: 97 additions & 4 deletions lib/rules/attr-bans/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { expect } = require("chai");
const linthtml = require("../../../index");
const none = require("../../../presets").presets.none;

function createLinter() {
return new linthtml.LegacyLinter(linthtml.rules);
}
describe("attr-bans", function() {
describe("legacy linter | attr-bans", function() {
function createLinter() {
return new linthtml.LegacyLinter(linthtml.rules);
}
it("Should not report an error for a tag named 'style'", async function() {
const linter = createLinter();
const html = "<body><style>hello</style></body>";
Expand Down Expand Up @@ -64,3 +64,96 @@ describe("attr-bans", function() {
expect(issues).to.have.lengthOf(1);
});
});
describe("attr-bans", function() {
function createLinter(rules) {
return linthtml.fromConfig({ rules });
}

it("Should not report an error for a tag named 'style'", async function() {
const linter = createLinter({ "attr-bans": [true, "style"] });
const html = "<body><style>hello</style></body>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should not report anything when disabled", async function() {
const linter = createLinter({
"attr-bans": false
});
const html = "<button style=\"color: red;\"></button>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should accept a single string as option", async function() {
const linter = createLinter({
"attr-bans": [
true,
"style"
]
});
const html = "<button style=\"color: red;\"></button>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});

it("Banned attributes should be case insensitive", async function() {
const linter = createLinter({
"attr-bans": [
true,
["ban0", "bAN1"]
]
});
const html = "<body ban0 ban1>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(2);
});

it("Should accept regexes as config", async function() {
const linter = createLinter({
"attr-bans": [
true,
[/on\w+/i]
]
});
const html = "<div onClick='' onfocus='' noop=''></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(2);
});

it("Should throw an error for an invalid config", async function() {
const config = {
"attr-bans": [
true,
true
]
};
expect(() => createLinter(config))
.to
.throw("Configuration for rule \"attr-bans\" is invalid: Expected string, RegExp or (string|RegExp)[] got boolean");
});

it("Should throw an error if not given a list of strings as config", function() {
const config = {
"attr-bans": [
true,
["string", true]
]
};
expect(() => createLinter(config))
.to
.throw("Configuration for rule \"attr-bans\" is invalid: Expected string, RegExp or (string|RegExp)[] got boolean[]");
});

it("Should report an error when the 'style' attribute is present", async function() {
const linter = createLinter({
"attr-bans": [
true,
"style"
]
});
const html = "<button style=\"color: red;\"></button>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});
});
177 changes: 173 additions & 4 deletions lib/rules/attr-name-style/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { expect } = require("chai");
const linthtml = require("../../../index");
const none = require("../../../presets").presets.none;

function createLinter() {
return new linthtml.LegacyLinter(linthtml.rules);
}
describe("attr-name-style", function() {
describe("legacy linter | attr-name-style", function() {
function createLinter() {
return new linthtml.LegacyLinter(linthtml.rules);
}
it("Should ignore attributes matching \"raw-ignore-text\"", async function() {
const linter = createLinter();
const html = "<div an-attribute {{ logic }} another-attribute {{ end }}></div>";
Expand Down Expand Up @@ -104,6 +104,175 @@ describe("attr-name-style", function() {
});
});
});
describe("attr-name-style", function() {
function createLinter(rules) {
return linthtml.fromConfig({ rules });
}
it("Should ignore attributes matching \"raw-ignore-text\"", async function() {
const linter = linthtml.fromConfig({
"raw-ignore-regex": "{{.*?}}",
rules: {
"attr-name-style": [
true,
"dash"
]
}
});
const html = "<div an-attribute {{ logic }} another-attribute {{ end }}></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should not report anything for correctly styled attribute names", async function() {
const linter = createLinter({
"attr-name-style": [
true,
"lowercase"
]
});
const html = "<div abc=\"\" fowj0wo3=\"\"></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should ignore ignored attributes", async function() {
const linter = linthtml.fromConfig({
"attr-name-ignore-regex": "xlink:href",
rules: {
"attr-name-style": [
true,
"dash"
]
}
});
const html = "<xml xlink:href=\"\"></xml>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should not report anything when disabled", async function() {
const linter = createLinter({
"attr-name-style": false
});
const html = "<div abc=\"\" 2fOwj_0o-3=\"\"></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

describe("'lowercase' format", function() {
it("Should not report an error for attributes with valid format", async function() {
const linter = createLinter({
"attr-name-style": [
true,
"lowercase"
]
});
const html = "<div foo=\"\"></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error for attributes with invalid format", async function() {
const linter = createLinter({
"attr-name-style": [
true,
"lowercase"
]
});
const html = "<div FooBar=\"\" foo-bar=\"\"></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(2);
});
});
describe("'dash' format", function() {
it("Should not report an error for attributes with valid format", async function() {
const linter = createLinter({
"attr-name-style": [
true,
"dash"
]
});
const html = "<div foo-bar=\"\"></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error for attributes with invalid format", async function() {
const linter = createLinter({
"attr-name-style": [
true,
"dash"
]
});
const html = "<div FooBar=\"\" foo_bar=\"\"></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(2);
});
});

it("Should throw an error when an invalid config is passed", function() {
const config = {
"attr-name-style": [
true,
["camel"]
]
};
expect(() => createLinter(config))
.to
.throw("Configuration for rule \"attr-name-style\" is invalid: Expected string or RegExp got object");
});

describe("'regexp' format", function() {
it("Should not report an error for attributes with valid format", async function() {
const linter = createLinter({
"attr-name-style": [
true,
/^[0-9a-o]+$/
]
});
const html = "<div foo=\"\"></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error for attributes with invalid format", async function() {
const linter = createLinter({
"attr-name-style": [
true,
/^[0-9a-o]+$/
]
});
const html = "<div bar></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});
});
describe("'camel' format", function() {
it("Should not report an error for attributes with valid format", async function() {
const linter = createLinter({
"attr-name-style": [
true,
"camel"
]
});
const html = "<div FooBar=\"\"></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error for attributes with invalid format", async function() {
const linter = createLinter({
"attr-name-style": [
true,
"camel"
]
});
const html = "<div foo-bar></div>";
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});
});
});
// NOPE
// {
// input: '<div deadbeef1337="" fail="" fails=""></div>',
Expand Down

0 comments on commit 826a476

Please sign in to comment.