emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org-protocol: decode "+" in query part as space
@ 2021-04-06 14:47 Maxim Nikulin
  2021-04-29 16:29 ` [PATCH] org-protocol: decode "+" in query part as space (v2) Maxim Nikulin
  0 siblings, 1 reply; 7+ messages in thread
From: Maxim Nikulin @ 2021-04-06 14:47 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 937 bytes --]

Hi,

Some time ago, when I tried org-protocol, I was quite surprised that 
query parameters generated using URLSearchParams JavaScript class are 
decoded incorrectly. "+" characters representing space are passed as is. 
I hope, I have found a proper place in the code to handle such case.

It does not affect previously recommended bookmarklets with abundant 
calls of encodeURIComponent that escapes spaces as "%20". I believe, new 
option is more readable:

     javascript:location.href='org-protocol://capture?' +
         new URLSearchParams({
             template: 'x',
             url: location.href,
             title: document.title,
             body: window.getSelection()});

I guess, with old org-protocol syntax only pure percent encoding was 
necessary due to each parameter was represented as path component. "+" 
is allowed only in query part. Such variant was just missed when new 
query-like syntax was introduced.

[-- Attachment #2: 0001-org-protocol.el-decode-in-query-part-as-space.patch --]
[-- Type: text/x-patch, Size: 7951 bytes --]

From bf14953cba974ef2199c8d551a47d58c819b9a33 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Tue, 6 Apr 2021 21:30:06 +0700
Subject: [PATCH] org-protocol.el: decode "+" in query part as space

* lisp/org-protocol.el (org-protocol-convert-query-to-plist):
Replace "+" chars by spaces before passing parameter string
to decoder.  Allow making org-protocol URIs with help of URLSearchParams
JavaScript class.
* lisp/org-protocol.el doc/org-manual.org etc/ORG-NEWS: Add examples
demonstrating new opportunity for browser bookmarklets.

Make parsing of URI parameters a bit closer to URL standard
https://url.spec.whatwg.org/#urlencoded-parsing
---
 doc/org-manual.org   | 22 ++++++++++++++++++++
 etc/ORG-NEWS         | 11 ++++++++++
 lisp/org-protocol.el | 48 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 75 insertions(+), 6 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index efe956877..122da066d 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -19625,11 +19625,20 @@ slashes, and probably quote those for the shell.
 To use this feature from a browser, add a bookmark with an arbitrary
 name, e.g., =Org: store-link= and enter this as /Location/:
 
+#+begin_example
+javascript:location.href='org-protocol://store-link?' +
+      new URLSearchParams({url:location.href, title:document.title});
+#+end_example
+
+Title is an optional parameter.  Another expression was recommended earlier:
+
 #+begin_example
 javascript:location.href='org-protocol://store-link?url='+
       encodeURIComponent(location.href);
 #+end_example
 
+The latter form is compatible with older Org versions from 9.0 to 9.4.
+
 *** The ~capture~ protocol
 :PROPERTIES:
 :DESCRIPTION: Fill a buffer with external information.
@@ -19645,6 +19654,15 @@ using acapture template.
 To use this feature, add a bookmark with an arbitrary name, e.g.,
 =Org: capture=, and enter this as =Location=:
 
+#+begin_example
+javascript:location.href='org-protocol://capture?' +
+      new URLSearchParams({
+            template: 'x', url: window.location.href,
+            title: document.title, body: window.getSelection()});
+#+end_example
+
+You might have seen another expression:
+
 #+begin_example
 javascript:location.href='org-protocol://capture?template=x'+
       '&url='+encodeURIComponent(window.location.href)+
@@ -19652,6 +19670,10 @@ javascript:location.href='org-protocol://capture?template=x'+
       '&body='+encodeURIComponent(window.getSelection());
 #+end_example
 
+It is a bit more cluttered than the former one, but it is compatible with
+previous Org versions 9.0-9.4. In these versions encoding of space as "+"
+character was not supported by URI decoder.
+
 #+vindex: org-protocol-default-template-key
 The capture template to be used can be specified in the bookmark (like
 =X= above).  If unspecified, the template key is set in the variable
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 8bf3a7d29..78c563e01 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -228,6 +228,17 @@ Storing links to files using ~org-store-link~ (=<C-c l>=) when
 description of the link, if available.  If no title exists it falls
 back to the filename as before.
 
+*** org-protocol compatibility with =URLSearchParams= JavaScript class
+
+Decoder of query part of org-protocol URI recognizes "+" as an encoded
+space characters now, so it is possible to avoid call to =encodeURIComponent=
+for each parameter and use more readable expression in bookmarklet:
+
+#+begin_example
+'org-protocol://store-link?' + new URLSearchParams({
+      url: location.href, title: document.title})
+#+end_example
+
 * Version 9.4
 ** Incompatible changes
 *** Possibly broken internal file links: please check and fix
diff --git a/lisp/org-protocol.el b/lisp/org-protocol.el
index 731f51e19..ebfd3887b 100644
--- a/lisp/org-protocol.el
+++ b/lisp/org-protocol.el
@@ -94,6 +94,15 @@
 ;; You may use the same bookmark URL for all those standard handlers and just
 ;; adjust the sub-protocol used:
 ;;
+;;     javascript:location.href='org-protocol://sub-protocol?'+
+;;           new URLSearchParams({
+;;                 url: location.href,
+;;                 title: document.title,
+;;                 body: window.getSelection()})
+;;
+;; Alternatively use the following expression that encodes space as \"%20\"
+;; instead of \"+\", so it is compatible with Org versions from 9.0 to 9.4:
+;;
 ;;     location.href='org-protocol://sub-protocol?url='+
 ;;           encodeURIComponent(location.href)+'&title='+
 ;;           encodeURIComponent(document.title)+'&body='+
@@ -103,6 +112,11 @@
 ;; char that, if present, triggers the use of a special template.
 ;; Example:
 ;;
+;;     location.href='org-protocol://capture?'+
+;;           new URLSearchParams({template:'x', /* ... */})
+;;
+;; or
+;;
 ;;     location.href='org-protocol://capture?template=x'+ ...
 ;;
 ;;  uses template ?x.
@@ -426,7 +440,12 @@ Parameters: url, title (optional), body (optional)
 Old-style links such as org-protocol://store-link://URL/TITLE are
 also recognized.
 
-The location for a browser's bookmark has to look like this:
+The location for a browser's bookmark may look like this:
+
+  javascript:location.href = \\='org-protocol://store-link?' +
+       new URLSearchParams({url:location.href, title:document.title});
+
+or to keep compatibility with Org versions from 9.0 to 9.4 it may be:
 
   javascript:location.href = \\
       \\='org-protocol://store-link?url=\\=' + \\
@@ -435,7 +454,9 @@ The location for a browser's bookmark has to look like this:
 
 Don't use `escape()'!  Use `encodeURIComponent()' instead.  The
 title of the page could contain slashes and the location
