Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build/ctct-style-attributes-content-handler-changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- **FEATURE**: A Style Attribute Content Handler has been added to the common content handler plugin collection.
The Style Attribute Content Handler will remove all unsupported style attribute items recursively
from all elements in the supplied content. The set of supported style attribute items is
configured in the same manner as other content handler plugins via config in
Aloha.settings.contentHandler.handler.styleattribute.
26 changes: 22 additions & 4 deletions doc/guides/source/plugin_contenthandler.textile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ h2. The Content Handler Plugin

After reading this guide, you will be able to:

* Understand what Content Handler are and how to use them
* Understand what Content Handlers are and how to use them
* Use the Content Handler API to create, modify and
* Extend Content Handler with custom implementations

Expand Down Expand Up @@ -88,12 +88,12 @@ h4. Writing your own Content Handler
['aloha', 'jquery', 'aloha/contenthandlermanager'],
function(Aloha, jQuery, ContentHandlerManager) {
"use strict";

var MyContentHandler = ContentHandlerManager.createHandler({
handleContent: function( content ) {

// do something with the content

return content; // return as HTML text not jQuery/DOM object
}
});
Expand Down Expand Up @@ -185,3 +185,21 @@ Aloha.settings.contentHandler.sanitize = {
}
}
</javascript>

h4. Style Attribute Content Handler

The Style Attribute Content Handler will remove all unsupported style attribute items recursively from the style tags
of all elements in the supplied content. The set of supported style attribute items is defined by plugin configuration.
You may specify your own configuration based on these default settings:

<javascript>
Aloha.settings.contentHandler.styleattribute = {
// style attribute items allowed in the content
supportedStyles: {
'text-align': true
}
}
</javascript>



Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define([
'contenthandler/wordcontenthandler',
'contenthandler/genericcontenthandler',
'contenthandler/sanitizecontenthandler',
'contenthandler/styleattributecontenthandler',
'contenthandler/blockelementcontenthandler'
], function (
$,
Expand All @@ -25,6 +26,7 @@ define([
WordContentHandler,
GenericContentHandler,
SanitizeContentHandler,
StyleAttributeContentHandler,
BlockelementContentHandler
) {
'use strict';
Expand All @@ -40,6 +42,7 @@ define([
word: WordContentHandler,
generic: GenericContentHandler,
sanitize: SanitizeContentHandler,
styleattribute: StyleAttributeContentHandler,
blockelement: BlockelementContentHandler
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* styleattributecontenthandler.js is part of Aloha Editor project http://aloha-editor.org
*
* Aloha Editor is a WYSIWYG HTML5 inline editing library and editor.
* Copyright (c) 2010-2012 Gentics Software GmbH, Vienna, Austria.
* Contributors http://aloha-editor.org/contribution.php
*
* Aloha Editor is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* Aloha Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* As an additional permission to the GNU GPL version 2, you may distribute
* non-source (e.g., minimized or compacted) forms of the Aloha-Editor
* source code without the copy of the GNU GPL normally required,
* provided you include this license notice and a URL through which
* recipients can access the Corresponding Source.
*/
define([
'jquery',
'aloha',
'aloha/contenthandlermanager'
], function($, Aloha, ContentHandlerManager) {
var config = null,
defaults = {
supportedStyles: {
'text-align': true
}
};

Aloha.settings.contentHandler = Aloha.settings.contentHandler || {};
Aloha.settings.contentHandler.handler = Aloha.settings.contentHandler.handler || {};
Aloha.settings.contentHandler.handler.styleattribute = Aloha.settings.contentHandler.handler.styleattribute || {};


var init = function() {
if (!config) {
config = $.extend(true, {}, defaults, Aloha.settings.contentHandler.handler.styleattribute.supportedStyles);
}
};

var cleanStyles = function(cfg, $el) {
var style, styleItem, styleItemSplit, styleItems, styles, _i, _len;

style = $el.attr('style');

if (style) {
styleItems = '';
styles = style.split(';');

for (_i = 0, _len = styles.length; _i < _len; _i++) {
styleItem = styles[_i];
styleItem = styleItem.trim();
styleItemSplit = styleItem.split(':');

if (styleItemSplit.length !== 2) continue;

if (cfg.supportedStyles[styleItemSplit[0].trim()]) {
styleItems += "" + styleItem + ";";
}
}

$el.removeAttr('style');
if (styleItems) {
$el.attr('style', "" + styleItems);
}
}
};

var cleanStyleAttribute = function($content) {
$content.children().each(function() {
var $child = $(this);
cleanStyleAttribute($child);
cleanStyles(config, $child);
});
};

var StyleAttributeContentHandler = ContentHandlerManager.createHandler({
handleContent: function(content) {
init();

$content = $('<div/>').append(content);
cleanStyleAttribute($content);
return $content.html();
}
});

return StyleAttributeContentHandler;
});