meta_oxide 0.1.1

Universal metadata extraction library supporting 13 formats (HTML Meta, Open Graph, Twitter Cards, JSON-LD, Microdata, Microformats, RDFa, Dublin Core, Web App Manifest, oEmbed, rel-links, Images, SEO) with 7 language bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Tests for Twitter Card extraction

#[cfg(test)]
mod tests {
    use crate::extractors::social::twitter::{extract, extract_with_fallback};

    #[test]
    fn test_basic_twitter_card() {
        let html = r#"
            <meta name="twitter:card" content="summary">
            <meta name="twitter:title" content="Test Tweet">
            <meta name="twitter:description" content="Tweet description">
            <meta name="twitter:image" content="https://example.com/image.jpg">
        "#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.card, Some("summary".to_string()));
        assert_eq!(card.title, Some("Test Tweet".to_string()));
        assert_eq!(card.description, Some("Tweet description".to_string()));
        assert_eq!(card.image, Some("https://example.com/image.jpg".to_string()));
    }

    #[test]
    fn test_twitter_card_large_image() {
        let html = r#"<meta name="twitter:card" content="summary_large_image">"#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.card, Some("summary_large_image".to_string()));
    }

    #[test]
    fn test_twitter_card_app() {
        let html = r#"<meta name="twitter:card" content="app">"#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.card, Some("app".to_string()));
    }

    #[test]
    fn test_twitter_card_player() {
        let html = r#"<meta name="twitter:card" content="player">"#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.card, Some("player".to_string()));
    }

    #[test]
    fn test_twitter_site_and_creator() {
        let html = r#"
            <meta name="twitter:site" content="@example">
            <meta name="twitter:creator" content="@author">
        "#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.site, Some("@example".to_string()));
        assert_eq!(card.creator, Some("@author".to_string()));
    }

    #[test]
    fn test_twitter_site_and_creator_ids() {
        let html = r#"
            <meta name="twitter:site:id" content="123456">
            <meta name="twitter:creator:id" content="789012">
        "#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.site_id, Some("123456".to_string()));
        assert_eq!(card.creator_id, Some("789012".to_string()));
    }

    #[test]
    fn test_twitter_image_alt() {
        let html = r#"<meta name="twitter:image:alt" content="Image description">"#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.image_alt, Some("Image description".to_string()));
    }

    #[test]
    fn test_twitter_player_card_full() {
        let html = r#"
            <meta name="twitter:card" content="player">
            <meta name="twitter:player" content="https://example.com/player">
            <meta name="twitter:player:width" content="1280">
            <meta name="twitter:player:height" content="720">
            <meta name="twitter:player:stream" content="https://example.com/stream.mp4">
        "#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.card, Some("player".to_string()));
        assert!(card.player.is_some());
        let player = card.player.unwrap();
        assert_eq!(player.url, "https://example.com/player");
        assert_eq!(player.width, Some(1280));
        assert_eq!(player.height, Some(720));
        assert_eq!(player.stream, Some("https://example.com/stream.mp4".to_string()));
    }

    #[test]
    fn test_twitter_player_minimal() {
        let html = r#"<meta name="twitter:player" content="https://example.com/player">"#;
        let card = extract(html, None).unwrap();
        assert!(card.player.is_some());
        let player = card.player.unwrap();
        assert_eq!(player.url, "https://example.com/player");
        assert_eq!(player.width, None);
        assert_eq!(player.height, None);
    }

    #[test]
    fn test_twitter_app_card_iphone() {
        let html = r#"
            <meta name="twitter:card" content="app">
            <meta name="twitter:app:name:iphone" content="MyApp">
            <meta name="twitter:app:id:iphone" content="123456">
            <meta name="twitter:app:url:iphone" content="myapp://open">
        "#;
        let card = extract(html, None).unwrap();
        assert!(card.app.is_some());
        let app = card.app.unwrap();
        assert_eq!(app.name_iphone, Some("MyApp".to_string()));
        assert_eq!(app.id_iphone, Some("123456".to_string()));
        assert_eq!(app.url_iphone, Some("myapp://open".to_string()));
    }

    #[test]
    fn test_twitter_app_card_ipad() {
        let html = r#"
            <meta name="twitter:app:name:ipad" content="MyApp HD">
            <meta name="twitter:app:id:ipad" content="234567">
            <meta name="twitter:app:url:ipad" content="myapp://open-ipad">
        "#;
        let card = extract(html, None).unwrap();
        assert!(card.app.is_some());
        let app = card.app.unwrap();
        assert_eq!(app.name_ipad, Some("MyApp HD".to_string()));
        assert_eq!(app.id_ipad, Some("234567".to_string()));
        assert_eq!(app.url_ipad, Some("myapp://open-ipad".to_string()));
    }

