Koenig - Standardise <b> and <i> elements when pasting

refs https://github.com/TryGhost/Ghost/issues/9623
- use our paste handler to perform a replacement on any pasted HTML
  - `<b>` -> `<strong>`
  - `<i>` -> `<em>`
This commit is contained in:
Kevin Ansfield 2018-05-17 14:42:20 +01:00
parent 823f6afa80
commit b9c81ac219

View File

@ -661,6 +661,40 @@ export default Component.extend({
editor.triggerEvent(editor.element, 'paste', pasteEvent); editor.triggerEvent(editor.element, 'paste', pasteEvent);
} }
// we need to standardise HTML here because parserPlugins do not get
// passed inline markup such as `<b>` or `<i>`
if (html) {
// prevent mobiledoc's default paste event handler firing
event.preventDefault();
event.stopImmediatePropagation();
let normalizedHtml = html
.replace(/<b(\s|>)/gi, '<strong$1')
.replace(/<\/b>/gi, '</strong>')
.replace(/<i(\s|>)/gi, '<em$1')
.replace(/<\/i>/gi, '</em>');
// we can't modify the paste event itself so we trigger a mock
// paste event with our own data
let pasteEvent = {
type: 'paste',
preventDefault() {},
target: editor.element,
clipboardData: {
getData(type) {
if (type === 'text/plain') {
return text;
}
if (type === 'text/html') {
return normalizedHtml;
}
}
}
};
editor.triggerEvent(editor.element, 'paste', pasteEvent);
}
}, },
/* Ember event handlers ------------------------------------------------- */ /* Ember event handlers ------------------------------------------------- */