Improve filename detection for attachments#2844
Conversation
|
I modified the new test into import weasyprint
import base64
data = base64.encodebytes(b'Weasyprint!\n').decode("utf-8")
html = f'''
<title>Test document</title>
<meta charset="utf-8">
<a
rel="attachment"
href="data:text/plain;base64,{data}"
download>A link that lets you download an attachment</a>
'''
weasyprint.HTML(string=html).write_pdf('test_data_url_with_line_break.pdf') Then there is no attachment. I think it is the line break at the end of <title>Test document</title>
<meta charset="utf-8">
<a
rel="attachment"
href="data:text/plain;base64,V2Vhc3lwcmludCEK
"
download>A link that lets you download an attachment</a>This works with 69.0. |
Oh, you’re right, thanks for the report. That’s because of other, unrelated changes in current main branch. |
That’s another tricky rule of HTML parsing: href attributes are "valid URLs potentially surrounded by spaces." Note that other attributes like id or lang don’t accept these spaces. Related to #2844.
aa7c82d to
7c6e35d
Compare
|
@wtschueller I updated the main branch and this pull request to handle these spaces correctly and test this case to avoid regressions. Everything should work now, thanks a lot for the report! |
|
I think there is still some odd behaviour: import weasyprint
import base64
data = base64.encodebytes(b'Weasyprint!\n'*5).decode("utf-8")
html = f'''
<title>Test document</title>
<meta charset="utf-8">
<a
rel="attachment"
href="data:text/plain;base64,{data}"
download>A link that lets you download an attachment</a>
'''
weasyprint.HTML(string=html).write_pdf('test_data_url_with_inner_line_break.pdf')produces an inner line break <title>Test document</title>
<meta charset="utf-8">
<a
rel="attachment"
href="data:text/plain;base64,V2Vhc3lwcmludCEKV2Vhc3lwcmludCEKV2Vhc3lwcmludCEKV2Vhc3lwcmludCEKV2Vhc3lwcmlu
dCEK
"
download>A link that lets you download an attachment</a>and then there is still no attachment for download. If I use the same procedure for |
|
I learned so many details thanks to this example, thanks a lot… In No. From the URL specification:
So, if a URL contains a newline, it should be an error, but we should remove the newline to give this poor URL a chance to be valid. It works in WeasyPrint for non-href attributes (href puts the link in CSS, that’s a hack that fails for all newlines), but only for base64 URLs, because Python’s function allows newlines in the base64 strings. I’ll open a new issue to allow URLs to be surrounded by spaces and to ignore ASCII tab and newline characters in them. |
|
Merging, as the remaining problems are in #2851, to be solved before next version. Thanks again for testing and reporting! |
Fix #1862.