.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-stringify / lib / util / copy-identifier-encoding.js
1 'use strict';
2
3 var entityPrefixLength = require('./entity-prefix-length');
4
5 module.exports = copy;
6
7 var PUNCTUATION = /[-!"#$%&'()*+,./:;<=>?@[\\\]^`{|}~_]/;
8
9 /* For shortcut and collapsed reference links, the contents
10  * is also an identifier, so we need to restore the original
11  * encoding and escaping that were present in the source
12  * string.
13  *
14  * This function takes the unescaped & unencoded value from
15  * shortcut's child nodes and the identifier and encodes
16  * the former according to the latter. */
17 function copy(value, identifier) {
18   var length = value.length;
19   var count = identifier.length;
20   var result = [];
21   var position = 0;
22   var index = 0;
23   var start;
24
25   while (index < length) {
26     /* Take next non-punctuation characters from `value`. */
27     start = index;
28
29     while (index < length && !PUNCTUATION.test(value.charAt(index))) {
30       index += 1;
31     }
32
33     result.push(value.slice(start, index));
34
35     /* Advance `position` to the next punctuation character. */
36     while (position < count && !PUNCTUATION.test(identifier.charAt(position))) {
37       position += 1;
38     }
39
40     /* Take next punctuation characters from `identifier`. */
41     start = position;
42
43     while (position < count && PUNCTUATION.test(identifier.charAt(position))) {
44       if (identifier.charAt(position) === '&') {
45         position += entityPrefixLength(identifier.slice(position));
46       }
47
48       position += 1;
49     }
50
51     result.push(identifier.slice(start, position));
52
53     /* Advance `index` to the next non-punctuation character. */
54     while (index < length && PUNCTUATION.test(value.charAt(index))) {
55       index += 1;
56     }
57   }
58
59   return result.join('');
60 }