dom1core.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. __license__ = '''
  2. This file is part of Dominate.
  3. Dominate is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation, either version 3 of
  6. the License, or (at your option) any later version.
  7. Dominate is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General
  12. Public License along with Dominate. If not, see
  13. <http://www.gnu.org/licenses/>.
  14. '''
  15. try:
  16. basestring = basestring
  17. except NameError: # py3
  18. basestring = str
  19. unicode = str
  20. class dom1core(object):
  21. '''
  22. Implements the Document Object Model (Core) Level 1
  23. http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/
  24. http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html
  25. '''
  26. @property
  27. def parentNode(self):
  28. '''
  29. DOM API: Returns the parent tag of the current element.
  30. '''
  31. return self.parent
  32. def getElementById(self, id):
  33. '''
  34. DOM API: Returns single element with matching id value.
  35. '''
  36. results = self.get(id=id)
  37. if len(results) > 1:
  38. raise ValueError('Multiple tags with id "%s".' % id)
  39. elif results:
  40. return results[0]
  41. else:
  42. return None
  43. def getElementsByTagName(self, name):
  44. '''
  45. DOM API: Returns all tags that match name.
  46. '''
  47. if isinstance(name, basestring):
  48. return self.get(name.lower())
  49. else:
  50. return None
  51. def appendChild(self, obj):
  52. '''
  53. DOM API: Add an item to the end of the children list.
  54. '''
  55. self.add(obj)
  56. return self