From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: sand@blarg.net Newsgroups: gmane.emacs.bugs Subject: [PATCH] xml-debug-print-internal needs to quote attributes and text Date: 17 Dec 2007 04:28:47 -0000 Message-ID: <20071217042847.31231.qmail@priss.frightenedpiglet.com> NNTP-Posting-Host: lo.gmane.org X-Trace: ger.gmane.org 1197865765 29587 80.91.229.12 (17 Dec 2007 04:29:25 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 17 Dec 2007 04:29:25 +0000 (UTC) To: bug-gnu-emacs@gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Dec 17 05:29:36 2007 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1J47bh-0008I8-Oz for geb-bug-gnu-emacs@m.gmane.org; Mon, 17 Dec 2007 05:29:36 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J47bM-000291-5W for geb-bug-gnu-emacs@m.gmane.org; Sun, 16 Dec 2007 23:29:12 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J47bG-00026U-7P for bug-gnu-emacs@gnu.org; Sun, 16 Dec 2007 23:29:06 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J47bB-00021V-PC for bug-gnu-emacs@gnu.org; Sun, 16 Dec 2007 23:29:05 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J47bB-00021G-IL for bug-gnu-emacs@gnu.org; Sun, 16 Dec 2007 23:29:01 -0500 Original-Received: from v-static-143-234.avvanta.com ([206.124.143.234] helo=priss.frightenedpiglet.com) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1J47b9-00074b-EX for bug-gnu-emacs@gnu.org; Sun, 16 Dec 2007 23:29:01 -0500 Original-Received: (qmail 31232 invoked by uid 1000); 17 Dec 2007 04:28:47 -0000 X-URL: http://home.blarg.net/~sand X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 2) X-BeenThere: bug-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:17174 Archived-At: 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!"