html-to-docx icon indicating copy to clipboard operation
html-to-docx copied to clipboard

Safari : Invalid regular expression: invalid group specifier name (image-to-base64)

Open zedtux opened this issue 2 years ago • 14 comments

I finally integrated well this library to my React app but trying it in Safari show me a blank page and there is this error in Safari's console :

Screenshot 2022-12-13 at 21 10 40

Like this SO response tells Safari doesn't support lookbehind (that is, the (?<=/)).

I finally found which dependency has the breaking regex and it is image-to-base64, which is now archived since Nov 9, 2022, and you added it to html-to-docx in this commit.

Second, the isValidUrl helper function from the src/utils/url.js is using the same lookbehind feature, breaking Safari.

zedtux avatar Dec 13 '22 20:12 zedtux

I found this fork which has fixed the issue, with an npm package published, not sure if it can be used.

zedtux avatar Dec 13 '22 20:12 zedtux

@privateOmega do you have an input on this issue please ?

zedtux avatar Dec 20 '22 07:12 zedtux

@privateOmega I'm interested in hearing your take on this too.

tasola avatar Jan 17 '23 09:01 tasola

@zedtux Lookbehind in regex seems to finally be on it's way to get implemented in Safari: WebKit bug report, PR

tasola avatar Jan 17 '23 10:01 tasola

@zedtux Sorry for the delay in getting to this issue so late.

@tasola Thanks for the notification message.

Since this isnt something that really breaks the usage for a majority of users, I am not sure if we should undertake the effort to fix this.

Also I am super bad at regex and would appreciate if one of you could validate if the changes made in the fork is good, I will take your word for it, and I can either raise a PR to use the fork or can merge if either of you raise it.

privateOmega avatar Jan 17 '23 19:01 privateOmega

@privateOmega Fair. I don't think it's a minor problem that the usage of this package doesn't work on one of the major browsers though... However I agree that it's probably not worth undertaking the effort (at least not for me), seeing how it might get fixed once Safari releases the PR linked above.

I just want to add: Good job developing this project, Kiran (and contributors)!

tasola avatar Jan 18 '23 08:01 tasola

I am using this package and I downgraded to 1.4.0 (npm install [email protected]) and it at least gets Safari to load and I was able to create a docx file. I haven't tested full functionality, but for now downgrading seemed to help.

bryaneaton13 avatar Jan 20 '23 14:01 bryaneaton13

@bryaneaton13 Thanks, it seems to work good for me too. And I can't notice any difference in functionality as far as my use case goes. @privateOmega, do you know any risks with running 1.4.0 right off the bat?

tasola avatar Jan 23 '23 10:01 tasola

Yeah ok I found one; https://github.com/privateOmega/html-to-docx/issues/41 seems to be happening to me when running 1.4.0 unfortunately...

tasola avatar Jan 24 '23 14:01 tasola

I ended up using 1.4.0, but iterating through each image and replace it with its wrapping <p> using :

const parentNode = images[i].parentNode as HTMLElement | undefined
parentNode?.replaceWith(images[i])

Far from optimal of course, but I'm very limited on time at the moment.

tasola avatar Jan 24 '23 14:01 tasola

I'm using patch-package in my project which allows me to patches node_modules/ libraries.

I used it to patch this lib in order to "fix" the regex breaking Safari. Would you like me to share it with you @privateOmega ?

zedtux avatar Jan 24 '23 20:01 zedtux

@tasola @zedtux Yes going back to 1.4.0 will render you without many functionalities and fixes like nested images, cannot use url in image src etc.

privateOmega avatar Jan 26 '23 06:01 privateOmega

Fixed the issue in a fork if anyone is interested: https://github.com/corwinstephen/fixed-html-to-docx

Two bad regexes, one in image-to-base64 and the other in utils/url.js. This switches to the fixed base64 package mentioned above and swaps the URL regex out for a different one.

corwinstephen avatar Feb 04 '23 05:02 corwinstephen

@privateOmega is there a fix for the regex in prospect? Same problem for me here.

xsliii avatar Feb 27 '23 14:02 xsliii