-definitely will.
+definitely will.  Org 9.4 and earlier could not decode \"+\"
+to space, that is why less readable latter expression may be necessary
+for backward compatibility.
 
 The sub-protocol used to reach this function is set in
 `org-protocol-protocol-alist'.
@@ -463,6 +484,14 @@ The sub-protocol used to reach this function is set in
 This function detects an URL, title and optional text, separated
 by `/'.  The location for a browser's bookmark looks like this:
 
+  javascript:location.href = \\='org-protocol://capture?' +
+        new URLSearchParams({
+              url: location.href,
+              title: document.title,
+              body: window.getSelection()})
+
+or to keep compatibility with Org versions from 9.0 to 9.4:
+
   javascript:location.href = \\='org-protocol://capture?url=\\='+ \\
         encodeURIComponent(location.href) + \\='&title=\\=' \\
         encodeURIComponent(document.title) + \\='&body=\\=' + \\
@@ -518,10 +547,11 @@ Now template ?b will be used."
 (defun org-protocol-convert-query-to-plist (query)
   "Convert QUERY key=value pairs in the URL to a property list."
   (when query
-    (apply 'append (mapcar (lambda (x)
-			     (let ((c (split-string x "=")))
-			       (list (intern (concat ":" (car c))) (cadr c))))
-			   (split-string query "&")))))
+    (let ((plus-decoded (replace-regexp-in-string "\\+" " " query t t)))
+      (apply 'append (mapcar (lambda (x)
+			       (let ((c (split-string x "=")))
+				 (list (intern (concat ":" (car c))) (cadr c))))
+			     (split-string plus-decoded "&"))))))
 
 (defun org-protocol-open-source (fname)
   "Process an org-protocol://open-source?url= style URL with FNAME.
@@ -531,6 +561,12 @@ in `org-protocol-project-alist'.
 
 The location for a browser's bookmark should look like this:
 
+  javascript:location.href = \\='org-protocol://open-source?' +
+        new URLSearchParams({url: location.href})
+
+or if you prefer to keep compatibility with older Org versions (9.0 to 9.4),
+consider the following expression:
+
   javascript:location.href = \\='org-protocol://open-source?url=\\=' + \\
         encodeURIComponent(location.href)"
   ;; As we enter this function for a match on our protocol, the return value
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] org-protocol: decode "+" in query part as space (v2)
  2021-04-06 14:47 [PATCH] org-protocol: decode "+" in query part as space Maxim Nikulin
@ 2021-04-29 16:29 ` Maxim Nikulin
  2021-09-27 10:38   ` Bastien
  0 siblings, 1 reply; 7+ messages in thread
From: Maxim Nikulin @ 2021-04-29 16:29 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1254 bytes --]

I have realized that only a half of new apostrophes in doc strings were 
properly escaped, so I am attaching updated patch. I still consider the 
change as a minor improvement.

On 06/04/2021 21:47, Maxim Nikulin wrote:
> 
> When I tried org-protocol for the first time, I was quite surprised that 
> query parameters generated using URLSearchParams JavaScript class are 
> decoded incorrectly. "+" characters representing space are passed as is. 
> I hope, I have found a proper place in the code to handle such case.
> 
> It does not affect previously recommended bookmarklets with abundant 
> calls of encodeURIComponent that escapes spaces as "%20". I believe, new 
> option is more readable:
> 
>      javascript:location.href='org-protocol://capture?' +
>          new URLSearchParams({
>              template: 'x',
>              url: location.href,
>              title: document.title,
>              body: window.getSelection()});
> 
> I guess, with old org-protocol syntax only pure percent encoding was 
> necessary due to each parameter was represented as path component. "+" 
> is allowed only in query part. Such variant was just missed when new 
> query-like syntax was introduced.


[-- Attachment #2: 0001-org-protocol.el-decode-in-query-part-as-space.patch --]
[-- Type: text/x-patch, Size: 7902 bytes --]

From 9bacd0761acd14fb5809042d96977bfd3088bee3 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Tue, 6 Apr 2021 21:30:06 +0700
Subject: [PATCH] org-protocol.el: decode "+" in query part as space

* lisp/org-protocol.el (org-protocol-convert-query-to-plist):
Replace "+" chars by spaces before passing parameter string
to decoder.  Allow making org-protocol URIs with help of URLSearchParams
JavaScript class.
* lisp/org-protocol.el doc/org-manual.org etc/ORG-NEWS: Add examples
demonstrating new opportunity for browser bookmarklets.

Make parsing of URI parameters a bit closer to URL standard
https://url.spec.whatwg.org/#urlencoded-parsing
---
 doc/org-manual.org   | 22 ++++++++++++++++++++
 etc/ORG-NEWS         | 11 ++++++++++
 lisp/org-protocol.el | 48 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 75 insertions(+), 6 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index b547a8a5f..d91aa7a50 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -19596,11 +19596,20 @@ slashes, and probably quote those for the shell.
 To use this feature from a browser, add a bookmark with an arbitrary
 name, e.g., =Org: store-link= and enter this as /Location/:
 
+#+begin_example
+javascript:location.href='org-protocol://store-link?' +
+      new URLSearchParams({url:location.href, title:document.title});
+#+end_example
+
+Title is an optional parameter.  Another expression was recommended earlier:
+
 #+begin_example
 javascript:location.href='org-protocol://store-link?url='+
       encodeURIComponent(location.href);
 #+end_example
 
+The latter form is compatible with older Org versions from 9.0 to 9.4.
+
 *** The ~capture~ protocol
 :PROPERTIES:
 :DESCRIPTION: Fill a buffer with external information.
@@ -19616,6 +19625,15 @@ using acapture template.
 To use this feature, add a bookmark with an arbitrary name, e.g.,
 =Org: capture=, and enter this as =Location=:
 
+#+begin_example
+javascript:location.href='org-protocol://capture?' +
+      new URLSearchParams({
+            template: 'x', url: window.location.href,
+            title: document.title, body: window.getSelection()});
+#+end_example
+
+You might have seen another expression:
+
 #+begin_example
 javascript:location.href='org-protocol://capture?template=x'+
       '&url='+encodeURIComponent(window.location.href)+
@@ -19623,6 +19641,10 @@ javascript:location.href='org-protocol://capture?template=x'+
       '&body='+encodeURIComponent(window.getSelection());
 #+end_example
 
+It is a bit more cluttered than the former one, but it is compatible with
+previous Org versions 9.0-9.4. In these versions encoding of space as "+"
+character was not supported by URI decoder.
+
 #+vindex: org-protocol-default-template-key
 The capture template to be used can be specified in the bookmark (like
 =X= above).  If unspecified, the template key is set in the variable
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 83dca5c03..724adc486 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -269,6 +269,17 @@ back to the filename as before.
 
 The function does not allow for a third optional parameter anymore.
 
+*** Make org-protocol compatible with =URLSearchParams= JavaScript class
+
+Decoder of query part of org-protocol URI recognizes "+" as an encoded
+space characters now, so it is possible to avoid call to =encodeURIComponent=
+for each parameter and use more readable expression in bookmarklet:
+
+#+begin_example
+'org-protocol://store-link?' + new URLSearchParams({
+      url: location.href, title: document.title})
+#+end_example
+
 * Version 9.4
 ** Incompatible changes
 *** Possibly broken internal file links: please check and fix
diff --git a/lisp/org-protocol.el b/lisp/org-protocol.el
index 30f5bc040..a09d7fbed 100644
--- a/lisp/org-protocol.el
+++ b/lisp/org-protocol.el
@@ -94,6 +94,15 @@
 ;; You may use the same bookmark URL for all those standard handlers and just
 ;; adjust the sub-protocol used:
 ;;
+;;     javascript:location.href='org-protocol://sub-protocol?'+
+;;           new URLSearchParams({
+;;                 url: location.href,
+;;                 title: document.title,
+;;                 body: window.getSelection()})
+;;
+;; Alternatively use the following expression that encodes space as \"%20\"
+;; instead of \"+\", so it is compatible with Org versions from 9.0 to 9.4:
+;;
 ;;     location.href='org-protocol://sub-protocol?url='+
 ;;           encodeURIComponent(location.href)+'&title='+
 ;;           encodeURIComponent(document.title)+'&body='+
@@ -103,6 +112,11 @@
 ;; char that, if present, triggers the use of a special template.
 ;; Example:
 ;;
+;;     location.href='org-protocol://capture?'+
+;;           new URLSearchParams({template:'x', /* ... */})
+;;
+;; or
+;;
 ;;     location.href='org-protocol://capture?template=x'+ ...
 ;;
 ;;  uses template ?x.
@@ -426,7 +440,12 @@ Parameters: url, title (optional), body (optional)
 Old-style links such as org-protocol://store-link://URL/TITLE are
 also recognized.
 
-The location for a browser's bookmark has to look like this:
+The location for a browser's bookmark may look like this:
+
+  javascript:location.href = \\='org-protocol://store-link?\\=' +
+       new URLSearchParams({url:location.href, title:document.title});
+
+or to keep compatibility with Org versions from 9.0 to 9.4 it may be:
 
   javascript:location.href = \\
       \\='org-protocol://store-link?url=\\=' + \\
@@ -435,7 +454,9 @@ The location for a browser's bookmark has to look like this:
 
 Don't use `escape()'!  Use `encodeURIComponent()' instead.  The
 title of the page could contain slashes and the location
-definitely will.
+definitely will.  Org 9.4 and earlier could not decode \"+\"
+to space, that is why less readable latter expression may be necessary
+for backward compatibility.
 
 The sub-protocol used to reach this function is set in
 `org-protocol-protocol-alist'.
@@ -463,6 +484,14 @@ The sub-protocol used to reach this function is set in
 This function detects an URL, title and optional text, separated
 by `/'.  The location for a browser's bookmark looks like this:
 
+  javascript:location.href = \\='org-protocol://capture?\\=' +
+        new URLSearchParams({
+              url: location.href,
+              title: document.title,
+              body: window.getSelection()})
+
+or to keep compatibility with Org versions from 9.0 to 9.4:
+
   javascript:location.href = \\='org-protocol://capture?url=\\='+ \\
         encodeURIComponent(location.href) + \\='&title=\\=' + \\
         encodeURIComponent(document.title) + \\='&body=\\=' + \\
@@ -518,10 +547,11 @@ Now template ?b will be used."
 (defun org-protocol-convert-query-to-plist (query)
   "Convert QUERY key=value pairs in the URL to a property list."
   (when query
-    (apply 'append (mapcar (lambda (x)
-			     (let ((c (split-string x "=")))
-			       (list (intern (concat ":" (car c))) (cadr c))))
-			   (split-string query "&")))))
+    (let ((plus-decoded (replace-regexp-in-string "\\+" " " query t t)))
+      (apply 'append (mapcar (lambda (x)
+			       (let ((c (split-string x "=")))
+				 (list (intern (concat ":" (car c))) (cadr c))))
+			     (split-string plus-decoded "&"))))))
 
 (defun org-protocol-open-source (fname)
   "Process an org-protocol://open-source?url= style URL with FNAME.
@@ -531,6 +561,12 @@ in `org-protocol-project-alist'.
 
 The location for a browser's bookmark should look like this:
 
+  javascript:location.href = \\='org-protocol://open-source?\\=' +
+        new URLSearchParams({url: location.href})
+
+or if you prefer to keep compatibility with older Org versions (9.0 to 9.4),
+consider the following expression:
+
   javascript:location.href = \\='org-protocol://open-source?url=\\=' + \\
         encodeURIComponent(location.href)"
   ;; As we enter this function for a match on our protocol, the return value
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] org-protocol: decode "+" in query part as space (v2)
  2021-04-29 16:29 ` [PATCH] org-protocol: decode "+" in query part as space (v2) Maxim Nikulin
@ 2021-09-27 10:38   ` Bastien
  2021-09-27 14:31     ` [PATCH] org-protocol: decode "+" in query part as space (v3) Max Nikulin
  0 siblings, 1 reply; 7+ messages in thread
From: Bastien @ 2021-09-27 10:38 UTC (permalink / raw)
  To: Maxim Nikulin; +Cc: emacs-orgmode

Hi Maxim,

Maxim Nikulin <manikulin@gmail.com> writes:

> I have realized that only a half of new apostrophes in doc strings
> were properly escaped, so I am attaching updated patch. I still
> consider the change as a minor improvement.

Probably given the delay, the patch does not apply anymore.

Would you be able to reformat and resend it?

Thanks,

-- 
 Bastien


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] org-protocol: decode "+" in query part as space (v3)
  2021-09-27 10:38   ` Bastien
@ 2021-09-27 14:31     ` Max Nikulin
  2021-09-27 15:07       ` Max Nikulin
  2021-09-27 15:52       ` Bastien
  0 siblings, 2 replies; 7+ messages in thread
