minidom.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # defusedxml
  2. #
  3. # Copyright (c) 2013 by Christian Heimes <christian@python.org>
  4. # Licensed to PSF under a Contributor Agreement.
  5. # See https://www.python.org/psf/license for licensing details.
  6. """Defused xml.dom.minidom
  7. """
  8. from __future__ import print_function, absolute_import
  9. from xml.dom.minidom import _do_pulldom_parse
  10. from . import expatbuilder as _expatbuilder
  11. from . import pulldom as _pulldom
  12. __origin__ = "xml.dom.minidom"
  13. def parse(
  14. file, parser=None, bufsize=None, forbid_dtd=False, forbid_entities=True, forbid_external=True
  15. ):
  16. """Parse a file into a DOM by filename or file object."""
  17. if parser is None and not bufsize:
  18. return _expatbuilder.parse(
  19. file,
  20. forbid_dtd=forbid_dtd,
  21. forbid_entities=forbid_entities,
  22. forbid_external=forbid_external,
  23. )
  24. else:
  25. return _do_pulldom_parse(
  26. _pulldom.parse,
  27. (file,),
  28. {
  29. "parser": parser,
  30. "bufsize": bufsize,
  31. "forbid_dtd": forbid_dtd,
  32. "forbid_entities": forbid_entities,
  33. "forbid_external": forbid_external,
  34. },
  35. )
  36. def parseString(
  37. string, parser=None, forbid_dtd=False, forbid_entities=True, forbid_external=True
  38. ):
  39. """Parse a file into a DOM from a string."""
  40. if parser is None:
  41. return _expatbuilder.parseString(
  42. string,
  43. forbid_dtd=forbid_dtd,
  44. forbid_entities=forbid_entities,
  45. forbid_external=forbid_external,
  46. )
  47. else:
  48. return _do_pulldom_parse(
  49. _pulldom.parseString,
  50. (string,),
  51. {
  52. "parser": parser,
  53. "forbid_dtd": forbid_dtd,
  54. "forbid_entities": forbid_entities,
  55. "forbid_external": forbid_external,
  56. },
  57. )