Commit graph

245 commits

Author SHA1 Message Date
Andrew Kushnir
bdd4d14db9 refactor(common): drop deprecated selector from the NgOptimizedImage directive (#47798)
This commit updates the NgOptimizedImage directive to:
- drop a deprecated selector (the `rawSrc` one)
- drop corresponding input getter

The `rawSrc` was replaced by the `ngSrc` one during the developer preview phase.

PR Close #47798
2022-10-19 15:23:03 +02:00
Kara Erickson
ce5880f93f fix(common): warn if using supported CDN but not built-in loader (#47330)
This commit adds a missing warning if the image directive
detects that you're hosting your image on one of our
supported image CDNs but you're not using the built-in loader
for it. This excludes applications that are using a custom
loader.

PR Close #47330
2022-10-19 15:21:51 +02:00
Alex Castle
9483343ebf feat(common): Add fill mode to NgOptimizedImage (#47738)
Add a new boolean attribute to NgOptimizedImage called `fill` which does the following:
* Removes the requirement for height and width
* Adds inline styling to cause the image to fill its containing element
* Adds a default `sizes` value of `100vw` which will cause the image to have a responsive srcset automatically generated

PR Close #47738
2022-10-12 15:56:56 +00:00
jaybell
75e6297f09 feat(common): add <link> preload tag on server for priority img (#47343)
This commit adds a logic that generates preload tags for priority images, when rendering happens on the server (e.g. Angular Universal).

PR Close #47343
2022-10-10 20:48:10 +00:00
Andrew Kushnir
c0c7efaf7c feat(common): add provideLocationMocks() function to provide Location mocks (#47674)
This commit adds the `provideLocationMocks()` function that returns mocks for the `Location` and `LocationStrategy` classes. This function can be used in tests to configure an environment where it's possible to fire simulated location events (helpful when testing Router configuration).

PR Close #47674
2022-10-10 19:42:11 +00:00
Alex Castle
4fde292bb5 feat(common): Add automatic srcset generation to ngOptimizedImage (#47547)
Add a feature to automatically generate the srcset attribute for images using the NgOptimizedImage directive. Uses the 'sizes' attribute to determine the appropriate srcset to generate.

PR Close #47547
2022-10-10 16:21:08 +00:00
Matthias Weiß
bdb5371033 feat(common): add injection token for default DatePipe configuration (#47157)
This commit introduces a new `DATE_PIPE_DEFAULT_OPTIONS` token, which
can be used to configure default DatePipe options, such as date
format and timezone.

DEPRECATED:

The `DATE_PIPE_DEFAULT_TIMEZONE` token is now deprecated in favor
of the `DATE_PIPE_DEFAULT_OPTIONS` token, which accepts an object
as a value and the timezone can be defined as a field (called `timezone`)
on that object.

PR Close #47157
2022-10-07 09:16:00 -07:00
Pawel Kozlowski
d3c25f20a3 test(common): demonstrate that image loaders are configurable on a component level (#47565)
Add a test to ensure that optimized image loaders can be configured at any
level of the DI hierarchy, including component's node injector.

PR Close #47565
2022-09-29 16:39:35 -07:00
Pawel Kozlowski
2c8579f1b4 refactor(common): decouple preconnect checks from image loaders (#47565)
Previously the built-in image loaders for the optimized image directive
were tightly coupled to the preconnect checks infrastructure. This was
creating a problem when developers were trying to provide a loader on
a component level (or, more generally, deeper in the DI hierarchy):
- PreconnectLinkChecker is the application-level service, provided in root;
- it makes sense to provide loaders in different parts of the DI hierarchy;

This refactoring removes the PreconnectLinkChecker configuration from
the loaders infrastructure and makes it application-wide config. The
PRECONNECT_CHECK_BLOCKLIST is also a simple provider (it was a
multi-provider previously) which should make the overall configuration
easier.

PR Close #47565
2022-09-29 16:39:35 -07:00
Andrew Kushnir
ca7bf65933 fix(common): rename rawSrc -> ngSrc in NgOptimizedImage directive (#47362)
As an ongoing effort to stabilize the NgOptimizedImage API before existing the Developer Preview, this commit renames the `rawSrc` attribute used for the NgOptimizedImage selector matching to `ngSrc`. The `rawSrcset` is also renamed to `ngSrcset` for consistency.

The motivation for this change is to align the attribute name better with other built-in directives, such as `ngFor`, `ngIf`, `ngClass`, `ngStyle`, etc.

Note: this is technically a breaking change, but since the NgOptimizedImage directive is in the Developer Preview mode, we land the change in a patch branch.

PR Close #47362
2022-09-09 14:25:37 -07:00
Andrew Kushnir
710d1da4ed refactor(common): create an NgFor alias for NgForOf directive (#47309)
This commit adds a re-export of the `NgForOf` class as `NgFor` to improve the DX for cases when the directive is used as standalone. Developers can import `NgFor` class, which better matches the `ngFor` attribute used in a template.

PR Close #47309
2022-09-09 14:04:54 -07:00
Pawel Kozlowski
4fdbd85d26 fix(common): consider density descriptors with multiple digits as valid (#47230)
Valid density descriptors used in the NgOptimizedImage can contain multiple
digits (ex. 1.25x, 25x). This change fixes the issue where density descriptors
with multiple digits (ex. 25x) where considered invalid.

Please note that a valid density descriptor might still be rejected by the
directive's validation logic if the supplied value is too big.

PR Close #47230
2022-08-23 16:21:14 -07:00
Kara Erickson
b3879dbf14 fix(common): support density descriptors with 2+ decimals (#47197)
This commit fixes a bug where `rawSrcset` in the image
directive would allow density descriptors like `1.5x`
but not like `1.25x`. Now descriptors with 2+ digits
after the decimal point should work.

PR Close #47197
2022-08-22 10:49:46 -07:00
Pawel Kozlowski
0f6b30bdc9 refactor(common): address review feedback for NgOptimizedImage (#47082)
Addresses part of the review feedback for the NgOptimizedImage directive.

PR Close #47082
2022-08-16 17:36:54 +00:00
Andrew Kushnir
213f57cb3d refactor(common): various NgOptimizedImage directive updates (#47082)
This commit contains a set of small changes based on the feedback received in https://github.com/angular/angular/pull/47082.

PR Close #47082
2022-08-16 17:36:54 +00:00
Kara Erickson
aa254244f4 refactor(common): clean up util.ts and preconnect_link_checker files (#47082)
This commit cleans up some stylistic nits and
moves functions out of util.ts into files that
are more specifically named (url.ts and error.ts).

PR Close #47082
2022-08-16 17:36:54 +00:00
Kara Erickson
d71dfe931f fix(common): set bound width and height onto host element (#47082)
This commit fixes a bug in NgOptimizedImage where
if you bound the width or height attribute (e.g.
`[width]=width`), the attribute would only be
reflected as "ng-reflect-x". The actual "width" or
"height" attribute would not be set on the host
element. This is a problem because the exact named
attribute must be set on the element for the
browser to detect it and use it to reserve space
for the image (and thus prevent CLS).

PR Close #47082
2022-08-16 17:36:54 +00:00
Andrew Kushnir
45cc85f3b8 refactor(common): use quotes instead of backticks in missing width/height error message (#47082)
This commit updates the error message thrown at runtime when an `<img>` doesn't contain width and/or height attributes.

PR Close #47082
2022-08-16 17:36:54 +00:00
Andrew Kushnir
d3c6c37a12 refactor(common): make NgOptimizedImage directive standalone (#47082)
This commit updates the NgOptimizedImage directive to become standalone and removes no longer needed NgOptimizedImageModule class.

PR Close #47082
2022-08-16 17:36:54 +00:00
Andrew Kushnir
f995f9fad1 refactor(common): update error messages of NgOptimizedImage directive (#47082)
This commit updates error messages that the NgOptimizedImage directive throws or logs into a console.

PR Close #47082
2022-08-16 17:36:54 +00:00
Katie Hempenius
451b85ca17 feat(common): explain why width/height is required (#47082)
Update error message text to explain why width & height are required
attributes.

PR Close #47082
2022-08-16 17:36:54 +00:00
Andrew Kushnir
244ad766c0 test(common): add parent injector to the createEnvironmentInjector calls in tests (#47082)
This commit updates the `createEnvironmentInjector` calls to pass parent injector, which became required as an argument.

PR Close #47082
2022-08-16 17:36:54 +00:00
Kara Erickson
b380fdd59e feat(common): add a density cap for image srcsets (#47082)
With this commit, the NgOptimizedImage directive will throw
a runtime error if it detects that one of the density descriptors
in rawSrcset is higher than 3x. It's generally not recommended
to use density descriptors higher than ~2, as it causes image to
download at very large sizes on mobile screens (thus slowing down
LCP). The density max is set conservatively to 3 in case apps
expect users to zoom in.

In future commits, we may want to throw even at densities >
than 2 and provide a configuration override for the zoom case.

PR Close #47082
2022-08-16 17:36:54 +00:00
Andrew Kushnir
8106084679 refactor(common): throw an error if an absolute URL is passed to Image Loaders (#47082)
This commit updates the logic of Image Loaders to throw an error in case an absolute URL is provided an an input to the loader function. The loaders can construct URLs based on image paths relative to the bas URL configured for a loader.

PR Close #47082
2022-08-16 17:36:53 +00:00
Andrew Kushnir
2ee4654546 Revert "fix(common): sanitize rawSrc and rawSrcset values in NgOptimizedImage directive" (#47082)
This reverts commit a0fdb8c7e9.

The `src` and `srcset` attributes don't pose security threats in modern browser, so sanitization is not really necessary.

PR Close #47082
2022-08-16 17:36:53 +00:00
Pawel Kozlowski
768a310b6e refactor(common): unify url error reporting in image loaders (#47082)
This refactoring pulls url-related error reporting into one place.
It also makes sure that error messages and the related error
reporting logic are tree-shakable.

PR Close #47082
2022-08-16 17:36:53 +00:00
Pawel Kozlowski
bd76ad6fa6 refactor(common): properly configure PRECONNECT_CHECK_BLOCKLIST (#47082)
Refactoring tests made me realise that we are misinterpreting the
ensurePreconnect option - in fact it should configure the
PRECONNECT_CHECK_BLOCKLIST provider _only_ if the ensurePreconnect
is set to false.

PR Close #47082
2022-08-16 17:36:53 +00:00
Pawel Kozlowski
3b17d4c011 test(common): remove unneeded describe block (#47082)
This commit removes a nested describe block as it
does't add any value this point. Also moved the
utils tests to the bottom of the file as those are
less important as compared to the other tests.

PR Close #47082
2022-08-16 17:36:53 +00:00
Katie Hempenius
8d3701cb4c feat(common): add warnings re: image distortion (#47082)
Checks whether image is visually distorted. Also adds a check to verify that width and height are set to a non-zero number.

PR Close #47082
2022-08-16 17:36:53 +00:00
Katie Hempenius
bff870db61 feat(common): add cloudflare loader (#47082)
Closes #46315

PR Close #47082
2022-08-16 17:36:53 +00:00
Andrew Kushnir
3836ebbbdf refactor(common): move loader util functions to a common location (#47082)
This commit refactors the code to avoid an extra utils file and instead, all extra helpers are moved to a common location.

PR Close #47082
2022-08-16 17:36:53 +00:00
Andrew Kushnir
dde5b9bff0 refactor(common): allow Cloudinary and ImageKit loaders to specify the ensurePreconnect option (#47082)
This commit modifies the provideCloudinaryLoader and provideImageKitLoader functions to support an additional ensurePreconnect option, similar to the Imgix loader functions.

PR Close #47082
2022-08-16 17:36:53 +00:00
Katie Hempenius
4e952ba216 feat(common): add loaders for cloudinary & imagekit (#47082)
This commit adds loaders for cloudinary and imagekit.

PR Close #47082
2022-08-16 17:36:53 +00:00
Andrew Kushnir
1cf43deb18 fix(common): sanitize rawSrc and rawSrcset values in NgOptimizedImage directive (#47082)
This commit applies a sanitization to values produced by a loader, before they are used for the `src` and `srcset` image element properties.

PR Close #47082
2022-08-16 17:36:53 +00:00
Andrew Kushnir
3774d84cfb test(common): optimize image loader tests of the NgOptimizedImage directive (#47082)
This commit updates the image loader tests to avoid creating the TestBed environment, since it's not really needed for the tests. Instead, the loader functions are invoked directly and the output is verified.

PR Close #47082
2022-08-16 17:36:53 +00:00
Pawel Kozlowski
98f20436ec refactor(common): allow loaders to specify the ensurePreconnect option (#47082)
This commit modifies the provideImgixLoader function to support
an additional ensurePreconnect option. Other loaders could follow
the same pattern to support a similar option as well.

As more loaders get developed we will refactor code to streamline
the loader authoring and enforce consistency.

PR Close #47082
2022-08-16 17:36:53 +00:00
Andrew Kushnir
586274fe65 feat(common): provide an ability to exclude origins from preconnect checks in NgOptimizedImage (#47082)
This commit adds an extra logic to add an ability to exclude origins from preconnect checks in NgOptimizedImage by configuring the `PRECONNECT_CHECK_BLOCKLIST` multi-provider.

PR Close #47082
2022-08-16 17:36:53 +00:00
Andrew Kushnir
7baf9a46cd feat(common): verify that priority images have preconnect links (#47082)
This commit updates the `NgOptimizedImage` directive to add a logic to detect whether an image, marked with the "priority" attribute has a corresponding `<link rel="preconnect">` tag in the `document.head` for better performance.

PR Close #47082
2022-08-16 17:36:53 +00:00
Kara Erickson
7ce497e5bc feat(common): add built-in Imgix loader (#47082)
This commit adds a built-in Imgix loader for the
NgOptimizedImage directive. If you provide the
desired Imgix hostname, an ImageLoader will be
generated with the correct options.

Usage looks like this:

```ts
providers: [
  provideImgixLoader('https://some.imgix.net')
]
```

It sets the "auto=format" flag by default, which
ensures that the smallest image format supported
by the browser is served.

This change also moves the IMAGE_LOADER, ImageLoader,
and ImageLoaderConfig into a new directory that will
be shared by all built-in image loaders.

PR Close #47082
2022-08-16 17:36:52 +00:00
Andrew Kushnir
e34e48c91e test: update error codes used in the NgOptimizedImage tests (#47082)
This commit updates the error codes used in the `NgOptimizedImage` tests. The error codes got u[dated recently in a PR that got merged earlier.

PR Close #47082
2022-08-16 17:36:52 +00:00
Katie Hempenius
e854a8cdde feat(common): add loading attr to NgOptimizedImage (#47082)
Add loading attribute to NgOptimizedImage.

PR Close #47082
2022-08-16 17:36:52 +00:00
Kara Erickson
57f3386e5b feat(common): support custom srcset attributes in NgOptimizedImage (#47082)
This commit adds a `rawSrcset` attribute as a replacement for the
`srcset` attribute. The `srcset` attribute cannot be set statically
on the image directive because it would cause images to start
downloading before the "loading" attribute could be set to "lazy".

Changing the name to `rawSrcset` allows the directive to control
the timing of image loading. It also makes it possible to support
custom loaders for `srcset` file names. Rather than having to repeat
the image origin for each image, the existing `rawSrc` value and
image loader can be composed to generate each full URL string. The
developer must only provide the necessary widths for the `srcset`.

For example, the developer might write:

```markup
<img rawSrc="hermes.jpg" rawSrcset="100w, 200w" ... />
```

with a loader like:

```js
const loader = (config) => `path/${config.src}?w=${config.width}`;
```

and the img tag will ultimately be set up as something like:

```markup
<img src="path/hermes.jpg?w=100" srcset="path/hermes.jpg?w=100 100w, path/hermes.jpg?w=200 200w" .../>
```

PR Close #47082
2022-08-16 17:36:52 +00:00
Kara Erickson
ae4405f0bf fix(common): throw if srcset is used with rawSrc (#47082)
Currently if you use a static `srcset` with the image directive,
lazy loading no longer works because the image would start to
load before the loading attribute could be set to "lazy". This
commit throws an error if you try to use `srcset` this way.

In a follow-up commit, a new attribute will be added to support
responsive images in a lazy-loading-friendly way.

PR Close #47082
2022-08-16 17:36:52 +00:00
Andrew Kushnir
bde319e534 refactor(common): create an NgModule for the NgOptimizedImage directive (#47082)
This commit updates the `NgOptimizedImage` directive to drop the `standalone` flag and create a new NgModule which declares and exports it, so that the directive can be used in apps that use pre-v14 version of Angular.

PR Close #47082
2022-08-16 17:36:52 +00:00
Kara Erickson
8627391cfb test(common): fix relative URL issue in web tests (#47082)
The CI failed to run on the last PR, so we didn't catch
that there were web tests failing due to absolute/relative
URL issues. This commit should fix the issue.

PR Close #47082
2022-08-16 17:36:52 +00:00
Kara Erickson
1ca2ce19ab fix(common): remove default for image width (#47082)
Previously NgOptimizedImage would default to requesting
an image at the width matching the width attribute in
the image HTML. While this works for width attrs that
match the intrinsic size of the image (e.g. responsive
images or images sized with CSS), this can be a sharp
edge for developers who use the width/height attributes
to set rendered size (i.e. instead of CSS, which one can
do for a fixed size image). In this case, defaulting to
the width attribute for the requested image would mean
requesting always at 1x quality, so screens with a DPR
 of 2+ get an image that is too small. Without a default
request width, the image served by the CDN would likely
be at intrinsic size, so 2x images would look correct.

This PR also updates the NgOptimizedImage sandbox and tests.

PR Close #47082
2022-08-16 17:36:52 +00:00
Andrew Kushnir
4a01b204d3 refactor(common): throw an error if width or height inputs are missing or invalid (#47082)
This commit updates the `NgOptimizeImage` directive to add asserts to make sure the `width` and `height` inputs are set and have correct values (numbers only).

PR Close #47082
2022-08-16 17:36:52 +00:00
Andrew Kushnir
2e0d09354f refactor(common): throw an error if NgOptimizeImage inputs change (#47082)
This commit updates the `NgOptimizeImage` directive to add asserts to make sure no inputs are changed after directive initialization.

PR Close #47082
2022-08-16 17:36:52 +00:00
Andrew Kushnir
c2854e8ef6 refactor(common): mark experimental NgOptimizedImage directive as standalone (#47082)
This commit updates the `NgOptimizedImage` directive as standalone, so it's easier to import it in
 an app (without importing any NgModules). The `NgOptimizedImageModule` is removed as no longer needed.

PR Close #47082
2022-08-16 17:36:52 +00:00
Andrew Kushnir
801daf82d1 fix(common): detect data: and blob: inputs in NgOptimizedImage directive (#47082)
This commit updates the `NgOptimizedImage` directive to throw an error when `data:` and `blob:` inputs are used.

PR Close #47082
2022-08-16 17:36:52 +00:00