WordPress Infinite Scroll – Ajax Load More - Version 5.1.7.1

Version Description

  • December 9, 2019 =
  • FIX - Hotfix to remove JavaSscript error/warning message for Single Posts and Next Page add-on users.
Download this release

Release Info

Developer dcooney
Plugin Icon 128x128 WordPress Infinite Scroll – Ajax Load More
Version 5.1.7.1
Comparing to
See all releases

Code changes from version 5.1.6.1 to 5.1.7.1

README.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://connekthq.com/donate/
4
  Tags: infinite scroll, infinite scrolling, scroll, infinite, lazy load, lazy loading, endless scroll, pagination, ajax pagination, ajax, ajax posts, woocommerce, ajax load more, masonry
5
  Requires at least: 4.0
6
  Tested up to: 5.3
7
- Stable tag: 5.1.6.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -382,6 +382,22 @@ How to install Ajax Load More.
382
 
383
  == Changelog ==
384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
  = 5.1.6.1 - November 19, 2019 =
386
  * NEW - Added new `placeholder` parameter that allows users to display a placeholder image while Ajax content is being loaded. [View Example](https://connekthq.com/plugins/ajax-load-more/examples/placeholder/).
387
  * FIX - Fixed issue with deep linking URLs in the filters add-on not sending user to correct location on the page.
4
  Tags: infinite scroll, infinite scrolling, scroll, infinite, lazy load, lazy loading, endless scroll, pagination, ajax pagination, ajax, ajax posts, woocommerce, ajax load more, masonry
5
  Requires at least: 4.0
6
  Tested up to: 5.3
7
+ Stable tag: 5.1.7.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
382
 
383
  == Changelog ==
384
 
385
+ = 5.1.7.1 - December 9, 2019 =
386
+ * FIX - Hotfix to remove JavaSscript error/warning message for Single Posts and Next Page add-on users.
387
+
388
+
389
+ = 5.1.7 - December 6, 2019 =
390
+ * NEW - Added support for multiple instances on ALM Masonry on the same page.
391
+ * NEW - Added `CTRL+S` and `CMD+S` support for saving Repeater Templates in the Ajax Load More admin :)
392
+ * NEW - Added built-in support for Elementor using the `elementor="true"` shortcode parameter. [View Blog Post](https://connekthq.com/elementor-infinite-scrolling/)
393
+ * FIX - Fixed issue where loading `placeholder` would remain on screen if no results were found.
394
+ * FIX - Added fix for Safari desktop moving browser window when an instance of ALM is filtered.
395
+ * FIX - Fixed error in PHP 7.2.2 in the `plugin_action_links_` filter related to a string array.
396
+ * UPDATE - Added functionality to get the current post ID is not specified in the shortcode for Single Posts and Next page add-ons.
397
+ * UPDATE - Updated Code Mirror code editor for Repeater Templates.
398
+ * UPDATE - Improved admin UI and UX.
399
+
400
+
401
  = 5.1.6.1 - November 19, 2019 =
402
  * NEW - Added new `placeholder` parameter that allows users to display a placeholder image while Ajax content is being loaded. [View Example](https://connekthq.com/plugins/ajax-load-more/examples/placeholder/).
403
  * FIX - Fixed issue with deep linking URLs in the filters add-on not sending user to correct location on the page.
admin/codemirror/lib/codemirror.css CHANGED
@@ -4,10 +4,8 @@
4
  /* Set height, width, borders, and global font properties here */
5
  font-family: monospace;
6
  height: 300px;
7
- }
8
- .CodeMirror-scroll {
9
- /* Set scrolling behaviour here */
10
- overflow: auto;
11
  }
12
 
13
  /* PADDING */
@@ -15,7 +13,8 @@
15
  .CodeMirror-lines {
16
  padding: 4px 0; /* Vertical padding around content */
17
  }
18
- .CodeMirror pre {
 
19
  padding: 0 4px; /* Horizontal padding of content */
20
  }
21
 
@@ -36,8 +35,7 @@
36
  min-width: 20px;
37
  text-align: right;
38
  color: #999;
39
- -moz-box-sizing: content-box;
40
- box-sizing: content-box;
41
  }
42
 
43
  .CodeMirror-guttermarker { color: black; }
@@ -45,53 +43,80 @@
45
 
46
  /* CURSOR */
47
 
48
- .CodeMirror div.CodeMirror-cursor {
49
  border-left: 1px solid black;
 
 
50
  }
51
  /* Shown when moving in bi-directional text */
52
  .CodeMirror div.CodeMirror-secondarycursor {
53
  border-left: 1px solid silver;
54
  }
55
- .CodeMirror.cm-keymap-fat-cursor div.CodeMirror-cursor {
56
  width: auto;
57
- border: 0;
58
  background: #7e7;
59
  }
 
 
 
 
 
 
 
 
 
60
  .cm-animate-fat-cursor {
61
  width: auto;
62
  border: 0;
63
  -webkit-animation: blink 1.06s steps(1) infinite;
64
  -moz-animation: blink 1.06s steps(1) infinite;
65
  animation: blink 1.06s steps(1) infinite;
 
66
  }
