From f844bb476fe648e29aba87d4706715cfca3b2441 Mon Sep 17 00:00:00 2001 From: Bob Czarnecki Date: Wed, 5 Feb 2014 11:12:07 -0500 Subject: [PATCH 1/2] Style Content Attribute Content Handler --- .../lib/contenthandler-plugin.js | 3 + .../lib/styleattributecontenthandler.js | 97 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/plugins/common/contenthandler/lib/styleattributecontenthandler.js diff --git a/src/plugins/common/contenthandler/lib/contenthandler-plugin.js b/src/plugins/common/contenthandler/lib/contenthandler-plugin.js index 47833840c6..b02842c4b6 100644 --- a/src/plugins/common/contenthandler/lib/contenthandler-plugin.js +++ b/src/plugins/common/contenthandler/lib/contenthandler-plugin.js @@ -17,6 +17,7 @@ define([ 'contenthandler/wordcontenthandler', 'contenthandler/genericcontenthandler', 'contenthandler/sanitizecontenthandler', + 'contenthandler/styleattributecontenthandler', 'contenthandler/blockelementcontenthandler' ], function ( $, @@ -25,6 +26,7 @@ define([ WordContentHandler, GenericContentHandler, SanitizeContentHandler, + StyleAttributeContentHandler, BlockelementContentHandler ) { 'use strict'; @@ -40,6 +42,7 @@ define([ word: WordContentHandler, generic: GenericContentHandler, sanitize: SanitizeContentHandler, + styleattribute: StyleAttributeContentHandler, blockelement: BlockelementContentHandler }; diff --git a/src/plugins/common/contenthandler/lib/styleattributecontenthandler.js b/src/plugins/common/contenthandler/lib/styleattributecontenthandler.js new file mode 100644 index 0000000000..2694d3362b --- /dev/null +++ b/src/plugins/common/contenthandler/lib/styleattributecontenthandler.js @@ -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.allowable); + } + }; + + 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 = $('
').append(content); + cleanStyleAttribute($content); + return $content.html(); + } + }); + + return StyleAttributeContentHandler; +}); \ No newline at end of file From bb0c8830d48a5f4b6325c9e9759332413a72daac Mon Sep 17 00:00:00 2001 From: Bob Czarnecki Date: Wed, 5 Feb 2014 13:59:52 -0500 Subject: [PATCH 2/2] changelog and guide --- ...le-attributes-content-handler-changelog.md | 5 ++++ .../source/plugin_contenthandler.textile | 26 ++++++++++++++++--- .../lib/styleattributecontenthandler.js | 2 +- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 build/ctct-style-attributes-content-handler-changelog.md diff --git a/build/ctct-style-attributes-content-handler-changelog.md b/build/ctct-style-attributes-content-handler-changelog.md new file mode 100644 index 0000000000..c0f0838b3d --- /dev/null +++ b/build/ctct-style-attributes-content-handler-changelog.md @@ -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. diff --git a/doc/guides/source/plugin_contenthandler.textile b/doc/guides/source/plugin_contenthandler.textile index 385d7ae849..1d612d24cd 100644 --- a/doc/guides/source/plugin_contenthandler.textile +++ b/doc/guides/source/plugin_contenthandler.textile @@ -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 @@ -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 } }); @@ -185,3 +185,21 @@ Aloha.settings.contentHandler.sanitize = { } } + +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: + + +Aloha.settings.contentHandler.styleattribute = { + // style attribute items allowed in the content + supportedStyles: { + 'text-align': true + } +} + + + + diff --git a/src/plugins/common/contenthandler/lib/styleattributecontenthandler.js b/src/plugins/common/contenthandler/lib/styleattributecontenthandler.js index 2694d3362b..df80c25212 100644 --- a/src/plugins/common/contenthandler/lib/styleattributecontenthandler.js +++ b/src/plugins/common/contenthandler/lib/styleattributecontenthandler.js @@ -43,7 +43,7 @@ define([ var init = function() { if (!config) { - config = $.extend(true, {}, defaults, Aloha.settings.contentHandler.handler.styleattribute.allowable); + config = $.extend(true, {}, defaults, Aloha.settings.contentHandler.handler.styleattribute.supportedStyles); } };