tests: Add unit tests for the escaping of quote characters in attribute values
This commit is contained in:
parent
b007d75d1c
commit
b3fe88ca58
3 changed files with 60 additions and 7 deletions
|
|
@ -647,7 +647,7 @@ def removeUnusedDefs(doc, defElem, elemsToRemove=None):
|
||||||
for elem in defElem.childNodes:
|
for elem in defElem.childNodes:
|
||||||
# only look at it if an element and not referenced anywhere else
|
# only look at it if an element and not referenced anywhere else
|
||||||
if elem.nodeType == Node.ELEMENT_NODE and (elem.getAttribute('id') == '' or
|
if elem.nodeType == Node.ELEMENT_NODE and (elem.getAttribute('id') == '' or
|
||||||
elem.getAttribute('id') not in referencedIDs):
|
elem.getAttribute('id') not in referencedIDs):
|
||||||
# we only inspect the children of a group in a defs if the group
|
# we only inspect the children of a group in a defs if the group
|
||||||
# is not referenced anywhere else
|
# is not referenced anywhere else
|
||||||
if elem.nodeName == 'g' and elem.namespaceURI == NS['SVG']:
|
if elem.nodeName == 'g' and elem.namespaceURI == NS['SVG']:
|
||||||
|
|
@ -3597,9 +3597,7 @@ def scourString(in_string, options=None):
|
||||||
|
|
||||||
|
|
||||||
# used mostly by unit tests
|
# used mostly by unit tests
|
||||||
# input is a filename
|
def scourXmlFileAndReturnString(filename, options=None):
|
||||||
# returns the minidom doc representation of the SVG
|
|
||||||
def scourXmlFile(filename, options=None):
|
|
||||||
# sanitize options (take missing attributes from defaults, discard unknown attributes)
|
# sanitize options (take missing attributes from defaults, discard unknown attributes)
|
||||||
options = sanitizeOptions(options)
|
options = sanitizeOptions(options)
|
||||||
# we need to make sure infilename is set correctly (otherwise relative references in the SVG won't work)
|
# we need to make sure infilename is set correctly (otherwise relative references in the SVG won't work)
|
||||||
|
|
@ -3608,7 +3606,14 @@ def scourXmlFile(filename, options=None):
|
||||||
# open the file and scour it
|
# open the file and scour it
|
||||||
with open(filename, "rb") as f:
|
with open(filename, "rb") as f:
|
||||||
in_string = f.read()
|
in_string = f.read()
|
||||||
out_string = scourString(in_string, options)
|
|
||||||
|
return scourString(in_string, options)
|
||||||
|
|
||||||
|
|
||||||
|
# used mostly by unit tests
|
||||||
|
# returns the minidom doc representation of the SVG
|
||||||
|
def scourXmlFile(filename, options=None):
|
||||||
|
out_string = scourXmlFileAndReturnString(filename, options)
|
||||||
|
|
||||||
# prepare the output xml.dom.minidom object
|
# prepare the output xml.dom.minidom object
|
||||||
doc = xml.dom.minidom.parseString(out_string.encode('utf-8'))
|
doc = xml.dom.minidom.parseString(out_string.encode('utf-8'))
|
||||||
|
|
|
||||||
44
testscour.py
44
testscour.py
|
|
@ -30,7 +30,12 @@ import unittest
|
||||||
import six
|
import six
|
||||||
from six.moves import map, range
|
from six.moves import map, range
|
||||||
|
|
||||||
from scour.scour import makeWellFormed, parse_args, scourString, scourXmlFile, start, run
|
from scour.scour import (
|
||||||
|
makeWellFormed, parse_args, scourString,
|
||||||
|
scourXmlFileAndReturnString, scourXmlFile,
|
||||||
|
start, run
|
||||||
|
)
|
||||||
|
|
||||||
from scour.svg_regex import svg_parser
|
from scour.svg_regex import svg_parser
|
||||||
from scour import __version__
|
from scour import __version__
|
||||||
|
|
||||||
|
|
@ -1779,7 +1784,42 @@ class XmlEntities(unittest.TestCase):
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
self.assertEqual(makeWellFormed('<>&'), '<>&',
|
self.assertEqual(makeWellFormed('<>&'), '<>&',
|
||||||
'Incorrectly translated XML entities')
|
'Incorrectly translated unquoted XML entities')
|
||||||
|
self.assertEqual(makeWellFormed('<>&', "'"), '<>&',
|
||||||
|
'Incorrectly translated single-quoted XML entities')
|
||||||
|
self.assertEqual(makeWellFormed('<>&', '"'), '<>&',
|
||||||
|
'Incorrectly translated double-quoted XML entities')
|
||||||
|
|
||||||
|
self.assertEqual(makeWellFormed("'"), "'",
|
||||||
|
'Incorrectly translated unquoted single quote')
|
||||||
|
self.assertEqual(makeWellFormed('"'), '"',
|
||||||
|
'Incorrectly translated unquoted double quote')
|
||||||
|
|
||||||
|
self.assertEqual(makeWellFormed("'", '"'), "'",
|
||||||
|
'Incorrectly translated double-quoted single quote')
|
||||||
|
self.assertEqual(makeWellFormed('"', "'"), '"',
|
||||||
|
'Incorrectly translated single-quoted double quote')
|
||||||
|
|
||||||
|
self.assertEqual(makeWellFormed("'", "'"), ''',
|
||||||
|
'Incorrectly translated single-quoted single quote')
|
||||||
|
self.assertEqual(makeWellFormed('"', '"'), '"',
|
||||||
|
'Incorrectly translated double-quoted double quote')
|
||||||
|
|
||||||
|
|
||||||
|
class HandleQuotesInAttributes(unittest.TestCase):
|
||||||
|
|
||||||
|
def runTest(self):
|
||||||
|
output = scourXmlFileAndReturnString('unittests/entities.svg')
|
||||||
|
self.assertTrue('a="\'"' in output,
|
||||||
|
'Failed on attribute value with non-double quote')
|
||||||
|
self.assertTrue("b='\"'" in output,
|
||||||
|
'Failed on attribute value with non-single quote')
|
||||||
|
self.assertTrue("c=\"''"\"" in output,
|
||||||
|
'Failed on attribute value with more single quotes than double quotes')
|
||||||
|
self.assertTrue('d=\'""'\'' in output,
|
||||||
|
'Failed on attribute value with more double quotes than single quotes')
|
||||||
|
self.assertTrue("e=\"''""\"" in output,
|
||||||
|
'Failed on attribute value with the same number of double quotes as single quotes')
|
||||||
|
|
||||||
|
|
||||||
class DoNotStripCommentsOutsideOfRoot(unittest.TestCase):
|
class DoNotStripCommentsOutsideOfRoot(unittest.TestCase):
|
||||||
|
|
|
||||||
8
unittests/entities.svg
Normal file
8
unittests/entities.svg
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg"
|
||||||
|
a="'"
|
||||||
|
b='"'
|
||||||
|
c="''""
|
||||||
|
d='""''
|
||||||
|
e='''""'
|
||||||
|
/>
|
||||||
|
After Width: | Height: | Size: 144 B |
Loading…
Add table
Add a link
Reference in a new issue