Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Doc/library/xml.sax.utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ or as base classes.
using the reference concrete syntax.


.. function:: is_valid_name(name)

Return ``True`` if the string is a valid element or attribute name,
``False`` otherwise.

Almost all characters are permitted in names, except control characters and
those which either are or reasonably could be used as delimiters.
Characters like ":", "-", ".", "_", and "·" are permitted, but "<", "/",
"!", "?", and "=" are forbidden.
The name cannot start with a digit or a character like "-", ".", and "·".

..versionadded:: next


.. class:: XMLGenerator(out=None, encoding='iso-8859-1', short_empty_elements=False)

This class implements the :class:`~xml.sax.handler.ContentHandler` interface
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,14 @@ xml.parsers.expat
.. _billion laughs: https://en.wikipedia.org/wiki/Billion_laughs_attack


xml.sax.saxutils
----------------

* Add the :func:`~xml.sax.saxutils.is_valid_name` function, which allows to check
whether a string can be used as an element or attribute name in XML.
(Contributed by Serhiy Storchaka in :gh:`139489`.)


zlib
----

Expand Down
22 changes: 20 additions & 2 deletions Lib/test/test_sax.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
except SAXReaderNotAvailable:
# don't try to test this module if we cannot create a parser
raise unittest.SkipTest("no XML parsers available")
from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
XMLFilterBase, prepare_input_source
from xml.sax.saxutils import (XMLGenerator, escape, unescape, quoteattr,
is_valid_name,
XMLFilterBase, prepare_input_source)
from xml.sax.expatreader import create_parser
from xml.sax.handler import (feature_namespaces, feature_external_ges,
LexicalHandler)
Expand Down Expand Up @@ -343,6 +344,23 @@ def test_single_double_quoteattr(self):
self.assertEqual(quoteattr("Includes 'single' and \"double\" quotes"),
"\"Includes 'single' and &quot;double&quot; quotes\"")

def test_is_valid_name(self):
self.assertFalse(is_valid_name(''))
self.assertTrue(is_valid_name('name'))
self.assertTrue(is_valid_name('NAME'))
self.assertTrue(is_valid_name('name0:-._·'))
self.assertTrue(is_valid_name('_'))
self.assertTrue(is_valid_name(':'))
self.assertTrue(is_valid_name('Ñàḿĕ'))
self.assertTrue(is_valid_name('\U000EFFFF'))
self.assertFalse(is_valid_name('0'))
self.assertFalse(is_valid_name('-'))
self.assertFalse(is_valid_name('.'))
self.assertFalse(is_valid_name('·'))
self.assertFalse(is_valid_name('na me'))
for c in '<>/!?=\x00\x01\x7f\ud800\udfff\ufffe\uffff\U000F0000':
self.assertFalse(is_valid_name('name' + c))

# ===== make_parser
def test_make_parser(self):
# Creating a parser should succeed - it should fall back
Expand Down
30 changes: 28 additions & 2 deletions Lib/xml/sax/saxutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
convenience of application and driver writers.
"""

import os, urllib.parse, urllib.request
import io
import codecs
import io
import os
import re
import urllib.parse
import urllib.request
from . import handler
from . import xmlreader

Expand Down Expand Up @@ -67,6 +70,29 @@ def quoteattr(data, entities={}):
data = '"%s"' % data
return data

def is_valid_name(name):
"""Test whether a string is a valid element or attribute name."""
# https://www.w3.org/TR/xml/#NT-Name
return re.fullmatch(
# NameStartChar
'['
':A-Z_a-z'
'\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF'
'\u200C\u200D'
'\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF'
'\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF'
']'
# NameChar
'['
r'\-.0-9:A-Z_a-z'
'\xB7'
'\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF'
'\u200C\u200D\u203F\u2040'
'\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF'
'\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF'
']*',
name) is not None


def _gettextwriter(out, encoding):
if out is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add the :func:`~xml.sax.saxutils.is_valid_name` function, which allows to check
whether a string can be used as an element or attribute name in XML.
Loading