Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Pygments / tests / support / structural_diff.py
Size: Mime:
import lxml.html
import lxml.etree
import pytest

def _serialize(t):
    for a, e in lxml.etree.iterwalk(t, events=("start", "end"),):
        text = e.text.strip() if e.text else ""
        yield (a, e.tag, repr(text), ', '.join([k[0]+':'+k[1] for k in sorted(e.attrib.items(), key = lambda x: x[0])]))

def structural_diff(a, b):
    """Check if there is a structural difference between two HTML files."""
    a_s = _serialize(lxml.html.fromstring(a))
    b_s = _serialize(lxml.html.fromstring(b))
    
    for e, f in zip(a_s, b_s):
        print(e, f)
        assert e == f