Skip to content

Commit 96956fd

Browse files
committed
chore: lf eol
1 parent bdb402b commit 96956fd

File tree

2 files changed

+184
-184
lines changed

2 files changed

+184
-184
lines changed

lib/index.js

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
const qs = require('query-string')
2-
const isUrl = require('is-url-superb')
3-
const matchHelper = require('posthtml-match-helper')
4-
5-
module.exports = (config = {}) => tree => {
6-
config.strict = typeof config.strict === 'boolean' ? config.strict : true
7-
8-
const process = node => {
9-
if (!config || !config.parameters) {
10-
return node
11-
}
12-
13-
const tags = Array.isArray(config.tags) ? config.tags : ['a']
14-
15-
const knownAttributes = new Set(config.attributes || ['href', 'src', 'poster', 'srcset', 'background'])
16-
17-
tree.match(matchHelper(tags.join(',')), node => {
18-
if (!node.attrs) {
19-
return node
20-
}
21-
22-
const matchingAttribute = Object.keys(node.attrs).find(key => knownAttributes.has(key))
23-
24-
if (!matchingAttribute) {
25-
return node
26-
}
27-
28-
const url = node.attrs[matchingAttribute]
29-
const parsed = qs.parseUrl(url, config.qs)
30-
31-
if (config.strict && !isUrl(parsed.url.trim())) {
32-
return node
33-
}
34-
35-
Object.keys(config.parameters).forEach(item => {
36-
parsed.query[item] = config.parameters[item]
37-
})
38-
39-
node.attrs[matchingAttribute] = qs.stringifyUrl(parsed, config.qs)
40-
41-
return node
42-
})
43-
44-
return node
45-
}
46-
47-
return new Promise(resolve => {
48-
tree.walk(process)
49-
resolve(tree)
50-
})
51-
}
1+
const qs = require('query-string')
2+
const isUrl = require('is-url-superb')
3+
const matchHelper = require('posthtml-match-helper')
4+
5+
module.exports = (config = {}) => tree => {
6+
config.strict = typeof config.strict === 'boolean' ? config.strict : true
7+
8+
const process = node => {
9+
if (!config || !config.parameters) {
10+
return node
11+
}
12+
13+
const tags = Array.isArray(config.tags) ? config.tags : ['a']
14+
15+
const knownAttributes = new Set(config.attributes || ['href', 'src', 'poster', 'srcset', 'background'])
16+
17+
tree.match(matchHelper(tags.join(',')), node => {
18+
if (!node.attrs) {
19+
return node
20+
}
21+
22+
const matchingAttribute = Object.keys(node.attrs).find(key => knownAttributes.has(key))
23+
24+
if (!matchingAttribute) {
25+
return node
26+
}
27+
28+
const url = node.attrs[matchingAttribute]
29+
const parsed = qs.parseUrl(url, config.qs)
30+
31+
if (config.strict && !isUrl(parsed.url.trim())) {
32+
return node
33+
}
34+
35+
Object.keys(config.parameters).forEach(item => {
36+
parsed.query[item] = config.parameters[item]
37+
})
38+
39+
node.attrs[matchingAttribute] = qs.stringifyUrl(parsed, config.qs)
40+
41+
return node
42+
})
43+
44+
return node
45+
}
46+
47+
return new Promise(resolve => {
48+
tree.walk(process)
49+
resolve(tree)
50+
})
51+
}

test/test.js

Lines changed: 133 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,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-
})
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

Comments
 (0)