Fix regression with regard to quotes in attribute values
Created by: mfwitten
The following commit removed the escaping of single quotes and double quotes, in order to fix issues #21 (closed) and #56 (closed):
commit 57f93efc
Date: Tue Aug 16 00:10:41 2016 +0200
However, this broke the quoting of an attribute value that has both a single quote and a double quote (see below for an example). This patch series re-introduces the escaping of quotes, but ultimately uses entities only when both a single quote and a double quote are present at the same time in an attribute value; whether single quotes or double quotes are escaped depends on which choice produces the shortest result. Unit tests are included.
The following presents a concrete, real-world example of the problem that is being fixed by this pull request.
$ file='https://upload.wikimedia.org/wikipedia/commons/archive/a/a5/20170825000948%21Example_of_an_iterative_DNS_resolver.svg'
$ curl "$file" >a.svg
$ inkscape a.svg
In inkscape
, select the text object that reads:
"Where's www.wikipedia.org?"
Convert it to a path object (Path > Object to Path
, or press Shift+Ctrl+C
); next, save the file as b.svg
, making sure to choose as the file type Optimized SVG (*.svg)
. Then:
$ grep -n Where b.svg
43: <g aria-label='"Where's www.wikipedia.org?"'>
Clearly, line 43 is wrong; you can check whether it's well formed, using a tool from the expat project:
$ xmlwf b.svg
b.svg:43:24: not well-formed (invalid token)
$ sed -n -e '43{p;q;}' b.svg | cut -c 24-
's www.wikipedia.org?"'>
Indeed, try opening the b.svg
in a browser; firefox 55.0.1
produces the following output (it even identifies a better character for the start of the error):
XML Parsing Error: not well-formed
Location: file:///tmp/b.svg
Line Number 43, Column 25:
<g aria-label='"Where's www.wikipedia.org?"'>
------------------------^
The browser of Chrome OS 59.0.3071.134
produces:
This page contains the following errors:
error on line 43 at column 25: attributes construct error
Below is a rendering of the page up to the first error.
This pull request fixes the problem.