38 lines
1.4 KiB
Python
38 lines
1.4 KiB
Python
import re,sys,json
|
|
def sels(p):
|
|
css=open(p,encoding='utf-8').read()
|
|
css=re.sub(r'/\*.*?\*/','',css,flags=re.S)
|
|
out={}
|
|
for m in re.finditer(r'\{',css):
|
|
s=m.start(); e=s-1
|
|
# walk back to last boundary
|
|
b=max(css.rfind('}',0,e), css.rfind('{',0,e), css.rfind(';',0,e))
|
|
pre=css[b+1:s].strip()
|
|
if not pre: continue
|
|
if pre.startswith('@'):
|
|
continue # at-rule wrapper, children handled separately
|
|
# body length
|
|
d=1;k=m.end()
|
|
while k<len(css) and d:
|
|
if css[k]=='{':d+=1
|
|
elif css[k]=='}':d-=1
|
|
k+=1
|
|
body=css[m.end():k-1]
|
|
for x in re.split(r',(?![^(]*\))',pre):
|
|
x=x.strip()
|
|
if x: out[x]=out.get(x,0)+len(body)+1
|
|
return out
|
|
a=sels(sys.argv[1]); b=sels(sys.argv[2])
|
|
oa=sorted(k for k in a if k not in b); ob=sorted(k for k in b if k not in a)
|
|
print(f"A={len(a)} B={len(b)} | ONLY_A={len(oa)} ({sum(a[k] for k in oa)} B) ONLY_B={len(ob)} ({sum(b[k] for k in ob)} B)")
|
|
json.dump(oa,open('sel-only-in-distb1.json','w'),ensure_ascii=False,indent=1)
|
|
for k in oa: print(' +',k)
|
|
print('--- only in B ---')
|
|
for k in ob: print(' -',k)
|
|
|
|
same=[k for k in a if k in b]
|
|
d=sum(a[k]-b[k] for k in same)
|
|
print("SHARED selectors:",len(same),"body-byte delta(A-B):",d)
|
|
diffs=[(a[k]-b[k],k) for k in same if a[k]!=b[k]]
|
|
for x,k in sorted(diffs,reverse=True)[:10]: print(" ~",x,k[:70])
|