diff --git a/CHANGELOG.md b/CHANGELOG.md index b439d92..c98e30c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,26 @@ # Changelog +## 5.2.1 (2026-11-02) + +### Fixed + +- **The attributes of a shortcode selected in the editor are parsed without creating DOM nodes.** + Opening the dialog on a selected shortcode built it as an element in the CMS document to read its + attributes. They are now parsed in an inert document; values, entities and lower-cased attribute + names come out as before. Upgrading is recommended. +- **Shortcode names with digits are matched whole** (#11). The placeholder pattern stopped a name at + its first digit, so `[year2]` was taken for `[year]` with an attribute `2` and saved back as + `[year 2]`, and a registered name such as `h2_title` got no placeholder. Names are now matched as + Silverstripe's `ShortcodeParser` matches them (`\w+`, ending at whitespace or `]`). + **Behaviour change:** a shortcode without attributes has no trailing space in its placeholder + title and `?sc=` any more (`[year]`, was `[year ]`), so its default placeholder is one character + narrower. + +### Changed + +- `composer.json` declares the licence as MIT, as `license.md` already did. +- UPGRADING: a fresh Silverstripe 5 install gets simpler 1.x; `~0.2` only keeps an existing 0.2/0.3. + ## 5.2.0 (2026-10-11) ### Changed diff --git a/UPGRADING.md b/UPGRADING.md index e6e59d4..2e07894 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -7,8 +7,13 @@ the `shortcodable_classes` config, and the methods a shortcodable class implemen ### From 4.x on Silverstripe 5 -Change the constraint from `^4` (or `~4.0`) to `^5.1`. Nothing else is required: simpler stays on its -`0.x` line (`~0.2`), and the dialog is loaded the same way as on 4.0.16. +Change the constraint from `^4` (or `~4.0`) to `^5.1`. Nothing else is required. The dialog is loaded +the same way as on 4.0.16. + +simpler is required as `~0.2 || ^1`, and simpler 1.x supports Silverstripe 5 as well as 6 (1.0.0 +requires `silverstripe/framework ^5 || ^6`). So a fresh install on Silverstripe 5, or a `composer +update` that includes simpler, gets simpler 1.x. `~0.2` only lets a project that already has simpler +0.2 or 0.3 keep it. Differences you may notice: diff --git a/client/dist/js/shortcodable.js b/client/dist/js/shortcodable.js index c1053cc..bd4bf4b 100644 --- a/client/dist/js/shortcodable.js +++ b/client/dist/js/shortcodable.js @@ -1 +1 @@ -(()=>{var e,o={448:()=>{var e;e=jQuery,shortcodable={controller_url:"admin/shortcodable",initialized:!1,selectionBookmark:null,selectionWasCollapsed:!1,init:function(){this.initialized=!0,e("body").on("change","select.shortcode-type",(function(o){e.post(shortcodable.controller_url,e(this).parents("form").serializeArray(),(function(o){simpler.modal.bodyHtml=e("#xhr_buffer").html(o).html()})).fail((function(e){shortcodable.handleErrorResponse(e)}))})),e("body").on("submit","#Form_ShortcodeForm",(function(e){return e.preventDefault(),shortcodable.insertShortcode(),simpler.modal.show=!1,!1}))},openDialog:function(){shortcodable.selectionWasCollapsed=tinyMCE.activeEditor.selection.isCollapsed(),shortcodable.selectionBookmark=tinyMCE.activeEditor.selection.getBookmark(2,!0),simpler.modal.show=!0,simpler.modal.title="Insert/edit shortcode",simpler.modal.closeBtn=!1,simpler.modal.closeTxt="Close",simpler.modal.saveBtn=!1,simpler.modal.saveTxt="Insert shortcode",simpler.modal.bodyHtml=simpler.spinner,e.post(shortcodable.controller_url,shortcodable.getCurrentEditorSelectionAsParsedShortcodeData(),(function(o){simpler.modal.bodyHtml=e("#xhr_buffer").html(o).html()})).fail((function(e){shortcodable.handleErrorResponse(e)}))},handleErrorResponse:function(e){var o=e.responseText.split('
Text [scb_year2] end
'); + await openPage(page, id); + // Not a registered shortcode: no placeholder. + await expect(placeholders(page)).toHaveCount(0); + + // Edit, so the editor content is serialised again on save. + await editor(page).locator('body p').click(); + await page.keyboard.press('End'); + await page.keyboard.type(' x'); + await save(page, id); + + const content = await savedContent(page, id); + expect(content).toContain('[scb_year2] end x'); + expect(content).not.toContain('[scb_year 2]'); + }); + + test('names with digits get a placeholder; a longer name or a hyphen does not match a shorter one', async ({ page }) => { + const { id } = await newPage(page, 'Plain text
'); + await openPage(page, id); + + // The conversion itself, with a placeholder list that holds a name with a digit (the editor + // gets its list from config; a TinyMCE target carries it in data-config). + const result = await page.evaluate(() => { + const target = document.createElement('div'); + target.setAttribute('data-config', JSON.stringify({ shortcodable_placeholder_codes: ['h2_title', 'button', 'scb_year'] })); + const html = (window as any).shortcodable.shortcodesToPlaceholders( + '[h2_title] [button-large] [scb_year2] [scb_year] [button id="3" label="Go"]
', + { targetElm: target }, + ); + // Read back in a parsed (inert) document: placeholder images in the live page would be + // fetched from a URL relative to this one and log a 404. + const doc = new DOMParser().parseFromString(html, 'text/html'); + return { + titles: Array.from(doc.querySelectorAll('img.sc-placeholder')).map((img) => img.getAttribute('title')), + text: doc.body.textContent, + }; + }); + + // No trailing space for a shortcode without attributes; one space before the attributes. + expect(result.titles).toEqual(['h2_title', 'scb_year', 'button id="3" label="Go"']); + expect(result.text).toContain('[button-large]'); + expect(result.text).toContain('[scb_year2]'); + }); +}); diff --git a/tests/browser/specs/selection.spec.ts b/tests/browser/specs/selection.spec.ts new file mode 100644 index 0000000..b341d78 --- /dev/null +++ b/tests/browser/specs/selection.spec.ts @@ -0,0 +1,79 @@ +import type { Page } from '@playwright/test'; +import { test, expect, editor, newPage, openDialog, openPage } from './support'; + +// Reading the attributes of a shortcode that is selected as plain text when the dialog is opened +// (shortcodable.js getCurrentEditorSelectionAsParsedShortcodeData). The selection is text typed by +// any CMS editor, so parsing it must never create live elements in the CMS document: attributes +// are data, not markup. + +/** + * Select the text of the Content editor's one paragraph (not the paragraph itself), through + * TinyMCE's own selection API. A keyboard select-all is platform-dependent: Meta+A on macOS selects + * the text, Control+A on Linux (CI) selects the whole, whose content then does not start with
+ * '[' and is never parsed, so a spec relying on it passed without testing anything.
+ */
+async function selectAll(page: Page): Promise [img src="data:," onerror="window.__scbProbe = 1"] [img src="/scb-probe-fetch.png"] [scb_plain title="Two words" single='it is' bare=plain amp="a & b <c>" Mixed="Case" accent="café ü"] fail without
+ // any network request, so on a live element the handler would fire within a tick; the second
+ // case's src would be fetched from this host. One shortcode per case: the selection is parsed
+ // as a single shortcode, so a second one in the same text would only add junk attributes.
+ for (const [label, content] of [
+ ['a handler attribute never runs', '