#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import asyncio

from wdom.tag import WebElement


def is_same_node(node1:WebElement, node2:WebElement):
    if node1.nodeType == node2.nodeType:
        if node1.nodeType in (node1.TEXT_NODE, node1.COMMENT_NODE):
            return node1.textContent == node2.textContent
        else:
            return node1.html_noid == node2.html_noid
    else:
        return False


def next_noempty(node:WebElement):
    new_node = node.nextSibling
    while new_node is not None:
        if isinstance(new_node, WebElement):
            return new_node
        else:
            text = new_node.textContent
            if text and not text.isspace():
                return new_node
            new_node = new_node.nextSibling
    return None


@asyncio.coroutine
def find_diff_node(base_node:WebElement, new_node:WebElement):
    _deleted = []
    _inserted = []
    _appended = []

    node1 = base_node.firstChild
    node2 = new_node.firstChild
    last_node2 = node2
    while node1 is not None and last_node2 is not None:  # Loop over old html
        yield from asyncio.sleep(0.0)
        if is_same_node(node1, node2):
            node1 = next_noempty(node1)
            node2 = next_noempty(node2)
            last_node2 = node2
        else:
            _pending = [node2]
            while True:  # Loop over new html
                node2 = next_noempty(node2)
                if node2 is None:
                    _deleted.append(node1)
                    node1 = next_noempty(node1)
                    node2 = last_node2
                    break
                elif is_same_node(node1, node2):
                    for n in _pending:
                        _inserted.append((node1, n))
                    node1 = next_noempty(node1)
                    node2 = next_noempty(node2)
                    last_node2 = node2
                    break
                else:
                    _pending.append(node2)

    if node1 is not None:
        n = node1
        while n is not None:
            _deleted.append(n)
            n = next_noempty(n)
    elif last_node2 is not None:
        n = last_node2
        while n is not None:
            _appended.append(n)
            n = next_noempty(n)

    return {'deleted': _deleted, 'inserted': _inserted,
            'appended': _appended}

@@ -417,28 +417,34 @@
 e%0A%0A%0Adef 
-n
+is_empty_t
 ext_no
+d
 e
-mpty
 (node:We
@@ -462,236 +462,271 @@
     
-new_node = node.nextSibling%0A    while new_node is not None:%0A        if isinstance(new_node, WebElement):%0A            return new_node%0A        else:%0A            text = new_node.textContent%0A            if text and not text.isspace(
+if isinstance(node, WebElement):%0A        return False%0A    text = node.textContent%0A    return not text or text.isspace()%0A%0A%0Adef next_noempty(node:WebElement):%0A    new_node = node.nextSibling%0A    while new_node is not None:%0A        if not is_empty_text_node(new_node
 ):%0A 
@@ -728,36 +728,32 @@
 e):%0A            
- 
-   
 return new_node%0A
@@ -752,16 +752,30 @@
 ew_node%0A
+        else:%0A
         

Add is_empty_text_node