Skip to content

Commit 7393ac4

Browse files
author
Marcin Szczepanski
committed
Don't fail if alt attribute not present
1 parent e73fc8b commit 7393ac4

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/gallery.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
item.className = "inactive";
3636
}
3737
var label = item.querySelector("label");
38-
if (!label) {
39-
var img = item.querySelector("img");
38+
var img = item.querySelector("img");
39+
if (!label && img.attributes.alt) {
4040
label = document.createElement("label");
4141
label.innerHTML = img.attributes.alt.value;
4242
item.appendChild(label);

tests/test.gallery.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ describe('Gallery', function () {
55

66
describe('#start', function () {
77
var itemElem1, itemElem2, items, galleryElem;
8-
var MockItem = function (hasLabel) {
8+
var MockItem = function (hasLabel, attributes) {
99
this.hasLabel = hasLabel;
10+
this.attributes = attributes;
1011
return this;
1112
};
1213
MockItem.prototype.querySelector = function (selector) {
1314
if (selector === "label") {
1415
return this.hasLabel;
1516
} else if (selector === "img") {
1617
return {
17-
attributes: {
18-
alt: {
19-
value: "ALT"
20-
}
21-
}
18+
attributes: this.attributes
2219
};
2320
}
2421
};
@@ -27,8 +24,8 @@ describe('Gallery', function () {
2724

2825
beforeEach(function () {
2926
/* Mock Item elements */
30-
itemElem1 = new MockItem(true);
31-
itemElem2 = new MockItem(false);
27+
itemElem1 = new MockItem(true, {});
28+
itemElem2 = new MockItem(false, {alt: { value: "ALT"}});
3229
items = [itemElem1, itemElem2];
3330
/* Mock DOM Gallery element */
3431
galleryElem = {
@@ -77,6 +74,13 @@ describe('Gallery', function () {
7774
appendChildSpy1.restore();
7875
appendChildSpy2.restore();
7976
});
77+
it("doesn't fail if items doesn't have an alt attribute", function () {
78+
delete itemElem2.attributes.alt;
79+
var appendChildSpy2 = sinon.spy(itemElem2, "appendChild");
80+
Gallery.start(galleryElem);
81+
expect(appendChildSpy2.called).to.be.false;
82+
appendChildSpy2.restore();
83+
});
8084
});
8185
describe('#step', function () {
8286
var items;

0 commit comments

Comments
 (0)