Skip to content

Commit

Permalink
Merge branch 'hotfix-1.1.x' into hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
wigentics committed Feb 18, 2015
2 parents 164f2df + 245a70a commit a93d5c8
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 106 deletions.
1 change: 1 addition & 0 deletions build/changelog/entries/2015/02/10178.SUP-142.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved editing preformatted text.
1 change: 1 addition & 0 deletions build/changelog/entries/2015/02/10180.SUP-223.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When deleting text, inline text elements like <a> links or bold elements (or any other inline elements) were not deleted in most cases. This has been fixed now.
9 changes: 9 additions & 0 deletions build/changelog/mappings/0.25.23.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "0.25.23",
"date": "17.02.2015",
"changeLogEntryFileNames": [
"10178.SUP-142.bugfix",
"10180.SUP-223.bugfix"
],
"genericProperties": {}
}
9 changes: 9 additions & 0 deletions build/changelog/mappings/1.0.8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "1.0.8",
"date": "17.02.2015",
"changeLogEntryFileNames": [
"10178.SUP-142.bugfix",
"10180.SUP-223.bugfix"
],
"genericProperties": {}
}
81 changes: 68 additions & 13 deletions src/lib/aloha/editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ define([
'aloha/block-jump',
'aloha/ephemera',
'util/dom2',
'util/browser',
'PubSub',
'aloha/copypaste',
'aloha/command',
Expand All @@ -35,6 +36,7 @@ define([
BlockJump,
Ephemera,
Dom,
Browser,
PubSub,
CopyPaste,
Command,
Expand Down Expand Up @@ -210,25 +212,16 @@ define([
}

/**
* Safely removes a placeholder and leaves its content at its place.
* This function is a helper to be passed to a jQuery each() invocation.
*
* Helper function for safely removing a placeholder that is a div
* @private
* @param {index} index (Unused) index of placeholder element in jQuery collection.
* @param {Element} placeholder DOM Element
*/
function removePlaceholder(index, placeholder) {
function removeDivPlaceholder(placeholder) {
var $placeholder = $(placeholder);

if (!$placeholder.hasClass('aloha-editable-div')) {
placeholder.remove();
return;
}

var child = placeholder.firstChild;

if (!child) {
placeholder.remove();
$placeholder.remove();
return;
}

Expand All @@ -243,8 +236,70 @@ define([
if (!$placeholder.is(':empty')) {
Dom.removeShallow(placeholder);
} else {
placeholder.remove();
$placeholder.remove();
}
}

/**
* Checks if the element given is an unmodified aloha placeholder
*
* @private
* @param {HTMLElement} node
* @return {Boolean} True if the given element is an aloha-editing-paragraph.
*/
function isUnmodifiedAlohaEditingP(node) {
return Browser.ie
? (node.className === 'aloha-editing-p aloha-placeholder'
&& node.children.length === 0
&& (!node.firstChild || node.firstChild.data === '\u2060'))
: (node.className === 'aloha-editing-p aloha-placeholder'
&& node.children.length >= 1
&& node.children[0].nodeName === 'BR'
&& node.children[0].className === 'aloha-end-br');
}

/**
* Helper function for safely removing a placeholder that is a paragraph
*
* @private
* @param {Element} placeholder DOM Element
*/
function removePPlaceholder(placeholder) {
var $placeholder = $(placeholder);
if (isUnmodifiedAlohaEditingP(placeholder)) {
$placeholder.remove();
} else {
$placeholder.removeClass('aloha-editable-p');
if (Browser.ie) {
//remove trailing or leading word joiner
var child = $placeholder[0].firstChild;
if (child && Dom.isTextNode(child) && child.data.indexOf('\u2060') >= -1) {
child.data = child.data.replace(/(^(\u2060)+)|((\u2060)+$)/g, '');
}
}
}
}


/**
* Safely removes a placeholder and leaves its content at its place.
* This function is a helper to be passed to a jQuery each() invocation.
*
* @private
* @param {index} index (Unused) index of placeholder element in jQuery collection.
* @param {Element} placeholder DOM Element
*/
function removePlaceholder(index, placeholder) {
var $placeholder = $(placeholder);
if ($placeholder.hasClass('aloha-editing-div')) {
removeDivPlaceholder(placeholder);
return;
}
if ($placeholder.hasClass('aloha-editing-p')) {
removePPlaceholder(placeholder);
return;
}
$placeholder.remove();
}

$(document).keydown(onKeydown);
Expand Down
98 changes: 91 additions & 7 deletions src/lib/aloha/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -5323,6 +5323,72 @@ define([
}
}

/**
* Remove the node if it doesn't have any children or 1 empty text node.
* This function doesn't handle all cases of unrendered nodes.
* In the following cases the node will not be removed:
* * The node contains multiple empty nodes
* * The child text node contains zero width characters
* * The child text node contains unrendered characters
*
* @param {Text} node The node to check
* @param {Element} range Range object to set the position
* @return {boolean} True if node got removed, false otherwise
*/
function removeNodeIfEmptyAndCorrectRange(node, range) {
var
removeNode = false,
parentNode,
nodeChildNodesLength,
offsetInParent,
firstChildNode;

if (!node || !range) {
return false;
}

parentNode = node.parentNode;

if (!parentNode) {
return false;
}

// Check if the node is empty
nodeChildNodesLength = node.childNodes.length;
if (nodeChildNodesLength === 0) {
removeNode = true;
}

// If the inline node only contains an empty text node,
// it is also considered being empty.
if (nodeChildNodesLength === 1) {
firstChildNode = node.childNodes[0];

if (firstChildNode.nodeType === $_.Node.TEXT_NODE
&& firstChildNode.nodeValue.length === 0) {
removeNode = true;
}
}

// Don't continue if node should not be removed or has no parent
if (!removeNode) {
return false;
}

// Before removing, remember the offset of node in its parent
offsetInParent = Dom.getIndexInParent(node);

// I'm not gonna comment this, it's clear what it does.
parentNode.removeChild(node);

// Now the cursor has to be set at the position where the
// removed parent was before.
range.setStart(parentNode, offsetInParent);
range.setEnd(parentNode, offsetInParent);

return true;
}

//@}
///// Deleting the contents of a range /////
//@{
Expand Down Expand Up @@ -5521,11 +5587,16 @@ define([
// "Canonicalize whitespace at (start node, start offset)."
canonicalizeWhitespace(startNode, startOffset);

// "Set range's end to its start."
// Ok, also set the range's start to its start, because modifying the text
// might have somehow corrupted the range
range.setStart(range.startContainer, range.startOffset);
range.setEnd(range.startContainer, range.startOffset);
// Remove the parent node, if it is an inline node and empty
// If the node is inline and empty, range correction will happen inside
// removeNodeIfEmptyAndCorrectRange, otherwise it'will be done below.
if (!isInlineNode(parent_) || !removeNodeIfEmptyAndCorrectRange(parent_, range)) {
// "Set range's end to its start."
// Ok, also set the range's start to its start, because modifying the text
// might have somehow corrupted the range
range.setStart(range.startContainer, range.startOffset);
range.setEnd(range.startContainer, range.startOffset);
}

// "Restore states and values from overrides."
restoreStatesAndValues(overrides, range);
Expand Down Expand Up @@ -5598,6 +5669,11 @@ define([
endNode.deleteData(0, endOffset);
}

// Remove parentNode, if it is an empty inline node
if (isInlineNode(startNode.parentNode)) {
removeNodeIfEmptyAndCorrectRange(startNode.parentNode, range);
}

// "Canonicalize whitespace at range's start."
canonicalizeWhitespace(range.startContainer, range.startOffset);

Expand Down Expand Up @@ -6614,7 +6690,9 @@ define([
*/
commands["delete"] = {
action: function (value, range) {
var i;
var
i,
deleteContentsRange;

// special behaviour for skipping zero-width whitespaces in IE7
if (Aloha.browser.msie && Aloha.browser.version <= 7) {
Expand Down Expand Up @@ -6711,7 +6789,13 @@ define([
if (node.nodeType == $_.Node.TEXT_NODE && offset != 0) {
range.setStart(node, offset - 1);
range.setEnd(node, offset - 1);
deleteContents(node, offset - 1, node, offset);
deleteContentsRange = deleteContents(node, offset - 1, node, offset);
if (deleteContentsRange) {
// The new range position can only be determined inside deleteContents,
// that's why it's necessary to take over the range if one was returned.
range.setStart(deleteContentsRange.startContainer, deleteContentsRange.startOffset);
range.setEnd(deleteContentsRange.endContainer, deleteContentsRange.endOffset);
}
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/aloha/markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ define([
var sibling = isGoingLeft ? prevVisibleNode(block) : nextVisibleNode(block);

if (!sibling || isBlock(sibling)) {
var $landing = jQuery('<div class="aloha-placeholder aloha-editable-div">&nbsp;</div>');
var $landing = jQuery('<div class="aloha-placeholder aloha-editing-div">&nbsp;</div>');

if (isGoingLeft) {
jQuery(block).before($landing);
Expand Down
61 changes: 61 additions & 0 deletions src/plugins/common/format/lib/format-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define('format/format-plugin', [
'util/arrays',
'util/html',
'util/dom',
'util/browser',
'util/maps',
'ui/ui',
'ui/toggleButton',
Expand All @@ -34,6 +35,7 @@ define('format/format-plugin', [
Arrays,
Html,
Dom,
Browser,
Maps,
Ui,
ToggleButton,
Expand Down Expand Up @@ -527,6 +529,65 @@ define('format/format-plugin', [
formatPlugin.multiSplitButton.setActiveItem(null);
}
}

handlePreformattedText(rangeObject.commonAncestorContainer);
}
/**
* Handles preformatted text.
* Adds empty paragraphs as landing stripes before and after a preformatted text.
*
* @private
* @param {Element} element a Dom element
*/
function handlePreformattedText(element) {
var $element = $(element);

if ($element.is('.aloha-editing-p.aloha-placeholder')) {
//remove all other placeholders
$element[0].className = '';
removePlaceholders();
$element[0].className = 'aloha-editing-p aloha-placeholder';
return;
}

removePlaceholders();

if ($element.is('pre')) {
//add placeholder before and after the preformatted text element
var nextSibling = $element[0].nextSibling;
var previousSibling = $element[0].previousSibling;
if (!previousSibling || !nextSibling) {
if (!previousSibling) {
$element.before(createLanding());
}
if (!nextSibling) {
$element.after(createLanding());
}
}
}
}

/**
* Helper function to create the placeholder jQuery element
*
* @private
* @returns {Object} the landing jQuery element
*/
function createLanding() {
//IE: add a "word joiner" character instead of a <br>
var landing = Browser.ie
? '<p class="aloha-editing-p aloha-placeholder">&#x2060;</p>'
: '<p class="aloha-editing-p aloha-placeholder"><br class="aloha-end-br"></p>';
return $(landing);
}

/**
* Remove editing placeholders
*
* @private
*/
function removePlaceholders(){
Aloha.activeEditable.removePlaceholder(Aloha.activeEditable.obj);
}

/**
Expand Down
Loading

0 comments on commit a93d5c8

Please sign in to comment.