Skip to content

Commit

Permalink
handle nested styles on paste, fixes #1333
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Apr 17, 2017
1 parent 643f65a commit 4e8d86e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
26 changes: 21 additions & 5 deletions modules/clipboard.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import extend from 'extend';
import Delta from 'quill-delta';
import Parchment from 'parchment';
import Quill from '../core/quill';
Expand Down Expand Up @@ -138,6 +139,22 @@ Clipboard.DEFAULTS = {
};


function applyFormat(delta, format, value) {
if (typeof format === 'object') {
return Object.keys(format).reduce(function(delta, key) {
return applyFormat(delta, key, format[key]);
}, delta);
} else {
return delta.reduce(function(delta, op) {
if (op.attributes && op.attributes[format]) {
return delta.push(op);
} else {
return delta.insert(op.insert, extend({}, {[format]: value}, op.attributes));
}
}, new Delta());
}
}

function computeStyle(node) {
if (node.nodeType !== Node.ELEMENT_NODE) return {};
const DOM_KEY = '__ql-computed-style';
Expand Down Expand Up @@ -185,7 +202,7 @@ function traverse(node, elementMatchers, textMatchers) { // Post-order


function matchAlias(format, node, delta) {
return delta.compose(new Delta().retain(delta.length(), { [format]: true }));
return applyFormat(delta, format, true);
}

function matchAttributor(node, delta) {
Expand All @@ -209,7 +226,7 @@ function matchAttributor(node, delta) {
}
});
if (Object.keys(formats).length > 0) {
delta = delta.compose(new Delta().retain(delta.length(), formats));
delta = applyFormat(delta, formats);
}
return delta;
}
Expand All @@ -225,8 +242,7 @@ function matchBlot(node, delta) {
delta = new Delta().insert(embed, match.formats(node));
}
} else if (typeof match.formats === 'function') {
let formats = { [match.blotName]: match.formats(node) };
delta = delta.compose(new Delta().retain(delta.length(), formats));
delta = applyFormat(delta, match.blotName, match.formats(node));
}
return delta;
}
Expand Down Expand Up @@ -287,7 +303,7 @@ function matchStyles(node, delta) {
formats.bold = true;
}
if (Object.keys(formats).length > 0) {
delta = delta.compose(new Delta().retain(delta.length(), formats));
delta = applyFormat(delta, formats);
}
if (parseFloat(style.textIndent || 0) > 0) { // Could be 0.5in
delta = new Delta().insert('\t').concat(delta);
Expand Down
5 changes: 5 additions & 0 deletions test/unit/modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ describe('Clipboard', function() {
expect(delta).toEqual(new Delta().insert('Test\n', { direction: 'rtl' }));
});

it('nested styles', function() {
let delta = this.clipboard.convert('<span style="color: red;"><span style="color: blue;">Test</span></span>');
expect(delta).toEqual(new Delta().insert('Test', { color: 'blue' }));
})

it('custom matcher', function() {
this.clipboard.addMatcher(Node.TEXT_NODE, function(node, delta) {
let index = 0;
Expand Down

0 comments on commit 4e8d86e

Please sign in to comment.