Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 14 additions & 3 deletions src/rules/no-html.js
Comment thread
TKDev7 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import { findOffsets } from "../util.js";
// Helpers
//-----------------------------------------------------------------------------

const htmlTagPattern = /<([a-z0-9]+(?:-[a-z0-9]+)*)/giu;
const htmlTagPattern =
Comment thread
lumirlumir marked this conversation as resolved.
/<([a-z0-9]+(?:-[a-z0-9]+)*)(?:\s+(?:[^>"']|"[^"]*"|'[^']*')*)?>/giu;
const lineEndingPattern = /\r\n?|\n/u;

//-----------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -68,13 +70,14 @@ export default {
},

create(context) {
const allowed = new Set(context.options[0]?.allowed);
const allowed = new Set(context.options[0].allowed);

return {
html(node) {
let match;

while ((match = htmlTagPattern.exec(node.value)) !== null) {
const fullMatch = match[0];
const tagName = match[1];
const { lineOffset, columnOffset } = findOffsets(
node.value,
Expand All @@ -84,9 +87,17 @@ export default {
line: node.position.start.line + lineOffset,
column: node.position.start.column + columnOffset,
};

const firstNewlineIndex =
fullMatch.search(lineEndingPattern);
const endColumn =
firstNewlineIndex === -1
? start.column + fullMatch.length
: start.column + firstNewlineIndex;
Comment thread
lumirlumir marked this conversation as resolved.

const end = {
line: start.line,
column: start.column + match[0].length + 1,
column: endColumn,
};

if (allowed.size === 0 || !allowed.has(tagName)) {
Expand Down
202 changes: 202 additions & 0 deletions tests/rules/no-html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,207 @@ ruleTester.run("no-html", rule, {
},
],
},
{
code: "<span >Content</span>",
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 8,
data: { name: "span" },
},
],
},
{
code: '<div id="foo">Some content</div>',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 15,
data: {
name: "div",
},
},
],
},
{
code: '<p class="text-center" data-attr="value">Content</p>',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 42,
data: {
name: "p",
},
},
],
},
{
code: '<span\nclass="highlight"\ndata-id="123">Text</span>',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 6,
data: {
name: "span",
},
},
],
},
{
code: '<span\rclass="highlight"\rdata-id="123">Text</span>',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 6,
data: {
name: "span",
},
},
],
},
{
code: '<span\r\nclass="highlight"\r\ndata-id="123">Text</span>',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 6,
data: {
name: "span",
},
},
],
},
{
code: '<div class="test" >Content</div>',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 24,
data: { name: "div" },
},
],
},
{
code: '<input type="text" placeholder="Enter text" />',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 47,
data: {
name: "input",
},
},
],
},
{
code: '<a href="#"><span class="highlight">Link</span></a>',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 13,
data: { name: "a" },
},
{
messageId: "disallowedElement",
line: 1,
column: 13,
endLine: 1,
endColumn: 37,
data: { name: "span" },
},
],
},
{
code: '<input placeholder=">" />',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 26,
data: {
name: "input",
},
},
],
},
{
code: "<input placeholder='>'></input>",
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 24,
data: {
name: "input",
},
},
],
},
{
code: '<input\nplaceholder=">" />',
errors: [
{
messageId: "disallowedElement",
line: 1,
column: 1,
endLine: 1,
endColumn: 7,
data: {
name: "input",
},
},
],
},
Comment thread
snitin315 marked this conversation as resolved.
{
code: dedent`
<!-- comment -->
<input
placeholder="Enter name"
name="First Name"
/>`,
errors: [
{
messageId: "disallowedElement",
line: 2,
column: 1,
endLine: 2,
endColumn: 7,
data: {
name: "input",
},
},
],
},
],
});
Loading