Skip to content

Commit

Permalink
[FIX] web_editor : fix whitelist of html pasted elements
Browse files Browse the repository at this point in the history
whitelist of html element was not designed to keep the style
set by our own editor ( style on <span>).

We add a system to keep the spans when they meet some requirements.
Requirements are basically having one of the few allowed inline styling css rule.

@see tests in `test/spec/copyPaste.test.js` section 'Html Paste cleaning'

task-2604627

X-original-commit: da92c23
Part-of: odoo#89210
  • Loading branch information
sebgeelen committed Apr 21, 2022
1 parent fcc035b commit b17cdad
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const CLIPBOARD_WHITELISTS = {
'STRONG',
// Table
'TABLE',
'THEAD',
'TH',
'TBODY',
'TR',
Expand Down Expand Up @@ -139,6 +140,15 @@ export const CLIPBOARD_WHITELISTS = {
/^fa/,
],
attributes: ['class', 'href', 'src'],
spanStyle: {
'text-decoration': { defaultValues: ['', 'none'] },
'font-weight': { defaultValues: ['', '400'] },
'background-color': { defaultValues: ['', '#fff', '#ffffff', 'rgb(255, 255, 255)', 'rgba(255, 255, 255, 1)'] },
'color': { defaultValues: ['', '#000', '#000000', 'rgb(0, 0, 0)', 'rgba(0, 0, 0, 1)'] },
'font-style': { defaultValues: ['', 'none', 'normal'] },
'text-decoration-line': { defaultValues: ['', 'none'] },
'font-size': { defaultValues: ['', '16px'] },
}
};

function defaultOptions(defaultObject, object) {
Expand Down Expand Up @@ -2163,9 +2173,26 @@ export class OdooEditor extends EventTarget {
// Remove all illegal attributes and classes from the node, then
// clean its children.
for (const attribute of [...node.attributes]) {
if (!this._isWhitelisted(attribute)) {
// we kee some style on span to be able to paste text styled in the editor
if (node.nodeName === 'SPAN' && attribute.name === 'style') {
const spanInlineStyles = attribute.value.split(';');
const allowedSpanInlineStyles = spanInlineStyles.filter(rawStyle => {
const [styleName, styleValue] = rawStyle.split(':');
const style = CLIPBOARD_WHITELISTS.spanStyle[styleName.trim()];
return style && !style.defaultValues.includes(styleValue.trim());
});
node.removeAttribute(attribute.name);
if (allowedSpanInlineStyles.length > 0) {
node.setAttribute(attribute.name, allowedSpanInlineStyles.join(';'));
} else {
for (const unwrappedNode of unwrapContents(node)) {
this._cleanForPaste(unwrappedNode);
}
}
} else if (!this._isWhitelisted(attribute)) {
node.removeAttribute(attribute.name);
}

}
for (const klass of [...node.classList]) {
if (!this._isWhitelisted(klass)) {
Expand Down Expand Up @@ -2193,9 +2220,13 @@ export class OdooEditor extends EventTarget {
okClass instanceof RegExp ? okClass.test(item) : okClass === item,
);
} else {
const allowedSpanStyles = Object.keys(CLIPBOARD_WHITELISTS.spanStyle).map(s => `span[style*="${s}"]`);
return (
item.nodeType === Node.TEXT_NODE ||
(item.matches && item.matches(CLIPBOARD_WHITELISTS.nodes.join(',')))
(
item.matches &&
item.matches([...CLIPBOARD_WHITELISTS.nodes, ...allowedSpanStyles].join(','))
)
);
}
}
Expand Down

0 comments on commit b17cdad

Please sign in to comment.