Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Fix EZP-26135: Line breaks added in richtext editor when editing cont…
Browse files Browse the repository at this point in the history
…ent twice
  • Loading branch information
dpobel committed Oct 7, 2016
1 parent 793d847 commit 061afc8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
38 changes: 32 additions & 6 deletions Resources/public/js/views/fields/ez-richtext-editview.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,37 @@ YUI.add('ez-richtext-editview', function (Y) {
* @return {DocumentFragment}
*/
_getHTMLDocumentFragment: function () {
var fragment = Y.config.doc.createDocumentFragment(),
root = Y.config.doc.createElement('div'),
var fragment = Y.config.doc.implementation.createHTMLDocument().createDocumentFragment(),
root = fragment.ownerDocument.createElement('div'),
doc = (new DOMParser()).parseFromString(this.get('field').fieldValue.xhtml5edit, "text/xml"),
i;
importChildNodes = function (parent, element, skipElement) {
// recursively import element and its descendants under
// parent this allows to correctly transform an `xhtml5edit`
// document (it's an XML document) to an HTML document a
// browser can understand.
var i, newElement;

if ( skipElement ) {
newElement = parent;
} else {
if ( element.nodeType === Node.ELEMENT_NODE ) {
newElement = parent.ownerDocument.createElement(element.localName);
for (i = 0; i != element.attributes.length; i++) {
importChildNodes(newElement, element.attributes[i], false);
}
parent.appendChild(newElement);
} else if ( element.nodeType === Node.TEXT_NODE ) {
parent.appendChild(parent.ownerDocument.createTextNode(element.nodeValue));
} else if ( element.nodeType === Node.ATTRIBUTE_NODE ) {
parent.setAttribute(element.localName, element.value);
} else {
return;
}
}
for (i = 0; i != element.childNodes.length; i++) {
importChildNodes(newElement, element.childNodes[i], false);
}
};

if ( !doc || !doc.documentElement || doc.querySelector("parsererror") ) {
console.warn(
Expand All @@ -279,9 +306,8 @@ YUI.add('ez-richtext-editview', function (Y) {
}

fragment.appendChild(root);
for (i = 0; i != doc.documentElement.childNodes.length; i++) {
root.appendChild(doc.documentElement.childNodes.item(i).cloneNode(true));
}

importChildNodes(root, doc.documentElement, true);
return fragment;
},

Expand Down
34 changes: 32 additions & 2 deletions Tests/js/views/fields/assets/ez-richtext-editview-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ YUI.add('ez-richtext-editview-tests', function (Y) {
editorTest, rerenderEditorTest, focusModeTest, editorFocusHandlingTest, appendToolbarConfigTest,
eventForwardTest, defaultEditorProcessorsTest, defaultProcessorsTest,
VALID_XHTML, INVALID_XHTML, RESULT_XHTML, EMPTY_XHTML, RESULT_EMPTY_XHTML,
VALID_XHTML_WITH_EMBED,
VALID_XHTML_WITH_EMBED, VALID_XHTML_BR, RESULT_BR_XHTML, VALID_XHTML_COMPLEX,
RESULT_COMPLEX_XHTML,
Assert = Y.Assert, Mock = Y.Mock,
destroyTearDown = function () {
// setTimeout is needed otherwise, the editor is destroyed while
Expand All @@ -27,6 +28,17 @@ YUI.add('ez-richtext-editview-tests', function (Y) {
VALID_XHTML += '<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">';
VALID_XHTML += '<p>I\'m not empty</p></section>';

VALID_XHTML_BR = '<?xml version="1.0" encoding="UTF-8"?>';
VALID_XHTML_BR += '<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">';
VALID_XHTML_BR += '<p>Line<br/>Breaks<br/>Line<br/>Breaks</p></section>';

VALID_XHTML_COMPLEX = '<?xml version="1.0" encoding="UTF-8"?>';
VALID_XHTML_COMPLEX += '<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">';
VALID_XHTML_COMPLEX += '<ul><li>Item 1</li><li>Item 2</li></ul>';
VALID_XHTML_COMPLEX += '<p>With<br/>breaks</p>';
VALID_XHTML_COMPLEX += '<div data-ezelement="ezembed" view="embed" data-href="ezcontent://52"/>';
VALID_XHTML_COMPLEX += '</section>';

VALID_XHTML_WITH_EMBED = '<?xml version="1.0" encoding="UTF-8"?>';
VALID_XHTML_WITH_EMBED += '<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">';
VALID_XHTML_WITH_EMBED += '<div data-ezelement="ezembed"></div></section>';
Expand All @@ -36,6 +48,12 @@ YUI.add('ez-richtext-editview-tests', function (Y) {

RESULT_EMPTY_XHTML = '<p></p>';

RESULT_BR_XHTML = '<p>Line<br>Breaks<br>Line<br>Breaks</p>';

RESULT_COMPLEX_XHTML = '<ul><li>Item 1</li><li>Item 2</li></ul>';
RESULT_COMPLEX_XHTML += '<p>With<br>breaks</p>';
RESULT_COMPLEX_XHTML += '<div data-ezelement="ezembed" view="embed" data-href="ezcontent://52"></div>';

RESULT_XHTML = '<p>I\'m not empty</p>';

CKEDITOR.plugins.add('ezaddcontent', {});
Expand Down Expand Up @@ -126,7 +144,10 @@ YUI.add('ez-richtext-editview-tests', function (Y) {
"The editable class should be available in the template"
);
Assert.areSame(expectRequired, variables.isRequired);
Assert.areSame(expectedXhtml, variables.xhtml);
Assert.areSame(
expectedXhtml, variables.xhtml,
"The HTML code should be available in the template"
);

return origTpl.call(this, variables);
};
Expand All @@ -148,6 +169,15 @@ YUI.add('ez-richtext-editview-tests', function (Y) {
"Should add a paragraph in empty document": function () {
this._testAvailableVariables(false, false, EMPTY_XHTML, RESULT_EMPTY_XHTML);
},

"Should transform the xhtml5edit code to HTML": function () {
this._testAvailableVariables(false, false, VALID_XHTML_COMPLEX, RESULT_COMPLEX_XHTML);
},

// regression test for EZP-26135
"Should not double the br tag": function () {
this._testAvailableVariables(false, false, VALID_XHTML_BR, RESULT_BR_XHTML);
},
});

validateTest = new Y.Test.Case({
Expand Down

0 comments on commit 061afc8

Please # to comment.