Skip to content

Commit dae8665

Browse files
authored
Merge pull request #6 from hmhealey/MM-31171_youtube-previews
Check for meta tags in the body
2 parents 816b660 + 006880f commit dae8665

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

opengraph/opengraph.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ func (og *OpenGraph) ProcessHTML(buffer io.Reader) error {
119119
return z.Err()
120120
case html.StartTagToken, html.SelfClosingTagToken, html.EndTagToken:
121121
name, hasAttr := z.TagName()
122-
if atom.Lookup(name) == atom.Body {
123-
return nil // OpenGraph is only in head, so we don't need body
124-
}
125122
if atom.Lookup(name) != atom.Meta || !hasAttr {
126123
continue
127124
}

opengraph/opengraph_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,79 @@ func TestOpenGraphProcessHTML(t *testing.T) {
153153
}
154154
}
155155

156+
func TestOpenGraphProcessHTML_YouTube(t *testing.T) {
157+
const youTubeHTML = `
158+
<!DOCTYPE html>
159+
<html style="font-size: 10px;font-family: Roboto, Arial, sans-serif;" lang="en-GB">
160+
<head>
161+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
162+
</head>
163+
<body dir="ltr" no-y-overflow>
164+
<meta charset="utf-8" />
165+
<meta name="viewport" content="width=device-width, initial-scale=1">
166+
<meta property="og:site_name" content="YouTube">
167+
<meta property="og:url" content="https://www.youtube.com/watch?v=test123">
168+
<meta property="og:title" content="Test 123">
169+
<meta property="og:image" content="https://i.ytimg.com/vi/test123/hqdefault.jpg">
170+
<meta property="og:image:width" content="480">
171+
<meta property="og:image:height" content="360">
172+
<meta property="og:description" content="This is a test.">
173+
<meta property="og:type" content="video.other">
174+
<meta property="og:video:url" content="https://www.youtube.com/embed/test123">
175+
<meta property="og:video:secure_url" content="https://www.youtube.com/embed/test123">
176+
<meta property="og:video:type" content="text/html">
177+
<meta property="og:video:width" content="480">
178+
<meta property="og:video:height" content="360">
179+
<meta property="og:video:tag" content="Test">
180+
<meta property="og:video:tag" content="Testing">
181+
</body>
182+
</html>
183+
`
184+
185+
og := opengraph.NewOpenGraph()
186+
err := og.ProcessHTML(strings.NewReader(youTubeHTML))
187+
188+
if err != nil {
189+
t.Fatal(err)
190+
}
191+
192+
if og.Type != "video.other" {
193+
t.Error("type parsed incorrectly")
194+
}
195+
196+
if len(og.Title) == 0 {
197+
t.Error("title parsed incorrectly")
198+
}
199+
200+
if len(og.URL) == 0 {
201+
t.Error("url parsed incorrectly")
202+
}
203+
204+
if len(og.Description) == 0 {
205+
t.Error("description parsed incorrectly")
206+
}
207+
208+
if len(og.Images) == 0 {
209+
t.Error("images parsed incorrectly")
210+
} else {
211+
if len(og.Images[0].URL) == 0 {
212+
t.Error("image url parsed incorrectly")
213+
}
214+
}
215+
216+
if len(og.SiteName) == 0 {
217+
t.Error("site name parsed incorrectly")
218+
}
219+
220+
if len(og.Videos) != 1 {
221+
t.Error("videos parsed incorrectly")
222+
} else {
223+
if len(og.Videos[0].URL) == 0 {
224+
t.Error("video url parsed incorrectly")
225+
}
226+
}
227+
}
228+
156229
func TestOpenGraphProcessMeta(t *testing.T) {
157230
og := opengraph.NewOpenGraph()
158231

0 commit comments

Comments
 (0)