    #[test]
    fn test_twitter_app_card_googleplay() {
        let html = r#"
            <meta name="twitter:app:name:googleplay" content="My Android App">
            <meta name="twitter:app:id:googleplay" content="com.example.app">
            <meta name="twitter:app:url:googleplay" content="myapp://open-android">
        "#;
        let card = extract(html, None).unwrap();
        assert!(card.app.is_some());
        let app = card.app.unwrap();
        assert_eq!(app.name_googleplay, Some("My Android App".to_string()));
        assert_eq!(app.id_googleplay, Some("com.example.app".to_string()));
        assert_eq!(app.url_googleplay, Some("myapp://open-android".to_string()));
    }

    #[test]
    fn test_twitter_app_card_all_platforms() {
        let html = r#"
            <meta name="twitter:card" content="app">
            <meta name="twitter:app:name:iphone" content="MyApp">
            <meta name="twitter:app:id:iphone" content="123456">
            <meta name="twitter:app:name:ipad" content="MyApp HD">
            <meta name="twitter:app:id:ipad" content="234567">
            <meta name="twitter:app:name:googleplay" content="My Android App">
            <meta name="twitter:app:id:googleplay" content="com.example.app">
            <meta name="twitter:app:country" content="US">
        "#;
        let card = extract(html, None).unwrap();
        assert!(card.app.is_some());
        let app = card.app.unwrap();
        assert_eq!(app.name_iphone, Some("MyApp".to_string()));
        assert_eq!(app.name_ipad, Some("MyApp HD".to_string()));
        assert_eq!(app.name_googleplay, Some("My Android App".to_string()));
        assert_eq!(app.country, Some("US".to_string()));
    }

    #[test]
    fn test_twitter_relative_url() {
        let html = r#"<meta name="twitter:image" content="/images/photo.jpg">"#;
        let card = extract(html, Some("https://example.com")).unwrap();
        assert_eq!(card.image, Some("https://example.com/images/photo.jpg".to_string()));
    }

    #[test]
    fn test_twitter_player_relative_url() {
        let html = r#"
            <meta name="twitter:player" content="/player/embed">
            <meta name="twitter:player:stream" content="/videos/stream.mp4">
        "#;
        let card = extract(html, Some("https://example.com")).unwrap();
        assert!(card.player.is_some());
        let player = card.player.unwrap();
        assert_eq!(player.url, "https://example.com/player/embed");
        assert_eq!(player.stream, Some("https://example.com/videos/stream.mp4".to_string()));
    }

    #[test]
    fn test_empty_html() {
        let html = "";
        let card = extract(html, None).unwrap();
        assert_eq!(card.card, None);
        assert_eq!(card.title, None);
    }

    #[test]
    fn test_no_twitter_tags() {
        let html = r#"
            <html>
            <head>
                <title>Regular HTML</title>
                <meta name="description" content="Not Twitter">
            </head>
            </html>
        "#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.card, None);
    }

    #[test]
    fn test_whitespace_handling() {
        let html = r#"
            <meta name="twitter:title" content="  Title with spaces  ">
            <meta name="twitter:description" content="
                Description with
                newlines
            ">
        "#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.title, Some("Title with spaces".to_string()));
        // Description should be trimmed
        assert!(card.description.is_some());
        let desc = card.description.unwrap();
        assert!(!desc.starts_with(' '));
        assert!(!desc.ends_with(' '));
    }

    #[test]
    fn test_empty_content_ignored() {
        let html = r#"
            <meta name="twitter:title" content="">
            <meta name="twitter:description" content="   ">
        "#;
        let card = extract(html, None).unwrap();
        assert_eq!(card.title, None);
        assert_eq!(card.description, None);
    }

    #[test]
    fn test_malformed_player_dimensions() {
        let html = r#"
            <meta name="twitter:player" content="https://example.com/player">
            <meta name="twitter:player:width" content="not-a-number">
            <meta name="twitter:player:height" content="abc">
        "#;
        let card = extract(html, None).unwrap();
        assert!(card.player.is_some());
        let player = card.player.unwrap();
        // Invalid dimensions should be ignored
        assert_eq!(player.width, None);
        assert_eq!(player.height, None);
    }

