
#-------------------------------------------------------------------------------
# Import the bits and pieces that we want to test ...

from xml.dom.minidom import getDOMImplementation

#-------------------------------------------------------------------------------
class CopyUserData:
  # Ref: http://www.w3.org/TR/DOM-Level-3-Core/core.html#UserDataHandler
  NODE_CLONED = 1

  def handle(self, operation, key, data, src, dst):
    assert operation == self.NODE_CLONED
    assert key == 'fish'
    assert data == 'meow'
    dst.setUserData(key, data, self)

def test_cloneNode_and_user_data_handlers_on_attributes():
  # Create DOM tree for: <a b=''/> ...
  tree = getDOMImplementation().createDocument(None, 'a', None)
  tree.firstChild.setAttribute('b', '')
  elemA = tree.firstChild
  attrB = elemA.getAttributeNode('b')

  # Set some user data ...
  attrB.setUserData('fish', 'meow', CopyUserData())

  # Check that setUserData() really worked ...
  assert attrB.getUserData('fish') == 'meow'

  # Let's try just cloning the attribute ...
  attrB2 = attrB.cloneNode(0)
  attrB3 = attrB.cloneNode(1)

  # Check that the user-data got copied ...
  assert attrB2.getUserData('fish') == 'meow'
  assert attrB3.getUserData('fish') == 'meow'

  # Let's try cloning the element containing the attribute ...
  elemA4 = elemA.cloneNode(1)
  attrB4 = elemA4.getAttributeNode('b')

  # Check that the user-data got copied ...
  assert attrB4.getUserData('fish') == 'meow'

#-------------------------------------------------------------------------------
# END.

