(root)/
libxml2-2.12.3/
python/
tests/
pushSAXhtml.py
       1  #!/usr/bin/env python3
       2  import sys
       3  import setup_test
       4  import libxml2
       5  
       6  # Memory debug specific
       7  libxml2.debugMemory(1)
       8  
       9  log = ""
      10  
      11  class ESC[4;38;5;81mcallback:
      12      def startDocument(self):
      13          global log
      14          log = log + "startDocument:"
      15  
      16      def endDocument(self):
      17          global log
      18          log = log + "endDocument:"
      19  
      20      def startElement(self, tag, attrs):
      21          global log
      22          log = log + "startElement %s %s:" % (tag, attrs)
      23  
      24      def endElement(self, tag):
      25          global log
      26          log = log + "endElement %s:" % (tag)
      27  
      28      def characters(self, data):
      29          global log
      30          log = log + "characters: %s:" % (data)
      31  
      32      def warning(self, msg):
      33          global log
      34          log = log + "warning: %s:" % (msg)
      35  
      36      def error(self, msg):
      37          global log
      38          log = log + "error: %s:" % (msg)
      39  
      40      def fatalError(self, msg):
      41          global log
      42          log = log + "fatalError: %s:" % (msg)
      43  
      44  handler = callback()
      45  
      46  ctxt = libxml2.htmlCreatePushParser(handler, "<foo", 4, "test.xml")
      47  chunk = " url='tst'>b"
      48  ctxt.htmlParseChunk(chunk, len(chunk), 0)
      49  chunk = "ar</foo>"
      50  ctxt.htmlParseChunk(chunk, len(chunk), 1)
      51  ctxt=None
      52  
      53  reference = """startDocument:startElement html None:startElement body None:startElement foo {'url': 'tst'}:error: Tag foo invalid
      54  :characters: bar:endElement foo:endElement body:endElement html:endDocument:"""
      55  if log != reference:
      56      print("Error got: %s" % log)
      57      print("Exprected: %s" % reference)
      58      sys.exit(1)
      59  
      60  # Memory debug specific
      61  libxml2.cleanupParser()
      62  if libxml2.debugMemory(1) == 0:
      63      print("OK")
      64  else:
      65      print("Memory leak %d bytes" % (libxml2.debugMemory(1)))