This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
339 lines (296 loc) · 8.8 KB
/
action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Utility methods for Emmet actions
* @author Sergey Chikuyonok ([email protected]) <http://chikuyonok.ru>
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var utils = require('./common');
var cssSections = require('./cssSections');
var abbreviationParser = require('../parser/abbreviation');
var htmlMatcher = require('../assets/htmlMatcher');
var xmlEditTree = require('../editTree/xml');
var range = require('../assets/range');
var resources = require('../assets/resources');
return {
mimeTypes: {
'gif' : 'image/gif',
'png' : 'image/png',
'jpg' : 'image/jpeg',
'jpeg': 'image/jpeg',
'svg' : 'image/svg+xml',
'html': 'text/html',
'htm' : 'text/html'
},
/**
* Extracts abbreviations from text stream, starting from the end
* @param {String} str
* @return {String} Abbreviation or empty string
* @memberOf emmet.actionUtils
*/
extractAbbreviation: function(str) {
var curOffset = str.length;
var startIndex = -1;
var groupCount = 0;
var braceCount = 0;
var textCount = 0;
while (true) {
curOffset--;
if (curOffset < 0) {
// moved to the beginning of the line
startIndex = 0;
break;
}
var ch = str.charAt(curOffset);
if (ch == ']') {
braceCount++;
} else if (ch == '[') {
if (!braceCount) { // unexpected brace
startIndex = curOffset + 1;
break;
}
braceCount--;
} else if (ch == '}') {
textCount++;
} else if (ch == '{') {
if (!textCount) { // unexpected brace
startIndex = curOffset + 1;
break;
}
textCount--;
} else if (ch == ')') {
groupCount++;
} else if (ch == '(') {
if (!groupCount) { // unexpected brace
startIndex = curOffset + 1;
break;
}
groupCount--;
} else {
if (braceCount || textCount)
// respect all characters inside attribute sets or text nodes
continue;
else if (!abbreviationParser.isAllowedChar(ch) || (ch == '>' && utils.endsWithTag(str.substring(0, curOffset + 1)))) {
// found stop symbol
startIndex = curOffset + 1;
break;
}
}
}
if (startIndex != -1 && !textCount && !braceCount && !groupCount)
// found something, remove some invalid symbols from the
// beginning and return abbreviation
return str.substring(startIndex).replace(/^[\*\+\>\^]+/, '');
else
return '';
},
/**
* Gets image size from image byte stream.
* @author http://romeda.org/rePublish/
* @param {String} stream Image byte stream (use <code>IEmmetFile.read()</code>)
* @return {Object} Object with <code>width</code> and <code>height</code> properties
*/
getImageSize: function(stream) {
var pngMagicNum = "\211PNG\r\n\032\n",
jpgMagicNum = "\377\330",
gifMagicNum = "GIF8",
pos = 0,
nextByte = function() {
return stream.charCodeAt(pos++);
};
if (stream.substr(0, 8) === pngMagicNum) {
// PNG. Easy peasy.
pos = stream.indexOf('IHDR') + 4;
return {
width: (nextByte() << 24) | (nextByte() << 16) | (nextByte() << 8) | nextByte(),
height: (nextByte() << 24) | (nextByte() << 16) | (nextByte() << 8) | nextByte()
};
} else if (stream.substr(0, 4) === gifMagicNum) {
pos = 6;
return {
width: nextByte() | (nextByte() << 8),
height: nextByte() | (nextByte() << 8)
};
} else if (stream.substr(0, 2) === jpgMagicNum) {
pos = 2;
var l = stream.length;
while (pos < l) {
if (nextByte() != 0xFF) return;
var marker = nextByte();
if (marker == 0xDA) break;
var size = (nextByte() << 8) | nextByte();
if (marker >= 0xC0 && marker <= 0xCF && !(marker & 0x4) && !(marker & 0x8)) {
pos += 1;
return {
height: (nextByte() << 8) | nextByte(),
width: (nextByte() << 8) | nextByte()
};
} else {
pos += size - 2;
}
}
}
},
/**
* Captures context XHTML element from editor under current caret position.
* This node can be used as a helper for abbreviation extraction
* @param {IEmmetEditor} editor
* @returns {Object}
*/
captureContext: function(editor, pos) {
var allowedSyntaxes = {'html': 1, 'xml': 1, 'xsl': 1, 'jsx': 1};
var syntax = editor.getSyntax();
if (syntax in allowedSyntaxes) {
var content = editor.getContent();
if (typeof pos === 'undefined') {
pos = editor.getCaretPos();
}
var tag = htmlMatcher.find(content, pos);
if (tag && tag.type == 'tag') {
var startTag = tag.open;
var contextNode = {
name: startTag.name,
attributes: [],
match: tag
};
// parse attributes
var tagTree = xmlEditTree.parse(startTag.range.substring(content));
if (tagTree) {
contextNode.attributes = tagTree.getAll().map(function(item) {
return {
name: item.name(),
value: item.value()
};
});
}
return contextNode;
}
}
return null;
},
/**
* Find expression bounds in current editor at caret position.
* On each character a <code>fn</code> function will be called and must
* return <code>true</code> if current character meets requirements,
* <code>false</code> otherwise
* @param {IEmmetEditor} editor
* @param {Function} fn Function to test each character of expression
* @return {Range}
*/
findExpressionBounds: function(editor, fn) {
var content = String(editor.getContent());
var il = content.length;
var exprStart = editor.getCaretPos() - 1;
var exprEnd = exprStart + 1;
// start by searching left
while (exprStart >= 0 && fn(content.charAt(exprStart), exprStart, content)) exprStart--;
// then search right
while (exprEnd < il && fn(content.charAt(exprEnd), exprEnd, content)) exprEnd++;
if (exprEnd > exprStart) {
return range([++exprStart, exprEnd]);
}
},
/**
* @param {IEmmetEditor} editor
* @param {Object} data
* @returns {Boolean}
*/
compoundUpdate: function(editor, data) {
if (data) {
var sel = editor.getSelectionRange();
editor.replaceContent(data.data, data.start, data.end, true);
editor.createSelection(data.caret, data.caret + sel.end - sel.start);
return true;
}
return false;
},
/**
* Common syntax detection method for editors that doesn’t provide any
* info about current syntax scope.
* @param {IEmmetEditor} editor Current editor
* @param {String} hint Any syntax hint that editor can provide
* for syntax detection. Default is 'html'
* @returns {String}
*/
detectSyntax: function(editor, hint) {
var syntax = hint || 'html';
if (!resources.hasSyntax(syntax)) {
syntax = 'html';
}
if (syntax == 'html' && (this.isStyle(editor) || this.isInlineCSS(editor))) {
syntax = 'css';
}
if (syntax == 'styl') {
syntax = 'stylus';
}
return syntax;
},
/**
* Common method for detecting output profile
* @param {IEmmetEditor} editor
* @returns {String}
*/
detectProfile: function(editor) {
var syntax = editor.getSyntax();
// get profile from syntax definition
var profile = resources.findItem(syntax, 'profile');
if (profile) {
return profile;
}
switch(syntax) {
case 'xml':
case 'xsl':
return 'xml';
case 'css':
if (this.isInlineCSS(editor)) {
return 'line';
}
break;
case 'html':
profile = resources.getVariable('profile');
if (!profile) { // no forced profile, guess from content
// html or xhtml?
profile = this.isXHTML(editor) ? 'xhtml': 'html';
}
return profile;
}
return 'xhtml';
},
/**
* Tries to detect if current document is XHTML one.
* @param {IEmmetEditor} editor
* @returns {Boolean}
*/
isXHTML: function(editor) {
return editor.getContent().search(/<!DOCTYPE[^>]+XHTML/i) != -1;
},
/**
* Check if current caret position is inside <style> tag
* @param {IEmmetEditor} editor
* @returns {Range} Inner range of <style> tag
*/
isStyle: function(editor) {
return !!cssSections.styleTagRange(editor.getContent(), editor.getCaretPos());
},
/**
* Check if given CSS dialect is supported by CSS actions
* @param {String} syntax
* @return {Boolean}
*/
isSupportedCSS: function(syntax) {
return syntax == 'css' || syntax == 'less' || syntax == 'scss';
},
/**
* Check if current caret position is inside "style" attribute of HTML
* element
* @param {IEmmetEditor} editor
* @returns {Range} Inner range of style attribute
*/
isInlineCSS: function(editor) {
return !!cssSections.styleAttrRange(editor.getContent(), editor.getCaretPos());
}
};
});