From e41d98fc22f2d473ae7484eb54c8795e95514abd Mon Sep 17 00:00:00 2001 From: Konrad Oboza <34310128+konradoboza@users.noreply.github.com> Date: Wed, 19 Feb 2020 15:53:30 +0100 Subject: [PATCH] EZP-31371: Copying a web page link w/o protocol in a Word paragraph leads to a 404 error (#1245) * EZP-31371: Copying a web page link w/o protocol in a Word paragraph leads to a 404 error * changed event to CKEditor's afterPaste --- .../scripts/fieldType/base/base-rich-text.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/bundle/Resources/public/js/scripts/fieldType/base/base-rich-text.js b/src/bundle/Resources/public/js/scripts/fieldType/base/base-rich-text.js index b9793d3a34..9bc8bbd58c 100644 --- a/src/bundle/Resources/public/js/scripts/fieldType/base/base-rich-text.js +++ b/src/bundle/Resources/public/js/scripts/fieldType/base/base-rich-text.js @@ -325,6 +325,9 @@ nativeEditor.on('change', saveRichText); nativeEditor.on('customUpdate', saveRichText); nativeEditor.on('editorInteraction', saveRichText); + nativeEditor.on('afterPaste', () => { + this.setLinksProtocol(container); + }); return alloyEditor; } @@ -390,6 +393,24 @@ splitIntoWords(text) { return text.split(' ').filter((word) => word.trim()); } + + setLinksProtocol(container) { + const links = container.querySelectorAll('a'); + const anchorPrefix = '#'; + const protocolPrefix = 'http://'; + + links.forEach((link) => { + const href = link.getAttribute('href'); + const protocolPattern = /^https?:\/\//i; + + if (href && href.indexOf(anchorPrefix) !== 0 && !protocolPattern.test(href)) { + const protocolHref = protocolPrefix.concat(href); + + link.setAttribute('href', protocolHref); + link.setAttribute('data-cke-saved-href', protocolHref); + } + }); + } }; eZ.BaseRichText = BaseRichText;