    #[test]
    fn test_real_world_summary_card() {
        let html = r#"
            <meta name="twitter:card" content="summary">
            <meta name="twitter:site" content="@example">
            <meta name="twitter:title" content="Article Title">
            <meta name="twitter:description" content="Article description">
            <meta name="twitter:image" content="https://example.com/image.jpg">
        "#;
        let card = extract(html, None).unwrap();

        assert_eq!(card.card, Some("summary".to_string()));
        assert_eq!(card.site, Some("@example".to_string()));
        assert_eq!(card.title, Some("Article Title".to_string()));
        assert_eq!(card.description, Some("Article description".to_string()));
        assert_eq!(card.image, Some("https://example.com/image.jpg".to_string()));
    }

    #[test]
    fn test_real_world_large_image_card() {
        let html = r#"
            <meta name="twitter:card" content="summary_large_image">
            <meta name="twitter:site" content="@nytimes">
            <meta name="twitter:creator" content="@journalist">
            <meta name="twitter:title" content="Breaking News Story">
            <meta name="twitter:description" content="Important development in ongoing story">
            <meta name="twitter:image" content="https://news.example.com/hero-image.jpg">
            <meta name="twitter:image:alt" content="Photo of the scene">
        "#;
        let card = extract(html, None).unwrap();

        assert_eq!(card.card, Some("summary_large_image".to_string()));
        assert_eq!(card.site, Some("@nytimes".to_string()));
        assert_eq!(card.creator, Some("@journalist".to_string()));
        assert_eq!(card.image_alt, Some("Photo of the scene".to_string()));
    }

    #[test]
    fn test_real_world_player_card() {
        let html = r#"
            <meta name="twitter:card" content="player">
            <meta name="twitter:site" content="@youtube">
            <meta name="twitter:title" content="Funny Cat Video">
            <meta name="twitter:description" content="You won't believe what this cat does">
            <meta name="twitter:image" content="https://youtube.com/thumbnail.jpg">
            <meta name="twitter:player" content="https://youtube.com/embed/abc123">
            <meta name="twitter:player:width" content="1280">
            <meta name="twitter:player:height" content="720">
            <meta name="twitter:player:stream" content="https://youtube.com/stream/abc123.mp4">
        "#;
        let card = extract(html, None).unwrap();

        assert_eq!(card.card, Some("player".to_string()));
        assert!(card.player.is_some());
        let player = card.player.unwrap();
        assert_eq!(player.width, Some(1280));
        assert_eq!(player.height, Some(720));
    }

    #[test]
    fn test_real_world_app_card() {
        let html = r#"
            <meta name="twitter:card" content="app">
            <meta name="twitter:site" content="@appstore">
            <meta name="twitter:title" content="Download Our App">
            <meta name="twitter:description" content="The best app for productivity">
            <meta name="twitter:image" content="https://apps.example.com/icon.png">
            <meta name="twitter:app:name:iphone" content="Productivity Plus">
            <meta name="twitter:app:id:iphone" content="987654321">
            <meta name="twitter:app:url:iphone" content="productivityplus://open">
            <meta name="twitter:app:name:googleplay" content="Productivity Plus">
            <meta name="twitter:app:id:googleplay" content="com.example.productivityplus">
        "#;
        let card = extract(html, None).unwrap();

        assert_eq!(card.card, Some("app".to_string()));
        assert!(card.app.is_some());
        let app = card.app.unwrap();
        assert_eq!(app.name_iphone, Some("Productivity Plus".to_string()));
        assert_eq!(app.id_iphone, Some("987654321".to_string()));
    }

    #[test]
    fn test_case_sensitivity() {
        // Twitter meta tags use name attribute (case-insensitive in HTML)
        let html = r#"
            <meta name="twitter:title" content="Lowercase">
            <meta name="TWITTER:TITLE" content="Uppercase">
            <meta name="Twitter:Title" content="Mixed">
        "#;
        let card = extract(html, None).unwrap();
        // Should match lowercase version (CSS selector is case-insensitive for name)
        assert_eq!(card.title, Some("Lowercase".to_string()));
    }

    #[test]
    fn test_special_characters_in_content() {
        let html = r#"
            <meta name="twitter:title" content="Title with &quot;quotes&quot; &amp; &lt;tags&gt;">
            <meta name="twitter:description" content="Description with &#x27;apostrophes&#x27;">
        "#;
        let card = extract(html, None).unwrap();
        // HTML entities should be decoded
        assert!(card.title.is_some());
        assert!(card.description.is_some());
    }

    #[test]
    fn test_player_without_dimensions() {
        let html = r#"<meta name="twitter:player" content="https://example.com/player">"#;
        let card = extract(html, None).unwrap();
        assert!(card.player.is_some());
        let player = card.player.unwrap();
        assert_eq!(player.url, "https://example.com/player");
        assert_eq!(player.width, None);
        assert_eq!(player.height, None);
        assert_eq!(player.stream, None);
    }

