def compress(source_or_path): if os.path.exists(source_or_path): content = open(source_or_path, 'rb').read() if hasattr(content, 'decode'): content = content.decode() else: content = source_or_path print(htmlcompressor.compress(content))
def test_join_script(): assert htmlcompressor.compress('''<html> <script>alert(1);</script> <script>alert(2);</script> <script>alert(3);</script> <script async="True" type="text/javascript" src="http://js.ir.la/jquery.js"> </script> </html>''') == ('<html><script>alert(1);alert(2);alert(3)</script>' '<script async src=http://js.ir.la/jquery.js></script></html>')
def test_compressor_single(): assert htmlcompressor.compress( '''<html> <head> <title> Test </title> <script type="text/javascript"> alert(1) </script> <script type="text/javascript"> alert(2); </script> </head> <body> <style type="text/css"> .class { display: none; } </style> </body> </html>''') == ( '<html><head><title>Test</title><script>alert(1);alert(2)</script>' '</head><body><style>.class{display:none}</style></body></html>' )
def test_join_script(): resp = htmlcompressor.compress('''<html> <script> <body> BUG <div> TESTE </div> </body> </html>''') print(resp == '<html><script>') assert resp == ('''<html> <script> <body> BUG <div> TESTE </div> </body> </html>''')
def test_css_inline_compress(): assert htmlcompressor.compress('''<html> <head> <title> Hello Word </title> <script>alert(1)</script> <script>alert(2)</script> <script>alert(3);</script> <script async="True" type="text/javascript" src="http://js.ir.la/jquery;js"> </script> <style TYPE="text/css"> .body { background-color: blue; width: 64px; height: 34px; } </style> <script async="True" type="text/javascript"> alert("Hi"); alert("Hi"); alert("Hi"); </SCRIPT> <script>alert(9999);</script> </head> <body> <div STYLE='background-color: red;'> TESTE </div > <body> </html>''') == ( '<html><head><title>Hello Word</title>' '<script>alert(1);alert(2);alert(3)</script>' '<script async src=http://js.ir.la/jquery;js></script>' '<style>.body{background-color:blue;width:64px;height:34px}</style>' '<script>alert("Hi");alert("Hi");alert("Hi");alert(9999)</script>' '</head><body><div style="background-color:red"> TESTE </div>' '<body></html>')
def test_js_no_comma_at_the_end(): assert htmlcompressor.compress('''<html> <head> <title> Hello Word </title> <script>alert(1)</script> <script>alert(2)</script> <script>alert(3);</script> <script async="True" type="text/javascript" src="http://js.ir.la/jquery;js"> </script> <script async="True" type="text/javascript"> alert("Hi"); alert("Hi"); alert("Hi"); </SCRIPT> <script>alert(9999);</script> </head> </html>''') == ( '<html><head><title>Hello Word</title>' '<script>alert(1);alert(2);alert(3)</script>' '<script async src=http://js.ir.la/jquery;js></script>' '<script>alert("Hi");alert("Hi");alert("Hi");alert(9999)</script>' '</head></html>')