Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code: Make isHtmlString a util #325

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 2 additions & 5 deletions src/rules/no-parse-html-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

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

// HTML regex (modified from jQuery)
const rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*)$/;
// Single tag regex (from jQuery)
const rsingleTag = /^<([a-z][^/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i;
const rsingleTagMinimal = /^<([a-z][^/\0>:\x20\t\r\n\f]*)>$/i;
Expand Down Expand Up @@ -74,11 +72,10 @@ module.exports = {
let expectedTag;
const arg = node.arguments[ 0 ];
if ( allowSingle ) {
const value = arg && utils.allLiteral( arg ) && utils.joinLiterals( arg );
if ( !( typeof value === 'string' && value ) || !rquickExpr.exec( value ) ) {
// Empty or non-string, or non-HTML
if ( !utils.isHtmlString( arg ) ) {
return;
}
const value = utils.joinLiterals( arg );
let match;
if ( ( match = rsingleTag.exec( value ) ) ) {
// Single tag
Expand Down
11 changes: 10 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
//
// Returns true if the function call node is attached to a jQuery element set.
function isjQuery( context, node ) {
const variablePattern = new RegExp(

Check warning on line 176 in src/utils.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Found non-literal argument to RegExp Constructor

Check warning on line 176 in src/utils.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Found non-literal argument to RegExp Constructor

Check warning on line 176 in src/utils.js

View workflow job for this annotation

GitHub Actions / build (22.x)

Found non-literal argument to RegExp Constructor

Check warning on line 176 in src/utils.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Found non-literal argument to RegExp Constructor

Check warning on line 176 in src/utils.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Found non-literal argument to RegExp Constructor

Check warning on line 176 in src/utils.js

View workflow job for this annotation

GitHub Actions / build (22.x)

Found non-literal argument to RegExp Constructor
( context.settings && context.settings[ 'no-jquery' ] && context.settings[ 'no-jquery' ].variablePattern ) ||
'^\\$.'
);
Expand Down Expand Up @@ -562,6 +562,14 @@
}
}

// HTML regex (modified from jQuery)
const rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*)$/;

function isHtmlString( arg ) {
const value = arg && allLiteral( arg ) && joinLiterals( arg );
return typeof value === 'string' && value && rquickExpr.exec( value );
}

module.exports = {
isjQuery,
isjQueryConstructor,
Expand All @@ -575,5 +583,6 @@
jQueryCollectionLink,
jQueryGlobalLink,
allLiteral,
joinLiterals
joinLiterals,
isHtmlString
};
Loading