snoopdave commented on code in PR #331: URL: https://github.com/apache/roller/pull/331#discussion_r4608432295
########## app/src/test/java/org/apache/roller/weblogger/ui/struts2/editor/EntryEditorRichContentTest.java: ########## @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.roller.weblogger.ui.struts2.editor; + +import java.io.IOException; +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.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Summernote runs event handlers and scripts in the HTML it loads, so the rich + * editor in EntryEditor.jsp checks the entry with rich-editor-content.js first + * and opens entries that fail the check in the textarea. Summernote also starts + * itself on any call, so each call must be behind the same check. The check + * itself is verified in a browser; this keeps the page wired to it. + */ +class EntryEditorRichContentTest { + + private static String read(String path) throws IOException { + return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8); + } + + @Test + void summernoteStartsOnlyAfterTheContentCheck() throws IOException { + String jsp = read("src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp"); + assertTrue(jsp.contains("/roller-ui/scripts/rich-editor-content.js")); + + int check = jsp.indexOf("RollerRichEditor.hasActiveHtml("); + int start = jsp.indexOf("richEditorStarted = true;"); + int init = jsp.indexOf("$('#edit_content').summernote({"); + assertTrue(check > 0 && check < start && start < init, "check, then start, then init"); + + // every other call is guarded by the flag + Matcher call = Pattern.compile("\\.summernote\\(\\s*\"").matcher(jsp); + int calls = 0; + while (call.find()) { + calls++; + String before = jsp.substring(Math.max(0, call.start() - 120), call.start()); + assertTrue(before.contains("if (richEditorStarted)"), "unguarded call near: " + before); + } + assertEquals(1, calls); + } + + @Test + void contentCheckScriptExists() throws IOException { + String script = read("src/main/webapp/roller-ui/scripts/rich-editor-content.js"); + assertTrue(script.contains("function hasActiveHtml(html)")); Review Comment: **Important:** This test passes even if `hasActiveHtml()` always returns `false`: it checks only for the function name and `DOMParser` token. Add executable cases for HTML that should open in the textarea and ordinary formatting that should remain in the rich editor. ########## app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp: ########## @@ -180,25 +200,41 @@ } ); // Added event listener to confirm once the editor content is changed - $('#edit_content').on('summernote.change', function(we, contents, $editable) { - var confirmFunction = function(event) { - // Chrome requires returnValue to be set and original event is found as originalEvent - // see https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Example - if (event.originalEvent) - event.originalEvent.returnValue = "Are you sure you want to leave?"; - return "Are you sure you want to leave?"; - } - $(window).on("beforeunload", confirmFunction); + $('#edit_content').on('summernote.change', warnBeforeLeaving); + }); - // Remove it if it is form submit - $(this.form).on('submit', function() { - $(window).off("beforeunload", confirmFunction); - }); + function warnBeforeLeaving() { + var confirmFunction = function(event) { + // Chrome requires returnValue to be set and original event is found as originalEvent + // see https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Example + if (event.originalEvent) + event.originalEvent.returnValue = "Are you sure you want to leave?"; + return "Are you sure you want to leave?"; + } + $(window).on("beforeunload", confirmFunction); + + // Remove it if it is form submit + $(this.form).on('submit', function() { + $(window).off("beforeunload", confirmFunction); }); - }); + } function insertMediaFile(toInsert) { - $('#edit_content').summernote("pasteHTML", toInsert); + if (richEditorStarted) { + $('#edit_content').summernote("pasteHTML", toInsert); + } else { + // at the cursor, or at the end if the textarea hasn't had one + var textArea = document.getElementById('edit_content'); + var start = textArea.selectionStart; + if (start === 0 && textArea.selectionEnd === 0) { Review Comment: **Important:** A user who places the cursor at the start of the textarea has `selectionStart` and `selectionEnd` equal to zero, so inserting a media file appends it instead. Preserve a deliberate cursor position at zero; track whether the textarea has received a selection if an untouched textarea should append. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
