From a3fa31fa21b77250e32bb4c9561f9a95970c51c6 Mon Sep 17 00:00:00 2001 From: Eric Merrill Date: Thu, 8 Apr 2021 15:20:24 -0400 Subject: [PATCH] MDL-52724 editor_atto: Scrub atto html for invalid ol, ul, and li tags Try to correct broken ul/ol/li tags, as they have an outsized impact on course layout. Uses basic regex and loops to track open and closed tags. Also adds a deep clean option to the HTML cleaner, that runs less frequent, more intensive cleanings. This is because normally _cleanHTML gets called after each keystroke, which could cause problems with large content on weak systems. Behat changes are a fix for setting multiline strings in Atto, and creating a multiline match step. --- lib/behat/form_field/behat_form_editor.php | 2 + lib/editor/atto/tests/behat/clean.feature | 169 ++++++++++++ .../moodle-editor_atto-editor-debug.js | 251 +++++++++++++++++- .../moodle-editor_atto-editor-min.js | 10 +- .../moodle-editor_atto-editor.js | 240 ++++++++++++++++- lib/editor/atto/yui/src/editor/js/clean.js | 249 ++++++++++++++++- lib/editor/atto/yui/src/editor/js/textarea.js | 2 +- lib/tests/behat/behat_forms.php | 13 + 8 files changed, 919 insertions(+), 17 deletions(-) create mode 100644 lib/editor/atto/tests/behat/clean.feature diff --git a/lib/behat/form_field/behat_form_editor.php b/lib/behat/form_field/behat_form_editor.php index 3d5df09af66..96e8c0a92ae 100644 --- a/lib/behat/form_field/behat_form_editor.php +++ b/lib/behat/form_field/behat_form_editor.php @@ -51,6 +51,8 @@ class behat_form_editor extends behat_form_textarea { $editorid = $this->field->getAttribute('id'); if ($this->running_javascript()) { $value = addslashes($value); + // This will be transported in JSON, which doesn't allow newlines in strings, so we must escape them. + $value = str_replace("\n", "\\n", $value); $js = ' (function() { var editor = Y.one(document.getElementById("'.$editorid.'editable")); diff --git a/lib/editor/atto/tests/behat/clean.feature b/lib/editor/atto/tests/behat/clean.feature new file mode 100644 index 00000000000..aea3fee77bd --- /dev/null +++ b/lib/editor/atto/tests/behat/clean.feature @@ -0,0 +1,169 @@ +@editor @editor_atto @atto @editor_moodleform +Feature: Atto HTML cleanup. + In order to test html cleaning functionality, I write in a HTML atto text field. + + @javascript + Scenario: Extra UL close and orphan LI items + Given I log in as "admin" + When I open my profile in edit mode + And I click on "Show more buttons" "button" + And I click on "HTML" "button" + And I set the field "Description" to multiline: + """ +
  • A
  • +
  • B
  • + + +
  • D
  • +
  • E
  • + """ + And I click on "HTML" "button" + Then the field "Description" matches multiline: + """ +
    1. A
    2. +
    3. B
    4. +
    + + + """ + + @javascript + Scenario: Missing LI close tags, extra closing OL, missing closing UL tag + Given I log in as "admin" + When I open my profile in edit mode + And I click on "Show more buttons" "button" + And I click on "HTML" "button" + And I set the field "Description" to multiline: + """ +
      +
    1. A
    2. +
    3. B +
    +