From: Lars Ingebrigtsen Date: Tue, 7 Sep 2021 15:41:31 +0000 (+0200) Subject: Fix encoding in browse-url-encode-url X-Git-Tag: emacs-28.0.90~1121 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=8448782b40d0e186eb1cb094e0eea86370330d7e;p=emacs.git Fix encoding in browse-url-encode-url * lisp/net/browse-url.el (browse-url-url-encode-chars): Document what the argument really is, and simplify the implementation (bug#50435). (browse-url-encode-url): Encode spaces. --- diff --git a/lisp/net/browse-url.el b/lisp/net/browse-url.el index 73b8c439f28..b8e1f1bf126 100644 --- a/lisp/net/browse-url.el +++ b/lisp/net/browse-url.el @@ -692,16 +692,11 @@ alist is deprecated. Use `browse-url-handlers' instead.") (defun browse-url-url-encode-chars (text chars) "URL-encode the chars in TEXT that match CHARS. -CHARS is a regexp-like character alternative (e.g., \"[)$]\")." - (let ((encoded-text (copy-sequence text)) - (s 0)) - (while (setq s (string-match chars encoded-text s)) - (setq encoded-text - (replace-match (format "%%%X" - (string-to-char (match-string 0 encoded-text))) - t t encoded-text) - s (1+ s))) - encoded-text)) +CHARS is a regexp that matches a character." + (replace-regexp-in-string chars + (lambda (s) + (format "%%%X" (string-to-char s))) + text)) (defun browse-url-encode-url (url) "Escape annoying characters in URL. @@ -710,7 +705,7 @@ regarding its parameter treatment." ;; FIXME: Is there an actual example of a web browser getting ;; confused? (This used to encode commas, but at least Firefox ;; handles commas correctly and doesn't accept encoded commas.) - (browse-url-url-encode-chars url "[\")$] ")) + (browse-url-url-encode-chars url "[\"()$ ]")) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; URL input diff --git a/test/lisp/net/browse-url-tests.el b/test/lisp/net/browse-url-tests.el index 898bef8513b..4264e03d912 100644 --- a/test/lisp/net/browse-url-tests.el +++ b/test/lisp/net/browse-url-tests.el @@ -68,11 +68,11 @@ (ert-deftest browse-url-tests-encode-url () (should (equal (browse-url-encode-url "") "")) - (should (equal (browse-url-encode-url "a b c") "a b c")) + (should (equal (browse-url-encode-url "a b c") "a%20b%20c")) (should (equal (browse-url-encode-url "\"a\" \"b\"") - "\"a%22\"b\"")) - (should (equal (browse-url-encode-url "(a) (b)") "(a%29(b)")) - (should (equal (browse-url-encode-url "a$ b$") "a%24b$"))) + "%22a%22%20%22b%22")) + (should (equal (browse-url-encode-url "(a) (b)") "%28a%29%20%28b%29")) + (should (equal (browse-url-encode-url "a$ b$") "a%24%20b%24"))) (ert-deftest browse-url-tests-url-at-point () (with-temp-buffer