Skip to content
Snippets Groups Projects
Verified Commit c4c4de21 authored by Théodore Biadala's avatar Théodore Biadala
Browse files

Issue #3492582 by tom konda, zaryab_drupal, nod_, smustgrave: Replace some of...

Issue #3492582 by tom konda, zaryab_drupal, nod_, smustgrave: Replace some of obj && obj.prop with optional chaining
parent 7afad30a
No related branches found
No related tags found
No related merge requests found
Showing
with 29 additions and 47 deletions
......@@ -456,7 +456,7 @@
// If there isn't a form, jQuery.ajax() will be used instead, allowing us to
// bind Ajax to links as well.
if (this.element && this.element.form) {
if (this.element?.form) {
/**
* @type {jQuery}
*/
......@@ -1038,7 +1038,7 @@
(executionQueue, key) =>
executionQueue.then(() => {
const { command } = response[key];
if (command && ajaxCommands[command]) {
if (ajaxCommands?.[command]) {
// When a command returns a promise, the remaining commands will not
// execute until that promise has been fulfilled. This is typically
// used to ensure JavaScript files added via the 'add_js' command
......@@ -1894,10 +1894,9 @@
xhr.getResponseHeader('X-Drupal-Ajax-Token') === '1' &&
// The isInProgress() function might not be defined if the Ajax request
// was initiated without Drupal.ajax() or new Drupal.Ajax().
settings.isInProgress &&
// Until this is false, the Ajax request isn't completely done (the
// response's commands might still be running).
settings.isInProgress()
settings?.isInProgress()
);
};
$.extend(true, $.event.special, {
......
......@@ -142,11 +142,7 @@
}
// Skip processing upon a form validation error on a non-empty
// machine name.
if (
$target.hasClass('error') &&
$target[0].value &&
$target[0].value.trim().length
) {
if ($target.hasClass('error') && $target[0].value?.trim().length) {
return;
}
// Figure out the maximum length for the machine name.
......
This diff is collapsed.
This diff is collapsed.
......@@ -99,11 +99,7 @@ export default class DrupalImageUploadAdapter {
const response = xhr.response;
if (!response || response.error) {
return reject(
response && response.error && response.error.message
? response.error.message
: genericErrorText,
);
return reject(response?.error?.message || genericErrorText);
}
// Resolve with the `urls` property and pass the response
// to allow customizing the behavior of features relying on the upload adapters.
......
......@@ -501,7 +501,7 @@ export default class DrupalElementStyleUi extends Plugin {
// If style is selected, use the label of the selected style as the
// default label of the splitbutton.
dropdownButtonView.bind('label').to(command, 'value', (commandValue) => {
if (commandValue && commandValue[group]) {
if (commandValue?.[group]) {
// eslint-disable-next-line no-restricted-syntax
for (const style of definedStyles) {
if (style.name === commandValue[group]) {
......
......@@ -130,7 +130,7 @@
});
}
if (drupalSettings.comment && drupalSettings.comment.newCommentsLinks) {
if (drupalSettings.comment?.newCommentsLinks) {
render(drupalSettings.comment.newCommentsLinks.node[fieldName]);
} else {
$.ajax({
......
......@@ -31,7 +31,7 @@
// We're given a generic name to look for so we find all inputs containing
// that name and copy over the input values that require all columns to be
// translatable.
if (options && options.dependent_selectors) {
if (options?.dependent_selectors) {
Object.keys(options.dependent_selectors).forEach((field) => {
$fields = $context.find(`input[name^="${field}"]`);
const dependentColumns = options.dependent_selectors[field];
......
......@@ -188,7 +188,7 @@
const uncachedTokens = [];
ids.forEach((contextualID) => {
const html = storage.getItem(`Drupal.contextual.${contextualID.id}`);
if (html && html.length) {
if (html?.length) {
// Initialize after the current execution cycle, to make the AJAX
// request for retrieving the uncached contextual links as soon as
// possible, but also to ensure that other Drupal behaviors have had
......
......@@ -124,8 +124,8 @@
// confirmation, because switching text formats might cause certain
// markup to be stripped away.
const supportContentFiltering =
drupalSettings.editor.formats[newFormatID] &&
drupalSettings.editor.formats[newFormatID].editorSupportsContentFiltering;
drupalSettings.editor.formats[newFormatID]
?.editorSupportsContentFiltering;
// If there is no content yet, it's always safe to change the text format.
const hasContent = field.value !== '';
if (hasContent && supportContentFiltering) {
......
......@@ -31,7 +31,7 @@
);
}
if (settings.file && settings.file.elements) {
if (settings.file?.elements) {
elements = settings.file.elements;
Object.keys(elements).forEach(initFileValidation);
}
......@@ -47,7 +47,7 @@
);
}
if (trigger === 'unload' && settings.file && settings.file.elements) {
if (trigger === 'unload' && settings.file?.elements) {
elements = settings.file.elements;
Object.keys(elements).forEach(removeFileValidation);
}
......
......@@ -16,7 +16,7 @@
// Use the data embedded in the page, if available.
let embeddedLastReadTimestamps = false;
if (drupalSettings.history && drupalSettings.history.lastReadTimestamps) {
if (drupalSettings.history?.lastReadTimestamps) {
embeddedLastReadTimestamps = drupalSettings.history.lastReadTimestamps;
}
......@@ -67,7 +67,7 @@
*/
getLastRead(nodeID) {
// Use the data embedded in the page, if available.
if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
if (embeddedLastReadTimestamps?.[nodeID]) {
return parseInt(embeddedLastReadTimestamps[nodeID], 10);
}
return parseInt(
......@@ -90,10 +90,7 @@
success(timestamp) {
// If the data is embedded in the page, don't store on the client
// side.
if (
embeddedLastReadTimestamps &&
embeddedLastReadTimestamps[nodeID]
) {
if (embeddedLastReadTimestamps?.[nodeID]) {
return;
}
......@@ -130,7 +127,7 @@
}
// Use the data embedded in the page, if available.
if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
if (embeddedLastReadTimestamps?.[nodeID]) {
return (
contentTimestamp > parseInt(embeddedLastReadTimestamps[nodeID], 10)
);
......
......@@ -12,7 +12,7 @@
// read. This still allows for Drupal behaviors (which are triggered on the
// "DOMContentReady" event) to add "new" and "updated" indicators.
window.addEventListener('load', () => {
if (drupalSettings.history && drupalSettings.history.nodesToMarkAsRead) {
if (drupalSettings.history?.nodesToMarkAsRead) {
Object.keys(drupalSettings.history.nodesToMarkAsRead).forEach(
Drupal.history.markAsRead,
);
......
......@@ -18,9 +18,9 @@
.find('.media-form-author')
.drupalSetSummary((context) => {
const nameInput = context.querySelector('.field--name-uid input');
const name = nameInput && nameInput.value;
const name = nameInput?.value;
const dateInput = context.querySelector('.field--name-created input');
const date = dateInput && dateInput.value;
const date = dateInput?.value;
if (name && date) {
return Drupal.t('By @name on @date', {
......
......@@ -18,9 +18,9 @@
$context.find('.node-form-author').drupalSetSummary((context) => {
const nameElement = context.querySelector('.field--name-uid input');
const name = nameElement && nameElement.value;
const name = nameElement?.value;
const dateElement = context.querySelector('.field--name-created input');
const date = dateElement && dateElement.value;
const date = dateElement?.value;
if (name && date) {
return Drupal.t('By @name on @date', {
......
......@@ -19,7 +19,7 @@
const pathElement = document.querySelector(
'.js-form-item-path-0-alias input',
);
const path = pathElement && pathElement.value;
const path = pathElement?.value;
return path
? Drupal.t('Alias: @alias', { '@alias': path })
: Drupal.t('No alias');
......
......@@ -14,7 +14,7 @@
*/
Drupal.behaviors.ViewsAjaxView = {};
Drupal.behaviors.ViewsAjaxView.attach = function (context, settings) {
if (settings && settings.views && settings.views.ajaxViews) {
if (settings?.views?.ajaxViews) {
const {
views: { ajaxViews },
} = settings;
......@@ -25,7 +25,7 @@
};
Drupal.behaviors.ViewsAjaxView.detach = (context, settings, trigger) => {
if (trigger === 'unload') {
if (settings && settings.views && settings.views.ajaxViews) {
if (settings?.views?.ajaxViews) {
const {
views: { ajaxViews },
} = settings;
......
......@@ -73,10 +73,7 @@
// To prevent this potential miscalculation, the spacer is momentarily
// removed when blur occurs on rows preceding it. The spacer is
// reintroduced immediately after the next item receives focus.
if (
nextSibling &&
nextSibling.getAttribute('data-drupal-table-row-spacer')
) {
if (nextSibling?.getAttribute('data-drupal-table-row-spacer')) {
nextSibling.parentNode.removeChild(nextSibling);
}
}
......
......@@ -14,10 +14,7 @@
comments
.querySelectorAll('[data-drupal-selector="comment"]')
.forEach((comment) => {
if (
comment.nextElementSibling != null &&
comment.nextElementSibling.matches('.indented')
) {
if (comment.nextElementSibling?.matches('.indented')) {
comment.classList.add('has-children');
}
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment