Skip to content

Allow URLs to be assigned to src property of HTMLImageElement #6440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TimvdLippe opened this issue Mar 3, 2021 · 2 comments
Closed

Allow URLs to be assigned to src property of HTMLImageElement #6440

TimvdLippe opened this issue Mar 3, 2021 · 2 comments

Comments

@TimvdLippe
Copy link
Contributor

Running the following code in Chrome and Firefox will be successful (wasn't able to verify what Safari does):

let image = document.createElement('img');
image.src = new URL("http://google.com");
console.log(image.src);

I initially wanted to report to TypeScript that this doesn't typecheck (it reports Type 'URL' is not assignable to type 'string'.(2322)), until I discovered that the HTML spec itself doesn't allow for URL assignment: https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-src and it is a USVString (https://heycam.github.io/webidl/#idl-USVString). I am not deeply familiar with these contents, but as I understand the terminology, URL assignment would not be allowed.

Can we update the spec to allow for URLs to be assigned to the src property of an HTMLImageElement?

@domenic
Copy link
Member

domenic commented Mar 3, 2021

The HTML spec already allows this. The trick is that the src setter, like all setters on HTML elements, does a conversion (in this case the conversion is named "USVString", which means ToString plus censoring surrogate pairs). Since URL defines a toString() method, it all works as you'd hope.

I'll close this since the HTML spec is fine as-is, but feel free to ask more questions in the closed thread!

@TimvdLippe
Copy link
Contributor Author

Thanks @domenic for the speedy reply and explaining the intent of USVString (TIL)! I filed microsoft/TypeScript#43066 based on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants