X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/d75ffb4ed0b2e72a9361a07d16a5c884a9459728..b4da2cbb0d5d5d7315b13f9bf7ad445d948caac2:/lisp/xml.el diff --git a/lisp/xml.el b/lisp/xml.el index 179fdd6b5c..a3d34670bf 100644 --- a/lisp/xml.el +++ b/lisp/xml.el @@ -1,6 +1,6 @@ ;;; xml.el --- XML parser -;; Copyright (C) 2000-2012 Free Software Foundation, Inc. +;; Copyright (C) 2000-2013 Free Software Foundation, Inc. ;; Author: Emmanuel Briot ;; Maintainer: Mark A. Hershberger @@ -200,7 +200,7 @@ See also `xml-get-attribute-or-nil'." ;; [68] EntityRef ::= '&' Name ';' (defconst xml-entity-ref (concat "&" xml-name-re ";")) -(defconst xml-entity-or-char-ref-re (concat "&\\(?:#\\(x\\)?\\([0-9]+\\)\\|\\(" +(defconst xml-entity-or-char-ref-re (concat "&\\(?:#\\(x\\)?\\([0-9a-fA-F]+\\)\\|\\(" xml-name-re "\\)\\);")) ;; [69] PEReference ::= '%' Name ';' @@ -611,7 +611,7 @@ references." xml-validating-parser (error "XML: (Validity) Invalid character reference `%s'" (match-string 0))) - (replace-match (or (string val) xml-undefined-entity) t t)) + (replace-match (if val (string val) xml-undefined-entity) t t)) ;; For an entity reference, search again from the start of ;; the replaced text, since the replacement can contain ;; entity or character references, or markup. @@ -620,7 +620,7 @@ references." (and (null val) xml-validating-parser (error "XML: (Validity) Undefined entity `%s'" ref)) - (replace-match (cdr val) t t) + (replace-match (or (cdr val) xml-undefined-entity) t t) (goto-char (match-beginning 0))) ;; Check for XML bombs. (and xml-entity-expansion-limit @@ -1011,13 +1011,25 @@ The first line is indented with the optional INDENT-STRING." (defalias 'xml-print 'xml-debug-print) (defun xml-escape-string (string) - "Return STRING with entity substitutions made from `xml-entity-alist'." - (mapconcat (lambda (byte) - (let ((char (char-to-string byte))) - (if (rassoc char xml-entity-alist) - (concat "&" (car (rassoc char xml-entity-alist)) ";") - char))) - string "")) + "Convert STRING into a string containing valid XML character data. +Replace occurrences of &<>'\" in STRING with their default XML +entity references (e.g. replace each & with &). + +XML character data must not contain & or < characters, nor the > +character under some circumstances. The XML spec does not impose +restriction on \" or ', but we just substitute for these too +\(as is permitted by the spec)." + (with-temp-buffer + (insert string) + (dolist (substitution '(("&" . "&") + ("<" . "<") + (">" . ">") + ("'" . "'") + ("\"" . """))) + (goto-char (point-min)) + (while (search-forward (car substitution) nil t) + (replace-match (cdr substitution) t t nil))) + (buffer-string))) (defun xml-debug-print-internal (xml indent-string) "Outputs the XML tree in the current buffer.