docs: def removeUnreferencedElements(doc, keepDefs):
      """
      Removes all unreferenced elements except for <svg>, <font>, <metadata>, <title>, and <desc>.
      Also vacuums the defs of any non-referenced renderable elements.
  
      Returns the number of unreferenced elements removed from the document.
      """
      global _num_elements_removed
      num = 0
  
      # Remove certain unreferenced elements outside of defs
      removeTags = ['linearGradient', 'radialGradient', 'pattern']
      identifiedElements = findElementsWithId(doc.documentElement)
      referencedIDs = findReferencedElements(doc.documentElement)
  
      for id in identifiedElements:
          if id not in referencedIDs:
              goner = identifiedElements[id]
              if (goner is not None and goner.nodeName in removeTags
                  and goner.parentNode is not None
                      and goner.parentNode.tagName != 'defs'):
                  goner.parentNode.removeChild(goner)
                  num += 1
                  _num_elements_removed += 1
  
      if not keepDefs:
          # Remove most unreferenced elements inside defs
          defs = doc.documentElement.getElementsByTagName('defs')
          for aDef in defs:
              elemsToRemove = removeUnusedDefs(doc, aDef)
              for elem in elemsToRemove:
                  elem.parentNode.removeChild(elem)
                  _num_elements_removed += 1
                  num += 1
      return num