From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Richard Stallman Newsgroups: gmane.emacs.devel Subject: [sand@blarg.net: [PATCH] xml-debug-print-internal needs to quote attributes and text] Date: Mon, 17 Dec 2007 12:22:32 -0500 Message-ID: Reply-To: rms@gnu.org NNTP-Posting-Host: lo.gmane.org Content-Type: text/plain; charset=ISO-8859-15 X-Trace: ger.gmane.org 1197912254 12906 80.91.229.12 (17 Dec 2007 17:24:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 17 Dec 2007 17:24:14 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Dec 17 18:24:25 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1J4JgF-0005ve-RJ for ged-emacs-devel@m.gmane.org; Mon, 17 Dec 2007 18:23:04 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J4Jfv-0003X1-NL for ged-emacs-devel@m.gmane.org; Mon, 17 Dec 2007 12:22:43 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J4Jfr-0003VQ-JN for emacs-devel@gnu.org; Mon, 17 Dec 2007 12:22:39 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J4Jfm-0003U9-IU for emacs-devel@gnu.org; Mon, 17 Dec 2007 12:22:38 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J4Jfm-0003U5-CX for emacs-devel@gnu.org; Mon, 17 Dec 2007 12:22:34 -0500 Original-Received: from fencepost.gnu.org ([140.186.70.10]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1J4Jfm-0003Ue-3K for emacs-devel@gnu.org; Mon, 17 Dec 2007 12:22:34 -0500 Original-Received: from rms by fencepost.gnu.org with local (Exim 4.60) (envelope-from ) id 1J4Jfk-0008EY-V6; Mon, 17 Dec 2007 12:22:33 -0500 X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:85226 Archived-At: Would someone please DTRT and ack? ------- Start of forwarded message ------- X-Spam-Status: No, score=0.6 required=5.0 tests=NO_REAL_NAME,SPF_PASS, UNPARSEABLE_RELAY autolearn=no version=3.1.0 Date: 17 Dec 2007 04:28:47 -0000 Message-ID: <20071217042847.31231.qmail@priss.frightenedpiglet.com> From: sand@blarg.net To: bug-gnu-emacs@gnu.org Subject: [PATCH] xml-debug-print-internal needs to quote attributes and text Load xml.el to define `xml-debug-print' and evaluate the following: (xml-debug-print '((foo ((attr . " & \"\"")) " & \"\""))) This will insert text into the current buffer. With the as-shipped `xml-debug-print' definition, the buffer gets: ""> & "" This is not legal XML. We have legal XML if we escape greater-than, less-than and ampersand in the attribute value and in the content, and escape quote in the attribute value: <bar> & "<bar>" The XML specifiction allows some leeway in the exact behavior, but the above substitutions are compliant. In particular, we do not escape apostrophes in the attribute value, since the code never quotes attribute values using apostrophes. The following redefinition of `xml-debug-print-internal' performs regexp substitution for each of the quotable characters. Note that the replacement lists are different for the two cases. (defun xml-debug-print-internal (xml indent-string) "Outputs the XML tree in the current buffer. The first line is indented with INDENT-STRING." (let ((tree xml) attlist) (insert indent-string ?< (symbol-name (xml-node-name tree))) ;; output the attribute list (setq attlist (xml-node-attributes tree)) (while attlist (let ((value (cdar attlist)) (replacements '(("&" . "&") ("<" . "<") (">" . ">") ("\"" . """)))) (while replacements (setq value (replace-regexp-in-string (caar replacements) (cdar replacements) value)) (setq replacements (cdr replacements))) (insert ?\ (symbol-name (caar attlist)) "=\"" value ?\")) (setq attlist (cdr attlist))) (setq tree (xml-node-children tree)) (if (null tree) (insert ?/ ?>) (insert ?>) ;; output the children (dolist (node tree) (cond ((listp node) (insert ?\n) (xml-debug-print-internal node (concat indent-string " "))) ((stringp node) (let ((replacements '(("&" . "&") ("<" . "<") (">" . ">")))) (while replacements (setq node (replace-regexp-in-string (caar replacements) (cdar replacements) node)) (setq replacements (cdr replacements))) (insert node))) (t (error "Invalid XML tree")))) (when (not (and (null (cdr tree)) (stringp (car tree)))) (insert ?\n indent-string)) (insert ?< ?/ (symbol-name (xml-node-name xml)) ?>)))) In GNU Emacs 22.1.1 (i486-pc-linux-gnu, GTK+ Version 2.12.1) of 2007-11-03 on pacem, modified by Debian Windowing system distributor `The X.Org Foundation', version 11.0.10400000 configured using `configure '--build=i486-linux-gnu' '--host=i486-linux-gnu' '--prefix=/usr' '--sharedstatedir=/var/lib' '--libexecdir=/usr/lib' '--localstatedir=/var/lib' '--infodir=/usr/share/info' '--mandir=/usr/share/man' '--with-pop=yes' '--enable-locallisppath=/etc/emacs22:/etc/emacs:/usr/local/share/emacs/22.1/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/22.1/site-lisp:/usr/share/emacs/site-lisp:/usr/share/emacs/22.1/leim' '--with-x=yes' '--with-x-toolkit=gtk' '--with-toolkit-scroll-bars' 'build_alias=i486-linux-gnu' 'host_alias=i486-linux-gnu' 'CFLAGS=-DDEBIAN -g -O2'' Derek - -- Derek Upham sand@blarg.net "Ha! Your Leaping Tiger Kung Fu is no match for my Frightened Piglet style!" ------- End of forwarded message -------