From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:bcc0::]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id gDFFIjziimDO8AAAgWs5BA (envelope-from ) for ; Thu, 29 Apr 2021 18:43:40 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id SIjaHTziimDTSwAAbx9fmQ (envelope-from ) for ; Thu, 29 Apr 2021 16:43:40 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 8BDA0260D7 for ; Thu, 29 Apr 2021 18:43:39 +0200 (CEST) Received: from localhost ([::1]:43778 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lc9lJ-00052V-F9 for larch@yhetil.org; Thu, 29 Apr 2021 12:43:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48468) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lc9YG-0008U4-TL for emacs-orgmode@gnu.org; Thu, 29 Apr 2021 12:30:08 -0400 Received: from ciao.gmane.io ([116.202.254.214]:49946) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lc9YE-0007Uu-Fk for emacs-orgmode@gnu.org; Thu, 29 Apr 2021 12:30:08 -0400 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1lc9YB-0003Hr-A9 for emacs-orgmode@gnu.org; Thu, 29 Apr 2021 18:30:03 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: emacs-orgmode@gnu.org From: Maxim Nikulin Subject: Re: [PATCH] org-protocol: decode "+" in query part as space (v2) Date: Thu, 29 Apr 2021 23:29:54 +0700 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------A0F47987C3F6711F59B5768F" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=116.202.254.214; envelope-from=geo-emacs-orgmode@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: 28 X-Spam_score: 2.8 X-Spam_bar: ++ X-Spam_report: (2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_ADSP_CUSTOM_MED=0.001, FORGED_GMAIL_RCVD=1, FORGED_MUA_MOZILLA=2.309, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, NICE_REPLY_A=-0.001, NML_ADSP_CUSTOM_MED=0.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1619714620; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=DI24lSh9o8wwllENnnsGyQWmhpnCrzbraP0Zg69Zjfo=; b=jM3WQq9vbAwfoiAnObmhAS+3TmTYOH23/5/knqgvUi6W2oLcwOr53Z1ZTGkBFyp8W51s+5 JG7py5gprkuAamQuaNmtZ5y1AaUoKEmxxALGkF0bSjlyb8fq6rpof0SU56PaX292LIh9Cg ZL68R3wwzDw/KNspdxpDR/TdogCmmwbrfiRTE8Km2xtbWV3ORFXEY7YHi4CxzwvLVH+Nne kRHQ5Ka7ju7Jv/VQImVaHlDpiCNc4lasy1lA+bx6TUiVBgZLFg3YHSIUxbL2s1LjB5Sp4w nIovgqr8BZ/G+1QVXT69VzqxpZckKxHwicm4nAZBX4Cv5KHGDU/ekD3HvxAAfQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1619714620; a=rsa-sha256; cv=none; b=YU5GgZghHCZcQm7Bem45BLYpMW0sRC9g2FVk8Sd2H5gFNBlFVtJ4bBZX8zsQRF4yMI7/cg BuTMN+tMgoqORY/ovP2+HclstKjc+sdkmtWsK6zJawa5XtZLfthtr+OXAVkkScqFCKnUWW VNcKwcTOFeEifN50PyL2Ezw7x0GSDGZWzcQlgG0XvguIuKGO51vJ+iHmCSYzBFlWbOgJAO TiQZCDK+MXaq/SD/jWYOMsRrvouzrm+wMlIDteQtqQdcqK9Q50U74/ARVS2At2/fnoJTMT pCTZ7Efr1M6ojAzUjcCLp9eHHkVEZXwP6itA+Wnl5kbPV9mlOvF21F5X4oHfLA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Spam-Score: -1.86 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: 8BDA0260D7 X-Spam-Score: -1.86 X-Migadu-Scanner: scn0.migadu.com X-TUID: FCQgntgFJVEQ This is a multi-part message in MIME format. --------------A0F47987C3F6711F59B5768F Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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. --------------A0F47987C3F6711F59B5768F Content-Type: text/x-patch; charset=UTF-8; name="0001-org-protocol.el-decode-in-query-part-as-space.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-org-protocol.el-decode-in-query-part-as-space.patch" >From 9bacd0761acd14fb5809042d96977bfd3088bee3 Mon Sep 17 00:00:00 2001 From: Max Nikulin 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 --------------A0F47987C3F6711F59B5768F--