|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { type DefaultTreeAdapterMap, parseFragment } from 'parse5' |
| 3 | +import { getNodeAssetAttributes } from '../assetSource' |
| 4 | + |
| 5 | +describe('getNodeAssetAttributes', () => { |
| 6 | + const getNode = (html: string) => { |
| 7 | + const ast = parseFragment(html, { sourceCodeLocationInfo: true }) |
| 8 | + return ast.childNodes[0] as DefaultTreeAdapterMap['element'] |
| 9 | + } |
| 10 | + |
| 11 | + test('handles img src', () => { |
| 12 | + const node = getNode('<img src="foo.jpg">') |
| 13 | + const attrs = getNodeAssetAttributes(node) |
| 14 | + expect(attrs).toHaveLength(1) |
| 15 | + expect(attrs[0]).toHaveProperty('type', 'src') |
| 16 | + expect(attrs[0]).toHaveProperty('key', 'src') |
| 17 | + expect(attrs[0]).toHaveProperty('value', 'foo.jpg') |
| 18 | + expect(attrs[0].attributes).toEqual({ src: 'foo.jpg' }) |
| 19 | + expect(attrs[0].location).toHaveProperty('startOffset', 5) |
| 20 | + expect(attrs[0].location).toHaveProperty('endOffset', 18) |
| 21 | + }) |
| 22 | + |
| 23 | + test('handles source srcset', () => { |
| 24 | + const node = getNode('<source srcset="foo.jpg 1x, bar.jpg 2x">') |
| 25 | + const attrs = getNodeAssetAttributes(node) |
| 26 | + expect(attrs).toHaveLength(1) |
| 27 | + expect(attrs[0]).toHaveProperty('type', 'srcset') |
| 28 | + expect(attrs[0]).toHaveProperty('key', 'srcset') |
| 29 | + expect(attrs[0]).toHaveProperty('value', 'foo.jpg 1x, bar.jpg 2x') |
| 30 | + expect(attrs[0].attributes).toEqual({ srcset: 'foo.jpg 1x, bar.jpg 2x' }) |
| 31 | + }) |
| 32 | + |
| 33 | + test('handles video src and poster', () => { |
| 34 | + const node = getNode('<video src="video.mp4" poster="poster.jpg">') |
| 35 | + const attrs = getNodeAssetAttributes(node) |
| 36 | + expect(attrs).toHaveLength(2) |
| 37 | + expect(attrs[0]).toHaveProperty('type', 'src') |
| 38 | + expect(attrs[0]).toHaveProperty('key', 'src') |
| 39 | + expect(attrs[0]).toHaveProperty('value', 'video.mp4') |
| 40 | + expect(attrs[0].attributes).toEqual({ |
| 41 | + src: 'video.mp4', |
| 42 | + poster: 'poster.jpg', |
| 43 | + }) |
| 44 | + expect(attrs[1]).toHaveProperty('type', 'src') |
| 45 | + expect(attrs[1]).toHaveProperty('key', 'poster') |
| 46 | + expect(attrs[1]).toHaveProperty('value', 'poster.jpg') |
| 47 | + }) |
| 48 | + |
| 49 | + test('handles link with allowed rel', () => { |
| 50 | + const node = getNode('<link rel="stylesheet" href="style.css">') |
| 51 | + const attrs = getNodeAssetAttributes(node) |
| 52 | + expect(attrs).toHaveLength(1) |
| 53 | + expect(attrs[0]).toHaveProperty('type', 'src') |
| 54 | + expect(attrs[0]).toHaveProperty('key', 'href') |
| 55 | + expect(attrs[0]).toHaveProperty('value', 'style.css') |
| 56 | + expect(attrs[0].attributes).toEqual({ |
| 57 | + rel: 'stylesheet', |
| 58 | + href: 'style.css', |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + test('handles meta with allowed name', () => { |
| 63 | + const node = getNode('<meta name="twitter:image" content="image.jpg">') |
| 64 | + const attrs = getNodeAssetAttributes(node) |
| 65 | + expect(attrs).toHaveLength(1) |
| 66 | + expect(attrs[0]).toHaveProperty('type', 'src') |
| 67 | + expect(attrs[0]).toHaveProperty('key', 'content') |
| 68 | + expect(attrs[0]).toHaveProperty('value', 'image.jpg') |
| 69 | + }) |
| 70 | + |
| 71 | + test('handles meta with allowed property', () => { |
| 72 | + const node = getNode('<meta property="og:image" content="image.jpg">') |
| 73 | + const attrs = getNodeAssetAttributes(node) |
| 74 | + expect(attrs).toHaveLength(1) |
| 75 | + expect(attrs[0]).toHaveProperty('type', 'src') |
| 76 | + expect(attrs[0]).toHaveProperty('key', 'content') |
| 77 | + expect(attrs[0]).toHaveProperty('value', 'image.jpg') |
| 78 | + }) |
| 79 | + |
| 80 | + test('does not handle meta with unknown name', () => { |
| 81 | + const node = getNode('<meta name="unknown" content="image.jpg">') |
| 82 | + const attrs = getNodeAssetAttributes(node) |
| 83 | + expect(attrs).toHaveLength(0) |
| 84 | + }) |
| 85 | + |
| 86 | + test('does not handle meta with unknown property', () => { |
| 87 | + const node = getNode('<meta property="unknown" content="image.jpg">') |
| 88 | + const attrs = getNodeAssetAttributes(node) |
| 89 | + expect(attrs).toHaveLength(0) |
| 90 | + }) |
| 91 | + |
| 92 | + test('does not handle meta with no known properties', () => { |
| 93 | + const node = getNode('<meta foo="bar" content="image.jpg">') |
| 94 | + const attrs = getNodeAssetAttributes(node) |
| 95 | + expect(attrs).toHaveLength(0) |
| 96 | + }) |
| 97 | +}) |
0 commit comments