3 * Defines the Drupal JavaScript API.
7 * A jQuery object, typically the return value from a `$(selector)` call.
9 * Holds an HTMLElement or a collection of HTMLElements.
11 * @typedef {object} jQuery
13 * @prop {number} length=0
14 * Number of elements contained in the jQuery object.
18 * Variable generated by Drupal that holds all translated strings from PHP.
20 * Content of this variable is automatically created by Drupal when using the
21 * Interface Translation module. It holds the translation of strings used on
24 * This variable is used to pass data from the backend to the frontend. Data
25 * contained in `drupalSettings` is used during behavior initialization.
29 * @var {object} drupalTranslations
33 * Global Drupal object.
35 * All Drupal JavaScript APIs are contained in this namespace.
41 window.Drupal = { behaviors: {}, locale: {} };
43 // JavaScript should be made compatible with libraries other than jQuery by
44 // wrapping it in an anonymous closure.
45 (function(Drupal, drupalSettings, drupalTranslations) {
47 * Helper to rethrow errors asynchronously.
49 * This way Errors bubbles up outside of the original callstack, making it
50 * easier to debug errors in the browser.
52 * @param {Error|string} error
53 * The error to be thrown.
55 Drupal.throwError = function(error) {
62 * Custom error thrown after attach/detach if one or more behaviors failed.
63 * Initializes the JavaScript behaviors for page loads and Ajax requests.
65 * @callback Drupal~behaviorAttach
67 * @param {HTMLDocument|HTMLElement} context
68 * An element to detach behaviors from.
69 * @param {?object} settings
70 * An object containing settings for the current context. It is rarely used.
72 * @see Drupal.attachBehaviors
76 * Reverts and cleans up JavaScript behavior initialization.
78 * @callback Drupal~behaviorDetach
80 * @param {HTMLDocument|HTMLElement} context
81 * An element to attach behaviors to.
82 * @param {object} settings
83 * An object containing settings for the current context.
84 * @param {string} trigger
85 * One of `'unload'`, `'move'`, or `'serialize'`.
87 * @see Drupal.detachBehaviors
91 * @typedef {object} Drupal~behavior
93 * @prop {Drupal~behaviorAttach} attach
94 * Function run on page load and after an Ajax call.
95 * @prop {Drupal~behaviorDetach} detach
96 * Function run when content is serialized or removed from the page.
100 * Holds all initialization methods.
102 * @namespace Drupal.behaviors
104 * @type {Object.<string, Drupal~behavior>}
108 * Defines a behavior to be run during attach and detach phases.
110 * Attaches all registered behaviors to a page element.
112 * Behaviors are event-triggered actions that attach to page elements,
113 * enhancing default non-JavaScript UIs. Behaviors are registered in the
114 * {@link Drupal.behaviors} object using the method 'attach' and optionally
117 * {@link Drupal.attachBehaviors} is added below to the `jQuery.ready` event
118 * and therefore runs on initial page load. Developers implementing Ajax in
119 * their solutions should also call this function after new page content has
120 * been loaded, feeding in an element to be processed, in order to attach all
121 * behaviors to the new content.
123 * Behaviors should use `var elements =
124 * $(context).find(selector).once('behavior-name');` to ensure the behavior is
125 * attached only once to a given element. (Doing so enables the reprocessing
126 * of given elements, which may be needed on occasion despite the ability to
127 * limit behavior attachment to a particular element.)
130 * Drupal.behaviors.behaviorName = {
131 * attach: function (context, settings) {
134 * detach: function (context, settings, trigger) {
139 * @param {HTMLDocument|HTMLElement} [context=document]
140 * An element to attach behaviors to.
141 * @param {object} [settings=drupalSettings]
142 * An object containing settings for the current context. If none is given,
143 * the global {@link drupalSettings} object is used.
145 * @see Drupal~behaviorAttach
146 * @see Drupal.detachBehaviors
148 * @throws {Drupal~DrupalBehaviorError}
150 Drupal.attachBehaviors = function(context, settings) {
151 context = context || document;
152 settings = settings || drupalSettings;
153 const behaviors = Drupal.behaviors;
154 // Execute all of them.
155 Object.keys(behaviors || {}).forEach(i => {
156 if (typeof behaviors[i].attach === 'function') {
157 // Don't stop the execution of behaviors in case of an error.
159 behaviors[i].attach(context, settings);
161 Drupal.throwError(e);
168 * Detaches registered behaviors from a page element.
170 * Developers implementing Ajax in their solutions should call this function
171 * before page content is about to be removed, feeding in an element to be
172 * processed, in order to allow special behaviors to detach from the content.
174 * Such implementations should use `.findOnce()` and `.removeOnce()` to find
175 * elements with their corresponding `Drupal.behaviors.behaviorName.attach`
176 * implementation, i.e. `.removeOnce('behaviorName')`, to ensure the behavior
177 * is detached only from previously processed elements.
179 * @param {HTMLDocument|HTMLElement} [context=document]
180 * An element to detach behaviors from.
181 * @param {object} [settings=drupalSettings]
182 * An object containing settings for the current context. If none given,
183 * the global {@link drupalSettings} object is used.
184 * @param {string} [trigger='unload']
185 * A string containing what's causing the behaviors to be detached. The
186 * possible triggers are:
187 * - `'unload'`: The context element is being removed from the DOM.
188 * - `'move'`: The element is about to be moved within the DOM (for example,
189 * during a tabledrag row swap). After the move is completed,
190 * {@link Drupal.attachBehaviors} is called, so that the behavior can undo
191 * whatever it did in response to the move. Many behaviors won't need to
192 * do anything simply in response to the element being moved, but because
193 * IFRAME elements reload their "src" when being moved within the DOM,
194 * behaviors bound to IFRAME elements (like WYSIWYG editors) may need to
196 * - `'serialize'`: When an Ajax form is submitted, this is called with the
197 * form as the context. This provides every behavior within the form an
198 * opportunity to ensure that the field elements have correct content
199 * in them before the form is serialized. The canonical use-case is so
200 * that WYSIWYG editors can update the hidden textarea to which they are
203 * @throws {Drupal~DrupalBehaviorError}
205 * @see Drupal~behaviorDetach
206 * @see Drupal.attachBehaviors
208 Drupal.detachBehaviors = function(context, settings, trigger) {
209 context = context || document;
210 settings = settings || drupalSettings;
211 trigger = trigger || 'unload';
212 const behaviors = Drupal.behaviors;
213 // Execute all of them.
214 Object.keys(behaviors || {}).forEach(i => {
215 if (typeof behaviors[i].detach === 'function') {
216 // Don't stop the execution of behaviors in case of an error.
218 behaviors[i].detach(context, settings, trigger);
220 Drupal.throwError(e);
227 * Encodes special characters in a plain-text string for display as HTML.
229 * @param {string} str
230 * The string to be encoded.
233 * The encoded string.
235 * @ingroup sanitization
237 Drupal.checkPlain = function(str) {
240 .replace(/&/g, '&')
241 .replace(/</g, '<')
242 .replace(/>/g, '>')
243 .replace(/"/g, '"')
244 .replace(/'/g, ''');
249 * Replaces placeholders with sanitized values in a string.
251 * @param {string} str
252 * A string with placeholders.
253 * @param {object} args
254 * An object of replacements pairs to make. Incidences of any key in this
255 * array are replaced with the corresponding value. Based on the first
256 * character of the key, the value is escaped and/or themed:
257 * - `'!variable'`: inserted as is.
258 * - `'@variable'`: escape plain text to HTML ({@link Drupal.checkPlain}).
259 * - `'%variable'`: escape text and theme as a placeholder for user-
260 * submitted content ({@link Drupal.checkPlain} +
261 * `{@link Drupal.theme}('placeholder')`).
264 * The formatted string.
268 Drupal.formatString = function(str, args) {
270 const processedArgs = {};
271 // Transform arguments before inserting them.
272 Object.keys(args || {}).forEach(key => {
273 switch (key.charAt(0)) {
276 processedArgs[key] = Drupal.checkPlain(args[key]);
281 processedArgs[key] = args[key];
284 // Escaped and placeholder.
286 processedArgs[key] = Drupal.theme('placeholder', args[key]);
291 return Drupal.stringReplace(str, processedArgs, null);
295 * Replaces substring.
297 * The longest keys will be tried first. Once a substring has been replaced,
298 * its new value will not be searched again.
300 * @param {string} str
301 * A string with placeholders.
302 * @param {object} args
304 * @param {Array|null} keys
305 * Array of keys from `args`. Internal use only.
308 * The replaced string.
310 Drupal.stringReplace = function(str, args, keys) {
311 if (str.length === 0) {
315 // If the array of keys is not passed then collect the keys from the args.
316 if (!Array.isArray(keys)) {
317 keys = Object.keys(args || {});
319 // Order the keys by the character length. The shortest one is the first.
320 keys.sort((a, b) => a.length - b.length);
323 if (keys.length === 0) {
327 // Take next longest one from the end.
328 const key = keys.pop();
329 const fragments = str.split(key);
332 for (let i = 0; i < fragments.length; i++) {
333 // Process each fragment with a copy of remaining keys.
334 fragments[i] = Drupal.stringReplace(fragments[i], args, keys.slice(0));
338 return fragments.join(args[key]);
342 * Translates strings to the page language, or a given language.
344 * See the documentation of the server-side t() function for further details.
346 * @param {string} str
347 * A string containing the English text to translate.
348 * @param {Object.<string, string>} [args]
349 * An object of replacements pairs to make after translation. Incidences
350 * of any key in this array are replaced with the corresponding value.
351 * See {@link Drupal.formatString}.
352 * @param {object} [options]
353 * Additional options for translation.
354 * @param {string} [options.context='']
355 * The context the source string belongs to.
358 * The formatted string.
359 * The translated string.
361 Drupal.t = function(str, args, options) {
362 options = options || {};
363 options.context = options.context || '';
365 // Fetch the localized version of the string.
367 typeof drupalTranslations !== 'undefined' &&
368 drupalTranslations.strings &&
369 drupalTranslations.strings[options.context] &&
370 drupalTranslations.strings[options.context][str]
372 str = drupalTranslations.strings[options.context][str];
376 str = Drupal.formatString(str, args);
382 * Returns the URL to a Drupal page.
384 * @param {string} path
385 * Drupal path to transform to URL.
390 Drupal.url = function(path) {
391 return drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix + path;
395 * Returns the passed in URL as an absolute URL.
397 * @param {string} url
398 * The URL string to be normalized to an absolute URL.
401 * The normalized, absolute URL.
403 * @see https://github.com/angular/angular.js/blob/v1.4.4/src/ng/urlUtils.js
404 * @see https://grack.com/blog/2009/11/17/absolutizing-url-in-javascript
405 * @see https://github.com/jquery/jquery-ui/blob/1.11.4/ui/tabs.js#L53
407 Drupal.url.toAbsolute = function(url) {
408 const urlParsingNode = document.createElement('a');
410 // Decode the URL first; this is required by IE <= 6. Decoding non-UTF-8
411 // strings may throw an exception.
413 url = decodeURIComponent(url);
418 urlParsingNode.setAttribute('href', url);
420 // IE <= 7 normalizes the URL when assigned to the anchor node similar to
421 // the other browsers.
422 return urlParsingNode.cloneNode(false).href;
426 * Returns true if the URL is within Drupal's base path.
428 * @param {string} url
429 * The URL string to be tested.
434 * @see https://github.com/jquery/jquery-ui/blob/1.11.4/ui/tabs.js#L58
436 Drupal.url.isLocal = function(url) {
437 // Always use browser-derived absolute URLs in the comparison, to avoid
438 // attempts to break out of the base path using directory traversal.
439 let absoluteUrl = Drupal.url.toAbsolute(url);
440 let { protocol } = window.location;
442 // Consider URLs that match this site's base URL but use HTTPS instead of HTTP
444 if (protocol === 'http:' && absoluteUrl.indexOf('https:') === 0) {
447 let baseUrl = `${protocol}//${
449 }${drupalSettings.path.baseUrl.slice(0, -1)}`;
451 // Decoding non-UTF-8 strings may throw an exception.
453 absoluteUrl = decodeURIComponent(absoluteUrl);
458 baseUrl = decodeURIComponent(baseUrl);
463 // The given URL matches the site's base URL, or has a path under the site's
465 return absoluteUrl === baseUrl || absoluteUrl.indexOf(`${baseUrl}/`) === 0;
469 * Formats a string containing a count of items.
471 * This function ensures that the string is pluralized correctly. Since
472 * {@link Drupal.t} is called by this function, make sure not to pass
473 * already-localized strings to it.
475 * See the documentation of the server-side
476 * \Drupal\Core\StringTranslation\TranslationInterface::formatPlural()
477 * function for more details.
479 * @param {number} count
480 * The item count to display.
481 * @param {string} singular
482 * The string for the singular case. Please make sure it is clear this is
483 * singular, to ease translation (e.g. use "1 new comment" instead of "1
484 * new"). Do not use @count in the singular string.
485 * @param {string} plural
486 * The string for the plural case. Please make sure it is clear this is
487 * plural, to ease translation. Use @count in place of the item count, as in
488 * "@count new comments".
489 * @param {object} [args]
490 * An object of replacements pairs to make after translation. Incidences
491 * of any key in this array are replaced with the corresponding value.
492 * See {@link Drupal.formatString}.
493 * Note that you do not need to include @count in this array.
494 * This replacement is done automatically for the plural case.
495 * @param {object} [options]
496 * The options to pass to the {@link Drupal.t} function.
499 * A translated string.
501 Drupal.formatPlural = function(count, singular, plural, args, options) {
503 args['@count'] = count;
505 const pluralDelimiter = drupalSettings.pluralDelimiter;
506 const translations = Drupal.t(
507 singular + pluralDelimiter + plural,
510 ).split(pluralDelimiter);
513 // Determine the index of the plural form.
515 typeof drupalTranslations !== 'undefined' &&
516 drupalTranslations.pluralFormula
519 count in drupalTranslations.pluralFormula
520 ? drupalTranslations.pluralFormula[count]
521 : drupalTranslations.pluralFormula.default;
522 } else if (args['@count'] !== 1) {
526 return translations[index];
530 * Encodes a Drupal path for use in a URL.
532 * For aesthetic reasons slashes are not escaped.
534 * @param {string} item
540 Drupal.encodePath = function(item) {
541 return window.encodeURIComponent(item).replace(/%2F/g, '/');
545 * Generates the themed representation of a Drupal object.
547 * All requests for themed output must go through this function. It examines
548 * the request and routes it to the appropriate theme function. If the current
549 * theme does not provide an override function, the generic theme function is
553 * <caption>To retrieve the HTML for text that should be emphasized and
554 * displayed as a placeholder inside a sentence.</caption>
555 * Drupal.theme('placeholder', text);
559 * @param {function} func
560 * The name of the theme function to call.
562 * Additional arguments to pass along to the theme function.
564 * @return {string|object|HTMLElement|jQuery}
565 * Any data the theme function returns. This could be a plain HTML string,
566 * but also a complex object.
568 Drupal.theme = function(func, ...args) {
569 if (func in Drupal.theme) {
570 return Drupal.theme[func](...args);
575 * Formats text for emphasized display in a placeholder inside a sentence.
577 * @param {string} str
578 * The text to format (plain-text).
581 * The formatted text (html).
583 Drupal.theme.placeholder = function(str) {
584 return `<em class="placeholder">${Drupal.checkPlain(str)}</em>`;
586 })(Drupal, window.drupalSettings, window.drupalTranslations);