From a2d5a5a323fd0b0c46a9d663b3932bc26b58f271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gergely=20B=C3=B3di?= Date: Wed, 19 Oct 2016 16:21:50 +0200 Subject: [PATCH 1/2] added new button: eval html basically for embedded videos and objects --- dist/js/medium-editor.js | 24 ++++++++++++++++++++---- spec/buttons.spec.js | 20 ++++++++++++++++++++ src/js/core.js | 5 +++++ src/js/defaults/buttons.js | 10 +++++++++- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/dist/js/medium-editor.js b/dist/js/medium-editor.js index 28a438a5c..051cd8caf 100644 --- a/dist/js/medium-editor.js +++ b/dist/js/medium-editor.js @@ -3350,6 +3350,14 @@ MediumEditor.extensions = {}; contentDefault: 'image', contentFA: '' }, + 'eval': { + name: 'eval', + action: 'eval', + aria: 'evaluate html', + tagNames: ['iframe', 'object'], + contentDefault: 'eval', + contentFA: '' + }, 'orderedlist': { name: 'orderedlist', action: 'insertorderedlist', @@ -3511,6 +3519,7 @@ MediumEditor.extensions = {}; }; })(); + (function () { 'use strict'; @@ -4305,7 +4314,8 @@ MediumEditor.extensions = {}; var WHITESPACE_CHARS, KNOWN_TLDS_FRAGMENT, LINK_REGEXP_TEXT, - KNOWN_TLDS_REGEXP; + KNOWN_TLDS_REGEXP, + LINK_REGEXP; WHITESPACE_CHARS = [' ', '\t', '\n', '\r', '\u00A0', '\u2000', '\u2001', '\u2002', '\u2003', '\u2028', '\u2029']; @@ -4327,6 +4337,8 @@ MediumEditor.extensions = {}; KNOWN_TLDS_REGEXP = new RegExp('^(' + KNOWN_TLDS_FRAGMENT + ')$', 'i'); + LINK_REGEXP = new RegExp(LINK_REGEXP_TEXT, 'gi'); + function nodeIsNotInsideAnchorTag(node) { return !MediumEditor.util.getClosestTag(node, 'a'); } @@ -4508,12 +4520,11 @@ MediumEditor.extensions = {}; }, findLinkableText: function (contenteditable) { - var linkRegExp = new RegExp(LINK_REGEXP_TEXT, 'gi'), - textContent = contenteditable.textContent, + var textContent = contenteditable.textContent, match = null, matches = []; - while ((match = linkRegExp.exec(textContent)) !== null) { + while ((match = LINK_REGEXP.exec(textContent)) !== null) { var matchOk = true, matchEnd = match.index + match[0].length; // If the regexp detected something as a link that has text immediately preceding/following it, bail out. @@ -7111,6 +7122,11 @@ MediumEditor.extensions = {}; return this.options.ownerDocument.execCommand('insertImage', false, src); } + if (action === 'eval') { + var html = this.options.contentWindow.getSelection().toString().trim(); + return MediumEditor.util.insertHTMLCommand(this.options.ownerDocument, html); + } + /* Issue: https://github.com/yabwe/medium-editor/issues/595 * If the action is to justify the text */ if (justifyAction.exec(action)) { diff --git a/spec/buttons.spec.js b/spec/buttons.spec.js index 066020f7e..1fb90df51 100644 --- a/spec/buttons.spec.js +++ b/spec/buttons.spec.js @@ -744,6 +744,26 @@ describe('Buttons TestCase', function () { }); }); + describe('Eval', function () { + it('should create an evaluated html tag', function () { + var editor = this.newMediumEditor('.editor', { + toolbar: { + buttons: ['eval'] + } + }), + toolbar = editor.getExtensionByName('toolbar'), + button = toolbar.getToolbarElement().querySelector('[data-action="eval"]'); + spyOn(document, 'execCommand').and.callThrough(); + this.el.innerHTML = '<iframe width="854" height="480" src="https://www.youtube.com/embed/QHH3iSeDBLo" frameborder="0" allowfullscreen></iframe> \n\n'; + + selectElementContentsAndFire(document.getElementById('span-eval')); + + fireEvent(button, 'click'); + + expect(this.el.innerHTML).toContain(''); + }); + }); + describe('OrderedList', function () { it('button should be active if the selection already has the element', function () { var editor = this.newMediumEditor('.editor', { diff --git a/src/js/core.js b/src/js/core.js index ae65003ea..2c5a185c8 100644 --- a/src/js/core.js +++ b/src/js/core.js @@ -604,6 +604,11 @@ return this.options.ownerDocument.execCommand('insertImage', false, src); } + if (action === 'eval') { + var html = this.options.contentWindow.getSelection().toString().trim(); + return MediumEditor.util.insertHTMLCommand(this.options.ownerDocument, html); + } + /* Issue: https://github.com/yabwe/medium-editor/issues/595 * If the action is to justify the text */ if (justifyAction.exec(action)) { diff --git a/src/js/defaults/buttons.js b/src/js/defaults/buttons.js index af0d9104d..036fe1746 100644 --- a/src/js/defaults/buttons.js +++ b/src/js/defaults/buttons.js @@ -87,6 +87,14 @@ contentDefault: 'image', contentFA: '' }, + 'eval': { + name: 'eval', + action: 'eval', + aria: 'evaluate html', + tagNames: ['iframe', 'object'], + contentDefault: 'eval', + contentFA: '' + }, 'orderedlist': { name: 'orderedlist', action: 'insertorderedlist', @@ -247,4 +255,4 @@ } }; -})(); \ No newline at end of file +})(); From f0dd84ec2011b622b7d6731412d536bb8762e19f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gergely=20B=C3=B3di?= Date: Wed, 19 Oct 2016 16:37:02 +0200 Subject: [PATCH 2/2] replaced dist/ back to original --- dist/js/medium-editor.js | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/dist/js/medium-editor.js b/dist/js/medium-editor.js index 051cd8caf..28a438a5c 100644 --- a/dist/js/medium-editor.js +++ b/dist/js/medium-editor.js @@ -3350,14 +3350,6 @@ MediumEditor.extensions = {}; contentDefault: 'image', contentFA: '' }, - 'eval': { - name: 'eval', - action: 'eval', - aria: 'evaluate html', - tagNames: ['iframe', 'object'], - contentDefault: 'eval', - contentFA: '' - }, 'orderedlist': { name: 'orderedlist', action: 'insertorderedlist', @@ -3519,7 +3511,6 @@ MediumEditor.extensions = {}; }; })(); - (function () { 'use strict'; @@ -4314,8 +4305,7 @@ MediumEditor.extensions = {}; var WHITESPACE_CHARS, KNOWN_TLDS_FRAGMENT, LINK_REGEXP_TEXT, - KNOWN_TLDS_REGEXP, - LINK_REGEXP; + KNOWN_TLDS_REGEXP; WHITESPACE_CHARS = [' ', '\t', '\n', '\r', '\u00A0', '\u2000', '\u2001', '\u2002', '\u2003', '\u2028', '\u2029']; @@ -4337,8 +4327,6 @@ MediumEditor.extensions = {}; KNOWN_TLDS_REGEXP = new RegExp('^(' + KNOWN_TLDS_FRAGMENT + ')$', 'i'); - LINK_REGEXP = new RegExp(LINK_REGEXP_TEXT, 'gi'); - function nodeIsNotInsideAnchorTag(node) { return !MediumEditor.util.getClosestTag(node, 'a'); } @@ -4520,11 +4508,12 @@ MediumEditor.extensions = {}; }, findLinkableText: function (contenteditable) { - var textContent = contenteditable.textContent, + var linkRegExp = new RegExp(LINK_REGEXP_TEXT, 'gi'), + textContent = contenteditable.textContent, match = null, matches = []; - while ((match = LINK_REGEXP.exec(textContent)) !== null) { + while ((match = linkRegExp.exec(textContent)) !== null) { var matchOk = true, matchEnd = match.index + match[0].length; // If the regexp detected something as a link that has text immediately preceding/following it, bail out. @@ -7122,11 +7111,6 @@ MediumEditor.extensions = {}; return this.options.ownerDocument.execCommand('insertImage', false, src); } - if (action === 'eval') { - var html = this.options.contentWindow.getSelection().toString().trim(); - return MediumEditor.util.insertHTMLCommand(this.options.ownerDocument, html); - } - /* Issue: https://github.com/yabwe/medium-editor/issues/595 * If the action is to justify the text */ if (justifyAction.exec(action)) {