Skip to content

Commit 669dd45

Browse files
committed
use regex pattern instead of indexOf
1 parent ebbb607 commit 669dd45

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/rules/no-html.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { findOffsets } from "../util.js";
2525
//-----------------------------------------------------------------------------
2626

2727
const htmlTagPattern = /<([a-z0-9]+(?:-[a-z0-9]+)*)[^>]*>/giu;
28+
const nextLinesPattern = /[\r\n][\s\S]*$/u;
2829

2930
//-----------------------------------------------------------------------------
3031
// Rule Definition
@@ -68,7 +69,7 @@ export default {
6869
},
6970

7071
create(context) {
71-
const allowed = new Set(context.options[0]?.allowed);
72+
const allowed = new Set(context.options[0].allowed);
7273

7374
return {
7475
html(node) {
@@ -86,11 +87,12 @@ export default {
8687
column: node.position.start.column + columnOffset,
8788
};
8889

89-
const firstLineEnd = fullMatch.indexOf("\n");
90+
const firstNewlineIndex =
91+
fullMatch.search(nextLinesPattern);
9092
const endColumn =
91-
firstLineEnd === -1
93+
firstNewlineIndex === -1
9294
? start.column + fullMatch.length
93-
: start.column + firstLineEnd;
95+
: start.column + firstNewlineIndex;
9496

9597
const end = {
9698
line: start.line,

tests/rules/no-html.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,36 @@ ruleTester.run("no-html", rule, {
188188
},
189189
],
190190
},
191+
{
192+
code: '<span\rclass="highlight"\rdata-id="123">Text</span>',
193+
errors: [
194+
{
195+
messageId: "disallowedElement",
196+
line: 1,
197+
column: 1,
198+
endLine: 1,
199+
endColumn: 6,
200+
data: {
201+
name: "span",
202+
},
203+
},
204+
],
205+
},
206+
{
207+
code: '<span\r\nclass="highlight"\r\ndata-id="123">Text</span>',
208+
errors: [
209+
{
210+
messageId: "disallowedElement",
211+
line: 1,
212+
column: 1,
213+
endLine: 1,
214+
endColumn: 6,
215+
data: {
216+
name: "span",
217+
},
218+
},
219+
],
220+
},
191221
{
192222
code: '<div class="test" >Content</div>',
193223
errors: [

0 commit comments

Comments
 (0)