From 60bd67ddd8627193a386f279346056b970cd7333 Mon Sep 17 00:00:00 2001 From: "David M. Johnson" Date: Sat, 21 Nov 2026 17:43:26 -0500 Subject: [PATCH] Preserve filenames when inserting media into entries --- CHANGES.md | 1 + .../WEB-INF/jsps/editor/EntryEditor.jsp | 6 ++- .../selenium/editor/RichEditorContentIT.java | 49 ++++++++++++++++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6da5553f1..8dc23cc77 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -82,6 +82,7 @@ ### Bug fixes +- **Media filenames keep their punctuation** when inserted into an entry. - **Changing a password ends the account's other sessions.** Saving a new password in the profile, an administrator's reset, or disabling an account now logs out that user's other browsers at their next Roller UI page; the diff --git a/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp b/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp index d653aef15..4a1a62b7a 100644 --- a/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp +++ b/app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp @@ -262,12 +262,14 @@ function onSelectMediaFile(name, url, isImage, hasThumbnail) { $("#mediafile_edit_lightbox").modal("hide"); $("#mediaFileEditor").attr('src', 'about:blank'); + var link = $('').attr('href', url); if (isImage === "true") { var src = hasThumbnail === "false" ? url : url + '?t=true'; - insertMediaFile('' + name + ''); + link.append($('').attr({src: src, alt: name})); } else { - insertMediaFile('' + name + ''); + link.text(name); } + insertMediaFile(link.prop('outerHTML')); } diff --git a/it-selenium/src/test/java/org/apache/roller/selenium/editor/RichEditorContentIT.java b/it-selenium/src/test/java/org/apache/roller/selenium/editor/RichEditorContentIT.java index c0e2d8992..922c5251a 100644 --- a/it-selenium/src/test/java/org/apache/roller/selenium/editor/RichEditorContentIT.java +++ b/it-selenium/src/test/java/org/apache/roller/selenium/editor/RichEditorContentIT.java @@ -19,10 +19,13 @@ package org.apache.roller.selenium.editor; +import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.junit.AfterClass; import org.junit.Before; @@ -40,7 +43,7 @@ /** * Runs the entry editor's rich-editor-content.js in a blank page in Firefox, * without a Roller server: which entries open in the HTML textarea instead of - * the rich editor, and where the textarea fallback inserts media files. + * the rich editor, and how media is inserted in the entry editor. * *

The module's verify phase starts Roller in Jetty first. To run just this * class without it, from it-selenium: @@ -162,6 +165,11 @@ public class RichEditorContentIT { // the rich editor loads entries with jQuery's html() private static final String JQUERY = "META-INF/resources/webjars/jquery/3.7.1/jquery.js"; + private static final Pattern MEDIA_CHOOSER_CALLBACK = + Pattern.compile("(?ms)^ function onSelectMediaFile\\(.*?^ }"); + private static final String MEDIA_NAME = "Notes ' \" & edits"; + private static final String MEDIA_URL = "/blog/mediaresource/123"; + private static WebDriver driver; private static String script; private static String jquery; @@ -258,6 +266,45 @@ public void whatStaysInTheRichEditorHasNoEventHandlersOnceJqueryLoadsIt() { assertEquals("should open in the textarea:", "", missed.toString()); } + @Test + public void mediaLinkKeepsFilenamePunctuation() throws IOException { + selectMedia(false, false); + assertEquals(MEDIA_NAME, js("return insertedMedia.querySelector('a').textContent;")); + } + + @Test + public void mediaImageWithThumbnailKeepsFilenamePunctuation() throws IOException { + selectMedia(true, true); + assertEquals(MEDIA_NAME, js("return insertedMedia.querySelector('img').alt;")); + assertEquals(MEDIA_URL + "?t=true", js("return insertedMedia.querySelector('img').getAttribute('src');")); + } + + @Test + public void mediaImageWithoutThumbnailKeepsFilenamePunctuation() throws IOException { + selectMedia(true, false); + assertEquals(MEDIA_NAME, js("return insertedMedia.querySelector('img').alt;")); + assertEquals(MEDIA_URL, js("return insertedMedia.querySelector('img').getAttribute('src');")); + } + + private static void selectMedia(boolean isImage, boolean hasThumbnail) throws IOException { + String jsp = Files.readString(Paths.get("../app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp")); + Matcher callback = MEDIA_CHOOSER_CALLBACK.matcher(jsp); + assertTrue("media chooser callback is present", callback.find()); + + js("var s = document.createElement('script');" + + "s.textContent = arguments[0];" + + "document.head.appendChild(s);", jquery + "\n" + callback.group()); + // Parse the markup passed to the editor without loading a media resource. + js("jQuery.fn.modal = function () { return this; };" + + "window.insertMediaFile = function (html) {" + + " var template = document.createElement('template');" + + " template.innerHTML = html;" + + " window.insertedMedia = template.content;" + + "};" + + "onSelectMediaFile(arguments[0], arguments[1], arguments[2], arguments[3]);", + MEDIA_NAME, MEDIA_URL, Boolean.toString(isImage), Boolean.toString(hasThumbnail)); + } + private static String insert(String text) { js("RollerRichEditor.insertAtCursor(document.getElementById('edit_content'), arguments[0]);", text);