edit: paste limited html, enforce filter on all events
[minimedit.git] / edit.js
diff --git a/edit.js b/edit.js
index 4cdc5725cba194d67726c8a8519dfe17d4e944ab..6dcb705cee1c72e649530d8b508ce518889338cc 100644 (file)
--- a/edit.js
+++ b/edit.js
@@ -19,7 +19,7 @@ CKEDITOR.plugins.add('inlinesave', {
                                        if (ajaxpost.status != 200)
                                                alert('Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText);
                                        else
-                                               alert('Pagina is goed opgeslagen');
+                                               editor.resetDirty();
                                };
                                ajaxpost.send(data);
                        },
@@ -45,6 +45,21 @@ CKEDITOR.on('dialogDefinition', function (event) {
 
 CKEDITOR.on('instanceCreated', function (event) {
        var editor = event.editor;
+       var pastefilter = 'h2 h3 p ul ol li blockquote em i strong b; a[!href]; img[alt,!src]';
+
+       editor.on('paste', function (e) {
+               var html = e.data.dataValue;
+               if (!/<[^>]* style="/.test(html) && !/<font/.test(html)) return;
+
+               // force pasteFilter on contents containing styling attributes
+               var filter = new CKEDITOR.filter(pastefilter),
+                       fragment = CKEDITOR.htmlParser.fragment.fromHtml(html),
+                       writer = new CKEDITOR.htmlParser.basicWriter();
+               filter.applyTo(fragment);
+               fragment.writeHtml(writer);
+               e.data.dataValue = writer.getHtml();
+       });
+
        editor.on('configLoaded', function () {
                var config = editor.config;
                config.language = 'nl';
@@ -53,14 +68,14 @@ CKEDITOR.on('instanceCreated', function (event) {
                config.allowedContent = true;
                config.entities = false; // keep unicode
                config.filebrowserImageUploadUrl = '/edit?type=img';
-               config.forcePasteAsPlainText = true;
-               config.contentsCss = '/excelsior.css';
+               config.pasteFilter = pastefilter;
+               config.contentsCss = document.styleSheets[0].href;
                config.toolbar = [
-                       ['Inlinesave', '-', 'ShowBlocks', 'Sourcedialog', '-', 'Undo', 'Redo'],
-                       ['Format'],
-                       ['BulletedList', 'NumberedList', '-', 'Blockquote'],
-                       ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat', '-', 'Anchor', 'Link'],
-                       ['HorizontalRule', 'Table', 'Image', 'CreatePlaceholder'],
+                       ['Inlinesave', '-', 'Undo', 'Redo'],
+                       ['Format', 'BulletedList', 'NumberedList', 'Blockquote'],
+                       ['Bold', 'Italic', 'Link'],
+                       ['HorizontalRule', 'Table', 'Image'],
+                       ['CreatePlaceholder', 'Sourcedialog'],
                ];
                config.toolbarCanCollapse = true;
                config.floatSpacePreferRight = true;
@@ -70,6 +85,12 @@ CKEDITOR.on('instanceCreated', function (event) {
                config.disableObjectResizing = true;
                document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
        });
+
+       window.onbeforeunload = function () {
+               if (editor.checkDirty()) {
+                       return 'Pagina verlaten zonder wijzigingen op te slaan?'; // message ignored in modern browsers
+               }
+       };
 });
 
        CKEDITOR.disableAutoInline = true;
@@ -77,10 +98,7 @@ CKEDITOR.on('instanceCreated', function (event) {
 // add edit link to menu
 var pagebody = document.getElementsByClassName('static')[0];
 if (pagebody) {
-       var editlink = document.createElement('a');
-       editlink.style.cursor = 'pointer';
-       editlink.appendChild(document.createTextNode('Wijzig'));
-       editlink.href = '#edit';
+       var editlink = document.querySelector('a[href="#edit"]');
        editlink.onclick = function (e) {
                editlink.style.fontWeight = 'bold';
                editlink.href = '';
@@ -95,6 +113,5 @@ if (pagebody) {
        if (window.location.hash == '#edit') {
                editlink.onclick();
        }
-       document.querySelector('header ul').appendChild(editlink);
 }