Unverified Commit a52e9376 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3101922 by bnjmnm, nod_, lauriii: Find replacement for Modernizr...

Issue #3101922 by bnjmnm, nod_, lauriii: Find replacement for Modernizr touchevent test and deprecate

(cherry picked from commit 3070c83b)
parent a7e1d105
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -713,6 +713,7 @@ drupal.tabledrag:
    - core/drupalSettings
    - core/once
    - core/jquery.once.bc
    - core/drupal.touchevents-test

drupal.tableheader:
  version: VERSION
@@ -758,6 +759,17 @@ drupal.timezone:
    - core/jquery.once.bc
    - core/drupal

drupal.touchevents-test:
  header: true
  version: VERSION
  js:
    # Set weight to -22 so it loads before the Modernizr test it is
    # replacing, located in modernizr-additional-tests.js. If the deprecated
    # Modernizr test sees either of the classes this test adds to <body>, the
    # test will not be added to Modernizr and deprecation warnings will not be
    # triggered.
    misc/touchevents-test.js: { weight: -22 }

drupal.vertical-tabs:
  version: VERSION
  js:
@@ -1219,6 +1231,7 @@ drupal.dialog.off_canvas:
    - core/drupal.announce
    - core/drupal.dialog
    - core/drupal.dialog.ajax
    - core/drupal.touchevents-test

js-cookie:
  remote: https://github.com/js-cookie/js-cookie
+79 −2
Original line number Diff line number Diff line
@@ -3,6 +3,81 @@
 * Provides additional Modernizr tests.
 */
((Modernizr) => {
  /**
   * Triggers deprecation error.
   *
   * Deprecation errors are only triggered if deprecation errors haven't
   * been suppressed.
   *
   * For performance concerns this method is inlined here to avoid adding a
   * dependency to core/drupal that would force drupal.js to be loaded in the
   * header like this script is.
   *
   * @param {Object} deprecation
   *   The deprecation options.
   * @param {string} deprecation.message
   *   The deprecation message.
   *
   * @see https://www.drupal.org/core/deprecation#javascript
   */
  const _deprecationErrorModernizrCopy = ({ message }) => {
    if (typeof console !== 'undefined' && console.warn) {
      console.warn(`[Deprecation] ${message}`);
    }
  };

  /**
   * Triggers deprecation error when object property is being used.
   *
   * @param {Object} deprecation
   *   The deprecation options.
   * @param {Object} deprecation.target
   *   The targeted object.
   * @param {string} deprecation.deprecatedProperty
   *   A key of the deprecated property.
   * @param {string} deprecation.message
   *   The deprecation message.
   *
   * @return {Object}
   *
   * @see https://www.drupal.org/core/deprecation#javascript
   */
  const _deprecatedPropertyModernizrCopy = ({
    target,
    deprecatedProperty,
    message,
  }) => {
    // Proxy and Reflect are not supported by all browsers. Unsupported browsers
    // are ignored since this is a development feature.
    if (!Proxy || !Reflect) {
      return target;
    }

    return new Proxy(target, {
      // eslint-disable-next-line no-shadow
      get: (target, key, ...rest) => {
        if (key === deprecatedProperty) {
          _deprecationErrorModernizrCopy({ message });
        }
        return Reflect.get(target, key, ...rest);
      },
    });
  };

  window.Modernizr = _deprecatedPropertyModernizrCopy({
    target: Modernizr,
    deprecatedProperty: 'touchevents',
    message:
      'The touchevents property of Modernizr has been deprecated in drupal:9.4.0 and is removed from drupal:10.0.0. There will be no replacement for this feature. See https://www.drupal.org/node/3277381.',
  });

  if (
    document.documentElement.classList.contains('touchevents') ||
    document.documentElement.classList.contains('no-touchevents')
  ) {
    return;
  }

  // This is a copy of Modernizr's touchevents test from version 3.3.1. Drupal
  // core has updated Modernizr to a version newer than 3.3.1, but this newer
  // version does not include the touchevents test in its build. Modernizr's
@@ -13,10 +88,12 @@
  // changes are refactoring the code to meet Drupal's JavaScript coding
  // standards and calling prefixes and testStyles() via the Modernizr object
  // as they are not in scope when adding a test via Modernizr.addTest();
  // @todo find alternative to Modernizr's deprecated touchevent test in
  //   http://drupal.org/node/3101922
  // @see https://github.com/Modernizr/Modernizr/blob/v3.3.1/feature-detects/touchevents.js
  Modernizr.addTest('touchevents', () => {
    _deprecationErrorModernizrCopy({
      message:
        'The Modernizr touch events test is deprecated in Drupal 9.4.0 and will be removed in Drupal 10.0.0. See https://www.drupal.org/node/3277381 for information on its replacement and how it should be used.',
    });
    let bool;

    if (
+48 −0
Original line number Diff line number Diff line
@@ -6,7 +6,55 @@
**/

(function (Modernizr) {
  var _deprecationErrorModernizrCopy = function _deprecationErrorModernizrCopy(_ref) {
    var message = _ref.message;

    if (typeof console !== 'undefined' && console.warn) {
      console.warn("[Deprecation] ".concat(message));
    }
  };

  var _deprecatedPropertyModernizrCopy = function _deprecatedPropertyModernizrCopy(_ref2) {
    var target = _ref2.target,
        deprecatedProperty = _ref2.deprecatedProperty,
        message = _ref2.message;

    if (!Proxy || !Reflect) {
      return target;
    }

    return new Proxy(target, {
      get: function get(target, key) {
        if (key === deprecatedProperty) {
          _deprecationErrorModernizrCopy({
            message: message
          });
        }

        for (var _len = arguments.length, rest = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
          rest[_key - 2] = arguments[_key];
        }

        return Reflect.get.apply(Reflect, [target, key].concat(rest));
      }
    });
  };

  window.Modernizr = _deprecatedPropertyModernizrCopy({
    target: Modernizr,
    deprecatedProperty: 'touchevents',
    message: 'The touchevents property of Modernizr has been deprecated in drupal:9.4.0 and is removed from drupal:10.0.0. There will be no replacement for this feature. See https://www.drupal.org/node/3277381.'
  });

  if (document.documentElement.classList.contains('touchevents') || document.documentElement.classList.contains('no-touchevents')) {
    return;
  }

  Modernizr.addTest('touchevents', function () {
    _deprecationErrorModernizrCopy({
      message: 'The Modernizr touch events test is deprecated in Drupal 9.4.0 and will be removed in Drupal 10.0.0. See https://www.drupal.org/node/3277381 for information on its replacement and how it should be used.'
    });

    var bool;

    if ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch) {
+11 −0
Original line number Diff line number Diff line
/**
 * @file
 * A replacement for Modernizr touch events detection.
 */

document.documentElement.classList.add(
  'ontouchstart' in window ||
    (window.DocumentTouch && document instanceof window.DocumentTouch)
    ? 'touchevents'
    : 'no-touchevents',
);
+8 −0
Original line number Diff line number Diff line
/**
* DO NOT EDIT THIS FILE.
* See the following change record for more information,
* https://www.drupal.org/node/2815083
* @preserve
**/

document.documentElement.classList.add('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch ? 'touchevents' : 'no-touchevents');
 No newline at end of file
Loading