Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 7 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion client/dist/js/shortcodable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 56 additions & 7 deletions client/src/js/shortcodable.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,50 @@
}

// Now 'parse' the shortcode attributes by creating it in jQuery as if it's an HTML node;
var shortcodeData = { 'ShortcodeType': selectedTxt.slice(1,-1).split(' ').shift() };
$.each($(`<${selectedTxt.slice(1,-1)} />`).get(0).attributes, function(index, attr){
shortcodeData[attr.name] = attr.value
// (no longer in jQuery: $('<...>') builds the element in the live CMS document, where
// an attribute such as onerror runs and a src is fetched. The selection is text any
// CMS editor can type, so it is parsed in an inert document instead, see parseAttributes)
var shortcodeName = selectedTxt.slice(1,-1).split(' ').shift();
var shortcodeData = { 'ShortcodeType': shortcodeName };
// $.each($(`<${selectedTxt.slice(1,-1)} />`).get(0).attributes, function(index, attr){
// shortcodeData[attr.name] = attr.value
// });
$.each(shortcodable.parseAttributes(selectedTxt.slice(1,-1).slice(shortcodeName.length)), function(name, value){
shortcodeData[name] = value;
});
// console.log('106: ', shortcodeData);

return shortcodeData;
},

// An HTML document without a browsing context: elements created in it never run event
// handlers or scripts and never fetch resources. Created once, on first use.
inertDocument: null,
getInertDocument: function() {
if (!shortcodable.inertDocument) {
shortcodable.inertDocument = document.implementation.createHTMLDocument('');
}
return shortcodable.inertDocument;
},

// Parse a shortcode's attribute string (everything after the name, eg ' id="1" title=x') into
// {name: value}. The HTML attribute parser of an inert document does the work, so quoting,
// entities and (lower-cased) names come out exactly as they did when this went through
// jQuery, but nothing in the string can act on the CMS document. A fixed tag name keeps the
// shortcode's own name (which may be 'table', 'tr', ...) out of the parsing.
parseAttributes: function(attrString) {
var wrapper = shortcodable.getInertDocument().createElement('div');
wrapper.innerHTML = '<span ' + attrString + '></span>';
var attrs = {};
var el = wrapper.firstChild;
if (el && el.attributes) {
$.each(el.attributes, function(index, attr){
attrs[attr.name] = attr.value;
});
}
return attrs;
},

// get a specific attribute value from a shortcode string by its key, eg 'id' from '[myshortcode id="1" other="etc"]
parseAttributeValue: function (string, key) {
var attr = new RegExp(key + '=\"([^\"]+)\"', 'g').exec(string);
Expand Down Expand Up @@ -168,14 +203,23 @@
}

if (placeholder_shortcodes) {
return source.replace(/\[([a-z_]+)\s*([^\]]*)\]/gi, function (found, name, params) {
// A name is what ShortcodeParser accepts (\w+) and ends at whitespace or ']' (#11:
// [a-z_]+ followed by \s* stopped a name at its first digit, so [year2] was taken
// for [year] with an attribute '2' and saved back as [year 2])
// return source.replace(/\[([a-z_]+)\s*([^\]]*)\]/gi, function (found, name, params) {
return source.replace(/\[(\w+)(?:\s+([^\]]*))?\]/g, function (found, name, params) {
// no attributes: params is undefined, and the shortcode text gets no trailing space
params = params || '';
var shortcodeText = params ? name + ' ' + params : name;
if (placeholder_shortcodes.indexOf(name) !== -1) {
var id = shortcodable.parseAttributeValue(params, 'id');
var src = encodeURI(shortcodable.controller_url + '/placeholder/' + name + '/' + id + '?sc=[' + name + ' ' + params + ']');
// var src = encodeURI(shortcodable.controller_url + '/placeholder/' + name + '/' + id + '?sc=[' + name + ' ' + params + ']');
var src = encodeURI(shortcodable.controller_url + '/placeholder/' + name + '/' + id + '?sc=[' + shortcodeText + ']');
var el = $('<img/>')
.attr('type', 'image/svg+xml')
.attr('class', 'sc-placeholder mceItem mceNonEditable')
.attr('title', name + ' ' + params)
// .attr('title', name + ' ' + params)
.attr('title', shortcodeText)
.attr('src', src);
// console.log(el.prop('outerHTML'));
return el.prop('outerHTML');
Expand All @@ -190,7 +234,12 @@
// Substitutes placeholder images with their shortcode equivalents
placeholdersToShortcodes: function(source, editor) {
// find & replace .sc-placeholder images in the html
var wrappedContent = $('<div>' + source + '</div>');
// (in an inert document rather than the live CMS one, so nothing in the content can run
// or load while it is rewritten; innerHTML directly, as jQuery's .html() may evaluate scripts)
// var wrappedContent = $('<div>' + source + '</div>');
var wrapper = shortcodable.getInertDocument().createElement('div');
wrapper.innerHTML = source;
var wrappedContent = $(wrapper);
wrappedContent.find('.sc-placeholder').each(function () {
var el = $(this);
el.replaceWith('[' + tinymce.trim(el.attr('title')) + ']');
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"wysiwyg",
"htmleditor"
],
"license": "BSD-3-Clause",
"license": "MIT",
"authors": [
{
"name": "Restruct",
Expand Down
Binary file removed docs/screens/button.png
Binary file not shown.
Binary file removed docs/screens/dialog.png
Binary file not shown.
8 changes: 5 additions & 3 deletions tests/browser/specs/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ test.describe('Editor integration', () => {

const img = placeholders(page);
await expect(img).toHaveCount(1);
await expect(img).toHaveAttribute('title', 'scb_year ');
// No trailing space since #11 (was 'scb_year ').
await expect(img).toHaveAttribute('title', 'scb_year');
await expect(img).toHaveAttribute('src', /^admin\/shortcodable\/placeholder\/scb_year\/\?sc=/);
// The placeholder action redirects to placehold.img, which answers an SVG.
const response = await svg;
expect(response.status()).toBe(200);
expect(response.headers()['content-type']).toContain('image/svg+xml');
// Default size: 46 px high, 60 + fontsize/1.8 per character of "[scb_year ]" wide.
expect(await loadedSize(img)).toEqual({ w: 157, h: 46 });
// Default size: 46 px high, 60 + fontsize/1.8 per character of "[scb_year]" wide (157 px for
// "[scb_year ]" before #11 dropped the trailing space).
expect(await loadedSize(img)).toEqual({ w: 148, h: 46 });
// The text around it is untouched.
await expect(editor(page).locator('body p')).toContainText('Before');
await expect(editor(page).locator('body p')).toContainText('after');
Expand Down
Loading
Loading