Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngSrc, ngSrcset): only interpolate if all expressions are defined #7290

Closed
wants to merge 2 commits into from

Conversation

btford
Copy link
Contributor

@btford btford commented Apr 28, 2014

BREAKING CHANGE

If bar is undefined, before <img src="foo/{{bar}}.jpg"> yields
<img src="foo/.jpg">. With this change, the binding will not set src.

If you want the old behavior, you can do this: <img src="foo/{{bar || ''}}.jpg">.

The same applies for srcset and href.

Closes #6984

@mary-poppins
Copy link

✨ wow ✨

@btford btford added this to the 1.3.0-beta.8 milestone Apr 28, 2014
@btford
Copy link
Contributor Author

btford commented Apr 28, 2014

Ping @IgorMinar @caitp: thoughts?

@caitp
Copy link
Contributor

caitp commented Apr 28, 2014

I'm not sure how I feel about reconstructing the interpolation here, but it seemed like the best way to avoid doing extra work.

I am not too keen on this, a small api change to $interpolate would be easier, I think. Like a 3rd (2nd?) parameter to the returned function that says "allow undefined expression results" or something. Having multiple implementations of interpolation seems like a bad idea to me (IMO)

@btford
Copy link
Contributor Author

btford commented Apr 29, 2014

Right; it's probably useful to expose this behavior on $interpolate for directives in user-land anyway.

I started reimplementing this feature like @caitp described. See:

@btford
Copy link
Contributor Author

btford commented Apr 30, 2014

@tbosch Can you take a look at this? I want to merge it soon~

@btford
Copy link
Contributor Author

btford commented Apr 30, 2014

I resolved with @tbosch to expose this behavior to attr. Refactoring now.

@btford
Copy link
Contributor Author

btford commented May 1, 2014

@IgorMinar @caitp I significantly refactored my implementation and improved tests and docs. I still don't have a good name for this new feature. I'm tentatively calling it allOrNothing.

Can you please take a look again when you have a minute? Thanks!

@caitp
Copy link
Contributor

caitp commented May 1, 2014

It still LGTM, although I'm not totally sure setAllOrNothing() is a great API. I would personally prefer if we had some other notation for this that authors were in control of. But, I'm not sure what that would look like, and it would probably complicate things. So it works for me.

@IgorMinar
Copy link
Contributor

well in that case separators.join('') === '', so no update will happen. that's the desired behavior, no?

@btford
Copy link
Contributor Author

btford commented May 1, 2014

Arg. bad example. Consider /something{{foo}}.jpg where foo === '' versus where foo === undefined. My point is that your suggested change treats undefined the same as ''.

@btford
Copy link
Contributor Author

btford commented May 2, 2014

@IgorMinar updated with the changes we discussed

@btford btford changed the title fix(ngSrc, ngSrcset, ngHref): only interpolate if all expressions are defined fix(ngSrc, ngSrcset): only interpolate if all expressions are defined May 2, 2014
@@ -1856,7 +1863,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

// we need to interpolate again, in case the attribute value has been updated
// (e.g. by another directive's compile function)
interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name));
interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name),
allOrNothingAttrs.indexOf(name) > -1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of indexOf you could make allOrNothingAttrs a map of boolean values. I think we even have a helper method that can turn an array into such map. then you can simply do allOrNothingAttrs[name] and you are done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 any idea what the helper is called/where it lives?

@IgorMinar
Copy link
Contributor

otherwise this looks good

@btford
Copy link
Contributor Author

btford commented May 2, 2014

Made that change; can I land this?

BREAKING CHANGE

If `bar` is `undefined`, before `<img src="foo/{{bar}}.jpg">` yields
`<img src="foo/.jpg">`. With this change, the binding will not set `src`.

If you want the old behavior, you can do this: `<img src="foo/{{bar || ''}}.jpg">`.

The same applies for `srcset` as well.

Closes angular#6984
@btford
Copy link
Contributor Author

btford commented May 2, 2014

Landed as 8d18038.

@btford btford closed this May 2, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ngHref prematurely adds href attribute when string is included as part of the expression
4 participants