67
  @-moz-keyframes blink {
68
- 0% { background: #7e7; }
69
- 50% { background: none; }
70
- 100% { background: #7e7; }
71
  }
72
  @-webkit-keyframes blink {
73
- 0% { background: #7e7; }
74
- 50% { background: none; }
75
- 100% { background: #7e7; }
76
  }
77
  @keyframes blink {
78
- 0% { background: #7e7; }
79
- 50% { background: none; }
80
- 100% { background: #7e7; }
81
  }
82
 
83
  /* Can style cursor different in overwrite (non-insert) mode */
84
- div.CodeMirror-overwrite div.CodeMirror-cursor {}
85
 
86
- .cm-tab { display: inline-block; }
87
 
 
 
 
 
 
88
  .CodeMirror-ruler {
89
  border-left: 1px solid #ccc;
 
90
  position: absolute;
91
  }
92
 
93
  /* DEFAULT THEME */
94
 
 
 
 
 
 
 
 
 
 
95
  .cm-s-default .cm-keyword {color: #708;}
96
  .cm-s-default .cm-atom {color: #219;}
97
  .cm-s-default .cm-number {color: #164;}
@@ -101,7 +126,7 @@ div.CodeMirror-overwrite div.CodeMirror-cursor {}
101
  .cm-s-default .cm-property,
102
  .cm-s-default .cm-operator {}
103
  .cm-s-default .cm-variable-2 {color: #05a;}
104
- .cm-s-default .cm-variable-3 {color: #085;}
105
  .cm-s-default .cm-comment {color: #a50;}
106
  .cm-s-default .cm-string {color: #a11;}
107
  .cm-s-default .cm-string-2 {color: #f50;}
@@ -111,24 +136,18 @@ div.CodeMirror-overwrite div.CodeMirror-cursor {}
111
  .cm-s-default .cm-bracket {color: #997;}
112
  .cm-s-default .cm-tag {color: #170;}
113
  .cm-s-default .cm-attribute {color: #00c;}
114
- .cm-s-default .cm-header {color: blue;}
115
- .cm-s-default .cm-quote {color: #090;}
116
  .cm-s-default .cm-hr {color: #999;}
117
  .cm-s-default .cm-link {color: #00c;}
118
 
119
- .cm-negative {color: #d44;}
120
- .cm-positive {color: #292;}
121
- .cm-header, .cm-strong {font-weight: bold;}
122
- .cm-em {font-style: italic;}
123
- .cm-link {text-decoration: underline;}
124
-
125
  .cm-s-default .cm-error {color: #f00;}
126
  .cm-invalidchar {color: #f00;}
127
 
 
 
128
  /* Default styles for common addons */
129
 
130
- div.CodeMirror span.CodeMirror-matchingbracket {color: #0f0;}
131
- div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
132
  .CodeMirror-matchingtag { background: rgba(255, 150, 0, .3); }
133
  .CodeMirror-activeline-background {background: #e8f2ff;}
134
 
@@ -138,14 +157,13 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
138
  the editor. You probably shouldn't touch them. */
139
 
140
  .CodeMirror {
141
- line-height: 1;
142
  position: relative;
143
  overflow: hidden;
144
  background: white;
145
- color: black;
146
  }
147
 
148
  .CodeMirror-scroll {
 
149
  /* 30px is the magic margin used to hide the element's real scrollbars */
150
  /* See overflow: hidden in .CodeMirror */
151
  margin-bottom: -30px; margin-right: -30px;
@@ -153,18 +171,14 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
153
  height: 100%;
154
  outline: none; /* Prevent dragging from highlighting the element */
155
  position: relative;
156
- -moz-box-sizing: content-box;
157
- box-sizing: content-box;
158
  }
159
  .CodeMirror-sizer {
160
  position: relative;
161
  border-right: 30px solid transparent;
162
- -moz-box-sizing: content-box;
163
- box-sizing: content-box;
164
  }
165
 
166
  /* The fake, visible scrollbars. Used to force redraw during scrolling
167
- before actuall scrolling happens, thus preventing shaking and
168
  flickering artifacts. */
169
  .CodeMirror-vscrollbar, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-gutter-filler {
170
  position: absolute;
@@ -190,31 +204,41 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
190
 
191
  .CodeMirror-gutters {
192
  position: absolute; left: 0; top: 0;
193
- padding-bottom: 30px;
194
  z-index: 3;
195
  }
196
  .CodeMirror-gutter {
197
  white-space: normal;
198
  height: 100%;
199
- -moz-box-sizing: content-box;
200
- box-sizing: content-box;
201
- padding-bottom: 30px;
202
- margin-bottom: -32px;
203
  display: inline-block;
204
- /* Hack to make IE7 behave */
205
- *zoom:1;
206
- *display:inline;
 
 
 
 
 
 
 
 
 
 
207
  }
208
  .CodeMirror-gutter-elt {
209
  position: absolute;
210
  cursor: default;
211
  z-index: 4;
212
  }
 
 
213
 
214
  .CodeMirror-lines {
215
  cursor: text;
 
216
  }
217
- .CodeMirror pre {
 
218
  /* Reset some styles that the rest of the page might have set */
219
  -moz-border-radius: 0; -webkit-border-radius: 0; border-radius: 0;
220
  border-width: 0;
@@ -229,8 +253,12 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
229
  z-index: 2;
230
  position: relative;
231
  overflow: visible;
 
 
 
232
  }
233
- .CodeMirror-wrap pre {
 
234
  word-wrap: break-word;
235
  white-space: pre-wrap;
236
  word-break: normal;
@@ -245,13 +273,25 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
245
  .CodeMirror-linewidget {
246
  position: relative;
247
  z-index: 2;
248
- overflow: auto;
249
  }
250
 
251
  .CodeMirror-widget {}
252
 
253
- .CodeMirror-wrap .CodeMirror-scroll {
254
- overflow-x: hidden;
 
 
 
 
 
 
 
 
 
 
 
 
255
  }
256
 
257
  .CodeMirror-measure {
@@ -261,19 +301,22 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
261
  overflow: hidden;
262
  visibility: hidden;
263
  }
264
- .CodeMirror-measure pre { position: static; }
265
 
266
- .CodeMirror div.CodeMirror-cursor {
267
  position: absolute;
268
- border-right: none;
269
- width: 0;
270
  }
 
271
 
272
  div.CodeMirror-cursors {
273
  visibility: hidden;
274
  position: relative;
275
- z-index: 1;
 
 
 
276
  }
 
277
  .CodeMirror-focused div.CodeMirror-cursors {
278
  visibility: visible;
279
  }
@@ -281,15 +324,14 @@ div.CodeMirror-cursors {
281
  .CodeMirror-selected { background: #d9d9d9; }
282
  .CodeMirror-focused .CodeMirror-selected { background: #d7d4f0; }
283
  .CodeMirror-crosshair { cursor: crosshair; }
 
 
284
 
285
  .cm-searching {
286
- background: #ffa;
287
- background: rgba(255, 255, 0, .4);
288
  }
289
 
290
- /* IE7 hack to prevent it from returning funny offsetTops on the spans */
291
- .CodeMirror span { *vertical-align: text-bottom; }
292
-
293
  /* Used to force a border model for a node */
294
  .cm-force-border { padding-right: .1px; }
295
 
@@ -299,3 +341,9 @@ div.CodeMirror-cursors {
299
  visibility: hidden;
300
  }
301
  }
 
 
 
 
 
 
4
  /* Set height, width, borders, and global font properties here */
5
  font-family: monospace;
6
  height: 300px;
7
+ color: black;
8
+ direction: ltr;
 
 
9
  }
10
 
11
  /* PADDING */
13
  .CodeMirror-lines {
14
  padding: 4px 0; /* Vertical padding around content */
15
  }
16
+ .CodeMirror pre.CodeMirror-line,
17
+ .CodeMirror pre.CodeMirror-line-like {
18
  padding: 0 4px; /* Horizontal padding of content */
19
  }
20
 
35
  min-width: 20px;
36
  text-align: right;
37
  color: #999;
38
+ white-space: nowrap;
 
39
  }
40
 
41
  .CodeMirror-guttermarker { color: black; }
43
 
44
  /* CURSOR */
45
 
46
+ .CodeMirror-cursor {
47
  border-left: 1px solid black;
48
+ border-right: none;
49
+ width: 0;
50
  }
51
  /* Shown when moving in bi-directional text */
52
  .CodeMirror div.CodeMirror-secondarycursor {
53
  border-left: 1px solid silver;
54
  }
55
+ .cm-fat-cursor .CodeMirror-cursor {
56
  width: auto;
57
+ border: 0 !important;
58
  background: #7e7;
59
  }
60
+ .cm-fat-cursor div.CodeMirror-cursors {
61
+ z-index: 1;
62
+ }
63
+ .cm-fat-cursor-mark {
64
+ background-color: rgba(20, 255, 20, 0.5);
65
+ -webkit-animation: blink 1.06s steps(1) infinite;
66
+ -moz-animation: blink 1.06s steps(1) infinite;
67
+ animation: blink 1.06s steps(1) infinite;
68
+ }
69
  .cm-animate-fat-cursor {
70
  width: auto;
71
  border: 0;
72
  -webkit-animation: blink 1.06s steps(1) infinite;
73
  -moz-animation: blink 1.06s steps(1) infinite;
74
  animation: blink 1.06s steps(1) infinite;
75
+ background-color: #7e7;
76
  }
77
  @-moz-keyframes blink {
78
+ 0% {}
79
+ 50% { background-color: transparent; }
80
+ 100% {}
81
  }
82
  @-webkit-keyframes blink {
83
+ 0% {}
84
+ 50% { background-color: transparent; }
85
+ 100% {}
86
  }
87
  @keyframes blink {
88
+ 0% {}
89
+ 50% { background-color: transparent; }
90
+ 100% {}
91
  }
92
 
93
  /* Can style cursor different in overwrite (non-insert) mode */
94
+ .CodeMirror-overwrite .CodeMirror-cursor {}
95
 
96
+ .cm-tab { display: inline-block; text-decoration: inherit; }
97
 
98
+ .CodeMirror-rulers {
99
+ position: absolute;
100
+ left: 0; right: 0; top: -50px; bottom: 0;
101
+ overflow: hidden;
102
+ }
103
  .CodeMirror-ruler {
104
  border-left: 1px solid #ccc;
105
+ top: 0; bottom: 0;
106
  position: absolute;
107
  }
108
 
109
  /* DEFAULT THEME */
110
 
111
+ .cm-s-default .cm-header {color: blue;}
112
+ .cm-s-default .cm-quote {color: #090;}
113
+ .cm-negative {color: #d44;}
114
+ .cm-positive {color: #292;}
115
+ .cm-header, .cm-strong {font-weight: bold;}
116
+ .cm-em {font-style: italic;}
117
+ .cm-link {text-decoration: underline;}
118
+ .cm-strikethrough {text-decoration: line-through;}
119
+
120
  .cm-s-default .cm-keyword {color: #708;}
121
  .cm-s-default .cm-atom {color: #219;}
122
  .cm-s-default .cm-number {color: #164;}
126
  .cm-s-default .cm-property,
127
  .cm-s-default .cm-operator {}
128
  .cm-s-default .cm-variable-2 {color: #05a;}
129
+ .cm-s-default .cm-variable-3, .cm-s-default .cm-type {color: #085;}
130
  .cm-s-default .cm-comment {color: #a50;}
131
  .cm-s-default .cm-string {color: #a11;}
132
  .cm-s-default .cm-string-2 {color: #f50;}
136
  .cm-s-default .cm-bracket {color: #997;}
137
  .cm-s-default .cm-tag {color: #170;}
138
  .cm-s-default .cm-attribute {color: #00c;}
 
 
139
  .cm-s-default .cm-hr {color: #999;}
140
  .cm-s-default .cm-link {color: #00c;}
141
 
 
 
 
 
 
 
142
  .cm-s-default .cm-error {color: #f00;}
143
  .cm-invalidchar {color: #f00;}
144
 
145
+ .CodeMirror-composing { border-bottom: 2px solid; }
146
+
147
  /* Default styles for common addons */
148
 
149
+ div.CodeMirror span.CodeMirror-matchingbracket {color: #0b0;}
150
+ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #a22;}
151
  .CodeMirror-matchingtag { background: rgba(255, 150, 0, .3); }
152
  .CodeMirror-activeline-background {background: #e8f2ff;}
153
 
157
  the editor. You probably shouldn't touch them. */
158
 
159
  .CodeMirror {
 
160
  position: relative;
161
  overflow: hidden;
162
  background: white;
 
163
  }
164
 
165
  .CodeMirror-scroll {
166
+ overflow: scroll !important; /* Things will break if this is overridden */
167
  /* 30px is the magic margin used to hide the element's real scrollbars */
168
  /* See overflow: hidden in .CodeMirror */
169
  margin-bottom: -30px; margin-right: -30px;
171
  height: 100%;
172
  outline: none; /* Prevent dragging from highlighting the element */
173
  position: relative;
 
 
174
  }
175
  .CodeMirror-sizer {
176
  position: relative;
177
  border-right: 30px solid transparent;
 
 
178
  }
179
 
180
  /* The fake, visible scrollbars. Used to force redraw during scrolling
181
+ before actual scrolling happens, thus preventing shaking and
182
  flickering artifacts. */
183
  .CodeMirror-vscrollbar, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-gutter-filler {
184
  position: absolute;
204
 
205
  .CodeMirror-gutters {
206
  position: absolute; left: 0; top: 0;
207
+ min-height: 100%;
208
  z-index: 3;
209
  }
210
  .CodeMirror-gutter {
211
  white-space: normal;
212
  height: 100%;
 
 
 
 
213
  display: inline-block;
214
+ vertical-align: top;
215
+ margin-bottom: -30px;
216
+ }
217
+ .CodeMirror-gutter-wrapper {
218
+ position: absolute;
219
+ z-index: 4;
220
+ background: none !important;
221
+ border: none !important;
222
+ }
223
+ .CodeMirror-gutter-background {
224
+ position: absolute;
225
+ top: 0; bottom: 0;
226
+ z-index: 4;
227
  }
228
  .CodeMirror-gutter-elt {
229
  position: absolute;
230
  cursor: default;
231
  z-index: 4;
232
  }
233
+ .CodeMirror-gutter-wrapper ::selection { background-color: transparent }
234
+ .CodeMirror-gutter-wrapper ::-moz-selection { background-color: transparent }
235
 
236
  .CodeMirror-lines {
237
  cursor: text;
238
+ min-height: 1px; /* prevents collapsing before first draw */
239
  }
240
+ .CodeMirror pre.CodeMirror-line,
241
+ .CodeMirror pre.CodeMirror-line-like {
242
  /* Reset some styles that the rest of the page might have set */
243
  -moz-border-radius: 0; -webkit-border-radius: 0; border-radius: 0;
244
  border-width: 0;
253
  z-index: 2;
254
  position: relative;
255
  overflow: visible;
256
+ -webkit-tap-highlight-color: transparent;
257
+ -webkit-font-variant-ligatures: contextual;
258
+ font-variant-ligatures: contextual;
259
  }
260
+ .CodeMirror-wrap pre.CodeMirror-line,
261
+ .CodeMirror-wrap pre.CodeMirror-line-like {
262
  word-wrap: break-word;
263
  white-space: pre-wrap;
264
  word-break: normal;
273
  .CodeMirror-linewidget {
274
  position: relative;
275
  z-index: 2;
276
+ padding: 0.1px; /* Force widget margins to stay inside of the container */
277
  }
278
 
279
  .CodeMirror-widget {}
280
 
281
+ .CodeMirror-rtl pre { direction: rtl; }
282
+
283
+ .CodeMirror-code {
284
+ outline: none;
285
+ }
286
+
287
+ /* Force content-box sizing for the elements where we expect it */
288
+ .CodeMirror-scroll,
289
+ .CodeMirror-sizer,
290
+ .CodeMirror-gutter,
291
+ .CodeMirror-gutters,
292
+ .CodeMirror-linenumber {
293
+ -moz-box-sizing: content-box;
294
+ box-sizing: content-box;
295
  }
296
 
297
  .CodeMirror-measure {
301
  overflow: hidden;
302
  visibility: hidden;
303
  }
 
304
 
305
+ .CodeMirror-cursor {
306
  position: absolute;
307
+ pointer-events: none;
 
308
  }
309
+ .CodeMirror-measure pre { position: static; }
310
 
311
  div.CodeMirror-cursors {
312
  visibility: hidden;
313
  position: relative;
314
+ z-index: 3;
315
+ }
316
+ div.CodeMirror-dragcursors {
317
+ visibility: visible;
318
  }
319
+
320
  .CodeMirror-focused div.CodeMirror-cursors {
321
  visibility: visible;
322
  }
324
  .CodeMirror-selected { background: #d9d9d9; }
325
  .CodeMirror-focused .CodeMirror-selected { background: #d7d4f0; }
326
  .CodeMirror-crosshair { cursor: crosshair; }
327
+ .CodeMirror-line::selection, .CodeMirror-line > span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }
328
+ .CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }
329
 
330
  .cm-searching {
331
+ background-color: #ffa;
332
+ background-color: rgba(255, 255, 0, .4);
333
  }
334
 
 
 
 
335
  /* Used to force a border model for a node */
336
  .cm-force-border { padding-right: .1px; }
337
 
341
  visibility: hidden;
342
  }
343
  }
344
+
345
+ /* See issue #2901 */
346
+ .cm-tab-wrap-hack:after { content: ''; }
347
+
348
+ /* Help users use markselection to safely style text background */
349
+ span.CodeMirror-selectedtext { background: none; }
admin/codemirror/lib/codemirror.js CHANGED
@@ -1,1445 +1,1253 @@
1
  // CodeMirror, copyright (c) by Marijn Haverbeke and others
2
- // Distributed under an MIT license: http://codemirror.net/LICENSE
3
 
4
- // This is CodeMirror (http://codemirror.net), a code editor
5
  // implemented in JavaScript on top of the browser's DOM.
6
  //
7
  // You can find some technical background for some of the code below
8
  // at http://marijnhaverbeke.nl/blog/#cm-internals .
9
 
10
- (function(mod) {
11
- if (typeof exports == "object" && typeof module == "object") // CommonJS
12
- module.exports = mod();
13
- else if (typeof define == "function" && define.amd) // AMD
14
- return define([], mod);
15
- else // Plain browser env
16
- this.CodeMirror = mod();
17
- })(function() {
18
- "use strict";
19
-
20
- // BROWSER SNIFFING
21
 
22
  // Kludges for bugs and behavior differences that can't be feature
23
  // detected are enabled based on userAgent etc sniffing.
24
-
25
- var gecko = /gecko\/\d/i.test(navigator.userAgent);
26
- // ie_uptoN means Internet Explorer version N or lower
27
- var ie_upto10 = /MSIE \d/.test(navigator.userAgent);
28
- var ie_11up = /Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(navigator.userAgent);
29
- var ie = ie_upto10 || ie_11up;
30
- var ie_version = ie && (ie_upto10 ? document.documentMode || 6 : ie_11up[1]);
31
- var webkit = /WebKit\//.test(navigator.userAgent);
32
- var qtwebkit = webkit && /Qt\/\d+\.\d+/.test(navigator.userAgent);
33
- var chrome = /Chrome\//.test(navigator.userAgent);
34
- var presto = /Opera\//.test(navigator.userAgent);
 
 
35
  var safari = /Apple Computer/.test(navigator.vendor);
36
- var khtml = /KHTML\//.test(navigator.userAgent);
37
- var mac_geMountainLion = /Mac OS X 1\d\D([8-9]|\d\d)\D/.test(navigator.userAgent);
38
- var phantom = /PhantomJS/.test(navigator.userAgent);
39
 
40
- var ios = /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent);
 
41
  // This is woefully incomplete. Suggestions for alternative methods welcome.
42
- var mobile = ios || /Android|webOS|BlackBerry|Opera Mini|Opera Mobi|IEMobile/i.test(navigator.userAgent);
43
- var mac = ios || /Mac/.test(navigator.platform);
44
- var windows = /win/i.test(navigator.platform);
 
45
 
46
- var presto_version = presto && navigator.userAgent.match(/Version\/(\d*\.\d*)/);
47
- if (presto_version) presto_version = Number(presto_version[1]);
48
  if (presto_version && presto_version >= 15) { presto = false; webkit = true; }
49
  // Some browsers use the wrong event properties to signal cmd/ctrl on OS X
50
  var flipCtrlCmd = mac && (qtwebkit || presto && (presto_version == null || presto_version < 12.11));
51
  var captureRightClick = gecko || (ie && ie_version >= 9);
52
 
53
- // Optimize some code when these features are not used.
54
- var sawReadOnlySpans = false, sawCollapsedSpans = false;
55
-
56
- // EDITOR CONSTRUCTOR
57
-
58
- // A CodeMirror instance represents an editor. This is the object
59
- // that user code is usually dealing with.
60
-
61
- function CodeMirror(place, options) {
62
- if (!(this instanceof CodeMirror)) return new CodeMirror(place, options);
63
-
64
- this.options = options = options || {};
65
- // Determine effective options based on given values and defaults.
66
- copyObj(defaults, options, false);
67
- setGuttersForLineNumbers(options);
68
-
69
- var doc = options.value;
70
- if (typeof doc == "string") doc = new Doc(doc, options.mode);
71
- this.doc = doc;
72
-
73
- var display = this.display = new Display(place, doc);
74
- display.wrapper.CodeMirror = this;
75
- updateGutters(this);
76
- themeChanged(this);
77
- if (options.lineWrapping)
78
- this.display.wrapper.className += " CodeMirror-wrap";
79
- if (options.autofocus && !mobile) focusInput(this);
80
-
81
- this.state = {
82
- keyMaps: [], // stores maps added by addKeyMap
83
- overlays: [], // highlighting overlays, as added by addOverlay
84
- modeGen: 0, // bumped when mode/overlay changes, used to invalidate highlighting info
85
- overwrite: false, focused: false,
86
- suppressEdits: false, // used to disable editing during key handlers when in readOnly mode
87
- pasteIncoming: false, cutIncoming: false, // help recognize paste/cut edits in readInput
88
- draggingText: false,
89
- highlight: new Delayed() // stores highlight worker timeout
90
- };
91
-
92
- // Override magic textarea content restore that IE sometimes does
93
- // on our hidden textarea on reload
94
- if (ie && ie_version < 11) setTimeout(bind(resetInput, this, true), 20);
95
-
96
- registerEventHandlers(this);
97
- ensureGlobalHandlers();
98
 
99
- var cm = this;
100
- runInOp(this, function() {
101
- cm.curOp.forceUpdate = true;
102
- attachDoc(cm, doc);
 
 
 
 
103
 
104
- if ((options.autofocus && !mobile) || activeElt() == display.input)
105
- setTimeout(bind(onFocus, cm), 20);
106
- else
107
- onBlur(cm);
 
108
 
109
- for (var opt in optionHandlers) if (optionHandlers.hasOwnProperty(opt))
110
- optionHandlers[opt](cm, options[opt], Init);
111
- maybeUpdateLineNumberWidth(cm);
112
- for (var i = 0; i < initHooks.length; ++i) initHooks[i](cm);
113
- });
114
  }
115
 
116
- // DISPLAY CONSTRUCTOR
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
- // The display handles the DOM integration, both for input reading
119
- // and content drawing. It holds references to DOM nodes and
120
- // display-related state.
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
- function Display(place, doc) {
123
- var d = this;
 
 
 
 
 
 
 
 
124
 
125
- // The semihidden textarea that is focused when the editor is
126
- // focused, and receives input.
127
- var input = d.input = elt("textarea", null, null, "position: absolute; padding: 0; width: 1px; height: 1em; outline: none");
128
- // The textarea is kept positioned near the cursor to prevent the
129
- // fact that it'll be scrolled into view on input from scrolling
130
- // our fake cursor out of view. On webkit, when wrap=off, paste is
131
- // very slow. So make the area wide instead.
132
- if (webkit) input.style.width = "1000px";
133
- else input.setAttribute("wrap", "off");
134
- // If border: 0; -- iOS fails to open keyboard (issue #1287)
135
- if (ios) input.style.border = "1px solid black";
136
- input.setAttribute("autocorrect", "off"); input.setAttribute("autocapitalize", "off"); input.setAttribute("spellcheck", "false");
 
 
137
 
138
- // Wraps and hides input textarea
139
- d.inputDiv = elt("div", [input], null, "overflow: hidden; position: relative; width: 3px; height: 0px;");
140
- // The fake scrollbar elements.
141
- d.scrollbarH = elt("div", [elt("div", null, null, "height: 100%; min-height: 1px")], "CodeMirror-hscrollbar");
142
- d.scrollbarV = elt("div", [elt("div", null, null, "min-width: 1px")], "CodeMirror-vscrollbar");
143
- // Covers bottom-right square when both scrollbars are present.
144
- d.scrollbarFiller = elt("div", null, "CodeMirror-scrollbar-filler");
145
- // Covers bottom of gutter when coverGutterNextToScrollbar is on
146
- // and h scrollbar is present.
147
- d.gutterFiller = elt("div", null, "CodeMirror-gutter-filler");
148
- // Will contain the actual code, positioned to cover the viewport.
149
- d.lineDiv = elt("div", null, "CodeMirror-code");
150
- // Elements are added to these to represent selection and cursors.
151
- d.selectionDiv = elt("div", null, null, "position: relative; z-index: 1");
152
- d.cursorDiv = elt("div", null, "CodeMirror-cursors");
153
- // A visibility: hidden element used to find the size of things.
154
- d.measure = elt("div", null, "CodeMirror-measure");
155
- // When lines outside of the viewport are measured, they are drawn in this.
156
- d.lineMeasure = elt("div", null, "CodeMirror-measure");
157
- // Wraps everything that needs to exist inside the vertically-padded coordinate system
158
- d.lineSpace = elt("div", [d.measure, d.lineMeasure, d.selectionDiv, d.cursorDiv, d.lineDiv],
159
- null, "position: relative; outline: none");
160
- // Moved around its parent to cover visible view.
161
- d.mover = elt("div", [elt("div", [d.lineSpace], "CodeMirror-lines")], null, "position: relative");
162
- // Set to the height of the document, allowing scrolling.
163
- d.sizer = elt("div", [d.mover], "CodeMirror-sizer");
164
- // Behavior of elts with overflow: auto and padding is
165
- // inconsistent across browsers. This is used to ensure the
166
- // scrollable area is big enough.
167
- d.heightForcer = elt("div", null, null, "position: absolute; height: " + scrollerCutOff + "px; width: 1px;");
168
- // Will contain the gutters, if any.
169
- d.gutters = elt("div", null, "CodeMirror-gutters");
170
- d.lineGutter = null;
171
- // Actual scrollable element.
172
- d.scroller = elt("div", [d.sizer, d.heightForcer, d.gutters], "CodeMirror-scroll");
173
- d.scroller.setAttribute("tabIndex", "-1");
174
- // The element in which the editor lives.
175
- d.wrapper = elt("div", [d.inputDiv, d.scrollbarH, d.scrollbarV,
176
- d.scrollbarFiller, d.gutterFiller, d.scroller], "CodeMirror");
177
 
178
- // Work around IE7 z-index bug (not perfect, hence IE7 not really being supported)
179
- if (ie && ie_version < 8) { d.gutters.style.zIndex = -1; d.scroller.style.paddingRight = 0; }
180
- // Needed to hide big blue blinking cursor on Mobile Safari
181
- if (ios) input.style.width = "0px";
182
- if (!webkit) d.scroller.draggable = true;
183
- // Needed to handle Tab key in KHTML
184
- if (khtml) { d.inputDiv.style.height = "1px"; d.inputDiv.style.position = "absolute"; }
185
- // Need to set a minimum width to see the scrollbar on IE7 (but must not set it on IE8).
186
- if (ie && ie_version < 8) d.scrollbarH.style.minHeight = d.scrollbarV.style.minWidth = "18px";
187
 
188
- if (place.appendChild) place.appendChild(d.wrapper);
189
- else place(d.wrapper);
 
 
190
 
191
- // Current rendered range (may be bigger than the view window).
192
- d.viewFrom = d.viewTo = doc.first;
193
- // Information about the rendered lines.
194
- d.view = [];
195
- // Holds info about a single rendered line when it was rendered
196
- // for measurement, while not in view.
197
- d.externalMeasured = null;
198
- // Empty space (in pixels) above the view
199
- d.viewOffset = 0;
200
- d.lastSizeC = 0;
201
- d.updateLineNumbers = null;
202
 
203
- // Used to only resize the line number gutter when necessary (when
204
- // the amount of lines crosses a boundary that makes its width change)
205
- d.lineNumWidth = d.lineNumInnerWidth = d.lineNumChars = null;
206
- // See readInput and resetInput
207
- d.prevInput = "";
208
- // Set to true when a non-horizontal-scrolling line widget is
209
- // added. As an optimization, line widget aligning is skipped when
210
- // this is false.
211
- d.alignWidgets = false;
212
- // Flag that indicates whether we expect input to appear real soon
213
- // now (after some event like 'keypress' or 'input') and are
214
- // polling intensively.
215
- d.pollingFast = false;
216
- // Self-resetting timeout for the poller
217
- d.poll = new Delayed();
 
218
 
219
- d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
- // Tracks when resetInput has punted to just putting a short
222
- // string into the textarea instead of the full selection.
223
- d.inaccurateSelection = false;
 
 
224
 
225
- // Tracks the maximum line length so that the horizontal scrollbar
226
- // can be kept static when scrolling.
227
- d.maxLine = null;
228
- d.maxLineLength = 0;
229
- d.maxLineChanged = false;
230
 
231
- // Used for measuring wheel scrolling granularity
232
- d.wheelDX = d.wheelDY = d.wheelStartX = d.wheelStartY = null;
 
233
 
234
- // True when shift is held down.
235
- d.shift = false;
236
 
237
- // Used to track whether anything happened since the context menu
238
- // was opened.
239
- d.selForContextMenu = null;
 
 
 
 
 
 
 
 
 
 
 
240
  }
241
 
242
- // STATE UPDATES
 
 
 
 
 
243
 
244
- // Used to get the editor into a consistent state again when options change.
245
 
246
- function loadMode(cm) {
247
- cm.doc.mode = CodeMirror.getMode(cm.options, cm.doc.modeOption);
248
- resetModeState(cm);
 
249
  }
250
 
251
- function resetModeState(cm) {
252
- cm.doc.iter(function(line) {
253
- if (line.stateAfter) line.stateAfter = null;
254
- if (line.styles) line.styles = null;
255
- });
256
- cm.doc.frontier = cm.doc.first;
257
- startWorker(cm, 100);
258
- cm.state.modeGen++;
259
- if (cm.curOp) regChange(cm);
260
  }
261
 
262
- function wrappingChanged(cm) {
263
- if (cm.options.lineWrapping) {
264
- addClass(cm.display.wrapper, "CodeMirror-wrap");
265
- cm.display.sizer.style.minWidth = "";
 
 
266
  } else {
267
- rmClass(cm.display.wrapper, "CodeMirror-wrap");
268
- findMaxLine(cm);
269
  }
270
- estimateLineHeights(cm);
271
- regChange(cm);
272
- clearCaches(cm);
273
- setTimeout(function(){updateScrollbars(cm);}, 100);
274
  }
275
 
276
- // Returns a function that estimates the height of a line, to use as
277
- // first approximation until the line becomes visible (and is thus
278
- // properly measurable).
279
- function estimateHeight(cm) {
280
- var th = textHeight(cm.display), wrapping = cm.options.lineWrapping;
281
- var perLine = wrapping && Math.max(5, cm.display.scroller.clientWidth / charWidth(cm.display) - 3);
282
- return function(line) {
283
- if (lineIsHidden(cm.doc, line)) return 0;
284
-
285
- var widgetsHeight = 0;
286
- if (line.widgets) for (var i = 0; i < line.widgets.length; i++) {
287
- if (line.widgets[i].height) widgetsHeight += line.widgets[i].height;
288
- }
289
-
290
- if (wrapping)
291
- return widgetsHeight + (Math.ceil(line.text.length / perLine) || 1) * th;
292
- else
293
- return widgetsHeight + th;
294
- };
295
  }
296
-
297
- function estimateLineHeights(cm) {
298
- var doc = cm.doc, est = estimateHeight(cm);
299
- doc.iter(function(line) {
300
- var estHeight = est(line);
301
- if (estHeight != line.height) updateLineHeight(line, estHeight);
302
- });
303
  }
304
 
305
- function keyMapChanged(cm) {
306
- var map = keyMap[cm.options.keyMap], style = map.style;
307
- cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-keymap-\S+/g, "") +
308
- (style ? " cm-keymap-" + style : "");
309
  }
310
 
311
- function themeChanged(cm) {
312
- cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-s-\S+/g, "") +
313
- cm.options.theme.replace(/(^|\s)\s*/g, " cm-s-");
314
- clearCaches(cm);
 
 
 
 
 
 
 
 
315
  }
316
 
317
- function guttersChanged(cm) {
318
- updateGutters(cm);
319
- regChange(cm);
320
- setTimeout(function(){alignHorizontally(cm);}, 20);
 
 
 
 
 
 
 
 
 
 
321
  }
322
 
323
- // Rebuild the gutter elements, ensure the margin to the left of the
324
- // code matches their width.
325
- function updateGutters(cm) {
326
- var gutters = cm.display.gutters, specs = cm.options.gutters;
327
- removeChildren(gutters);
328
- for (var i = 0; i < specs.length; ++i) {
329
- var gutterClass = specs[i];
330
- var gElt = gutters.appendChild(elt("div", null, "CodeMirror-gutter " + gutterClass));
331
- if (gutterClass == "CodeMirror-linenumbers") {
332
- cm.display.lineGutter = gElt;
333
- gElt.style.width = (cm.display.lineNumWidth || 1) + "px";
334
  }
335
  }
336
- gutters.style.display = i ? "" : "none";
337
- updateGutterSpace(cm);
338
  }
339
 
340
- function updateGutterSpace(cm) {
341
- var width = cm.display.gutters.offsetWidth;
342
- cm.display.sizer.style.marginLeft = width + "px";
343
- cm.display.scrollbarH.style.left = cm.options.fixedGutter ? width + "px" : 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  }
345
 
346
- // Compute the character length of a line, taking into account
347
- // collapsed ranges (see markText) that might hide parts, and join
348
- // other lines onto it.
349
- function lineLength(line) {
350
- if (line.height == 0) return 0;
351
- var len = line.text.length, merged, cur = line;
352
- while (merged = collapsedSpanAtStart(cur)) {
353
- var found = merged.find(0, true);
354
- cur = found.from.line;
355
- len += found.from.ch - found.to.ch;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  }
357
- cur = line;
358
- while (merged = collapsedSpanAtEnd(cur)) {
359
- var found = merged.find(0, true);
360
- len -= cur.text.length - found.from.ch;
361
- cur = found.to.line;
362
- len += cur.text.length - found.to.ch;
 
363
  }
364
- return len;
365
- }
366
 
367
- // Find the longest line in the document.
368
- function findMaxLine(cm) {
369
- var d = cm.display, doc = cm.doc;
370
- d.maxLine = getLine(doc, doc.first);
371
- d.maxLineLength = lineLength(d.maxLine);
372
- d.maxLineChanged = true;
373
- doc.iter(function(line) {
374
- var len = lineLength(line);
375
- if (len > d.maxLineLength) {
376
- d.maxLineLength = len;
377
- d.maxLine = line;
 
 
 
 
 
378
  }
379
- });
380
- }
381
 
382
- // Make sure the gutters options contains the element
383
- // "CodeMirror-linenumbers" when the lineNumbers option is true.
384
- function setGuttersForLineNumbers(options) {
385
- var found = indexOf(options.gutters, "CodeMirror-linenumbers");
386
- if (found == -1 && options.lineNumbers) {
387
- options.gutters = options.gutters.concat(["CodeMirror-linenumbers"]);
388
- } else if (found > -1 && !options.lineNumbers) {
389
- options.gutters = options.gutters.slice(0);
390
- options.gutters.splice(found, 1);
391
- }
392
- }
393
 
394
- // SCROLLBARS
 
 
 
 
 
 
 
 
 
395
 
396
- function hScrollbarTakesSpace(cm) {
397
- return cm.display.scroller.clientHeight - cm.display.wrapper.clientHeight < scrollerCutOff - 3;
398
- }
 
 
 
 
 
 
 
 
 
 
 
 
399
 
400
- // Prepare DOM reads needed to update the scrollbars. Done in one
401
- // shot to minimize update/measure roundtrips.
402
- function measureForScrollbars(cm) {
403
- var scroll = cm.display.scroller;
404
- return {
405
- clientHeight: scroll.clientHeight,
406
- barHeight: cm.display.scrollbarV.clientHeight,
407
- scrollWidth: scroll.scrollWidth, clientWidth: scroll.clientWidth,
408
- hScrollbarTakesSpace: hScrollbarTakesSpace(cm),
409
- barWidth: cm.display.scrollbarH.clientWidth,
410
- docHeight: Math.round(cm.doc.height + paddingVert(cm.display))
411
- };
412
- }
413
 
414
- // Re-synchronize the fake scrollbars with the actual size of the
415
- // content.
416
- function updateScrollbars(cm, measure) {
417
- if (!measure) measure = measureForScrollbars(cm);
418
- var d = cm.display, sWidth = scrollbarWidth(d.measure);
419
- var scrollHeight = measure.docHeight + scrollerCutOff;
420
- var needsH = measure.scrollWidth > measure.clientWidth;
421
- if (needsH && measure.scrollWidth <= measure.clientWidth + 1 &&
422
- sWidth > 0 && !measure.hScrollbarTakesSpace)
423
- needsH = false; // (Issue #2562)
424
- var needsV = scrollHeight > measure.clientHeight;
 
 
 
 
 
 
425
 
426
- if (needsV) {
427
- d.scrollbarV.style.display = "block";
428
- d.scrollbarV.style.bottom = needsH ? sWidth + "px" : "0";
429
- // A bug in IE8 can cause this value to be negative, so guard it.
430
- d.scrollbarV.firstChild.style.height =
431
- Math.max(0, scrollHeight - measure.clientHeight + (measure.barHeight || d.scrollbarV.clientHeight)) + "px";
432
- } else {
433
- d.scrollbarV.style.display = "";
434
- d.scrollbarV.firstChild.style.height = "0";
435
- }
436
- if (needsH) {
437
- d.scrollbarH.style.display = "block";
438
- d.scrollbarH.style.right = needsV ? sWidth + "px" : "0";
439
- d.scrollbarH.firstChild.style.width =
440
- (measure.scrollWidth - measure.clientWidth + (measure.barWidth || d.scrollbarH.clientWidth)) + "px";
441
- } else {
442
- d.scrollbarH.style.display = "";
443
- d.scrollbarH.firstChild.style.width = "0";
444
- }
445
- if (needsH && needsV) {
446
- d.scrollbarFiller.style.display = "block";
447
- d.scrollbarFiller.style.height = d.scrollbarFiller.style.width = sWidth + "px";
448
- } else d.scrollbarFiller.style.display = "";
449
- if (needsH && cm.options.coverGutterNextToScrollbar && cm.options.fixedGutter) {
450
- d.gutterFiller.style.display = "block";
451
- d.gutterFiller.style.height = sWidth + "px";
452
- d.gutterFiller.style.width = d.gutters.offsetWidth + "px";
453
- } else d.gutterFiller.style.display = "";
454
-
455
- if (!cm.state.checkedOverlayScrollbar && measure.clientHeight > 0) {
456
- if (sWidth === 0) {
457
- var w = mac && !mac_geMountainLion ? "12px" : "18px";
458
- d.scrollbarV.style.minWidth = d.scrollbarH.style.minHeight = w;
459
- var barMouseDown = function(e) {
460
- if (e_target(e) != d.scrollbarV && e_target(e) != d.scrollbarH)
461
- operation(cm, onMouseDown)(e);
462
- };
463
- on(d.scrollbarV, "mousedown", barMouseDown);
464
- on(d.scrollbarH, "mousedown", barMouseDown);
465
  }
466
- cm.state.checkedOverlayScrollbar = true;
 
467
  }
 
 
 
 
 
 
 
 
 
468
  }
469
 
470
- // Compute the lines that are visible in a given viewport (defaults
471
- // the the current scroll position). viewport may contain top,
472
- // height, and ensure (see op.scrollToPos) properties.
473
- function visibleLines(display, doc, viewport) {
474
- var top = viewport && viewport.top != null ? Math.max(0, viewport.top) : display.scroller.scrollTop;
475
- top = Math.floor(top - paddingTop(display));
476
- var bottom = viewport && viewport.bottom != null ? viewport.bottom : top + display.wrapper.clientHeight;
477
 
478
- var from = lineAtHeight(doc, top), to = lineAtHeight(doc, bottom);
479
- // Ensure is a {from: {line, ch}, to: {line, ch}} object, and
480
- // forces those lines into the viewport (if possible).
481
- if (viewport && viewport.ensure) {
482
- var ensureFrom = viewport.ensure.from.line, ensureTo = viewport.ensure.to.line;
483
- if (ensureFrom < from)
484
- return {from: ensureFrom,
485
- to: lineAtHeight(doc, heightAtLine(getLine(doc, ensureFrom)) + display.wrapper.clientHeight)};
486
- if (Math.min(ensureTo, doc.lastLine()) >= to)
487
- return {from: lineAtHeight(doc, heightAtLine(getLine(doc, ensureTo)) - display.wrapper.clientHeight),
488
- to: ensureTo};
489
- }
490
- return {from: from, to: Math.max(to, from + 1)};
491
- }
492
 
493
- // LINE NUMBERS
494
 
495
- // Re-align line numbers and gutter marks to compensate for
496
- // horizontal scrolling.
497
- function alignHorizontally(cm) {
498
- var display = cm.display, view = display.view;
499
- if (!display.alignWidgets && (!display.gutters.firstChild || !cm.options.fixedGutter)) return;
500
- var comp = compensateForHScroll(display) - display.scroller.scrollLeft + cm.doc.scrollLeft;
501
- var gutterW = display.gutters.offsetWidth, left = comp + "px";
502
- for (var i = 0; i < view.length; i++) if (!view[i].hidden) {
503
- if (cm.options.fixedGutter && view[i].gutter)
504
- view[i].gutter.style.left = left;
505
- var align = view[i].alignable;
506
- if (align) for (var j = 0; j < align.length; j++)
507
- align[j].style.left = left;
508
  }
509
- if (cm.options.fixedGutter)
510
- display.gutters.style.left = (comp + gutterW) + "px";
 
 
511
  }
512
 
513
- // Used to ensure that the line number gutter is still the right
514
- // size for the current document size. Returns true when an update
515
- // is needed.
516
- function maybeUpdateLineNumberWidth(cm) {
517
- if (!cm.options.lineNumbers) return false;
518
- var doc = cm.doc, last = lineNumberFor(cm.options, doc.first + doc.size - 1), display = cm.display;
519
- if (last.length != display.lineNumChars) {
520
- var test = display.measure.appendChild(elt("div", [elt("div", last)],
521
- "CodeMirror-linenumber CodeMirror-gutter-elt"));
522
- var innerW = test.firstChild.offsetWidth, padding = test.offsetWidth - innerW;
523
- display.lineGutter.style.width = "";
524
- display.lineNumInnerWidth = Math.max(innerW, display.lineGutter.offsetWidth - padding);
525
- display.lineNumWidth = display.lineNumInnerWidth + padding;
526
- display.lineNumChars = display.lineNumInnerWidth ? last.length : -1;
527
- display.lineGutter.style.width = display.lineNumWidth + "px";
528
- updateGutterSpace(cm);
529
- return true;
530
  }
531
- return false;
532
  }
533
 
534
- function lineNumberFor(options, i) {
535
- return String(options.lineNumberFormatter(i + options.firstLineNumber));
 
 
 
536
  }
537
 
538
- // Computes display.scroller.scrollLeft + display.gutters.offsetWidth,
539
- // but using getBoundingClientRect to get a sub-pixel-accurate
540
- // result.
541
- function compensateForHScroll(display) {
542
- return display.scroller.getBoundingClientRect().left - display.sizer.getBoundingClientRect().left;
 
 
 
543
  }
544
 
545
- // DISPLAY DRAWING
546
-
547
- function DisplayUpdate(cm, viewport, force) {
548
- var display = cm.display;
 
 
 
549
 
550
- this.viewport = viewport;
551
- // Store some values that we'll need later (but don't want to force a relayout for)
552
- this.visible = visibleLines(display, cm.doc, viewport);
553
- this.editorIsHidden = !display.wrapper.offsetWidth;
554
- this.wrapperHeight = display.wrapper.clientHeight;
555
- this.oldViewFrom = display.viewFrom; this.oldViewTo = display.viewTo;
556
- this.oldScrollerWidth = display.scroller.clientWidth;
557
- this.force = force;
558
- this.dims = getDimensions(cm);
559
  }
560
 
561
- // Does the actual updating of the line display. Bails out
562
- // (returning false) when there is nothing to be done and forced is
563
- // false.
564
- function updateDisplayIfNeeded(cm, update) {
565
- var display = cm.display, doc = cm.doc;
566
- if (update.editorIsHidden) {
567
- resetView(cm);
568
- return false;
569
- }
570
 
571
- // Bail out if the visible area is already rendered and nothing changed.
572
- if (!update.force &&
573
- update.visible.from >= display.viewFrom && update.visible.to <= display.viewTo &&
574
- (display.updateLineNumbers == null || display.updateLineNumbers >= display.viewTo) &&
575
- countDirtyView(cm) == 0)
576
- return false;
577
 
578
- if (maybeUpdateLineNumberWidth(cm)) {
579
- resetView(cm);
580
- update.dims = getDimensions(cm);
581
- }
 
 
 
 
 
 
 
 
582
 
583
- // Compute a suitable new viewport (from & to)
584
- var end = doc.first + doc.size;
585
- var from = Math.max(update.visible.from - cm.options.viewportMargin, doc.first);
586
- var to = Math.min(end, update.visible.to + cm.options.viewportMargin);
587
- if (display.viewFrom < from && from - display.viewFrom < 20) from = Math.max(doc.first, display.viewFrom);
588
- if (display.viewTo > to && display.viewTo - to < 20) to = Math.min(end, display.viewTo);
589
- if (sawCollapsedSpans) {
590
- from = visualLineNo(cm.doc, from);
591
- to = visualLineEndNo(cm.doc, to);
592
  }
 
 
 
593
 
594
- var different = from != display.viewFrom || to != display.viewTo ||
595
- display.lastSizeC != update.wrapperHeight;
596
- adjustView(cm, from, to);
597
-
598
- display.viewOffset = heightAtLine(getLine(cm.doc, display.viewFrom));
599
- // Position the mover div to align with the current scroll position
600
- cm.display.mover.style.top = display.viewOffset + "px";
601
-
602
- var toUpdate = countDirtyView(cm);
603
- if (!different && toUpdate == 0 && !update.force &&
604
- (display.updateLineNumbers == null || display.updateLineNumbers >= display.viewTo))
605
- return false;
606
-
607
- // For big changes, we hide the enclosing element during the
608
- // update, since that speeds up the operations on most browsers.
609
- var focused = activeElt();
610
- if (toUpdate > 4) display.lineDiv.style.display = "none";
611
- patchDisplay(cm, display.updateLineNumbers, update.dims);
612
- if (toUpdate > 4) display.lineDiv.style.display = "";
613
- // There might have been a widget with a focused element that got
614
- // hidden or updated, if so re-focus it.
615
- if (focused && activeElt() != focused && focused.offsetHeight) focused.focus();
616
-
617
- // Prevent selection and cursors from interfering with the scroll
618
- // width.
619
- removeChildren(display.cursorDiv);
620
- removeChildren(display.selectionDiv);
621
 
622
- if (different) {
623
- display.lastSizeC = update.wrapperHeight;
624
- startWorker(cm, 400);
 
 
 
 
625
  }
 
 
 
 
 
626
 
627
- display.updateLineNumbers = null;
628
-
629
- return true;
 
 
 
 
 
 
 
630
  }
631
 
632
- function postUpdateDisplay(cm, update) {
633
- var force = update.force, viewport = update.viewport;
634
- for (var first = true;; first = false) {
635
- if (first && cm.options.lineWrapping && update.oldScrollerWidth != cm.display.scroller.clientWidth) {
636
- force = true;
 
 
 
 
 
 
 
637
  } else {
638
- force = false;
639
- // Clip forced viewport to actual scrollable area.
640
- if (viewport && viewport.top != null)
641
- viewport = {top: Math.min(cm.doc.height + paddingVert(cm.display) - scrollerCutOff -
642
- cm.display.scroller.clientHeight, viewport.top)};
643
- // Updated line heights might result in the drawn area not
644
- // actually covering the viewport. Keep looping until it does.
645
- update.visible = visibleLines(cm.display, cm.doc, viewport);
646
- if (update.visible.from >= cm.display.viewFrom && update.visible.to <= cm.display.viewTo)
647
- break;
648
  }
649
- if (!updateDisplayIfNeeded(cm, update)) break;
650
- updateHeightsInViewport(cm);
651
- var barMeasure = measureForScrollbars(cm);
652
- updateSelection(cm);
653
- setDocumentHeight(cm, barMeasure);
654
- updateScrollbars(cm, barMeasure);
655
  }
 
 
656
 
657
- signalLater(cm, "update", cm);
658
- if (cm.display.viewFrom != update.oldViewFrom || cm.display.viewTo != update.oldViewTo)
659
- signalLater(cm, "viewportChange", cm, cm.display.viewFrom, cm.display.viewTo);
660
- }
 
 
 
 
 
 
661
 
662
- function updateDisplaySimple(cm, viewport) {
663
- var update = new DisplayUpdate(cm, viewport);
664
- if (updateDisplayIfNeeded(cm, update)) {
665
- postUpdateDisplay(cm, update);
666
- var barMeasure = measureForScrollbars(cm);
667
- updateSelection(cm);
668
- setDocumentHeight(cm, barMeasure);
669
- updateScrollbars(cm, barMeasure);
670
- }
671
- }
672
 
673
- function setDocumentHeight(cm, measure) {
674
- cm.display.sizer.style.minHeight = cm.display.heightForcer.style.top = measure.docHeight + "px";
675
- cm.display.gutters.style.height = Math.max(measure.docHeight, measure.clientHeight - scrollerCutOff) + "px";
 
 
 
 
676
  }
677
 
678
- function checkForWebkitWidthBug(cm, measure) {
679
- // Work around Webkit bug where it sometimes reserves space for a
680
- // non-existing phantom scrollbar in the scroller (Issue #2420)
681
- if (cm.display.sizer.offsetWidth + cm.display.gutters.offsetWidth < cm.display.scroller.clientWidth - 1) {
682
- cm.display.sizer.style.minHeight = cm.display.heightForcer.style.top = "0px";
683
- cm.display.gutters.style.height = measure.docHeight + "px";
684
- }
685
- }
686
 
687
- // Read the actual heights of the rendered lines, and update their
688
- // stored heights to match.
689
- function updateHeightsInViewport(cm) {
690
- var display = cm.display;
691
- var prevBottom = display.lineDiv.offsetTop;
692
- for (var i = 0; i < display.view.length; i++) {
693
- var cur = display.view[i], height;
694
- if (cur.hidden) continue;
695
- if (ie && ie_version < 8) {
696
- var bot = cur.node.offsetTop + cur.node.offsetHeight;
697
- height = bot - prevBottom;
698
- prevBottom = bot;
699
- } else {
700
- var box = cur.node.getBoundingClientRect();
701
- height = box.bottom - box.top;
702
- }
703
- var diff = cur.line.height - height;
704
- if (height < 2) height = textHeight(display);
705
- if (diff > .001 || diff < -.001) {
706
- updateLineHeight(cur.line, height);
707
- updateWidgetHeight(cur.line);
708
- if (cur.rest) for (var j = 0; j < cur.rest.length; j++)
709
- updateWidgetHeight(cur.rest[j]);
710
- }
711
- }
712
  }
713
 
714
- // Read and store the height of line widgets associated with the
715
- // given line.
716
- function updateWidgetHeight(line) {
717
- if (line.widgets) for (var i = 0; i < line.widgets.length; ++i)
718
- line.widgets[i].height = line.widgets[i].node.offsetHeight;
719
  }
720
 
721
- // Do a bulk-read of the DOM positions and sizes needed to draw the
722
- // view, so that we don't interleave reading and writing to the DOM.
723
- function getDimensions(cm) {
724
- var d = cm.display, left = {}, width = {};
725
- for (var n = d.gutters.firstChild, i = 0; n; n = n.nextSibling, ++i) {
726
- left[cm.options.gutters[i]] = n.offsetLeft;
727
- width[cm.options.gutters[i]] = n.offsetWidth;
 
 
 
 
 
 
 
728
  }
729
- return {fixedPos: compensateForHScroll(d),
730
- gutterTotalWidth: d.gutters.offsetWidth,
731
- gutterLeft: left,
732
- gutterWidth: width,
733
- wrapperWidth: d.wrapper.clientWidth};
734
  }
735
 
736
- // Sync the actual display DOM structure with display.view, removing
737
- // nodes for lines that are no longer in view, and creating the ones
738
- // that are not there yet, and updating the ones that are out of
739
- // date.
740
- function patchDisplay(cm, updateNumbersFrom, dims) {
741
- var display = cm.display, lineNumbers = cm.options.lineNumbers;
742
- var container = display.lineDiv, cur = container.firstChild;
743
-
744
- function rm(node) {
745
- var next = node.nextSibling;
746
- // Works around a throw-scroll bug in OS X Webkit
747
- if (webkit && mac && cm.display.currentWheelTarget == node)
748
- node.style.display = "none";
749
- else
750
- node.parentNode.removeChild(node);
751
- return next;
752
- }
753
-
754
- var view = display.view, lineN = display.viewFrom;
755
- // Loop over the elements in the view, syncing cur (the DOM nodes
756
- // in display.lineDiv) with the view as we go.
757
- for (var i = 0; i < view.length; i++) {
758
- var lineView = view[i];
759
- if (lineView.hidden) {
760
- } else if (!lineView.node) { // Not drawn yet
761
- var node = buildLineElement(cm, lineView, lineN, dims);
762
- container.insertBefore(node, cur);
763
- } else { // Already drawn
764
- while (cur != lineView.node) cur = rm(cur);
765
- var updateNumber = lineNumbers && updateNumbersFrom != null &&
766
- updateNumbersFrom <= lineN && lineView.lineNumber;
767
- if (lineView.changes) {
768
- if (indexOf(lineView.changes, "gutter") > -1) updateNumber = false;
769
- updateLineForChanges(cm, lineView, lineN, dims);
770
- }
771
- if (updateNumber) {
772
- removeChildren(lineView.lineNumber);
773
- lineView.lineNumber.appendChild(document.createTextNode(lineNumberFor(cm.options, lineN)));
774
- }
775
- cur = lineView.node.nextSibling;
776
  }
777
- lineN += lineView.size;
778
  }
779
- while (cur) cur = rm(cur);
780
- }
 
 
781
 
782
- // When an aspect of a line changes, a string is added to
783
- // lineView.changes. This updates the relevant part of the line's
784
- // DOM structure.
785
- function updateLineForChanges(cm, lineView, lineN, dims) {
786
- for (var j = 0; j < lineView.changes.length; j++) {
787
- var type = lineView.changes[j];
788
- if (type == "text") updateLineText(cm, lineView);
789
- else if (type == "gutter") updateLineGutter(cm, lineView, lineN, dims);
790
- else if (type == "class") updateLineClasses(lineView);
791
- else if (type == "widget") updateLineWidgets(lineView, dims);
792
- }
793
- lineView.changes = null;
794
  }
795
 
796
- // Lines with gutter elements, widgets or a background class need to
797
- // be wrapped, and have the extra elements added to the wrapper div
798
- function ensureLineWrapped(lineView) {
799
- if (lineView.node == lineView.text) {
800
- lineView.node = elt("div", null, null, "position: relative");
801
- if (lineView.text.parentNode)
802
- lineView.text.parentNode.replaceChild(lineView.node, lineView.text);
803
- lineView.node.appendChild(lineView.text);
804
- if (ie && ie_version < 8) lineView.node.style.zIndex = 2;
805
- }
806
- return lineView.node;
807
  }
808
 
809
- function updateLineBackground(lineView) {
810
- var cls = lineView.bgClass ? lineView.bgClass + " " + (lineView.line.bgClass || "") : lineView.line.bgClass;
811
- if (cls) cls += " CodeMirror-linebackground";
812
- if (lineView.background) {
813
- if (cls) lineView.background.className = cls;
814
- else { lineView.background.parentNode.removeChild(lineView.background); lineView.background = null; }
815
- } else if (cls) {
816
- var wrap = ensureLineWrapped(lineView);
817
- lineView.background = wrap.insertBefore(elt("div", null, cls), wrap.firstChild);
818
  }
 
819
  }
820
 
821
- // Wrapper around buildLineContent which will reuse the structure
822
- // in display.externalMeasured when possible.
823
- function getLineContent(cm, lineView) {
824
- var ext = cm.display.externalMeasured;
825
- if (ext && ext.line == lineView.line) {
826
- cm.display.externalMeasured = null;
827
- lineView.measure = ext.measure;
828
- return ext.built;
 
829
  }
830
- return buildLineContent(cm, lineView);
831
  }
832
 
833
- // Redraw the line's text. Interacts with the background and text
834
- // classes because the mode may output tokens that influence these
835
- // classes.
836
- function updateLineText(cm, lineView) {
837
- var cls = lineView.text.className;
838
- var built = getLineContent(cm, lineView);
839
- if (lineView.text == lineView.node) lineView.node = built.pre;
840
- lineView.text.parentNode.replaceChild(built.pre, lineView.text);
841
- lineView.text = built.pre;
842
- if (built.bgClass != lineView.bgClass || built.textClass != lineView.textClass) {
843
- lineView.bgClass = built.bgClass;
844
- lineView.textClass = built.textClass;
845
- updateLineClasses(lineView);
846
- } else if (cls) {
847
- lineView.text.className = cls;
848
- }
849
  }
850
 
851
- function updateLineClasses(lineView) {
852
- updateLineBackground(lineView);
853
- if (lineView.line.wrapClass)
854
- ensureLineWrapped(lineView).className = lineView.line.wrapClass;
855
- else if (lineView.node != lineView.text)
856
- lineView.node.className = "";
857
- var textClass = lineView.textClass ? lineView.textClass + " " + (lineView.line.textClass || "") : lineView.line.textClass;
858
- lineView.text.className = textClass || "";
859
- }
860
 
861
- function updateLineGutter(cm, lineView, lineN, dims) {
862
- if (lineView.gutter) {
863
- lineView.node.removeChild(lineView.gutter);
864
- lineView.gutter = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
865
  }
866
- var markers = lineView.line.gutterMarkers;
867
- if (cm.options.lineNumbers || markers) {
868
- var wrap = ensureLineWrapped(lineView);
869
- var gutterWrap = lineView.gutter =
870
- wrap.insertBefore(elt("div", null, "CodeMirror-gutter-wrapper", "position: absolute; left: " +
871
- (cm.options.fixedGutter ? dims.fixedPos : -dims.gutterTotalWidth) + "px"),
872
- lineView.text);
873
- if (cm.options.lineNumbers && (!markers || !markers["CodeMirror-linenumbers"]))
874
- lineView.lineNumber = gutterWrap.appendChild(
875
- elt("div", lineNumberFor(cm.options, lineN),
876
- "CodeMirror-linenumber CodeMirror-gutter-elt",
877
- "left: " + dims.gutterLeft["CodeMirror-linenumbers"] + "px; width: "
878
- + cm.display.lineNumInnerWidth + "px"));
879
- if (markers) for (var k = 0; k < cm.options.gutters.length; ++k) {
880
- var id = cm.options.gutters[k], found = markers.hasOwnProperty(id) && markers[id];
881
- if (found)
882
- gutterWrap.appendChild(elt("div", [found], "CodeMirror-gutter-elt", "left: " +
883
- dims.gutterLeft[id] + "px; width: " + dims.gutterWidth[id] + "px"));
884
  }
 
 
 
 
 
885
  }
886
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
887
 
888
- function updateLineWidgets(lineView, dims) {
889
- if (lineView.alignable) lineView.alignable = null;
890
- for (var node = lineView.node.firstChild, next; node; node = next) {
891
- var next = node.nextSibling;
892
- if (node.className == "CodeMirror-linewidget")
893
- lineView.node.removeChild(node);
 
 
 
 
 
894
  }
895
- insertLineWidgets(lineView, dims);
896
  }
897
 
898
- // Build a line's DOM representation from scratch
899
- function buildLineElement(cm, lineView, lineN, dims) {
900
- var built = getLineContent(cm, lineView);
901
- lineView.text = lineView.node = built.pre;
902
- if (built.bgClass) lineView.bgClass = built.bgClass;
903
- if (built.textClass) lineView.textClass = built.textClass;
904
-
905
- updateLineClasses(lineView);
906
- updateLineGutter(cm, lineView, lineN, dims);
907
- insertLineWidgets(lineView, dims);
908
- return lineView.node;
 
 
 
 
 
 
 
909
  }
910
 
911
- // A lineView may contain multiple logical lines (when merged by
912
- // collapsed spans). The widgets for all of them need to be drawn.
913
- function insertLineWidgets(lineView, dims) {
914
- insertLineWidgetsFor(lineView.line, lineView, dims, true);
915
- if (lineView.rest) for (var i = 0; i < lineView.rest.length; i++)
916
- insertLineWidgetsFor(lineView.rest[i], lineView, dims, false);
917
  }
918
 
919
- function insertLineWidgetsFor(line, lineView, dims, allowAbove) {
920
- if (!line.widgets) return;
921
- var wrap = ensureLineWrapped(lineView);
922
- for (var i = 0, ws = line.widgets; i < ws.length; ++i) {
923
- var widget = ws[i], node = elt("div", [widget.node], "CodeMirror-linewidget");
924
- if (!widget.handleMouseEvents) node.ignoreEvents = true;
925
- positionLineWidget(widget, node, lineView, dims);
926
- if (allowAbove && widget.above)
927
- wrap.insertBefore(node, lineView.gutter || lineView.text);
928
- else
929
- wrap.appendChild(node);
930
- signalLater(widget, "redraw");
931
  }
 
932
  }
933
 
934
- function positionLineWidget(widget, node, lineView, dims) {
935
- if (widget.noHScroll) {
936
- (lineView.alignable || (lineView.alignable = [])).push(node);
937
- var width = dims.wrapperWidth;
938
- node.style.left = dims.fixedPos + "px";
939
- if (!widget.coverGutter) {
940
- width -= dims.gutterTotalWidth;
941
- node.style.paddingLeft = dims.gutterTotalWidth + "px";
 
 
942
  }
943
- node.style.width = width + "px";
944
- }
945
- if (widget.coverGutter) {
946
- node.style.zIndex = 5;
947
- node.style.position = "relative";
948
- if (!widget.noHScroll) node.style.marginLeft = -dims.gutterTotalWidth + "px";
 
949
  }
 
950
  }
951
 
952
- // POSITION OBJECT
953
-
954
- // A Pos instance represents a position within the text.
955
- var Pos = CodeMirror.Pos = function(line, ch) {
956
- if (!(this instanceof Pos)) return new Pos(line, ch);
957
- this.line = line; this.ch = ch;
958
- };
959
-
960
- // Compare two positions, return 0 if they are the same, a negative
961
- // number when a is less, and a positive number otherwise.
962
- var cmp = CodeMirror.cmpPos = function(a, b) { return a.line - b.line || a.ch - b.ch; };
963
-
964
- function copyPos(x) {return Pos(x.line, x.ch);}
965
- function maxPos(a, b) { return cmp(a, b) < 0 ? b : a; }
966
- function minPos(a, b) { return cmp(a, b) < 0 ? a : b; }
967
-
968
- // SELECTION / CURSOR
969
 
970
- // Selection objects are immutable. A new one is created every time
971
- // the selection changes. A selection is one or more non-overlapping
972
- // (and non-touching) ranges, sorted, and an integer that indicates
973
- // which one is the primary selection (the one that's scrolled into
974
- // view, that getCursor returns, etc).
975
- function Selection(ranges, primIndex) {
976
- this.ranges = ranges;
977
- this.primIndex = primIndex;
978
  }
979
 
980
- Selection.prototype = {
981
- primary: function() { return this.ranges[this.primIndex]; },
982
- equals: function(other) {
983
- if (other == this) return true;
984
- if (other.primIndex != this.primIndex || other.ranges.length != this.ranges.length) return false;
985
- for (var i = 0; i < this.ranges.length; i++) {
986
- var here = this.ranges[i], there = other.ranges[i];
987
- if (cmp(here.anchor, there.anchor) != 0 || cmp(here.head, there.head) != 0) return false;
988
- }
989
- return true;
990
- },
991
- deepCopy: function() {
992
- for (var out = [], i = 0; i < this.ranges.length; i++)
993
- out[i] = new Range(copyPos(this.ranges[i].anchor), copyPos(this.ranges[i].head));
994
- return new Selection(out, this.primIndex);
995
- },
996
- somethingSelected: function() {
997
- for (var i = 0; i < this.ranges.length; i++)
998
- if (!this.ranges[i].empty()) return true;
999
- return false;
1000
- },
1001
- contains: function(pos, end) {
1002
- if (!end) end = pos;
1003
- for (var i = 0; i < this.ranges.length; i++) {
1004
- var range = this.ranges[i];
1005
- if (cmp(end, range.from()) >= 0 && cmp(pos, range.to()) <= 0)
1006
- return i;
1007
- }
1008
- return -1;
1009
- }
1010
- };
1011
 
1012
- function Range(anchor, head) {
1013
- this.anchor = anchor; this.head = head;
 
 
1014
  }
1015
 
1016
- Range.prototype = {
1017
- from: function() { return minPos(this.anchor, this.head); },
1018
- to: function() { return maxPos(this.anchor, this.head); },
1019
- empty: function() {
1020
- return this.head.line == this.anchor.line && this.head.ch == this.anchor.ch;
1021
- }
1022
- };
1023
 
1024
- // Take an unsorted, potentially overlapping set of ranges, and
1025
- // build a selection out of it. 'Consumes' ranges array (modifying
1026
- // it).
1027
- function normalizeSelection(ranges, primIndex) {
1028
- var prim = ranges[primIndex];
1029
- ranges.sort(function(a, b) { return cmp(a.from(), b.from()); });
1030
- primIndex = indexOf(ranges, prim);
1031
- for (var i = 1; i < ranges.length; i++) {
1032
- var cur = ranges[i], prev = ranges[i - 1];
1033
- if (cmp(prev.to(), cur.from()) >= 0) {
1034
- var from = minPos(prev.from(), cur.from()), to = maxPos(prev.to(), cur.to());
1035
- var inv = prev.empty() ? cur.from() == cur.head : prev.from() == prev.head;
1036
- if (i <= primIndex) --primIndex;
1037
- ranges.splice(--i, 2, new Range(inv ? to : from, inv ? from : to));
1038
- }
1039
- }
1040
- return new Selection(ranges, primIndex);
1041
- }
1042
 
1043
- function simpleSelection(anchor, head) {
1044
- return new Selection([new Range(anchor, head || anchor)], 0);
1045
- }
1046
 
1047
  // Most of the external API clips given positions to make sure they
1048
  // actually exist within the document.
1049
- function clipLine(doc, n) {return Math.max(doc.first, Math.min(n, doc.first + doc.size - 1));}
1050
  function clipPos(doc, pos) {
1051
- if (pos.line < doc.first) return Pos(doc.first, 0);
1052
  var last = doc.first + doc.size - 1;
1053
- if (pos.line > last) return Pos(last, getLine(doc, last).text.length);
1054
- return clipToLen(pos, getLine(doc, pos.line).text.length);
1055
  }
1056
  function clipToLen(pos, linelen) {
1057
  var ch = pos.ch;
1058
- if (ch == null || ch > linelen) return Pos(pos.line, linelen);
1059
- else if (ch < 0) return Pos(pos.line, 0);
1060
- else return pos;
1061
  }
1062
- function isLine(doc, l) {return l >= doc.first && l < doc.first + doc.size;}
1063
  function clipPosArray(doc, array) {
1064
- for (var out = [], i = 0; i < array.length; i++) out[i] = clipPos(doc, array[i]);
1065
- return out;
 
1066
  }
1067
 
1068
- // SELECTION UPDATES
 
 
 
1069
 
1070
- // The 'scroll' parameter given to many of these indicated whether
1071
- // the new cursor position should be scrolled into view after
1072
- // modifying the selection.
 
 
 
 
 
1073
 
1074
- // If shift is held or the extend flag is set, extends a range to
1075
- // include a given position (and optionally a second position).
1076
- // Otherwise, simply returns the range between the given positions.
1077
- // Used for cursor motion and such.
1078
- function extendRange(doc, range, head, other) {
1079
- if (doc.cm && doc.cm.display.shift || doc.extend) {
1080
- var anchor = range.anchor;
1081
- if (other) {
1082
- var posBefore = cmp(head, anchor) < 0;
1083
- if (posBefore != (cmp(other, anchor) < 0)) {
1084
- anchor = head;
1085
- head = other;
1086
- } else if (posBefore != (cmp(head, other) < 0)) {
1087
- head = other;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1088
  }
1089
- }
1090
- return new Range(anchor, head);
1091
- } else {
1092
- return new Range(other || head, head);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1093
  }
 
1094
  }
1095
 
1096
- // Extend the primary selection range, discard the rest.
1097
- function extendSelection(doc, head, other, options) {
1098
- setSelection(doc, new Selection([extendRange(doc, doc.sel.primary(), head, other)], 0), options);
 
 
 
 
 
 
 
 
 
 
 
 
1099
  }
1100
 
1101
- // Extend all selections (pos is an array of selections with length
1102
- // equal the number of selections)
1103
- function extendSelections(doc, heads, options) {
1104
- for (var out = [], i = 0; i < doc.sel.ranges.length; i++)
1105
- out[i] = extendRange(doc, doc.sel.ranges[i], heads[i], null);
1106
- var newSel = normalizeSelection(out, doc.sel.primIndex);
1107
- setSelection(doc, newSel, options);
 
 
 
 
 
1108
  }
1109
 
1110
- // Updates a single range in the selection.
1111
- function replaceOneSelection(doc, i, range, options) {
1112
- var ranges = doc.sel.ranges.slice(0);
1113
- ranges[i] = range;
1114
- setSelection(doc, normalizeSelection(ranges, doc.sel.primIndex), options);
1115
  }
1116
 
1117
- // Reset the selection to a single range.
1118
- function setSimpleSelection(doc, anchor, head, options) {
1119
- setSelection(doc, simpleSelection(anchor, head), options);
 
 
 
 
1120
  }
1121
 
1122
- // Give beforeSelectionChange handlers a change to influence a
1123
- // selection update.
1124
- function filterSelectionChange(doc, sel) {
1125
- var obj = {
1126
- ranges: sel.ranges,
1127
- update: function(ranges) {
1128
- this.ranges = [];
1129
- for (var i = 0; i < ranges.length; i++)
1130
- this.ranges[i] = new Range(clipPos(doc, ranges[i].anchor),
1131
- clipPos(doc, ranges[i].head));
1132
- }
1133
- };
1134
- signal(doc, "beforeSelectionChange", doc, obj);
1135
- if (doc.cm) signal(doc.cm, "beforeSelectionChange", doc.cm, obj);
1136
- if (obj.ranges != sel.ranges) return normalizeSelection(obj.ranges, obj.ranges.length - 1);
1137
- else return sel;
 
 
 
 
1138
  }
1139
 
1140
- function setSelectionReplaceHistory(doc, sel, options) {
1141
- var done = doc.history.done, last = lst(done);
1142
- if (last && last.ranges) {
1143
- done[done.length - 1] = sel;
1144
- setSelectionNoUndo(doc, sel, options);
1145
- } else {
1146
- setSelection(doc, sel, options);
1147
- }
1148
- }
1149
-
1150
- // Set a new selection.
1151
- function setSelection(doc, sel, options) {
1152
- setSelectionNoUndo(doc, sel, options);
1153
- addSelectionToHistory(doc, doc.sel, doc.cm ? doc.cm.curOp.id : NaN, options);
1154
- }
1155
-
1156
- function setSelectionNoUndo(doc, sel, options) {
1157
- if (hasHandler(doc, "beforeSelectionChange") || doc.cm && hasHandler(doc.cm, "beforeSelectionChange"))
1158
- sel = filterSelectionChange(doc, sel);
1159
-
1160
- var bias = options && options.bias ||
1161
- (cmp(sel.primary().head, doc.sel.primary().head) < 0 ? -1 : 1);
1162
- setSelectionInner(doc, skipAtomicInSelection(doc, sel, bias, true));
1163
-
1164
- if (!(options && options.scroll === false) && doc.cm)
1165
- ensureCursorVisible(doc.cm);
1166
- }
1167
-
1168
- function setSelectionInner(doc, sel) {
1169
- if (sel.equals(doc.sel)) return;
1170
-
1171
- doc.sel = sel;
1172
-
1173
- if (doc.cm) {
1174
- doc.cm.curOp.updateInput = doc.cm.curOp.selectionChanged = true;
1175
- signalCursorActivity(doc.cm);
1176
- }
1177
- signalLater(doc, "cursorActivity", doc);
1178
- }
1179
-
1180
- // Verify that the selection does not partially select any atomic
1181
- // marked ranges.
1182
- function reCheckSelection(doc) {
1183
- setSelectionInner(doc, skipAtomicInSelection(doc, doc.sel, null, false), sel_dontScroll);
1184
- }
1185
-
1186
- // Return a selection that does not partially select any atomic
1187
- // ranges.
1188
- function skipAtomicInSelection(doc, sel, bias, mayClear) {
1189
- var out;
1190
- for (var i = 0; i < sel.ranges.length; i++) {
1191
- var range = sel.ranges[i];
1192
- var newAnchor = skipAtomic(doc, range.anchor, bias, mayClear);
1193
- var newHead = skipAtomic(doc, range.head, bias, mayClear);
1194
- if (out || newAnchor != range.anchor || newHead != range.head) {
1195
- if (!out) out = sel.ranges.slice(0, i);
1196
- out[i] = new Range(newAnchor, newHead);
1197
- }
1198
- }
1199
- return out ? normalizeSelection(out, sel.primIndex) : sel;
1200
  }
1201
 
1202
- // Ensure a given position is not inside an atomic range.
1203
- function skipAtomic(doc, pos, bias, mayClear) {
1204
- var flipped = false, curPos = pos;
1205
- var dir = bias || 1;
1206
- doc.cantEdit = false;
1207
- search: for (;;) {
1208
- var line = getLine(doc, curPos.line);
1209
- if (line.markedSpans) {
1210
- for (var i = 0; i < line.markedSpans.length; ++i) {
1211
- var sp = line.markedSpans[i], m = sp.marker;
1212
- if ((sp.from == null || (m.inclusiveLeft ? sp.from <= curPos.ch : sp.from < curPos.ch)) &&
1213
- (sp.to == null || (m.inclusiveRight ? sp.to >= curPos.ch : sp.to > curPos.ch))) {
1214
- if (mayClear) {
1215
- signal(m, "beforeCursorEnter");
1216
- if (m.explicitlyCleared) {
1217
- if (!line.markedSpans) break;
1218
- else {--i; continue;}
1219
- }
1220
- }
1221
- if (!m.atomic) continue;
1222
- var newPos = m.find(dir < 0 ? -1 : 1);
1223
- if (cmp(newPos, curPos) == 0) {
1224
- newPos.ch += dir;
1225
- if (newPos.ch < 0) {
1226
- if (newPos.line > doc.first) newPos = clipPos(doc, Pos(newPos.line - 1));
1227
- else newPos = null;
1228
- } else if (newPos.ch > line.text.length) {
1229
- if (newPos.line < doc.first + doc.size - 1) newPos = Pos(newPos.line + 1, 0);
1230
- else newPos = null;
1231
- }
1232
- if (!newPos) {
1233
- if (flipped) {
1234
- // Driven in a corner -- no valid cursor position found at all
1235
- // -- try again *with* clearing, if we didn't already
1236
- if (!mayClear) return skipAtomic(doc, pos, bias, true);
1237
- // Otherwise, turn off editing until further notice, and return the start of the doc
1238
- doc.cantEdit = true;
1239
- return Pos(doc.first, 0);
1240
- }
1241
- flipped = true; newPos = pos; dir = -dir;
1242
- }
1243
- }
1244
- curPos = newPos;
1245
- continue search;
1246
- }
1247
- }
1248
  }
1249
- return curPos;
1250
- }
1251
- }
1252
-
1253
- // SELECTION DRAWING
1254
-
1255
- // Redraw the selection and/or cursor
1256
- function drawSelection(cm) {
1257
- var display = cm.display, doc = cm.doc, result = {};
1258
- var curFragment = result.cursors = document.createDocumentFragment();
1259
- var selFragment = result.selection = document.createDocumentFragment();
1260
-
1261
- for (var i = 0; i < doc.sel.ranges.length; i++) {
1262
- var range = doc.sel.ranges[i];
1263
- var collapsed = range.empty();
1264
- if (collapsed || cm.options.showCursorWhenSelecting)
1265
- drawSelectionCursor(cm, range, curFragment);
1266
- if (!collapsed)
1267
- drawSelectionRange(cm, range, selFragment);
1268
- }
1269
-
1270
- // Move the hidden textarea near the cursor to prevent scrolling artifacts
1271
- if (cm.options.moveInputWithCursor) {
1272
- var headPos = cursorCoords(cm, doc.sel.primary().head, "div");
1273
- var wrapOff = display.wrapper.getBoundingClientRect(), lineOff = display.lineDiv.getBoundingClientRect();
1274
- result.teTop = Math.max(0, Math.min(display.wrapper.clientHeight - 10,
1275
- headPos.top + lineOff.top - wrapOff.top));
1276
- result.teLeft = Math.max(0, Math.min(display.wrapper.clientWidth - 10,
1277
- headPos.left + lineOff.left - wrapOff.left));
1278
- }
1279
-
1280
- return result;
1281
- }
1282
-
1283
- function updateSelection(cm, drawn) {
1284
- if (!drawn) drawn = drawSelection(cm);
1285
- removeChildrenAndAdd(cm.display.cursorDiv, drawn.cursors);
1286
- removeChildrenAndAdd(cm.display.selectionDiv, drawn.selection);
1287
- if (drawn.teTop != null) {
1288
- cm.display.inputDiv.style.top = drawn.teTop + "px";
1289
- cm.display.inputDiv.style.left = drawn.teLeft + "px";
1290
- }
1291
- }
1292
-
1293
- // Draws a cursor for the given range
1294
- function drawSelectionCursor(cm, range, output) {
1295
- var pos = cursorCoords(cm, range.head, "div", null, null, !cm.options.singleCursorHeightPerLine);
1296
-
1297
- var cursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor"));
1298
- cursor.style.left = pos.left + "px";
1299
- cursor.style.top = pos.top + "px";
1300
- cursor.style.height = Math.max(0, pos.bottom - pos.top) * cm.options.cursorHeight + "px";
1301
-
1302
- if (pos.other) {
1303
- // Secondary cursor, shown when on a 'jump' in bi-directional text
1304
- var otherCursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor CodeMirror-secondarycursor"));
1305
- otherCursor.style.display = "";
1306
- otherCursor.style.left = pos.other.left + "px";
1307
- otherCursor.style.top = pos.other.top + "px";
1308
- otherCursor.style.height = (pos.other.bottom - pos.other.top) * .85 + "px";
1309
- }
1310
- }
1311
-
1312
- // Draws the given range as a highlighted selection
1313
- function drawSelectionRange(cm, range, output) {
1314
- var display = cm.display, doc = cm.doc;
1315
- var fragment = document.createDocumentFragment();
1316
- var padding = paddingH(cm.display), leftSide = padding.left, rightSide = display.lineSpace.offsetWidth - padding.right;
1317
-
1318
- function add(left, top, width, bottom) {
1319
- if (top < 0) top = 0;
1320
- top = Math.round(top);
1321
- bottom = Math.round(bottom);
1322
- fragment.appendChild(elt("div", null, "CodeMirror-selected", "position: absolute; left: " + left +
1323
- "px; top: " + top + "px; width: " + (width == null ? rightSide - left : width) +
1324
- "px; height: " + (bottom - top) + "px"));
1325
- }
1326
-
1327
- function drawForLine(line, fromArg, toArg) {
1328
- var lineObj = getLine(doc, line);
1329
- var lineLen = lineObj.text.length;
1330
- var start, end;
1331
- function coords(ch, bias) {
1332
- return charCoords(cm, Pos(line, ch), "div", lineObj, bias);
1333
  }
1334
-
1335
- iterateBidiSections(getOrder(lineObj), fromArg || 0, toArg == null ? lineLen : toArg, function(from, to, dir) {
1336
- var leftPos = coords(from, "left"), rightPos, left, right;
1337
- if (from == to) {
1338
- rightPos = leftPos;
1339
- left = right = leftPos.left;
1340
- } else {
1341
- rightPos = coords(to - 1, "right");
1342
- if (dir == "rtl") { var tmp = leftPos; leftPos = rightPos; rightPos = tmp; }
1343
- left = leftPos.left;
1344
- right = rightPos.right;
1345
- }
1346
- if (fromArg == null && from == 0) left = leftSide;
1347
- if (rightPos.top - leftPos.top > 3) { // Different lines, draw top part
1348
- add(left, leftPos.top, null, leftPos.bottom);
1349
- left = leftSide;
1350
- if (leftPos.bottom < rightPos.top) add(left, leftPos.bottom, null, rightPos.top);
1351
- }
1352
- if (toArg == null && to == lineLen) right = rightSide;
1353
- if (!start || leftPos.top < start.top || leftPos.top == start.top && leftPos.left < start.left)
1354
- start = leftPos;
1355
- if (!end || rightPos.bottom > end.bottom || rightPos.bottom == end.bottom && rightPos.right > end.right)
1356
- end = rightPos;
1357
- if (left < leftSide + 1) left = leftSide;
1358
- add(left, rightPos.top, right - left, rightPos.bottom);
1359
- });
1360
- return {start: start, end: end};
1361
- }
1362
-
1363
- var sFrom = range.from(), sTo = range.to();
1364
- if (sFrom.line == sTo.line) {
1365
- drawForLine(sFrom.line, sFrom.ch, sTo.ch);
1366
- } else {
1367
- var fromLine = getLine(doc, sFrom.line), toLine = getLine(doc, sTo.line);
1368
- var singleVLine = visualLine(fromLine) == visualLine(toLine);
1369
- var leftEnd = drawForLine(sFrom.line, sFrom.ch, singleVLine ? fromLine.text.length + 1 : null).end;
1370
- var rightStart = drawForLine(sTo.line, singleVLine ? 0 : null, sTo.ch).start;
1371
- if (singleVLine) {
1372
- if (leftEnd.top < rightStart.top - 2) {
1373
- add(leftEnd.right, leftEnd.top, null, leftEnd.bottom);
1374
- add(leftSide, rightStart.top, rightStart.left, rightStart.bottom);
1375
- } else {
1376
- add(leftEnd.right, leftEnd.top, rightStart.left - leftEnd.right, leftEnd.bottom);
1377
  }
 
1378
  }
1379
- if (leftEnd.bottom < rightStart.top)
1380
- add(leftSide, leftEnd.bottom, null, rightStart.top);
 
 
 
 
 
 
 
1381
  }
1382
-
1383
- output.appendChild(fragment);
1384
- }
1385
-
1386
- // Cursor-blinking
1387
- function restartBlink(cm) {
1388
- if (!cm.state.focused) return;
1389
- var display = cm.display;
1390
- clearInterval(display.blinker);
1391
- var on = true;
1392
- display.cursorDiv.style.visibility = "";
1393
- if (cm.options.cursorBlinkRate > 0)
1394
- display.blinker = setInterval(function() {
1395
- display.cursorDiv.style.visibility = (on = !on) ? "" : "hidden";
1396
- }, cm.options.cursorBlinkRate);
1397
- else if (cm.options.cursorBlinkRate < 0)
1398
- display.cursorDiv.style.visibility = "hidden";
1399
- }
1400
-
1401
- // HIGHLIGHT WORKER
1402
-
1403
- function startWorker(cm, time) {
1404
- if (cm.doc.mode.startState && cm.doc.frontier < cm.display.viewTo)
1405
- cm.state.highlight.set(time, bind(highlightWorker, cm));
1406
- }
1407
-
1408
- function highlightWorker(cm) {
1409
- var doc = cm.doc;
1410
- if (doc.frontier < doc.first) doc.frontier = doc.first;
1411
- if (doc.frontier >= cm.display.viewTo) return;
1412
- var end = +new Date + cm.options.workTime;
1413
- var state = copyState(doc.mode, getStateBefore(cm, doc.frontier));
1414
- var changedLines = [];
1415
-
1416
- doc.iter(doc.frontier, Math.min(doc.first + doc.size, cm.display.viewTo + 500), function(line) {
1417
- if (doc.frontier >= cm.display.viewFrom) { // Visible
1418
- var oldStyles = line.styles;
1419
- var highlighted = highlightLine(cm, line, state, true);
1420
- line.styles = highlighted.styles;
1421
- var oldCls = line.styleClasses, newCls = highlighted.classes;
1422
- if (newCls) line.styleClasses = newCls;
1423
- else if (oldCls) line.styleClasses = null;
1424
- var ischange = !oldStyles || oldStyles.length != line.styles.length ||
1425
- oldCls != newCls && (!oldCls || !newCls || oldCls.bgClass != newCls.bgClass || oldCls.textClass != newCls.textClass);
1426
- for (var i = 0; !ischange && i < oldStyles.length; ++i) ischange = oldStyles[i] != line.styles[i];
1427
- if (ischange) changedLines.push(doc.frontier);
1428
- line.stateAfter = copyState(doc.mode, state);
1429
- } else {
1430
- processLine(cm, line.text, state);
1431
- line.stateAfter = doc.frontier % 5 == 0 ? copyState(doc.mode, state) : null;
1432
- }
1433
- ++doc.frontier;
1434
- if (+new Date > end) {
1435
- startWorker(cm, cm.options.workDelay);
1436
- return true;
1437
- }
1438
- });
1439
- if (changedLines.length) runInOp(cm, function() {
1440
- for (var i = 0; i < changedLines.length; i++)
1441
- regLineChange(cm, changedLines[i], "text");
1442
- });
1443
  }
1444
 
1445
  // Finds the line to start with when starting a parse. Tries to
@@ -1451,709 +1259,735 @@
1451
  var minindent, minline, doc = cm.doc;
1452
  var lim = precise ? -1 : n - (cm.doc.mode.innerMode ? 1000 : 100);
1453
  for (var search = n; search > lim; --search) {
1454
- if (search <= doc.first) return doc.first;
1455
- var line = getLine(doc, search - 1);
1456
- if (line.stateAfter && (!precise || search <= doc.frontier)) return search;
 
1457
  var indented = countColumn(line.text, null, cm.options.tabSize);
1458
  if (minline == null || minindent > indented) {
1459
  minline = search - 1;
1460
  minindent = indented;
1461
  }
1462
  }
1463
- return minline;
1464
- }
1465
-
1466
- function getStateBefore(cm, n, precise) {
1467
- var doc = cm.doc, display = cm.display;
1468
- if (!doc.mode.startState) return true;
1469
- var pos = findStartLine(cm, n, precise), state = pos > doc.first && getLine(doc, pos-1).stateAfter;
1470
- if (!state) state = startState(doc.mode);
1471
- else state = copyState(doc.mode, state);
1472
- doc.iter(pos, n, function(line) {
1473
- processLine(cm, line.text, state);
1474
- var save = pos == n - 1 || pos % 5 == 0 || pos >= display.viewFrom && pos < display.viewTo;
1475
- line.stateAfter = save ? copyState(doc.mode, state) : null;
1476
- ++pos;
1477
- });
1478
- if (precise) doc.frontier = pos;
1479
- return state;
1480
- }
1481
-
1482
- // POSITION MEASUREMENT
1483
-
1484
- function paddingTop(display) {return display.lineSpace.offsetTop;}
1485
- function paddingVert(display) {return display.mover.offsetHeight - display.lineSpace.offsetHeight;}
1486
- function paddingH(display) {
1487
- if (display.cachedPaddingH) return display.cachedPaddingH;
1488
- var e = removeChildrenAndAdd(display.measure, elt("pre", "x"));
1489
- var style = window.getComputedStyle ? window.getComputedStyle(e) : e.currentStyle;
1490
- var data = {left: parseInt(style.paddingLeft), right: parseInt(style.paddingRight)};
1491
- if (!isNaN(data.left) && !isNaN(data.right)) display.cachedPaddingH = data;
1492
- return data;
1493
  }
1494
 
1495
- // Ensure the lineView.wrapping.heights array is populated. This is
1496
- // an array of bottom offsets for the lines that make up a drawn
1497
- // line. When lineWrapping is on, there might be more than one
1498
- // height.
1499
- function ensureLineHeights(cm, lineView, rect) {
1500
- var wrapping = cm.options.lineWrapping;
1501
- var curWidth = wrapping && cm.display.scroller.clientWidth;
1502
- if (!lineView.measure.heights || wrapping && lineView.measure.width != curWidth) {
1503
- var heights = lineView.measure.heights = [];
1504
- if (wrapping) {
1505
- lineView.measure.width = curWidth;
1506
- var rects = lineView.text.firstChild.getClientRects();
1507
- for (var i = 0; i < rects.length - 1; i++) {
1508
- var cur = rects[i], next = rects[i + 1];
1509
- if (Math.abs(cur.bottom - next.bottom) > 2)
1510
- heights.push((cur.bottom + next.top) / 2 - rect.top);
1511
- }
1512
  }
1513
- heights.push(rect.bottom - rect.top);
1514
  }
 
1515
  }
1516
 
1517
- // Find a line map (mapping character offsets to text nodes) and a
1518
- // measurement cache for the given line number. (A line view might
1519
- // contain multiple lines when collapsed ranges are present.)
1520
- function mapFromLineView(lineView, line, lineN) {
1521
- if (lineView.line == line)
1522
- return {map: lineView.measure.map, cache: lineView.measure.cache};
1523
- for (var i = 0; i < lineView.rest.length; i++)
1524
- if (lineView.rest[i] == line)
1525
- return {map: lineView.measure.maps[i], cache: lineView.measure.caches[i]};
1526
- for (var i = 0; i < lineView.rest.length; i++)
1527
- if (lineNo(lineView.rest[i]) > lineN)
1528
- return {map: lineView.measure.maps[i], cache: lineView.measure.caches[i], before: true};
1529
- }
1530
 
1531
- // Render a line into the hidden node display.externalMeasured. Used
1532
- // when measurement is needed for a line that's not in the viewport.
1533
- function updateExternalMeasurement(cm, line) {
1534
- line = visualLine(line);
1535
- var lineN = lineNo(line);
1536
- var view = cm.display.externalMeasured = new LineView(cm.doc, line, lineN);
1537
- view.lineN = lineN;
1538
- var built = view.built = buildLineContent(cm, view);
1539
- view.text = built.pre;
1540
- removeChildrenAndAdd(cm.display.lineMeasure, built.pre);
1541
- return view;
1542
  }
1543
 
1544
- // Get a {top, bottom, left, right} box (in line-local coordinates)
1545
- // for a given character.
1546
- function measureChar(cm, line, ch, bias) {
1547
- return measureCharPrepared(cm, prepareMeasureForLine(cm, line), ch, bias);
1548
  }
1549
 
1550
- // Find a line view that corresponds to the given line number.
1551
- function findViewForLine(cm, lineN) {
1552
- if (lineN >= cm.display.viewFrom && lineN < cm.display.viewTo)
1553
- return cm.display.view[findViewIndex(cm, lineN)];
1554
- var ext = cm.display.externalMeasured;
1555
- if (ext && lineN >= ext.lineN && lineN < ext.lineN + ext.size)
1556
- return ext;
1557
  }
1558
 
1559
- // Measurement can be split in two steps, the set-up work that
1560
- // applies to the whole line, and the measurement of the actual
1561
- // character. Functions like coordsChar, that need to do a lot of
1562
- // measurements in a row, can thus ensure that the set-up work is
1563
- // only done once.
1564
- function prepareMeasureForLine(cm, line) {
1565
- var lineN = lineNo(line);
1566
- var view = findViewForLine(cm, lineN);
1567
- if (view && !view.text)
1568
- view = null;
1569
- else if (view && view.changes)
1570
- updateLineForChanges(cm, view, lineN, getDimensions(cm));
1571
- if (!view)
1572
- view = updateExternalMeasurement(cm, line);
1573
-
1574
- var info = mapFromLineView(view, line, lineN);
1575
- return {
1576
- line: line, view: view, rect: null,
1577
- map: info.map, cache: info.cache, before: info.before,
1578
- hasHeights: false
1579
- };
1580
  }
1581
 
1582
- // Given a prepared measurement object, measures the position of an
1583
- // actual character (or fetches it from the cache).
1584
- function measureCharPrepared(cm, prepared, ch, bias, varHeight) {
1585
- if (prepared.before) ch = -1;
1586
- var key = ch + (bias || ""), found;
1587
- if (prepared.cache.hasOwnProperty(key)) {
1588
- found = prepared.cache[key];
1589
- } else {
1590
- if (!prepared.rect)
1591
- prepared.rect = prepared.view.text.getBoundingClientRect();
1592
- if (!prepared.hasHeights) {
1593
- ensureLineHeights(cm, prepared.view, prepared.rect);
1594
- prepared.hasHeights = true;
1595
  }
1596
- found = measureCharInner(cm, prepared, ch, bias);
1597
- if (!found.bogus) prepared.cache[key] = found;
1598
- }
1599
- return {left: found.left, right: found.right,
1600
- top: varHeight ? found.rtop : found.top,
1601
- bottom: varHeight ? found.rbottom : found.bottom};