From 74fc8bbb8c23e1194fb13274fe5ad585109015cd Mon Sep 17 00:00:00 2001 From: Jason Chen Date: Thu, 2 Jun 2016 17:35:45 -0700 Subject: [PATCH] allow data protocol in images closes #721 --- formats/image.js | 4 ++-- formats/link.js | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/formats/image.js b/formats/image.js index bf3ad636e9..a64b5571c5 100644 --- a/formats/image.js +++ b/formats/image.js @@ -1,5 +1,5 @@ import Embed from '../blots/embed'; -import Link from '../formats/link'; +import Link, { sanitize } from '../formats/link'; class Image extends Embed { @@ -16,7 +16,7 @@ class Image extends Embed { } static sanitize(url) { - return Link.sanitize(url); + return sanitize(url, ['http', 'https', 'data']) ? url : '//:0'; } static value(domNode) { diff --git a/formats/link.js b/formats/link.js index bb56487457..7be620ac79 100644 --- a/formats/link.js +++ b/formats/link.js @@ -15,14 +15,7 @@ class Link extends Inline { } static sanitize(url) { - let anchor = document.createElement('a'); - anchor.href = url; - let protocol = anchor.href.slice(0, anchor.href.indexOf(':')); - if (['http', 'https', 'mailto'].indexOf(protocol) > -1) { - return url; - } else { - return this.SANITIZED_URL; - } + return sanitize(url, ['http', 'https', 'mailto']) ? url : this.SANITIZED_URL; } format(name, value) { @@ -36,4 +29,12 @@ Link.tagName = 'A'; Link.SANITIZED_URL = 'about:blank'; -export default Link; +function sanitize(url, protocols) { + let anchor = document.createElement('a'); + anchor.href = url; + let protocol = anchor.href.slice(0, anchor.href.indexOf(':')); + return protocols.indexOf(protocol) > -1; +} + + +export { Link as default, sanitize };