Skip to content

Commit 9e9aef8

Browse files
committed
Support cloning to another format as long as the pen is empty
1 parent 0bcffe8 commit 9e9aef8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/MagicPen.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,9 @@ MagicPen.prototype.space = MagicPen.prototype.sp = function (count) {
439439
});
440440

441441
MagicPen.prototype.clone = function (format) {
442-
this.ensureCompatibleFormat(format);
442+
if (!this.isEmpty()) {
443+
this.ensureCompatibleFormat(format);
444+
}
443445

444446
function MagicPenClone() {}
445447
MagicPenClone.prototype = this;

test/magicpen.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,26 @@ describe('magicpen', function () {
830830
});
831831
});
832832

833+
describe('clone', function () {
834+
describe('when given a format', function () {
835+
it('can be cloned to another format when the format is not specified yet', function () {
836+
pen = pen.clone('text');
837+
expect(pen.format, 'to equal', 'text');
838+
});
839+
840+
it('can be cloned to another format when the pen is empty', function () {
841+
pen = pen.clone('text').clone('html');
842+
expect(pen.format, 'to equal', 'html');
843+
});
844+
845+
it('fails when given an incompatible format', function () {
846+
expect(function () {
847+
pen.clone('text').text('foobar').clone('html');
848+
}, 'to throw', 'This pen is only compatible with the format: text');
849+
});
850+
});
851+
});
852+
833853
describe('in text mode', function () {
834854
it('ignores unknown styles', function () {
835855
pen.text('>').write({ style: 'test', args: ['text'] }).text('<');

0 commit comments

Comments
 (0)