Skip to content

Commit

Permalink
fix(b-img): Allow empty alt prop (fixes #5524) (#5545)
Browse files Browse the repository at this point in the history
* Allow empty `alt`

* default to null to avoid check

* remove unused import

* add avatar support

* add test cases

* spelling

Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
  • Loading branch information
Hiws and jacobmllr95 authored Jul 6, 2020
1 parent 190325e commit b22829d
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/avatar/avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export const BAvatar = /*#__PURE__*/ Vue.extend({
} = this
const link = !button && isLink(this)
const tag = button ? BButton : link ? BLink : 'span'
const alt = this.alt || null
const alt = this.alt
const ariaLabel = this.ariaLabel || null

let $content = null
Expand Down
30 changes: 30 additions & 0 deletions src/components/avatar/avatar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,34 @@ describe('avatar', () => {

wrapper2.destroy()
})

it('should render `alt` attribute if `alt` prop is empty string', async () => {
const wrapper = mount(BAvatar, {
propsData: {
src: '/foo/bar',
alt: ''
}
})
expect(wrapper.vm).toBeDefined()
expect(wrapper.find('img').exists()).toBe(true)
expect(wrapper.find('img').attributes('src')).toEqual('/foo/bar')
expect(wrapper.find('img').attributes('alt')).toEqual('')

wrapper.destroy()
})

it('should not render `alt` attribute if `alt` prop is null', async () => {
const wrapper = mount(BAvatar, {
propsData: {
src: '/foo/bar',
alt: null
}
})
expect(wrapper.vm).toBeDefined()
expect(wrapper.find('img').exists()).toBe(true)
expect(wrapper.find('img').attributes('src')).toEqual('/foo/bar')
expect(wrapper.find('img').attributes('alt')).not.toBeDefined()

wrapper.destroy()
})
})
16 changes: 16 additions & 0 deletions src/components/card/card-img-lazy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ describe('card-image', () => {
wrapper.destroy()
})

it('has attribute alt when prop `alt` is empty', async () => {
const wrapper = mount(BCardImgLazy, {
context: {
props: {
src: 'https://picsum.photos/600/300/?image=25',
alt: ''
}
}
})

expect(wrapper.attributes('alt')).toBeDefined()
expect(wrapper.attributes('alt')).toBe('')

wrapper.destroy()
})

it('has attribute width when prop width set', async () => {
const wrapper = mount(BCardImgLazy, {
context: {
Expand Down
6 changes: 3 additions & 3 deletions src/components/card/card-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const props = {
required: true
},
alt: {
type: String
// default: null
type: String,
default: null
},
top: {
type: Boolean,
Expand Down Expand Up @@ -69,7 +69,7 @@ export const BCardImg = /*#__PURE__*/ Vue.extend({
class: [baseClass],
attrs: {
src: props.src || null,
alt: props.alt || null,
alt: props.alt,
height: props.height || null,
width: props.width || null
}
Expand Down
16 changes: 16 additions & 0 deletions src/components/card/card-img.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ describe('card-image', () => {
wrapper.destroy()
})

it('has attribute alt when prop `alt` is empty', async () => {
const wrapper = mount(BCardImg, {
context: {
props: {
src: 'https://picsum.photos/600/300/?image=25',
alt: ''
}
}
})

expect(wrapper.attributes('alt')).toBeDefined()
expect(wrapper.attributes('alt')).toBe('')

wrapper.destroy()
})

it('has attribute width when prop width set', async () => {
const wrapper = mount(BCardImg, {
context: {
Expand Down
6 changes: 3 additions & 3 deletions src/components/image/img.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export const props = {
// default: null
},
alt: {
type: String
// default: null
type: String,
default: null
},
width: {
type: [Number, String]
Expand Down Expand Up @@ -153,7 +153,7 @@ export const BImg = /*#__PURE__*/ Vue.extend({
mergeData(data, {
attrs: {
src: src,
alt: props.alt || null,
alt: props.alt,
width: width ? toString(width) : null,
height: height ? toString(height) : null,
srcset: srcset || null,
Expand Down
29 changes: 29 additions & 0 deletions src/components/image/img.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ describe('img', () => {
wrapper.destroy()
})

it('default does not have attributes alt, width, or height', async () => {
const wrapper = mount(BImg, {
context: {
props: {
src: 'https://picsum.photos/600/300/?image=25'
}
}
})

expect(wrapper.attributes('alt')).not.toBeDefined()
expect(wrapper.attributes('width')).not.toBeDefined()
expect(wrapper.attributes('height')).not.toBeDefined()

wrapper.destroy()
})

it('should have class "img-fluid" when prop fluid set', async () => {
const wrapper = mount(BImg, {
propsData: {
Expand Down Expand Up @@ -223,4 +239,17 @@ describe('img', () => {

wrapper.destroy()
})

it('should have alt attribute when `alt` prop is empty', async () => {
const wrapper = mount(BImg, {
propsData: {
alt: ''
}
})

expect(wrapper.attributes('alt')).toBeDefined()
expect(wrapper.attributes('alt')).toEqual('')

wrapper.destroy()
})
})

1 comment on commit b22829d

@vercel
Copy link

@vercel vercel bot commented on b22829d Jul 6, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.