all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Richard Stallman <rms@gnu.org>
To: emacs-devel@gnu.org
Subject: [sand@blarg.net: [PATCH] xml-debug-print-internal needs to quote attributes and text]
Date: Mon, 17 Dec 2007 12:22:32 -0500	[thread overview]
Message-ID: <E1J4Jfk-0008EY-V6@fencepost.gnu.org> (raw)

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 . "<bar> & \"<bar>\"")) "<bar> & \"<bar>\"")))

This will insert text into the current buffer.  With the as-shipped
`xml-debug-print' definition, the buffer gets:

  <foo attr="<bar> & "<bar>""><bar> & "<bar>"</foo>

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:

  <foo attr="&lt;bar&gt; &amp; &quot;&lt;bar&gt;&quot;">&lt;bar&gt; &amp; "&lt;bar&gt;"</foo>

The XML specifiction <http://www.w3.org/TR/REC-xml/#syntax> 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 '(("&" . "&amp;")
                              ("<" . "&lt;")
                              (">" . "&gt;")
                              ("\"" . "&quot;"))))
          (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 '(("&" . "&amp;")
                              ("<" . "&lt;")
                              (">" . "&gt;"))))
          (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 -------

             reply	other threads:[~2007-12-17 17:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-17 17:22 Richard Stallman [this message]
2007-12-18  3:31 ` [PATCH] xml-debug-print-internal needs to quote attributes and text Mark A. Hershberger
2007-12-21  0:33   ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1J4Jfk-0008EY-V6@fencepost.gnu.org \
    --to=rms@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.