From: Max Nikulin @ 2021-09-27 14:31 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 455 bytes --]

On 27/09/2021 17:38, Bastien wrote:
> Maxim Nikulin writes:
> 
>> I have realized that only a half of new apostrophes in doc strings
>> were properly escaped, so I am attaching updated patch. I still
>> consider the change as a minor improvement.
> 
> Probably given the delay, the patch does not apply anymore.
> 
> Would you be able to reformat and resend it?

Surprisingly there is no conflicts during rebase. I expected changed 
context in ORG-NEWS.


[-- Attachment #2: 0001-org-protocol.el-decode-in-query-part-as-space.patch --]
[-- Type: text/x-patch, Size: 7965 bytes --]

From be23adf20852966b904583970bd106a5e8385ec7 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Tue, 6 Apr 2021 21:30:06 +0700
Subject: [PATCH] org-protocol.el: decode "+" in query part as space

* lisp/org-protocol.el (org-protocol-convert-query-to-plist):
Replace "+" chars by spaces before passing parameter string
to decoder.  Allow making org-protocol URIs with help of URLSearchParams
JavaScript class.
* lisp/org-protocol.el doc/org-manual.org etc/ORG-NEWS: Add examples
demonstrating new opportunity for browser bookmarklets.

Make parsing of URI parameters a bit closer to URL standard
https://url.spec.whatwg.org/#urlencoded-parsing
---
 doc/org-manual.org   | 22 ++++++++++++++++++++
 etc/ORG-NEWS         | 11 ++++++++++
 lisp/org-protocol.el | 48 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 75 insertions(+), 6 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index adf9c132c..96f55616f 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -19783,11 +19783,20 @@ slashes, and probably quote those for the shell.
 To use this feature from a browser, add a bookmark with an arbitrary
 name, e.g., =Org: store-link= and enter this as /Location/:
 
+#+begin_example
+javascript:location.href='org-protocol://store-link?' +
+      new URLSearchParams({url:location.href, title:document.title});
+#+end_example
+
+Title is an optional parameter.  Another expression was recommended earlier:
+
 #+begin_example
 javascript:location.href='org-protocol://store-link?url='+
       encodeURIComponent(location.href);
 #+end_example
 
+The latter form is compatible with older Org versions from 9.0 to 9.4.
+
 *** The ~capture~ protocol
 :PROPERTIES:
 :DESCRIPTION: Fill a buffer with external information.
@@ -19803,6 +19812,15 @@ using acapture template.
 To use this feature, add a bookmark with an arbitrary name, e.g.,
 =Org: capture=, and enter this as =Location=:
 
+#+begin_example
+javascript:location.href='org-protocol://capture?' +
+      new URLSearchParams({
+            template: 'x', url: window.location.href,
+            title: document.title, body: window.getSelection()});
+#+end_example
+
+You might have seen another expression:
+
 #+begin_example
 javascript:location.href='org-protocol://capture?template=x'+
       '&url='+encodeURIComponent(window.location.href)+
@@ -19810,6 +19828,10 @@ javascript:location.href='org-protocol://capture?template=x'+
       '&body='+encodeURIComponent(window.getSelection());
 #+end_example
 
+It is a bit more cluttered than the former one, but it is compatible with
+previous Org versions 9.0-9.4. In these versions encoding of space as "+"
+character was not supported by URI decoder.
+
 #+vindex: org-protocol-default-template-key
 The capture template to be used can be specified in the bookmark (like
 =X= above).  If unspecified, the template key is set in the variable
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 53915d8e4..9d19a812c 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -517,6 +517,17 @@ Use =org-get-previous-sibling= instead.  This is just a rename to have
 a more consistent naming.  E.g. recall the pair of funtctions
 =next-line= / =previous-line=.
 
+*** Make org-protocol compatible with =URLSearchParams= JavaScript class
+
+Decoder of query part of org-protocol URI recognizes "+" as an encoded
+space characters now, so it is possible to avoid call to =encodeURIComponent=
+for each parameter and use more readable expression in bookmarklet:
+
+#+begin_example
+'org-protocol://store-link?' + new URLSearchParams({
+      url: location.href, title: document.title})
+#+end_example
+
 * Version 9.4
 ** Incompatible changes
 *** Possibly broken internal file links: please check and fix
diff --git a/lisp/org-protocol.el b/lisp/org-protocol.el
index e4578d421..703d0d7a0 100644
--- a/lisp/org-protocol.el
+++ b/lisp/org-protocol.el
@@ -94,6 +94,15 @@
 ;; You may use the same bookmark URL for all those standard handlers and just
 ;; adjust the sub-protocol used:
 ;;
+;;     javascript:location.href='org-protocol://sub-protocol?'+
+;;           new URLSearchParams({
+;;                 url: location.href,
+;;                 title: document.title,
+;;                 body: window.getSelection()})
+;;
+;; Alternatively use the following expression that encodes space as \"%20\"
+;; instead of \"+\", so it is compatible with Org versions from 9.0 to 9.4:
+;;
 ;;     location.href='org-protocol://sub-protocol?url='+
 ;;           encodeURIComponent(location.href)+'&title='+
 ;;           encodeURIComponent(document.title)+'&body='+
@@ -103,6 +112,11 @@
 ;; char that, if present, triggers the use of a special template.
 ;; Example:
 ;;
+;;     location.href='org-protocol://capture?'+
+;;           new URLSearchParams({template:'x', /* ... */})
+;;
+;; or
+;;
 ;;     location.href='org-protocol://capture?template=x'+ ...
 ;;
 ;;  uses template ?x.
@@ -427,7 +441,12 @@ Parameters: url, title (optional), body (optional)
 Old-style links such as org-protocol://store-link://URL/TITLE are
 also recognized.
 
-The location for a browser's bookmark has to look like this:
+The location for a browser's bookmark may look like this:
+
+  javascript:location.href = \\='org-protocol://store-link?\\=' +
+       new URLSearchParams({url:location.href, title:document.title});
+
+or to keep compatibility with Org versions from 9.0 to 9.4 it may be:
 
   javascript:location.href = \\
       \\='org-protocol://store-link?url=\\=' + \\
@@ -436,7 +455,9 @@ The location for a browser's bookmark has to look like this:
 
 Don't use `escape()'!  Use `encodeURIComponent()' instead.  The
 title of the page could contain slashes and the location
-definitely will.
+definitely will.  Org 9.4 and earlier could not decode \"+\"
+to space, that is why less readable latter expression may be necessary
+for backward compatibility.
 
 The sub-protocol used to reach this function is set in
 `org-protocol-protocol-alist'.
@@ -464,6 +485,14 @@ The sub-protocol used to reach this function is set in
 This function detects an URL, title and optional text, separated
 by `/'.  The location for a browser's bookmark looks like this:
 
+  javascript:location.href = \\='org-protocol://capture?\\=' +
+        new URLSearchParams({
+              url: location.href,
+              title: document.title,
+              body: window.getSelection()})
+
+or to keep compatibility with Org versions from 9.0 to 9.4:
+
   javascript:location.href = \\='org-protocol://capture?url=\\='+ \\
         encodeURIComponent(location.href) + \\='&title=\\=' + \\
         encodeURIComponent(document.title) + \\='&body=\\=' + \\
@@ -519,10 +548,11 @@ Now template ?b will be used."
 (defun org-protocol-convert-query-to-plist (query)
   "Convert QUERY key=value pairs in the URL to a property list."
   (when query
-    (apply 'append (mapcar (lambda (x)
-			     (let ((c (split-string x "=")))
-			       (list (intern (concat ":" (car c))) (cadr c))))
-			   (split-string query "&")))))
+    (let ((plus-decoded (replace-regexp-in-string "\\+" " " query t t)))
+      (apply 'append (mapcar (lambda (x)
+			       (let ((c (split-string x "=")))
+				 (list (intern (concat ":" (car c))) (cadr c))))
+			     (split-string plus-decoded "&"))))))
 
 (defun org-protocol-open-source (fname)
   "Process an org-protocol://open-source?url= style URL with FNAME.
@@ -532,6 +562,12 @@ in `org-protocol-project-alist'.
 
 The location for a browser's bookmark should look like this:
 
+  javascript:location.href = \\='org-protocol://open-source?\\=' +
+        new URLSearchParams({url: location.href})
+
+or if you prefer to keep compatibility with older Org versions (9.0 to 9.4),
+consider the following expression:
+
   javascript:location.href = \\='org-protocol://open-source?url=\\=' + \\
         encodeURIComponent(location.href)"
   ;; As we enter this function for a match on our protocol, the return value
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] org-protocol: decode "+" in query part as space (v3)
  2021-09-27 14:31     ` [PATCH] org-protocol: decode "+" in query part as space (v3) Max Nikulin
@ 2021-09-27 15:07       ` Max Nikulin
  2021-09-27 15:52         ` Bastien
  2021-09-27 15:52       ` Bastien
  1 sibling, 1 reply; 7+ messages in thread
