mraible commented on code in PR #319:
URL: https://github.com/apache/roller/pull/319#discussion_r4609100518


##########
app/src/main/webapp/WEB-INF/jsps/editor/EntryEditor.jsp:
##########
@@ -176,25 +196,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:
   The textarea fallback now remembers whether the cursor has been placed 
(focus or select), so a media file goes at position 0 when the cursor was put 
there and at the end only when it never was; the logic moved into 
rich-editor-content.js as insertAtCursor and trackCursor. RichEditorContentIT 
covers an untouched textarea, a cursor at the start and a selection.



##########
app/src/main/webapp/roller-ui/scripts/rich-editor-content.js:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.
+ */
+
+/*
+ * Summernote loads an entry's HTML into the live page, where event handler
+ * attributes, scripts and embedded frames run as the person editing it. 
Entries
+ * can hold such HTML (videos, slide decks and other embeds), and Summernote
+ * would also drop some of it on save, so those entries open in the plain HTML
+ * textarea instead. The stored HTML is never changed.
+ *
+ * hasActiveHtml(html) parses the HTML in an inert document and returns true if
+ * it contains anything the rich editor would run or can't keep: script, frame,
+ * plugin, style and document-level elements, on* attributes, or javascript: 
and
+ * vbscript: URLs in any attribute. It returns true if the HTML can't be 
parsed.
+ */
+var RollerRichEditor = (function () {
+
+    // jQuery runs <script> elements it inserts; <base>, <meta>, <link> and
+    // <style> act on the whole page; <noscript> parses differently in the 
inert document,
+    // which has scripting off, so its contents can't be checked; SVG <set> and
+    // <animate> can change other attributes after loading.
+    var ACTIVE_ELEMENTS = "script, iframe, frame, frameset, object, embed, 
applet, "
+        + "base, meta, link, style, noscript, set, animate";
+
+    // control characters and whitespace that browsers ignore inside a URL 
scheme
+    var IGNORED_IN_SCHEME = 
/[\s\u0000-\u001f\u007f-\u00a0\u200b-\u200f\u2060\ufeff]/g;
+
+    function hasActiveUrl(value) {
+        var compact = value.replace(IGNORED_IN_SCHEME, "").toLowerCase();
+        return compact.indexOf("javascript:") !== -1 || 
compact.indexOf("vbscript:") !== -1;

Review Comment:
   The scheme check now only looks at URL attributes (href, src, action, 
formaction, xlink:href, data, poster, background, cite and each srcset 
candidate), and only at the start of the value after dropping whitespace and 
control characters, so title="JavaScript: The Good Parts" stays in the rich 
editor. RichEditorContentIT has that case and other descriptive attributes, 
plus script URLs in each URL attribute.



##########
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:
   Added RichEditorContentIT in it-playwright, which loads 
rich-editor-content.js into a blank page and checks 29 entries that should open 
in the textarea and 14 that should stay in the rich editor, plus where media is 
inserted; it needs no Roller server (mvn verify -Dit.test=RichEditorContentIT). 
14 of its cases fail against the earlier script and all pass now; the unit test 
now checks only the JSP wiring.



-- 
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]

Reply via email to