Skip to content

Commit 2da42df

Browse files
committed
handle empty XCardsImage cases
1 parent b8b08a8 commit 2da42df

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

extract_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,10 @@ func TestExtractor_Extract(t *testing.T) {
965965
URL: `https://github.com/aafeher/go-microdata-extract`,
966966
Description: `X Cards with image`,
967967
XCardsImage: []extract.XCardsImage{
968+
{
969+
URL: "",
970+
Width: 1200,
971+
},
968972
{
969973
URL: "https://picsum.photos/200/300",
970974
},

extractors/xcards.go

+5
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ func handleXCardsImageProperty(xc *XCards, parts []string, content string) {
306306
}
307307
lastIdx := len(xc.XCardsImage) - 1
308308

309+
if lastIdx < 0 {
310+
xc.XCardsImage = append(xc.XCardsImage, XCardsImage{})
311+
lastIdx = 0
312+
}
313+
309314
if len(parts) == 2 {
310315
xc.XCardsImage[lastIdx].URL = content
311316
return

test/test-17-xcards-image.html

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<meta name="twitter:title" content="go-microdata-extract" />
88
<meta name="twitter:url" content="https://github.com/aafeher/go-microdata-extract" />
99
<meta name="twitter:description" content="X Cards with image" />
10+
<meta name="twitter:image:width" content="1200" />
1011
<meta name="twitter:image" content="https://picsum.photos/200/300" />
1112
<meta name="twitter:image" content="https://picsum.photos/210/310" />
1213
<meta name="twitter:image:secure_url" content="https://picsum.photos/210/310" />

0 commit comments

Comments
 (0)