def handle_spaces(text):
    """Chained whitespaces are removed and replaced by a single space.
    Exception when between two containers separated by whitespaces: there is no space.
    Eg. '..._<html>_<head>_...' => '..._<html><head>_...' """
    ret_splitted = text.split(' ')
    ret = list()
    for i in range(len(ret_splitted)):
        e = ret_splitted[i]
        if len(e) > 0:
            if e[0] == '<' and e[-1] == '>' and '<' not in e[1:]:
                if len(ret) > 0:
                    last = ret[-1]
                    ret.pop(-1)
                    ret.append(''.join([last, e]))
                else:
                    ret.append(e)
            else:
                ret.append(e)
    return ' '.join(ret)
