Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(ckeditor): stop the editor crashing at the edge of linked inline code (closes #11077)
Elian Doran631d00432613
diff --git a/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts b/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.tsindex cc71864d6d..7bae43e0bd 100644--- a/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts+++ b/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts@@ -1,4 +1,4 @@-import { _setModelData as setModelData, ClassicEditor, Code, Essentials, Paragraph, WidgetToolbarRepository } from "ckeditor5";+import { _setModelData as setModelData, ClassicEditor, Code, Essentials, Link, Paragraph, WidgetToolbarRepository } from "ckeditor5";import { beforeEach, describe, expect, it } from "vitest";import { createTestEditor } from "../../test/editor-kit.js";@@ -14,7 +14,7 @@ describe("InlineCodeToolbar", () => {let editor: ClassicEditor;beforeEach(async () => {- editor = await createTestEditor([Essentials, Paragraph, Code, CopyToClipboardButton, InlineCodeToolbar]);+ editor = await createTestEditor([Essentials, Paragraph, Code, Link, CopyToClipboardButton, InlineCodeToolbar]);});it("loads the plugin successfully", () => {@@ -60,6 +60,25 @@ describe("InlineCodeToolbar", () => {expect(related).toBeNull();});+ // A linked <code> would put this toolbar and the link balloon in the same stack, and the+ // caret move that dismisses both crashes ContextualBalloon (ckeditor/ckeditor5#11762).+ it("returns null when the inline code is inside a link, whichever way the two nest", () => {+ const def = getInlineCodeDefinition();++ setModelData(editor.model, "<paragraph><$text code=\"true\" linkHref=\"https://example.com\">hel[]lo</$text></paragraph>");+ expect(def?.getRelatedElement(editor.editing.view.document.selection)).toBeNull();++ setModelData(editor.model, "<paragraph><$text linkHref=\"https://example.com\">a</$text><$text code=\"true\" linkHref=\"https://example.com\">co[]de</$text></paragraph>");+ expect(def?.getRelatedElement(editor.editing.view.document.selection)).toBeNull();+ });++ it("still returns the <code> element for a link elsewhere in the same paragraph", () => {+ setModelData(editor.model, "<paragraph><$text linkHref=\"https://example.com\">link</$text><$text code=\"true\">co[]de</$text></paragraph>");++ const related = getInlineCodeDefinition()?.getRelatedElement(editor.editing.view.document.selection);+ expect(related?.is("attributeElement", "code")).toBe(true);+ });+it("returns null when the selection has no first position (null guard)", () => {const related = getInlineCodeDefinition()?.getRelatedElement({ getFirstPosition: () => null });expect(related).toBeNull();diff --git a/packages/ckeditor5/src/plugins/inline_code_toolbar.ts b/packages/ckeditor5/src/plugins/inline_code_toolbar.tsindex ac76a307fc..f42e521987 100644--- a/packages/ckeditor5/src/plugins/inline_code_toolbar.ts+++ b/packages/ckeditor5/src/plugins/inline_code_toolbar.ts@@ -1,4 +1,4 @@-import { Plugin, ViewDocumentFragment, WidgetToolbarRepository, type ViewNode } from "ckeditor5";+import { Plugin, ViewDocumentFragment, WidgetToolbarRepository, type ViewAttributeElement, type ViewNode } from "ckeditor5";import CopyToClipboardButton from "./copy_to_clipboard_button";/**@@ -23,15 +23,24 @@ export default class InlineCodeToolbar extends Plugin {return null;}+ let codeElement: ViewAttributeElement | null = null;let parent: ViewNode | ViewDocumentFragment | null = selectionPosition.parent;while (parent) {- if (parent.is("attributeElement", "code")) {- return parent;+ // Linked inline code stays without a toolbar: it would share the balloon stack+ // with the link balloon, and a caret move that dismisses both re-enters+ // ContextualBalloon#_showView() and crashes it (ckeditor/ckeditor5#11762).+ if (parent.is("attributeElement", "a")) {+ return null;}++ if (!codeElement && parent.is("attributeElement", "code")) {+ codeElement = parent;+ }+parent = parent.parent;}- return null;+ return codeElement;}});}