1 | (function () {
|
2 |
|
3 | if (typeof Prism === 'undefined' || typeof document === 'undefined') {
|
4 | return;
|
5 | }
|
6 |
|
7 | function mapClassName(name) {
|
8 | var customClass = Prism.plugins.customClass;
|
9 | if (customClass) {
|
10 | return customClass.apply(name, 'none');
|
11 | } else {
|
12 | return name;
|
13 | }
|
14 | }
|
15 |
|
16 | var PARTNER = {
|
17 | '(': ')',
|
18 | '[': ']',
|
19 | '{': '}',
|
20 | };
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var NAMES = {
|
26 | '(': 'brace-round',
|
27 | '[': 'brace-square',
|
28 | '{': 'brace-curly',
|
29 | };
|
30 |
|
31 |
|
32 |
|
33 | var BRACE_ALIAS_MAP = {
|
34 | '${': '{',
|
35 | };
|
36 |
|
37 | var LEVEL_WARP = 12;
|
38 |
|
39 | var pairIdCounter = 0;
|
40 |
|
41 | var BRACE_ID_PATTERN = /^(pair-\d+-)(close|open)$/;
|
42 |
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function getPartnerBrace(brace) {
|
50 | var match = BRACE_ID_PATTERN.exec(brace.id);
|
51 | return document.querySelector('#' + match[1] + (match[2] == 'open' ? 'close' : 'open'));
|
52 | }
|
53 |
|
54 | |
55 |
|
56 |
|
57 | function hoverBrace() {
|
58 | if (!Prism.util.isActive(this, 'brace-hover', true)) {
|
59 | return;
|
60 | }
|
61 |
|
62 | [this, getPartnerBrace(this)].forEach(function (e) {
|
63 | e.classList.add(mapClassName('brace-hover'));
|
64 | });
|
65 | }
|
66 | |
67 |
|
68 |
|
69 | function leaveBrace() {
|
70 | [this, getPartnerBrace(this)].forEach(function (e) {
|
71 | e.classList.remove(mapClassName('brace-hover'));
|
72 | });
|
73 | }
|
74 | |
75 |
|
76 |
|
77 | function clickBrace() {
|
78 | if (!Prism.util.isActive(this, 'brace-select', true)) {
|
79 | return;
|
80 | }
|
81 |
|
82 | [this, getPartnerBrace(this)].forEach(function (e) {
|
83 | e.classList.add(mapClassName('brace-selected'));
|
84 | });
|
85 | }
|
86 |
|
87 | Prism.hooks.add('complete', function (env) {
|
88 |
|
89 |
|
90 | var code = env.element;
|
91 | var pre = code.parentElement;
|
92 |
|
93 | if (!pre || pre.tagName != 'PRE') {
|
94 | return;
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 | var toMatch = [];
|
100 | if (Prism.util.isActive(code, 'match-braces')) {
|
101 | toMatch.push('(', '[', '{');
|
102 | }
|
103 |
|
104 | if (toMatch.length == 0) {
|
105 |
|
106 | return;
|
107 | }
|
108 |
|
109 | if (!pre.__listenerAdded) {
|
110 |
|
111 | pre.addEventListener('mousedown', function removeBraceSelected() {
|
112 |
|
113 | var code = pre.querySelector('code');
|
114 | var className = mapClassName('brace-selected');
|
115 | Array.prototype.slice.call(code.querySelectorAll('.' + className)).forEach(function (e) {
|
116 | e.classList.remove(className);
|
117 | });
|
118 | });
|
119 | Object.defineProperty(pre, '__listenerAdded', { value: true });
|
120 | }
|
121 |
|
122 |
|
123 | var punctuation = Array.prototype.slice.call(
|
124 | code.querySelectorAll('span.' + mapClassName('token') + '.' + mapClassName('punctuation'))
|
125 | );
|
126 |
|
127 |
|
128 | var allBraces = [];
|
129 |
|
130 | toMatch.forEach(function (open) {
|
131 | var close = PARTNER[open];
|
132 | var name = mapClassName(NAMES[open]);
|
133 |
|
134 |
|
135 | var pairs = [];
|
136 |
|
137 | var openStack = [];
|
138 |
|
139 | for (var i = 0; i < punctuation.length; i++) {
|
140 | var element = punctuation[i];
|
141 | if (element.childElementCount == 0) {
|
142 | var text = element.textContent;
|
143 | text = BRACE_ALIAS_MAP[text] || text;
|
144 | if (text === open) {
|
145 | allBraces.push({ index: i, open: true, element: element });
|
146 | element.classList.add(name);
|
147 | element.classList.add(mapClassName('brace-open'));
|
148 | openStack.push(i);
|
149 | } else if (text === close) {
|
150 | allBraces.push({ index: i, open: false, element: element });
|
151 | element.classList.add(name);
|
152 | element.classList.add(mapClassName('brace-close'));
|
153 | if (openStack.length) {
|
154 | pairs.push([i, openStack.pop()]);
|
155 | }
|
156 | }
|
157 | }
|
158 | }
|
159 |
|
160 | pairs.forEach(function (pair) {
|
161 | var pairId = 'pair-' + (pairIdCounter++) + '-';
|
162 |
|
163 | var opening = punctuation[pair[0]];
|
164 | var closing = punctuation[pair[1]];
|
165 |
|
166 | opening.id = pairId + 'open';
|
167 | closing.id = pairId + 'close';
|
168 |
|
169 | [opening, closing].forEach(function (e) {
|
170 | e.addEventListener('mouseenter', hoverBrace);
|
171 | e.addEventListener('mouseleave', leaveBrace);
|
172 | e.addEventListener('click', clickBrace);
|
173 | });
|
174 | });
|
175 | });
|
176 |
|
177 | var level = 0;
|
178 | allBraces.sort(function (a, b) { return a.index - b.index; });
|
179 | allBraces.forEach(function (brace) {
|
180 | if (brace.open) {
|
181 | brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
|
182 | level++;
|
183 | } else {
|
184 | level = Math.max(0, level - 1);
|
185 | brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
|
186 | }
|
187 | });
|
188 | });
|
189 |
|
190 | }());
|