From 3a6f2d0ba212b28ad10ab0d2a9800afacb71e0f5 Mon Sep 17 00:00:00 2001
From: Albert Graef <aggraef@gmail.com>
Date: Fri, 27 Sep 2024 00:42:33 +0200
Subject: [PATCH] Fix up end-of-line whitespace regexes.

This was an ill-fated attempt to employ variable-length lookback in
order to make the regex "count" the parity of consecutive backslashes
preceding trailing whitespace, like the Pd binbuf parser does. But that
doesn't work with the JS RE engine at least, so I removed those bits
again.

What this means is that trailing whitespace will be kept in some cases
where the binbuf parser might have removed it, which shouldn't have any
grave consequences (fingers crossed). The important thing here is that
escaped trailing whitespace is kept and does not suddenly disappear when
editing an object, and that still works with the simplified regexes.
---
 pd/nw/pd_canvas.js | 4 ++--
 pd/nw/pdgui.js     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index 6804473af..5c2b10277 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -206,7 +206,7 @@ var canvas_events = (function() {
         text_to_fudi = function(text, obj_class, escapes) {
             if (obj_class !== "comment") {
                 // trim whitespace at the beginning and end (unless escaped)
-                text = text.replace(/^\s+|(?<!(\\\\)*\\)\s+$/g, "");
+                text = text.replace(/^\s+|(?<!\\)\s+$/g, "");
             }
 
             // special case for draw path d="arbitrary path string" ...
@@ -260,7 +260,7 @@ var canvas_events = (function() {
                 // ag: make sure to exclude \v below since we need these as
                 // newline substitutes which survive binbuf treatment
                 // split on newlines or (unescaped) spaces
-                in_array = msg.split(/(?<!(\\\\)*\\) |[\t\n]/);
+                in_array = msg.split(/(?<!\\) |[\t\n]/);
                 while (in_array.length) {
                     left = in_array.slice(); // make a copy of in_array
                     if (left.toString().length > chunk_max) {
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index a3a97f4e5..d3f73b58d 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -1706,7 +1706,7 @@ function enquote (x) {
     .replace(/"/g, "\\\"")
     .replace(/ /g, "\\ ")
     // trim whitespace (unless escaped)
-    .replace(/^\s+|(?<!(\\\\)*\\)\s+$/g, "");
+    .replace(/^\s+|(?<!\\)\s+$/g, "");
 }
 
 // from stackoverflow.com/questions/21698906/how-to-check-if-a-path-is-absolute-or-relative
@@ -7819,7 +7819,7 @@ function gui_textarea(cid, tag, type, x, y, width_spec, height_spec, text,
         // remove leading/trailing whitespace
         if (type !== "comment") {
             // trim whitespace at the beginning and end (unless escaped)
-            text = text.replace(/^\s+|(?<!(\\\\)*\\)\s+$/g, "");
+            text = text.replace(/^\s+|(?<!\\)\s+$/g, "");
         }
         p.textContent = text;
         // append to doc body
-- 
GitLab