Skip to content

Commit

Permalink
fix(link): skip onUpdateLink when link mark not added (#1600)
Browse files Browse the repository at this point in the history
* fix(link-extension): skip onUpdateLink when link mark not added

When attempting to auto-link in a node that disallows link marks, the
onUpdateLink handler was previously still being called. This skips
auto-link attempt entirely when we know the node disallows it.

Signed-off-by: Evan Owen <kainosnoema@gmail.com>

* chore(link): add changeset

Signed-off-by: Evan Owen <kainosnoema@gmail.com>

Co-authored-by: Will Hawker <whawker@users.noreply.github.com>
  • Loading branch information
kainosnoema and whawker committed Apr 19, 2022
1 parent e78cef9 commit e00e415
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-experts-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remirror/extension-link': patch
---

Prevent onUpdateLink from being called in nodes that disallow marks
40 changes: 30 additions & 10 deletions packages/remirror__extension-link/__tests__/link-extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ describe('schema', () => {
});
});

class NoMarkBlockExtension extends NodeExtension {
get name() {
return 'nomark' as const;
}

createTags() {
return [ExtensionTag.Block];
}

createNodeSpec(extra: ApplySchemaAttributes): NodeExtensionSpec {
return {
content: 'text*',
marks: '',
attrs: { ...extra.defaults() },
toDOM: (node) => ['div', extra.dom(node), 0],
};
}
}

function create(options: LinkOptions = {}) {
const linkExtension = new LinkExtension(options);

Expand All @@ -125,7 +144,7 @@ function create(options: LinkOptions = {}) {
linkExtension.addHandler('onUpdateLink', options.onUpdateLink);
}

return renderEditor([linkExtension, new BoldExtension()]);
return renderEditor([linkExtension, new BoldExtension(), new NoMarkBlockExtension()]);
}

describe('commands', () => {
Expand Down Expand Up @@ -483,7 +502,7 @@ describe('autolinking', () => {
let onUpdateLink = jest.fn(() => {});
let editor = create({ autoLink: true, onUpdateLink });
let { link } = editor.attributeMarks;
let { doc, p } = editor.nodes;
let { doc, p, nomark } = editor.nodes;
let { bold } = editor.marks;

beforeEach(() => {
Expand All @@ -492,7 +511,7 @@ describe('autolinking', () => {
editor = create({ autoLink: true, onUpdateLink });
({
attributeMarks: { link },
nodes: { doc, p },
nodes: { doc, p, nomark },
marks: { bold },
} = editor);
});
Expand Down Expand Up @@ -628,7 +647,7 @@ describe('autolinking', () => {
});
});

it('should be off by default', () => {
it('should not autolink or call onUpdateLink by default', () => {
const editor = create({ onUpdateLink });
const { doc, p } = editor.nodes;

Expand All @@ -637,16 +656,17 @@ describe('autolinking', () => {

editor.insertText(' https://test.com ');
expect(editor.doc).toEqualRemirrorDocument(doc(p('test.co https://test.com ')));
});

it('should not call the onUpdateLink handler when links are inserted using default settings', () => {
const editor = create({ onUpdateLink });
const { doc, p } = editor.nodes;

editor.add(doc(p('<cursor>'))).insertText('test.co');
expect(onUpdateLink).not.toHaveBeenCalled();
});

it('should not autolink or call onUpdateLink when mark is not allowed', () => {
editor.add(doc(nomark('<cursor>'))).insertText('test.co');
expect(editor.doc).toEqualRemirrorDocument(doc(nomark('test.co')));

editor.insertText(' https://test.com ');
expect(editor.doc).toEqualRemirrorDocument(doc(nomark('test.co https://test.com ')));

expect(onUpdateLink).not.toHaveBeenCalled();
});

Expand Down
2 changes: 1 addition & 1 deletion packages/remirror__extension-link/src/link-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export class LinkExtension extends MarkExtension<LinkOptions> {

// Find text that can be auto linked
tr.doc.nodesBetween(from, to, (node, pos) => {
if (!node.isTextblock) {
if (!node.isTextblock || !node.type.allowsMarkType(this.type)) {
return;
}

Expand Down

0 comments on commit e00e415

Please sign in to comment.