    #[test]
    fn test_app_metadata_without_main_tag() {
        // App metadata without twitter:app:name should still be captured
        let html = r#"
            <meta name="twitter:app:id:iphone" content="123456">
            <meta name="twitter:app:url:iphone" content="myapp://open">
        "#;
        let card = extract(html, None).unwrap();
        assert!(card.app.is_some());
        let app = card.app.unwrap();
        assert_eq!(app.id_iphone, Some("123456".to_string()));
    }

    #[test]
    fn test_player_metadata_without_player_tag() {
        // Player dimensions without twitter:player URL should be ignored
        let html = r#"
            <meta name="twitter:player:width" content="1280">
            <meta name="twitter:player:height" content="720">
        "#;
        let card = extract(html, None).unwrap();
        // Without player URL, no player object should be created
        assert_eq!(card.player, None);
    }

    #[test]
    fn test_complete_card() {
        let html = r#"
            <meta name="twitter:card" content="summary_large_image">
            <meta name="twitter:site" content="@example">
            <meta name="twitter:site:id" content="111111">
            <meta name="twitter:creator" content="@author">
            <meta name="twitter:creator:id" content="222222">
            <meta name="twitter:title" content="Complete Example">
            <meta name="twitter:description" content="All fields filled">
            <meta name="twitter:image" content="https://example.com/image.jpg">
            <meta name="twitter:image:alt" content="Image description">
        "#;
        let card = extract(html, None).unwrap();

        assert_eq!(card.card, Some("summary_large_image".to_string()));
        assert_eq!(card.site, Some("@example".to_string()));
        assert_eq!(card.site_id, Some("111111".to_string()));
        assert_eq!(card.creator, Some("@author".to_string()));
        assert_eq!(card.creator_id, Some("222222".to_string()));
        assert_eq!(card.title, Some("Complete Example".to_string()));
        assert_eq!(card.description, Some("All fields filled".to_string()));
        assert_eq!(card.image, Some("https://example.com/image.jpg".to_string()));
        assert_eq!(card.image_alt, Some("Image description".to_string()));
    }

    // ========== PHASE 3: EDGE CASES AND STRESS TESTS ==========

    #[test]
    fn test_twitter_with_invalid_dimensions() {
        let html = r#"
            <meta name="twitter:card" content="player">
            <meta name="twitter:player" content="https://example.com/player">
            <meta name="twitter:player:width" content="invalid">
            <meta name="twitter:player:height" content="-100">
        "#;
        let card = extract(html, None).unwrap();
        // Should handle invalid dimensions gracefully
        assert!(card.player.is_some());
    }

    #[test]
    fn test_twitter_app_partial_data() {
        let html = r#"
            <meta name="twitter:card" content="app">
            <meta name="twitter:app:name:iphone" content="MyApp">
            <meta name="twitter:app:id:iphone" content="123">
        "#;
        let card = extract(html, None).unwrap();
        assert!(card.app.is_some());
        let app = card.app.unwrap();
        assert_eq!(app.name_iphone, Some("MyApp".to_string()));
        // Other platforms should be None
        assert!(app.name_ipad.is_none());
    }

    #[test]
    fn test_twitter_with_very_long_content() {
        let long_title = "T".repeat(5000);
        let html = format!(r#"<meta name="twitter:title" content="{}">"#, long_title);
        let card = extract(&html, None).unwrap();
        assert_eq!(card.title, Some(long_title));
    }

    #[test]
    fn test_twitter_with_emoji_and_unicode() {
        let html = r#"
            <meta name="twitter:title" content="🎉 Announcement! 日本語">
            <meta name="twitter:description" content="Check this out 😊 Ça marche!">
        "#;
        let card = extract(html, None).unwrap();
        assert!(card.title.unwrap().contains("🎉"));
        assert!(card.description.unwrap().contains("😊"));
    }

    #[test]
    fn test_twitter_fallback_with_og_unicode() {
        let html = r#"
            <meta property="og:title" content="日本語タイトル">
            <meta property="og:description" content="Français description">
        "#;
        let card = extract_with_fallback(html, None).unwrap();
        assert!(card.title.unwrap().contains("日本語"));
    }

    #[test]
    fn test_twitter_with_malformed_player_url() {
        let html = r#"
            <meta name="twitter:card" content="player">
            <meta name="twitter:player" content="not a url">
        "#;
        let card = extract(html, None).unwrap();
        // Should handle malformed URL gracefully
        assert!(card.player.is_some());
    }
}