Commit 6740bfd0 authored by Jeff Hipp's avatar Jeff Hipp Committed by Clayton Dewey
Browse files

Issue #3230544 by hipp2bsquare, cedewey, boehmrya: Apply safe truncate HTML...

Issue #3230544 by hipp2bsquare, cedewey, boehmrya: Apply safe truncate HTML behavior by default on rich text fields
parent 8c2343d6
Loading
Loading
Loading
Loading
+0 −2
Changes for config/schema/maxlength.schema.yml: 0 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -12,5 +12,3 @@ field.widget.third_party.maxlength:
      type: label
    maxlength_js_enforce:
      type: boolean
    maxlength_js_truncate_html:
      type: boolean
+0 −32
Changes for js/maxlength.js: 0 added lines, 32 removed lines.
Original line number Diff line number Diff line
@@ -19,9 +19,6 @@
        if ($this.hasClass('maxlength_js_enforce')) {
          options['enforce'] = true;
        }
        if ($this.hasClass('maxlength_js_truncate_html')) {
          options['truncateHtml'] = true;
        }
        $this.charCount(options);
      });
    },
@@ -55,13 +52,8 @@
    var limit = parseInt(obj.attr('data-maxlength'));

    if (count == undefined) {
      if (options.truncateHtml) {
      count = ml.strip_tags(obj.val()).length;
    }
      else {
        count = ml.twochar_lineending(obj.val()).length;
      }
    }

    var available = limit - count;

@@ -78,26 +70,14 @@
      if (options.enforce) {
        if (wysiwyg != undefined) {
          if (typeof ml[getter] == 'function' && typeof ml[setter] == 'function') {
              if (options.truncateHtml) {
            var new_html = ml.truncate_html(ml[getter](wysiwyg), limit);
            ml[setter](wysiwyg, new_html);
            count = ml.strip_tags(new_html).length;
              } else {
                var new_html = ml[getter](wysiwyg).substr(0, limit);
                ml[setter](wysiwyg, new_html);
                count = new_html.length;
              }
          }
        } else {
          if (options.truncateHtml) {
          obj.val(ml.truncate_html(obj.val(), limit));
          // Re calculate text length
          count = ml.strip_tags(obj.val()).length;
          } else {
            obj.val(obj.val().substr(0, limit));
            // Re calculate text length
            count = ml.twochar_lineending(obj.val()).length;
          }
        }
      }
      available = limit - count;
@@ -251,7 +231,6 @@
      counterText: Drupal.t('Content limited to @limit characters, remaining: <strong>@remaining</strong>'),
      action: 'attach',
      enforce: false,
      truncateHtml: false
    };

    var options = $.extend(defaults, options);
@@ -310,12 +289,6 @@
          } else {
            ml.options[e.editor.element.getId()].enforce = false;
          }
          // Check if we should strip the tags when counting.
          if (editor.hasClass('maxlength_js_truncate_html')) {
            ml.options[e.editor.element.getId()].truncateHtml = true;
          } else {
            ml.options[e.editor.element.getId()].truncateHtml = false;
          }
          // Add the events on the editor.
          e.editor.on('key', function(e) {
            setTimeout(function(){ml.ckeditorChange(e)}, 100);
@@ -334,12 +307,7 @@
  ml.ckeditorChange = function(e) {
    // Clone to avoid changing defaults
    var options = $.extend({}, ml.options[e.editor.element.getId()]);
    if (options.truncateHtml){
    ml.calculate($('#' + e.editor.element.getId()), options, ml.strip_tags(ml.ckeditorGetData(e)).length, e, 'ckeditorGetData', 'ckeditorSetData');
    }
    else {
      ml.calculate($('#' + e.editor.element.getId()), options, ml.twochar_lineending(ml.ckeditorGetData(e)).length, e, 'ckeditorGetData', 'ckeditorSetData');
    }
  };

  // Gets the data from the ckeditor.
+1 −14
Changes for maxlength.module: 1 added line, 14 removed lines.
Original line number Diff line number Diff line
@@ -108,12 +108,6 @@ function maxlength_field_widget_third_party_settings_form(WidgetInterface $plugi
          If unchecked, a user sees a negative character count when typing past the limit.'),
        '#default_value' => $plugin->getThirdPartySetting('maxlength', 'maxlength_js_enforce'),
      ];
      $element['maxlength_js_truncate_html'] = [
        '#type' => 'checkbox',
        '#title' => t('Safe truncate html'),
        '#description' => t('Use this option if the input field may contain html and you want to exclude the html tag characters from the character count. This will also overwrite the maxlength validation from core, so that it will strip the tags before checking the length.'),
        '#default_value' => $plugin->getThirdPartySetting('maxlength', 'maxlength_js_truncate_html'),
      ];
    }
    return $element;
  }
@@ -134,10 +128,7 @@ function maxlength_field_widget_settings_summary_alter(&$summary, $context) {
      $summary[] = t('Maximum summary length: @count', ['@count' => $context['widget']->getThirdPartySetting('maxlength', 'maxlength_js_summary')]);
    }
    if (!empty($allowed_settings['truncate_setting'])) {
      if ($context['widget']->getThirdPartySetting('maxlength', 'maxlength_js_truncate_html')) {
        $summary[] = t('Maximum length - Safe truncate html');
      }
      elseif ($context['widget']->getThirdPartySetting('maxlength', 'maxlength_js_enforce')) {
      if ($context['widget']->getThirdPartySetting('maxlength', 'maxlength_js_enforce')) {
        $summary[] = t('Maximum length - Force truncate');
      }
    }
@@ -189,9 +180,5 @@ function maxlength_field_widget_form_alter(&$element, FormStateInterface $form_s
      $element['value']['#attributes']['#maxlength_js_enforce'] = TRUE;
      $element['#attributes']['#maxlength_js_enforce'] = TRUE;
    }
    if (isset($thirdPartySettings['maxlength']['maxlength_js_truncate_html']) && $thirdPartySettings['maxlength']['maxlength_js_truncate_html']) {
      $element['value']['#attributes']['#maxlength_js_truncate_html'] = TRUE;
      $element['#attributes']['#maxlength_js_truncate_html'] = TRUE;
    }
  }
}
+0 −3
Changes for src/MaxLengthCallbacks.php: 0 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -55,9 +55,6 @@ class MaxLengthCallbacks implements TrustedCallbackInterface {
    if (isset($element['#attributes']['#maxlength_js_enforce']) && $element['#attributes']['#maxlength_js_enforce']) {
      $element['#attributes']['class'][] = 'maxlength_js_enforce';
    }
    if (isset($element['#attributes']['#maxlength_js_truncate_html']) && $element['#attributes']['#maxlength_js_truncate_html']) {
      $element['#attributes']['class'][] = 'maxlength_js_truncate_html';
    }
    return $element;
  }