From efa6900476a3f9ff4b801e594d610356c0c3b51b Mon Sep 17 00:00:00 2001 From: VanMess <694248616@qq.com> Date: Fri, 24 May 2019 14:53:47 +0800 Subject: [PATCH] Image: add support for transmit "attrs" and "listeners" (#15578) --- packages/image/src/main.vue | 18 +++++++++----- test/unit/specs/image.spec.js | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/packages/image/src/main.vue b/packages/image/src/main.vue index c00c775b893..775c20852e7 100644 --- a/packages/image/src/main.vue +++ b/packages/image/src/main.vue @@ -9,9 +9,9 @@ @@ -37,14 +37,13 @@ name: 'ElImage', mixins: [Locale], + inheritAttrs: false, props: { src: String, fit: String, lazy: Boolean, - scrollContainer: {}, - alt: String, - referrerPolicy: String + scrollContainer: {} }, data() { @@ -104,13 +103,20 @@ const img = new Image(); img.onload = e => this.handleLoad(e, img); img.onerror = this.handleError.bind(this); + + // bind html attrs + // so it can behave consistently + Object.keys(this.$attrs) + .forEach((key) => { + const value = this.$attrs[key]; + img.setAttribute(key, value); + }); img.src = this.src; }, handleLoad(e, img) { this.imageWidth = img.width; this.imageHeight = img.height; this.loading = false; - this.$emit('load', e); }, handleError(e) { this.loading = false; diff --git a/test/unit/specs/image.spec.js b/test/unit/specs/image.spec.js index 6dca7ec597a..e7a1375c65b 100644 --- a/test/unit/specs/image.spec.js +++ b/test/unit/specs/image.spec.js @@ -64,5 +64,49 @@ describe('Image', () => { await wait(); expect(image2.loading).to.be.false; }); + + it('$attrs', async() => { + vm = createVue({ + template: ` + + `, + data() { + return { + src + }; + } + }, true); + + await wait(); + const $img = vm.$el.querySelector('.el-image__inner'); + expect($img.getAttribute('alt')).to.be.equal('$attrs test'); + expect($img.getAttribute('referrerpolicy')).to.be.equal('origin'); + }); + + it('pass event listeners', async() => { + let result; + vm = createVue({ + template: ` + + `, + data() { + return { + src + }; + }, + methods: { + handleClick(e) { + result = e; + } + } + }, true); + await wait(); + vm.$el.querySelector('.el-image__inner').click(); + await wait(); + expect(result).to.exist; + }); });