|
1 | | -const test = require('ava') |
2 | | -const plugin = require('../lib') |
3 | | -const posthtml = require('posthtml') |
4 | | - |
5 | | -const clean = html => html.replace(/[^\S\r\n]+$/gm, '').trim() |
6 | | - |
7 | | -const process = (html, options, log = false) => { |
8 | | - return posthtml([plugin(options)]) |
9 | | - .process(html) |
10 | | - .then(result => log ? console.log(result.html) : clean(result.html)) |
11 | | -} |
12 | | - |
13 | | -test('Skip if config or parameters missing', async t => { |
14 | | - const html = await process('<a href="https://example.com">Test</a>') |
15 | | - |
16 | | - t.is(html, '<a href="https://example.com">Test</a>') |
17 | | -}) |
18 | | - |
19 | | -test('Skip if invalid URL (`strict` enabled)', async t => { |
20 | | - const html = await process('<a href="undefined">Test</a>', { |
21 | | - parameters: {foo: 'bar'} |
22 | | - }) |
23 | | - |
24 | | - t.is(html, '<a href="undefined">Test</a>') |
25 | | -}) |
26 | | - |
27 | | -test('Apply to invalid URL (`strict` disabled)', async t => { |
28 | | - const html = await process('<a href="undefined">Test</a>', { |
29 | | - parameters: {foo: 'bar'}, |
30 | | - strict: false |
31 | | - }) |
32 | | - |
33 | | - t.is(html, '<a href="undefined?foo=bar">Test</a>') |
34 | | -}) |
35 | | - |
36 | | -test('Adds parameters to a[href] attribute value', async t => { |
37 | | - const html = await process('<a href="https://example.com">Test</a>', { |
38 | | - parameters: {foo: 'bar', baz: 'qux'} |
39 | | - }) |
40 | | - |
41 | | - t.is(html, '<a href="https://example.com?baz=qux&foo=bar">Test</a>') |
42 | | -}) |
43 | | - |
44 | | -test('URL with special characters', async t => { |
45 | | - const html = await process('<a href="https://example.com/{{ var }}?foo=bar">Test</a>', { |
46 | | - parameters: {bar: 'baz'}, |
47 | | - strict: false |
48 | | - }) |
49 | | - |
50 | | - t.is(html, '<a href="https://example.com/{{ var }}?bar=baz&foo=bar">Test</a>') |
51 | | -}) |
52 | | - |
53 | | -test('Does not encode parameters if `encode` option is false', async t => { |
54 | | - const html = await process('<a href="https://example.com">Test</a>', { |
55 | | - qs: {encode: false}, |
56 | | - parameters: {foo: '@Bar@'} |
57 | | - }) |
58 | | - |
59 | | - t.is(html, '<a href="https://example.com?foo=@Bar@">Test</a>') |
60 | | -}) |
61 | | - |
62 | | -test('Does not sort parameters if `sort` option is false', async t => { |
63 | | - const html = await process('<a href="https://example.com">Test</a>', { |
64 | | - qs: {sort: false}, |
65 | | - parameters: {foo: 'bar', baz: 'qux'} |
66 | | - }) |
67 | | - |
68 | | - t.is(html, '<a href="https://example.com?foo=bar&baz=qux">Test</a>') |
69 | | -}) |
70 | | - |
71 | | -test('Appends new parameters to existing parameters', async t => { |
72 | | - const html = await process('<a href="https://example.com?s=test">Test</a>', { |
73 | | - parameters: {foo: 'bar', baz: 'qux'} |
74 | | - }) |
75 | | - |
76 | | - t.is(html, '<a href="https://example.com?baz=qux&foo=bar&s=test">Test</a>') |
77 | | -}) |
78 | | - |
79 | | -test('Processes only tags provided in the `tags` option', async t => { |
80 | | - const html = await process( |
81 | | - `<a href="https://example.com">Test</a> |
82 | | - <a href="https://skip.me">Skip</a> |
83 | | - <link rel="stylesheet" href="https://example.com/style.css"> |
84 | | - <module href="https://example.com/header.html">`, |
85 | | - { |
86 | | - tags: ['a[href*="example.com"]', 'link'], |
87 | | - parameters: {foo: 'bar'} |
88 | | - } |
89 | | - ) |
90 | | - |
91 | | - t.is(html, `<a href="https://example.com?foo=bar">Test</a> |
92 | | - <a href="https://skip.me">Skip</a> |
93 | | - <link rel="stylesheet" href="https://example.com/style.css?foo=bar"> |
94 | | - <module href="https://example.com/header.html"></module>`) |
95 | | -}) |
96 | | - |
97 | | -test('Adds parameters to known attribute values', async t => { |
98 | | - const html = await process(` |
99 | | - <img src="https://example.com/image.jpg"> |
100 | | - <video poster="https://example.com/poster.jpg"></video> |
101 | | - <table><td background="https://example.com/image.jpg"></td></table> |
102 | | - `, { |
103 | | - parameters: {foo: 'bar', baz: 'qux'}, |
104 | | - tags: ['img[src]', 'video[poster]', 'td[background]'] |
105 | | - }) |
106 | | - |
107 | | - t.is(html, `<img src="https://example.com/image.jpg?baz=qux&foo=bar"> |
108 | | - <video poster="https://example.com/poster.jpg?baz=qux&foo=bar"></video> |
109 | | - <table><td background="https://example.com/image.jpg?baz=qux&foo=bar"></td></table>`) |
110 | | -}) |
111 | | - |
112 | | -test('Adds parameters to specified attribute values only', async t => { |
113 | | - const html = await process(` |
114 | | - <a href="foo.html" data-href="https://example.com">Test</a> |
115 | | - <img src="image.jpg"> |
116 | | - `, { |
117 | | - parameters: {foo: 'bar'}, |
118 | | - tags: ['a', 'img'], |
119 | | - attributes: ['data-href'] |
120 | | - }) |
121 | | - |
122 | | - t.is(html, `<a href="foo.html" data-href="https://example.com?foo=bar">Test</a> |
123 | | - <img src="image.jpg">`) |
124 | | -}) |
125 | | - |
126 | | -test('Skip if node has no attributes', async t => { |
127 | | - const html = await process('<a>Test</a>', { |
128 | | - parameters: {foo: 'bar'}, |
129 | | - tags: ['a'] |
130 | | - }) |
131 | | - |
132 | | - t.is(html, '<a>Test</a>') |
133 | | -}) |
| 1 | +const test = require('ava') |
| 2 | +const plugin = require('../lib') |
| 3 | +const posthtml = require('posthtml') |
| 4 | + |
| 5 | +const clean = html => html.replace(/[^\S\r\n]+$/gm, '').trim() |
| 6 | + |
| 7 | +const process = (html, options, log = false) => { |
| 8 | + return posthtml([plugin(options)]) |
| 9 | + .process(html) |
| 10 | + .then(result => log ? console.log(result.html) : clean(result.html)) |
| 11 | +} |
| 12 | + |
| 13 | +test('Skip if config or parameters missing', async t => { |
| 14 | + const html = await process('<a href="https://example.com">Test</a>') |
| 15 | + |
| 16 | + t.is(html, '<a href="https://example.com">Test</a>') |
| 17 | +}) |
| 18 | + |
| 19 | +test('Skip if invalid URL (`strict` enabled)', async t => { |
| 20 | + const html = await process('<a href="undefined">Test</a>', { |
| 21 | + parameters: {foo: 'bar'} |
| 22 | + }) |
| 23 | + |
| 24 | + t.is(html, '<a href="undefined">Test</a>') |
| 25 | +}) |
| 26 | + |
| 27 | +test('Apply to invalid URL (`strict` disabled)', async t => { |
| 28 | + const html = await process('<a href="undefined">Test</a>', { |
| 29 | + parameters: {foo: 'bar'}, |
| 30 | + strict: false |
| 31 | + }) |
| 32 | + |
| 33 | + t.is(html, '<a href="undefined?foo=bar">Test</a>') |
| 34 | +}) |
| 35 | + |
| 36 | +test('Adds parameters to a[href] attribute value', async t => { |
| 37 | + const html = await process('<a href="https://example.com">Test</a>', { |
| 38 | + parameters: {foo: 'bar', baz: 'qux'} |
| 39 | + }) |
| 40 | + |
| 41 | + t.is(html, '<a href="https://example.com?baz=qux&foo=bar">Test</a>') |
| 42 | +}) |
| 43 | + |
| 44 | +test('URL with special characters', async t => { |
| 45 | + const html = await process('<a href="https://example.com/{{ var }}?foo=bar">Test</a>', { |
| 46 | + parameters: {bar: 'baz'}, |
| 47 | + strict: false |
| 48 | + }) |
| 49 | + |
| 50 | + t.is(html, '<a href="https://example.com/{{ var }}?bar=baz&foo=bar">Test</a>') |
| 51 | +}) |
| 52 | + |
| 53 | +test('Does not encode parameters if `encode` option is false', async t => { |
| 54 | + const html = await process('<a href="https://example.com">Test</a>', { |
| 55 | + qs: {encode: false}, |
| 56 | + parameters: {foo: '@Bar@'} |
| 57 | + }) |
| 58 | + |
| 59 | + t.is(html, '<a href="https://example.com?foo=@Bar@">Test</a>') |
| 60 | +}) |
| 61 | + |
| 62 | +test('Does not sort parameters if `sort` option is false', async t => { |
| 63 | + const html = await process('<a href="https://example.com">Test</a>', { |
| 64 | + qs: {sort: false}, |
| 65 | + parameters: {foo: 'bar', baz: 'qux'} |
| 66 | + }) |
| 67 | + |
| 68 | + t.is(html, '<a href="https://example.com?foo=bar&baz=qux">Test</a>') |
| 69 | +}) |
| 70 | + |
| 71 | +test('Appends new parameters to existing parameters', async t => { |
| 72 | + const html = await process('<a href="https://example.com?s=test">Test</a>', { |
| 73 | + parameters: {foo: 'bar', baz: 'qux'} |
| 74 | + }) |
| 75 | + |
| 76 | + t.is(html, '<a href="https://example.com?baz=qux&foo=bar&s=test">Test</a>') |
| 77 | +}) |
| 78 | + |
| 79 | +test('Processes only tags provided in the `tags` option', async t => { |
| 80 | + const html = await process( |
| 81 | + `<a href="https://example.com">Test</a> |
| 82 | + <a href="https://skip.me">Skip</a> |
| 83 | + <link rel="stylesheet" href="https://example.com/style.css"> |
| 84 | + <module href="https://example.com/header.html">`, |
| 85 | + { |
| 86 | + tags: ['a[href*="example.com"]', 'link'], |
| 87 | + parameters: {foo: 'bar'} |
| 88 | + } |
| 89 | + ) |
| 90 | + |
| 91 | + t.is(html, `<a href="https://example.com?foo=bar">Test</a> |
| 92 | + <a href="https://skip.me">Skip</a> |
| 93 | + <link rel="stylesheet" href="https://example.com/style.css?foo=bar"> |
| 94 | + <module href="https://example.com/header.html"></module>`) |
| 95 | +}) |
| 96 | + |
| 97 | +test('Adds parameters to known attribute values', async t => { |
| 98 | + const html = await process(` |
| 99 | + <img src="https://example.com/image.jpg"> |
| 100 | + <video poster="https://example.com/poster.jpg"></video> |
| 101 | + <table><td background="https://example.com/image.jpg"></td></table> |
| 102 | + `, { |
| 103 | + parameters: {foo: 'bar', baz: 'qux'}, |
| 104 | + tags: ['img[src]', 'video[poster]', 'td[background]'] |
| 105 | + }) |
| 106 | + |
| 107 | + t.is(html, `<img src="https://example.com/image.jpg?baz=qux&foo=bar"> |
| 108 | + <video poster="https://example.com/poster.jpg?baz=qux&foo=bar"></video> |
| 109 | + <table><td background="https://example.com/image.jpg?baz=qux&foo=bar"></td></table>`) |
| 110 | +}) |
| 111 | + |
| 112 | +test('Adds parameters to specified attribute values only', async t => { |
| 113 | + const html = await process(` |
| 114 | + <a href="foo.html" data-href="https://example.com">Test</a> |
| 115 | + <img src="image.jpg"> |
| 116 | + `, { |
| 117 | + parameters: {foo: 'bar'}, |
| 118 | + tags: ['a', 'img'], |
| 119 | + attributes: ['data-href'] |
| 120 | + }) |
| 121 | + |
| 122 | + t.is(html, `<a href="foo.html" data-href="https://example.com?foo=bar">Test</a> |
| 123 | + <img src="image.jpg">`) |
| 124 | +}) |
| 125 | + |
| 126 | +test('Skip if node has no attributes', async t => { |
| 127 | + const html = await process('<a>Test</a>', { |
| 128 | + parameters: {foo: 'bar'}, |
| 129 | + tags: ['a'] |
| 130 | + }) |
| 131 | + |
| 132 | + t.is(html, '<a>Test</a>') |
| 133 | +}) |
0 commit comments