Skip to content

Commit

Permalink
MDL-52724 editor_atto: Scrub atto html for invalid ol, ul, and li tags
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ericmerrill committed Apr 8, 2021
1 parent 18aafd0 commit 59704c6
Show file tree
Hide file tree
Showing 8 changed files with 919 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/behat/form_field/behat_form_editor.php
Expand Up @@ -51,6 +51,8 @@ public function set_value($value) {
$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"));
Expand Down
169 changes: 169 additions & 0 deletions 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:
"""
<li>A</li>
<li>B</li>
</ol>
<ul>
<li>C</li>
</ul></ul>
<li class="someclass ul UL">D</li>
<li>E</li>
"""
And I click on "HTML" "button"
Then the field "Description" matches multiline:
"""
<ol><li>A</li>
<li>B</li>
</ol>
<ul>
<li>C</li>
</ul>
<ul><li class="someclass ul UL">D</li>
<li>E</li></ul>
"""

@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:
"""
<div class="ol"><ol>
<li>A</li>
<li>B
</ol></div>
<ul>
<li>C
<li>D</li>
</ol>
"""
And I click on "HTML" "button"
Then the field "Description" matches multiline:
"""
<div class="ol"><ol>
<li>A</li>
<li>B
</li></ol></div>
<ul>
<li>C
</li><li>D</li></ul>
"""

@javascript
Scenario: Missing beginning OL tag, empty LI close 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:
"""
<p>Before</p>
<li>A</li></li>
<li>B</li>
</ol>
<p>After</p>
<ul data-info="UL ul OL ol">
<ul>
C</li>
<li>D</li>
<li>E
</ul>
</ul><ul>
<p>After 2</p>
"""
And I click on "HTML" "button"
Then the field "Description" matches multiline:
"""
<p>Before</p>
<ol><li>A</li>
<li>B</li>
</ol>
<p>After</p>
<ul data-info="UL ul OL ol">
<ul><li>
C</li>
<li>D</li>
<li>E
</li></ul>
</ul>
<p>After 2</p>
"""

@javascript
Scenario: Random close LI tag, extra LI open tag, missing OL 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:
"""
<p>Before</p></li><ul>
<ul>
<li>A</li>
B</li>
<li>C</li>
<ol>
<li>D</li>
<li>E
<p>After</p>
"""
And I click on "HTML" "button"
Then the field "Description" matches multiline:
"""
<p>Before</p>
<ul>
<li>A</li><li>
B</li>
<li>C</li></ul>
<ol>
<li>D</li></ol>
E
<p>After</p>
"""

@javascript
Scenario: Missing opening LI tags, 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:
"""
<li>Before</li>
<ul>
<li>A</li>
B</li>
<ol>
1</li>
</ol>
<li>C
<li>D</li>
<p>After</p>
"""
And I click on "HTML" "button"
Then the field "Description" matches multiline:
"""
<ul><li>Before</li></ul>
<ul>
<li>A</li><li>
B</li>
<ol><li>
1</li>
</ol>
<li>C
</li><li>D</li></ul>
<p>After</p>
"""

0 comments on commit 59704c6

Please sign in to comment.