Issue #3439646 by Tom Konda, smustgrave: Some of string comparisons should use String.prototype.startsWith() or String.prototype.endsWith()

merge-requests/7986/head
nod_ 2024-05-09 05:41:59 +09:00
parent d436711365
commit f5cc65456a
No known key found for this signature in database
GPG Key ID: 76624892606FA197
7 changed files with 22 additions and 18 deletions

View File

@ -84,7 +84,7 @@
// jQuery UI does not support percentages on heights, convert to pixels.
if (
typeof optionValue === 'string' &&
/%$/.test(optionValue) &&
optionValue.endsWith('%') &&
/height/i.test(option)
) {
// Take offsets in account.

View File

@ -448,7 +448,7 @@ window.Drupal = { behaviors: {}, locale: {} };
// Consider URLs that match this site's base URL but use HTTPS instead of HTTP
// as local as well.
if (protocol === 'http:' && absoluteUrl.indexOf('https:') === 0) {
if (protocol === 'http:' && absoluteUrl.startsWith('https:')) {
protocol = 'https:';
}
let baseUrl = `${protocol}//${
@ -469,7 +469,7 @@ window.Drupal = { behaviors: {}, locale: {} };
// The given URL matches the site's base URL, or has a path under the site's
// base URL.
return absoluteUrl === baseUrl || absoluteUrl.indexOf(`${baseUrl}/`) === 0;
return absoluteUrl === baseUrl || absoluteUrl.startsWith(`${baseUrl}/`);
};
/**

View File

@ -29,15 +29,18 @@
const regexVertical = /top|center|bottom/;
const regexOffset = /[+-]\d+(\.[\d]+)?%?/;
const regexPosition = /^\w+/;
const regexPercent = /%$/;
const _position = $.fn.position;
function getOffsets(offsets, width, height) {
return [
parseFloat(offsets[0]) *
(regexPercent.test(offsets[0]) ? width / 100 : 1),
(typeof offsets[0] === 'string' && offsets[0].endsWith('%')
? width / 100
: 1),
parseFloat(offsets[1]) *
(regexPercent.test(offsets[1]) ? height / 100 : 1),
(typeof offsets[1] === 'string' && offsets[1].endsWith('%')
? height / 100
: 1),
];
}

View File

@ -26,7 +26,7 @@
_.chain(storage)
.keys()
.each((key) => {
if (key.substring(0, 18) === 'Drupal.contextual.') {
if (key.startsWith('Drupal.contextual.')) {
storage.removeItem(key);
}
});

View File

@ -59,7 +59,7 @@
drupalSettings.path.baseUrl.length,
);
// Ensure we have a correct path.
if (viewHref && path.substring(0, viewHref.length + 1) === `${viewHref}/`) {
if (viewHref && path.startsWith(`${viewHref}/`)) {
returnObj.view_args = decodeURIComponent(
path.substring(viewHref.length + 1, path.length),
);
@ -80,7 +80,7 @@
Drupal.Views.pathPortion = function (href) {
// Remove e.g. http://example.com if present.
const protocol = window.location.protocol;
if (href.substring(0, protocol.length) === protocol) {
if (href.startsWith(protocol)) {
// 2 is the length of the '//' that normally follows the protocol.
href = href.substring(href.indexOf('/', protocol.length + 2));
}
@ -99,8 +99,8 @@
Drupal.Views.getPath = function (href) {
href = Drupal.Views.pathPortion(href);
href = href.substring(drupalSettings.path.baseUrl.length, href.length);
if (href.startsWith('?q=')) {
// 3 is the length of the '?q=' added to the URL without clean URLs.
if (href.substring(0, 3) === '?q=') {
href = href.substring(3, href.length);
}
const chars = ['#', '?', '&'];

View File

@ -245,8 +245,9 @@
// Set the URL to go to the anchor.
elementSettings.url = $link.attr('href');
if (
Drupal.Views.getPath(elementSettings.url).substring(0, 21) !==
'admin/structure/views'
!Drupal.Views.getPath(elementSettings.url).startsWith(
'admin/structure/views',
)
) {
return true;
}
@ -273,8 +274,9 @@
// Set the URL to go to the anchor.
elementSettings.url = $(submit.form).attr('action');
if (
Drupal.Views.getPath(elementSettings.url).substring(0, 21) !==
'admin/structure/views'
!Drupal.Views.getPath(elementSettings.url).startsWith(
'admin/structure/views',
)
) {
return true;
}

View File

@ -820,7 +820,6 @@ module.exports = {
const regexVertical = /top|center|bottom/;
const regexOffset = /[+-]\d+(\.[\d]+)?%?/;
const regexPosition = /^\w+/;
const regexPercent = /%$/;
let positions = offset.split(' ');
if (positions.length === 1) {
if (regexHorizontal.test(positions[0])) {
@ -837,13 +836,13 @@ module.exports = {
return {
horizontalOffset: horizontalOffset
? parseFloat(horizontalOffset[0]) *
(regexPercent.test(horizontalOffset[0])
(horizontalOffset[0].endsWith('%')
? element.offsetWidth / 100
: 1)
: 0,
verticalOffset: verticalOffset
? parseFloat(verticalOffset[0]) *
(regexPercent.test(verticalOffset[0])
(verticalOffset[0].endsWith('%')
? element.offsetWidth / 100
: 1)
: 0,