Skip to content

Commit

Permalink
feat(Image): add crossorigin & referrerpolicy props (youzan#12641)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jungzl authored and CatsAndMice committed Apr 8, 2024
1 parent 16b389b commit ec01eaf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/vant/src/image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
type PropType,
type CSSProperties,
type ExtractPropTypes,
type ImgHTMLAttributes,
} from 'vue';

// Utils
Expand Down Expand Up @@ -49,6 +50,8 @@ export const imageProps = {
iconPrefix: String,
showLoading: truthProp,
loadingIcon: makeStringProp('photo'),
crossorigin: String as PropType<ImgHTMLAttributes['crossorigin']>,
referrerpolicy: String as PropType<ImgHTMLAttributes['referrerpolicy']>,
};

export type ImageProps = ExtractPropTypes<typeof imageProps>;
Expand Down Expand Up @@ -154,6 +157,8 @@ export default defineComponent({
objectFit: props.fit,
objectPosition: props.position,
},
crossorigin: props.crossorigin,
referrerpolicy: props.referrerpolicy,
};

if (props.lazyLoad) {
Expand Down
2 changes: 2 additions & 0 deletions packages/vant/src/image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ app.use(Lazyload);
| loading-icon | Loading icon | _string_ | `photo` |
| icon-size | Icon size | _number \| string_ | `32px` |
| icon-prefix | Icon className prefix | _string_ | `van-icon` |
| crossorigin | same as [crossorigin](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/crossOrigin) | _string_ | - |
| referrerpolicy | same as [referrerpolicy](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/referrerPolicy) | _string_ | - |

### fit optional value

Expand Down
2 changes: 2 additions & 0 deletions packages/vant/src/image/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ app.use(Lazyload);
| loading-icon | 加载时提示的图标名称或图片链接,等同于 Icon 组件的 [name 属性](#/zh-CN/icon#props) | _string_ | `photo` |
| icon-size | 加载图标和失败图标的大小 | _number \| string_ | `32px` |
| icon-prefix | 图标类名前缀,等同于 Icon 组件的 [class-prefix 属性](#/zh-CN/icon#props) | _string_ | `van-icon` |
| crossorigin | 等同于原生的 [crossorigin](https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLImageElement/crossOrigin) 属性 | _string_ | - |
| referrerpolicy | 等同于原生的 [referrerpolicy](https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLImageElement/referrerPolicy) 属性 | _string_ | - |

### 图片填充模式

Expand Down
11 changes: 11 additions & 0 deletions packages/vant/src/image/test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ exports[`should emit load event after image loaded 1`] = `
</div>
`;
exports[`should pass props to img 1`] = `
<div class="van-image">
<img
src="https://img.com"
class="van-image__img"
crossorigin="anonymous"
referrerpolicy="no-referrer"
>
</div>
`;
exports[`should render default slot correctly 1`] = `
<div class="van-image">
<img
Expand Down
13 changes: 13 additions & 0 deletions packages/vant/src/image/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ test('should emit load event after image loaded', async () => {
expect(wrapper.html()).toMatchSnapshot();
});

test('should pass props to img', async () => {
const wrapper = mount(VanImage, {
props: {
src: IMAGE_URL,
referrerpolicy: 'no-referrer',
crossorigin: 'anonymous',
},
});

await wrapper.find('img').trigger('load');
expect(wrapper.html()).toMatchSnapshot();
});

test('should watch src and reset', async () => {
const wrapper = mount(VanImage, {
props: {
Expand Down

0 comments on commit ec01eaf

Please sign in to comment.