Fix lang/web's html entity encode/decode functions

Also, add unit tests and don't encode spaces.
This commit is contained in:
Henrik Lissner 2017-07-03 02:57:31 +02:00
parent 0f6884f9d6
commit 7477546892
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 26 additions and 8 deletions

View file

@ -16,12 +16,14 @@ character.")
"HTML encode/decode TEXT. Based on Xah's replace HTML named entities function
@ http://ergoemacs.org/emacs/elisp_replace_html_entities_command.html"
(interactive "<!><r>")
(dolist (rep +web-entities-list text)
(seq-doseq (rep +web-entities-list)
(let ((from (elt rep (if decode-p 0 1)))
(to (elt rep (if decode-p 1 0)))
(case-fold-search t))
(when (string-match-p (regexp-quote from) text)
(setq text (s-replace from to text))))))
case-fold-search)
(when (and (not (equal from " "))
(string-match-p (regexp-quote from) text))
(setq text (s-replace from to text)))))
text)
(defun +web--entities-region (beg end &optional decode-p)
"HTML encode/decode the selected region. Based on Xah's replace HTML named entities
@ -29,10 +31,13 @@ function @ http://ergoemacs.org/emacs/elisp_replace_html_entities_command.html"
(save-restriction
(narrow-to-region beg end)
(let (case-fold-search)
(dolist (rep reps)
(goto-char (point-min))
(while (search-forward (elt rep (if decode-p 0 1)) nil t)
(replace-match (elt rep (if decode-p 1 0)) 'FIXEDCASE 'LITERAL))))))
(seq-doseq (rep +web-entities-list)
(let ((from (elt rep (if decode-p 0 1)))
(to (elt rep (if decode-p 1 0))))
(unless (equal from " ")
(goto-char (point-min))
(while (search-forward from nil t)
(replace-match to 'FIXEDCASE 'LITERAL))))))))
;;;###autoload
(defun +web-encode-entities (text)