Skip to content

Commit bd0218c

Browse files
authored
code: Make isHtmlString a util (#325)
1 parent b068236 commit bd0218c

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/rules/no-parse-html-literal.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
const utils = require( '../utils.js' );
44

5-
// HTML regex (modified from jQuery)
6-
const rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*)$/;
75
// Single tag regex (from jQuery)
86
const rsingleTag = /^<([a-z][^/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i;
97
const rsingleTagMinimal = /^<([a-z][^/\0>:\x20\t\r\n\f]*)>$/i;
@@ -74,11 +72,10 @@ module.exports = {
7472
let expectedTag;
7573
const arg = node.arguments[ 0 ];
7674
if ( allowSingle ) {
77-
const value = arg && utils.allLiteral( arg ) && utils.joinLiterals( arg );
78-
if ( !( typeof value === 'string' && value ) || !rquickExpr.exec( value ) ) {
79-
// Empty or non-string, or non-HTML
75+
if ( !utils.isHtmlString( arg ) ) {
8076
return;
8177
}
78+
const value = utils.joinLiterals( arg );
8279
let match;
8380
if ( ( match = rsingleTag.exec( value ) ) ) {
8481
// Single tag

src/utils.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,14 @@ function joinLiterals( node ) {
562562
}
563563
}
564564

565+
// HTML regex (modified from jQuery)
566+
const rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*)$/;
567+
568+
function isHtmlString( arg ) {
569+
const value = arg && allLiteral( arg ) && joinLiterals( arg );
570+
return typeof value === 'string' && value && rquickExpr.exec( value );
571+
}
572+
565573
module.exports = {
566574
isjQuery,
567575
isjQueryConstructor,
@@ -575,5 +583,6 @@ module.exports = {
575583
jQueryCollectionLink,
576584
jQueryGlobalLink,
577585
allLiteral,
578-
joinLiterals
586+
joinLiterals,
587+
isHtmlString
579588
};

0 commit comments

Comments
 (0)