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
/
profile.js
273 lines (233 loc) · 7.36 KB
/
profile.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
/**
* Output profile module.
* Profile defines how XHTML output data should look like
*/
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('../utils/common');
var resources = require('./resources');
var prefs = require('./preferences');
prefs.define('profile.allowCompactBoolean', true,
'This option can be used to globally disable compact form of boolean ' +
'attribues (attributes where name and value are equal). With compact' +
'form enabled, HTML tags can be outputted as <code><div contenteditable></code> ' +
'instead of <code><div contenteditable="contenteditable"></code>');
prefs.define('profile.booleanAttributes', '^contenteditable|seamless|async|autofocus|autoplay|checked|controls|defer|disabled|formnovalidate|hidden|ismap|loop|multiple|muted|novalidate|readonly|required|reversed|selected|typemustmatch$',
'A regular expression for attributes that should be boolean by default.' +
'If attribute name matches this expression, you don’t have to write dot ' +
'after attribute name in Emmet abbreviation to mark it as boolean.');
var profiles = {};
var defaultProfile = {
tag_case: 'asis',
attr_case: 'asis',
attr_quotes: 'double',
// Each tag on new line
tag_nl: 'decide',
// With tag_nl === true, defines if leaf node (e.g. node with no children)
// should have formatted line breaks
tag_nl_leaf: false,
place_cursor: true,
// Indent tags
indent: true,
// How many inline elements should be to force line break
// (set to 0 to disable)
inline_break: 3,
// Produce compact notation of boolean attribues:
// attributes where name and value are equal.
// With this option enabled, HTML filter will
// produce <div contenteditable> instead of <div contenteditable="contenteditable">
compact_bool: false,
// Use self-closing style for writing empty elements, e.g. <br /> or <br>
self_closing_tag: 'xhtml',
// Profile-level output filters, re-defines syntax filters
filters: '',
// Additional filters applied to abbreviation.
// Unlike "filters", this preference doesn't override default filters
// but add the instead every time given profile is chosen
extraFilters: ''
};
/**
* @constructor
* @type OutputProfile
* @param {Object} options
*/
function OutputProfile(options) {
utils.extend(this, defaultProfile, options);
}
OutputProfile.prototype = {
/**
* Transforms tag name case depending on current profile settings
* @param {String} name String to transform
* @returns {String}
*/
tagName: function(name) {
return stringCase(name, this.tag_case);
},
/**
* Transforms attribute name case depending on current profile settings
* @param {String} name String to transform
* @returns {String}
*/
attributeName: function(name) {
return stringCase(name, this.attr_case);
},
/**
* Returns quote character for current profile
* @returns {String}
*/
attributeQuote: function() {
return this.attr_quotes == 'single' ? "'" : '"';
},
/**
* Returns self-closing tag symbol for current profile
* @returns {String}
*/
selfClosing: function() {
if (this.self_closing_tag == 'xhtml')
return ' /';
if (this.self_closing_tag === true)
return '/';
return '';
},
/**
* Returns cursor token based on current profile settings
* @returns {String}
*/
cursor: function() {
return this.place_cursor ? utils.getCaretPlaceholder() : '';
},
/**
* Check if attribute with given name is boolean,
* e.g. written as `contenteditable` instead of
* `contenteditable="contenteditable"`
* @param {String} name Attribute name
* @return {Boolean}
*/
isBoolean: function(name, value) {
if (name == value) {
return true;
}
var boolAttrs = prefs.get('profile.booleanAttributes');
if (!value && boolAttrs) {
boolAttrs = new RegExp(boolAttrs, 'i');
return boolAttrs.test(name);
}
return false;
},
/**
* Check if compact boolean attribute record is
* allowed for current profile
* @return {Boolean}
*/
allowCompactBoolean: function() {
return this.compact_bool && prefs.get('profile.allowCompactBoolean');
}
};
/**
* Helper function that converts string case depending on
* <code>caseValue</code>
* @param {String} str String to transform
* @param {String} caseValue Case value: can be <i>lower</i>,
* <i>upper</i> and <i>leave</i>
* @returns {String}
*/
function stringCase(str, caseValue) {
switch (String(caseValue || '').toLowerCase()) {
case 'lower':
return str.toLowerCase();
case 'upper':
return str.toUpperCase();
}
return str;
}
/**
* Creates new output profile
* @param {String} name Profile name
* @param {Object} options Profile options
*/
function createProfile(name, options) {
return profiles[name.toLowerCase()] = new OutputProfile(options);
}
function createDefaultProfiles() {
createProfile('xhtml');
createProfile('html', {self_closing_tag: false, compact_bool: true});
createProfile('xml', {self_closing_tag: true, tag_nl: true});
createProfile('plain', {tag_nl: false, indent: false, place_cursor: false});
createProfile('line', {tag_nl: false, indent: false, extraFilters: 's'});
createProfile('css', {tag_nl: true});
createProfile('css_line', {tag_nl: false});
}
createDefaultProfiles();
return {
/**
* Creates new output profile and adds it into internal dictionary
* @param {String} name Profile name
* @param {Object} options Profile options
* @memberOf emmet.profile
* @returns {Object} New profile
*/
create: function(name, options) {
if (arguments.length == 2)
return createProfile(name, options);
else
// create profile object only
return new OutputProfile(utils.defaults(name || {}, defaultProfile));
},
/**
* Returns profile by its name. If profile wasn't found, returns
* 'plain' profile
* @param {String} name Profile name. Might be profile itself
* @param {String} syntax. Optional. Current editor syntax. If defined,
* profile is searched in resources first, then in predefined profiles
* @returns {Object}
*/
get: function(name, syntax) {
if (!name && syntax) {
// search in user resources first
var profile = resources.findItem(syntax, 'profile');
if (profile) {
name = profile;
}
}
if (!name) {
return profiles.plain;
}
if (name instanceof OutputProfile) {
return name;
}
if (typeof name === 'string' && name.toLowerCase() in profiles) {
return profiles[name.toLowerCase()];
}
return this.create(name);
},
/**
* Deletes profile with specified name
* @param {String} name Profile name
*/
remove: function(name) {
name = (name || '').toLowerCase();
if (name in profiles)
delete profiles[name];
},
/**
* Resets all user-defined profiles
*/
reset: function() {
profiles = {};
createDefaultProfiles();
},
/**
* Helper function that converts string case depending on
* <code>caseValue</code>
* @param {String} str String to transform
* @param {String} caseValue Case value: can be <i>lower</i>,
* <i>upper</i> and <i>leave</i>
* @returns {String}
*/
stringCase: stringCase
};
});