From: Max Nikulin @ 2021-09-27 15:07 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 619 bytes --]

On 27/09/2021 21:31, Max Nikulin wrote:
> On 27/09/2021 17:38, Bastien wrote:
>> Maxim Nikulin writes:
>>
>>> I have realized that only a half of new apostrophes in doc strings
>>> were properly escaped, so I am attaching updated patch. I still
>>> consider the change as a minor improvement.
>>
>> Probably given the delay, the patch does not apply anymore.
>>
>> Would you be able to reformat and resend it?
> 
> Surprisingly there is no conflicts during rebase. I expected changed 
> context in ORG-NEWS.

I had some test cases for decoding of "+" in org-protocol URIs, so I 
have put them to test-org-protocol.el.


[-- Attachment #2: 0001-test-org-protocol.el-Decode-to-tests.patch --]
[-- Type: text/x-patch, Size: 2564 bytes --]

From 55d5128add97e7ef73f41eb52f4dccfedbeabc6b Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Mon, 27 Sep 2021 22:02:09 +0700
Subject: [PATCH] test-org-protocol.el: Decode "+" to " " tests

testing/lisp/test-org-protocol.el
(test-org-protocol/org-protocol-store-link)
(test-org-protocol/org-protocol-capture): Cases to check that "+" is
decoded to space in query parameters (new style of URIs) but preserved
in path components (old style of org-protocol links).
---
 testing/lisp/test-org-protocol.el | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/testing/lisp/test-org-protocol.el b/testing/lisp/test-org-protocol.el
index d33052b30..95fc7f862 100644
--- a/testing/lisp/test-org-protocol.el
+++ b/testing/lisp/test-org-protocol.el
@@ -76,7 +76,15 @@
   ;; New link style
   (let ((uri "/some/directory/org-protocol://store-link?url=URL3&title=TITLE3"))
     (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
-    (should (equal (car org-stored-links) '("URL3" "TITLE3")))))
+    (should (equal (car org-stored-links) '("URL3" "TITLE3"))))
+  ;; Do not decode "+" in old-style link
+  (let ((uri "/org-protocol:/store-link:/one+one/plus+preserved"))
+    (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
+    (should (equal (car org-stored-links) '("one+one" "plus+preserved"))))
+  ;; Decode "+" to space in new-style link
+  (let ((uri "/org-protocol:/store-link/?url=one+two&title=plus+is+space"))
+    (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
+    (should (equal (car org-stored-links) '("one two" "plus is space")))))
 
 (ert-deftest test-org-protocol/org-protocol-store-link-file ()
   "store-link: `org-protocol-sanitize-uri' could distort URL."
@@ -133,6 +141,12 @@
 	    ;; - query parameters, not sure how to include them in template
 	    ("/some/directory/org-protocol:/capture?template=x&url=URL&title=TITLE&body=BODY&from=example"
 	     . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
+            ;; - "+" is not decoded to space in old-style URIs
+            ("/org-protocol:/capture:/t/https%3A%2F%2Forgmode.org%2Fsome+thing/org+mode/Body+plus"
+             . "** TODO\n\nBody+plus\n\n[[https://orgmode.org/some+thing][org+mode]]\n")
+            ;; - decode "+" to space
+            ("/org-protocol:/capture?template=t&url=URL&title=Mailing+list&body=Body+no+plus"
+             . "** TODO\n\nBody no plus\n\n[[URL][Mailing list]]\n")
 	    )))
     ;; Old link style
     (mapc
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] org-protocol: decode "+" in query part as space (v3)
  2021-09-27 14:31     ` [PATCH] org-protocol: decode "+" in query part as space (v3) Max Nikulin
  2021-09-27 15:07       ` Max Nikulin
@ 2021-09-27 15:52       ` Bastien
  1 sibling, 0 replies; 7+ messages in thread
From: Bastien @ 2021-09-27 15:52 UTC (permalink / raw)
  To: Max Nikulin; +Cc: emacs-orgmode

Max Nikulin <manikulin@gmail.com> writes:

> Surprisingly there is no conflicts during rebase. I expected changed
> context in ORG-NEWS.

Applied, thanks!

-- 
 Bastien


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] org-protocol: decode "+" in query part as space (v3)
  2021-09-27 15:07       ` Max Nikulin
@ 2021-09-27 15:52         ` Bastien
  0 siblings, 0 replies; 7+ messages in thread
From: Bastien @ 2021-09-27 15:52 UTC (permalink / raw)
  To: Max Nikulin; +Cc: emacs-orgmode

Hi Max,

Max Nikulin <manikulin@gmail.com> writes:

> I had some test cases for decoding of "+" in org-protocol URIs, so I
> have put them to test-org-protocol.el.

Applied too, thanks again.

-- 
 Bastien


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-09-27 15:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-06 14:47 [PATCH] org-protocol: decode "+" in query part as space Maxim Nikulin
2021-04-29 16:29 ` [PATCH] org-protocol: decode "+" in query part as space (v2) Maxim Nikulin
2021-09-27 10:38   ` Bastien
2021-09-27 14:31     ` [PATCH] org-protocol: decode "+" in query part as space (v3) Max Nikulin
2021-09-27 15:07       ` Max Nikulin
2021-09-27 15:52         ` Bastien
2021-09-27 15:52       ` Bastien

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).