Skip to content

Commit

Permalink
fix safari issue
Browse files Browse the repository at this point in the history
  • Loading branch information
eyaler committed Jul 6, 2022
1 parent 0ed87e8 commit e66574a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,4 @@ ZTML pipeline:

*Automatic capitalization recovery is currently partial.

Note: If the decoding is mangled on Safari, this may be due to serving JS with gzip content encoding.
Upgrade to a modern browser, or try adding in the server's .htaccess:
```
BrowserMatch Safari gzip-only-text/html
```
Note: files larger than a few MB may not work on [iOS Safari](https://pqina.nl/blog/canvas-area-exceeds-the-maximum-limit) or [macOS Safary 15](https://bugs.webkit.org/show_bug.cgi?id=230855)
5 changes: 3 additions & 2 deletions ztml/deflate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
https://github.com/google/zopfli
https://github.com/hattya/zopflipy
https://github.com/jhildenbiddle/canvas-size#test-results
https://pqina.nl/blog/canvas-area-exceeds-the-maximum-limit
https://bugs.webkit.org/show_bug.cgi?id=230855
"""


Expand Down Expand Up @@ -73,7 +75,7 @@ def get_js_create_image(bytearray_name: str = default_names.bytearray,
image_name: str = default_names.image
) -> str:
return '''IMAGE_NAME=new Image
IMAGE_NAME.src=URL.createObjectURL(new Blob([BYTEARRAY_NAME],{type:'image/png'}))
IMAGE_NAME.src=URL.createObjectURL(new Blob([BYTEARRAY_NAME]))
'''.replace('IMAGE_NAME', image_name).replace('BYTEARRAY_NAME', bytearray_name)


Expand All @@ -85,7 +87,6 @@ def get_js_image_data(after_script: str = '',
c=document.createElement('canvas')
x=c.getContext('2d')
c=[c.width,c.height]=[IMAGE_NAME.width,IMAGE_NAME.height]
x.imageSmoothingEnabled=0
x.drawImage(IMAGE_NAME,0,0)
BITARRAY_NAME=x.getImageData(0,0,...c).data
AFTER_SCRIPT})'''.replace('AFTER_SCRIPT', after_script.strip()).replace('IMAGE_NAME', image_name).replace('BITARRAY_NAME', bitarray_name)
Expand Down
8 changes: 4 additions & 4 deletions ztml/huffman.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def get_js_decoder(charset: str,
) -> str:
return '''s=CHARSET
d=CANONICAL_TABLE
for(j=0,TEXT_NAME='';j<BITARRAY_NAME.length;TEXT_NAME+=s[d[k][1]+m])for(c='',k=-1;!((m=d[++k]?.[0]-parseInt(c,2))>=0);j+=4)c+=BITARRAY_NAME[j]&1
'''.replace('CHARSET', charset).replace('CANONICAL_TABLE', canonical_table).replace('BITARRAY_NAME', bitarray_name).replace('TEXT_NAME', text_name)
for(j=0,TEXT_NAME='';j<BITARRAY_NAME.length;TEXT_NAME+=s[d[k][1]+m])for(c='',k=-1;!((m=d[++k]?.[0]-parseInt(c,2))>=0);j+=4)c+=BITARRAY_NAME[j]>>
'''.replace('CHARSET', charset).replace('CANONICAL_TABLE', canonical_table).replace('BITARRAY_NAME', bitarray_name).replace('TEXT_NAME', text_name) # note using >>7 instead of &1 to deal with safari rendering inaccduracy


def get_legacy_js_decoder(lengths: str,
Expand All @@ -84,8 +84,8 @@ def get_legacy_js_decoder(lengths: str,
return '''s=LENGTHS
d={}
for(j=0,c='';j<s.length;j+=2)c+='0'.repeat(parseInt(s[j],36)),d[c]=s[j+1],c=(parseInt(c,2)+1).toString(2).padStart(c.length,0)
for(j=0,c=TEXT_NAME='';j<BITARRAY_NAME.length;j+=4)(c+=BITARRAY_NAME[j]&1)in d&&(TEXT_NAME+=d[c],c='')
'''.replace('LENGTHS', lengths).replace('BITARRAY_NAME', bitarray_name).replace('TEXT_NAME', text_name)
for(j=0,c=TEXT_NAME='';j<BITARRAY_NAME.length;j+=4)(c+=BITARRAY_NAME[j]>>7)in d&&(TEXT_NAME+=d[c],c='')
'''.replace('LENGTHS', lengths).replace('BITARRAY_NAME', bitarray_name).replace('TEXT_NAME', text_name) # note using >>7 instead of &1 to deal with safari rendering inaccduracy


def encode_and_get_js_decoder(text: str,
Expand Down
10 changes: 7 additions & 3 deletions ztml/validate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import os
import re
import regex
from time import time
from typing import Optional

Expand Down Expand Up @@ -46,6 +46,7 @@ def validate_html(filename: str,
compare_caps: bool = True,
eos: str = text_utils.default_eos,
ignore_regex: str = '',
unicode_A: int = 0,
browser: str = default_browsers[0],
timeout: int = default_timeout,
element_name: str = default_element_name,
Expand All @@ -58,7 +59,9 @@ def validate_html(filename: str,
render = render.lower()
text = text.lower()
text = text.rstrip(eos)
render = re.sub(ignore_regex, '', render)
render = regex.sub(ignore_regex, '', render)
if unicode_A:
render = regex.sub('[^\\p{Z}\\p{C}]', lambda m: chr(ord(m[0]) - unicode_A + 65 + (6 if ord(m[0]) - unicode_A + 65 > 90 else 0)), render)
if render == text:
return True
if verbose:
Expand All @@ -83,6 +86,7 @@ def validate_files(filenames: dict[str, str],
compare_caps: bool = True,
eos: str = text_utils.default_eos,
ignore_regex: str = '',
unicode_A: int = 0,
element_name: str = default_element_name,
browsers: Optional[list[str]] = None,
timeout: int = default_timeout,
Expand Down Expand Up @@ -124,7 +128,7 @@ def validate_files(filenames: dict[str, str],
times = ''
for browser in browsers:
start_time = time()
valid = validate_html(filename, text, compare_caps, eos, ignore_regex, browser, timeout, element_name, verbose)
valid = validate_html(filename, text, compare_caps, eos, ignore_regex, unicode_A, browser, timeout, element_name, verbose)
assert valid is not False, filename
times += f' {browser}=' + (f'{time() - start_time :.1f}' if valid else f'{timeout}(timeout)')
if verbose:
Expand Down

0 comments on commit e66574a

Please sign in to comment.