forked from emmetio/emmet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get rid of some node deps for better Webpack budle
- Loading branch information
1 parent
96996d4
commit 6bd5715
Showing
3 changed files
with
71 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/** | ||
* Parsed resources (snippets, abbreviations, variables, etc.) for Emmet. | ||
* Contains convenient method to get access for snippets with respect of | ||
* Contains convenient method to get access for snippets with respect of | ||
* inheritance. Also provides ability to store data in different vocabularies | ||
* ('system' and 'user') for fast and safe resource update | ||
* @author Sergey Chikuyonok ([email protected]) | ||
|
@@ -22,15 +22,15 @@ define(function(require, exports, module) { | |
|
||
var VOC_SYSTEM = 'system'; | ||
var VOC_USER = 'user'; | ||
|
||
var cache = {}; | ||
|
||
/** Regular expression for XML tag matching */ | ||
var reTag = /^<(\w+\:?[\w\-]*)((?:\s+[@\!]?[\w\:\-]+\s*=\s*(['"]).*?\3)*)\s*(\/?)>/; | ||
|
||
var systemSettings = {}; | ||
var userSettings = {}; | ||
|
||
/** @type HandlerList List of registered abbreviation resolvers */ | ||
var resolvers = handlerList.create(); | ||
|
||
|
@@ -43,7 +43,7 @@ define(function(require, exports, module) { | |
fn(obj[key], key); | ||
}); | ||
} | ||
|
||
/** | ||
* Normalizes caret plceholder in passed text: replaces | character with | ||
* default caret placeholder | ||
|
@@ -53,19 +53,19 @@ define(function(require, exports, module) { | |
function normalizeCaretPlaceholder(text) { | ||
return utils.replaceUnescapedSymbol(text, '|', utils.getCaretPlaceholder()); | ||
} | ||
|
||
function parseItem(name, value, type) { | ||
value = normalizeCaretPlaceholder(value); | ||
|
||
if (type == 'snippets') { | ||
return elements.create('snippet', value); | ||
} | ||
|
||
if (type == 'abbreviations') { | ||
return parseAbbreviation(name, value); | ||
} | ||
} | ||
|
||
/** | ||
* Parses single abbreviation | ||
* @param {String} key Abbreviation name | ||
|
@@ -82,7 +82,7 @@ define(function(require, exports, module) { | |
return elements.create('reference', value); | ||
} | ||
} | ||
|
||
/** | ||
* Normalizes snippet key name for better fuzzy search | ||
* @param {String} str | ||
|
@@ -131,15 +131,15 @@ define(function(require, exports, module) { | |
|
||
voc[syntax] = _section; | ||
}); | ||
|
||
|
||
if (type == VOC_SYSTEM) { | ||
systemSettings = voc; | ||
} else { | ||
userSettings = voc; | ||
} | ||
}, | ||
|
||
/** | ||
* Returns resource vocabulary by its name | ||
* @param {String} name Vocabulary name ('system' or 'user') | ||
|
@@ -148,27 +148,27 @@ define(function(require, exports, module) { | |
getVocabulary: function(name) { | ||
return name == VOC_SYSTEM ? systemSettings : userSettings; | ||
}, | ||
|
||
/** | ||
* Returns resource (abbreviation, snippet, etc.) matched for passed | ||
* Returns resource (abbreviation, snippet, etc.) matched for passed | ||
* abbreviation | ||
* @param {AbbreviationNode} node | ||
* @param {String} syntax | ||
* @returns {Object} | ||
*/ | ||
getMatchedResource: function(node, syntax) { | ||
return resolvers.exec(null, utils.toArray(arguments)) | ||
return resolvers.exec(null, utils.toArray(arguments)) | ||
|| this.findSnippet(syntax, node.name()); | ||
}, | ||
|
||
/** | ||
* Returns variable value | ||
* @return {String} | ||
*/ | ||
getVariable: function(name) { | ||
return (this.getSection('variables') || {})[name]; | ||
}, | ||
|
||
/** | ||
* Store runtime variable in user storage | ||
* @param {String} name Variable name | ||
|
@@ -178,38 +178,38 @@ define(function(require, exports, module) { | |
var voc = this.getVocabulary('user') || {}; | ||
if (!('variables' in voc)) | ||
voc.variables = {}; | ||
|
||
voc.variables[name] = value; | ||
this.setVocabulary(voc, 'user'); | ||
}, | ||
|
||
/** | ||
* Check if there are resources for specified syntax | ||
* @param {String} syntax | ||
* @return {Boolean} | ||
*/ | ||
hasSyntax: function(syntax) { | ||
return syntax in this.getVocabulary(VOC_USER) | ||
return syntax in this.getVocabulary(VOC_USER) | ||
|| syntax in this.getVocabulary(VOC_SYSTEM); | ||
}, | ||
|
||
/** | ||
* Registers new abbreviation resolver. | ||
* @param {Function} fn Abbreviation resolver which will receive | ||
* @param {Function} fn Abbreviation resolver which will receive | ||
* abbreviation as first argument and should return parsed abbreviation | ||
* object if abbreviation has handled successfully, <code>null</code> | ||
* otherwise | ||
* @param {Object} options Options list as described in | ||
* @param {Object} options Options list as described in | ||
* {@link HandlerList#add()} method | ||
*/ | ||
addResolver: function(fn, options) { | ||
resolvers.add(fn, options); | ||
}, | ||
|
||
removeResolver: function(fn) { | ||
resolvers.remove(fn); | ||
}, | ||
|
||
/** | ||
* Returns actual section data, merged from both | ||
* system and user data | ||
|
@@ -220,11 +220,11 @@ define(function(require, exports, module) { | |
getSection: function(name) { | ||
if (!name) | ||
return null; | ||
|
||
if (!(name in cache)) { | ||
cache[name] = utils.deepMerge({}, systemSettings[name], userSettings[name]); | ||
} | ||
|
||
var data = cache[name], subsections = utils.toArray(arguments, 1), key; | ||
while (data && (key = subsections.shift())) { | ||
if (key in data) { | ||
|
@@ -233,10 +233,10 @@ define(function(require, exports, module) { | |
return null; | ||
} | ||
} | ||
|
||
return data; | ||
}, | ||
|
||
/** | ||
* Recursively searches for a item inside top level sections (syntaxes) | ||
* with respect of `extends` attribute | ||
|
@@ -249,32 +249,32 @@ define(function(require, exports, module) { | |
while (data) { | ||
if (subsection in data) | ||
return data[subsection]; | ||
|
||
data = this.getSection(data['extends']); | ||
} | ||
}, | ||
|
||
/** | ||
* Recursively searches for a snippet definition inside syntax section. | ||
* Definition is searched inside `snippets` and `abbreviations` | ||
* subsections | ||
* Definition is searched inside `snippets` and `abbreviations` | ||
* subsections | ||
* @param {String} syntax Top-level section name (syntax) | ||
* @param {String} name Snippet name | ||
* @returns {Object} | ||
*/ | ||
findSnippet: function(syntax, name, memo) { | ||
if (!syntax || !name) | ||
return null; | ||
|
||
memo = memo || []; | ||
|
||
var names = [name]; | ||
// create automatic aliases to properties with colons, | ||
// e.g. pos-a == pos:a | ||
if (~name.indexOf('-')) { | ||
names.push(name.replace(/\-/g, ':')); | ||
} | ||
|
||
var data = this.getSection(syntax), matchedItem = null; | ||
['snippets', 'abbreviations'].some(function(sectionName) { | ||
var data = this.getSection(syntax, sectionName); | ||
|
@@ -286,16 +286,16 @@ define(function(require, exports, module) { | |
}); | ||
} | ||
}, this); | ||
|
||
memo.push(syntax); | ||
if (!matchedItem && data['extends'] && !~memo.indexOf(data['extends'])) { | ||
// try to find item in parent syntax section | ||
return this.findSnippet(data['extends'], name, memo); | ||
} | ||
|
||
return matchedItem; | ||
}, | ||
|
||
/** | ||
* Performs fuzzy search of snippet definition | ||
* @param {String} syntax Top-level section name (syntax) | ||
|
@@ -313,7 +313,7 @@ define(function(require, exports, module) { | |
minScore = minScore || 0.3; | ||
name = normalizeName(name); | ||
var snippets = this.getAllSnippets(syntax); | ||
|
||
return Object.keys(snippets) | ||
.map(function(key) { | ||
var value = snippets[key]; | ||
|
@@ -331,7 +331,7 @@ define(function(require, exports, module) { | |
}) | ||
.reverse(); | ||
}, | ||
|
||
/** | ||
* Returns plain dictionary of all available abbreviations and snippets | ||
* for specified syntax with respect of inheritance | ||
|
@@ -343,12 +343,12 @@ define(function(require, exports, module) { | |
if (!cache[cacheKey]) { | ||
var stack = [], sectionKey = syntax; | ||
var memo = []; | ||
|
||
do { | ||
var section = this.getSection(sectionKey); | ||
if (!section) | ||
break; | ||
|
||
['snippets', 'abbreviations'].forEach(function(sectionName) { | ||
var stackItem = {}; | ||
each(section[sectionName] || null, function(v, k) { | ||
|
@@ -359,18 +359,18 @@ define(function(require, exports, module) { | |
type: sectionName | ||
}; | ||
}); | ||
|
||
stack.push(stackItem); | ||
}); | ||
|
||
memo.push(sectionKey); | ||
sectionKey = section['extends']; | ||
} while (sectionKey && !~memo.indexOf(sectionKey)); | ||
|
||
|
||
cache[cacheKey] = utils.extend.apply(utils, stack.reverse()); | ||
} | ||
|
||
return cache[cacheKey]; | ||
}, | ||
|
||
|
@@ -382,7 +382,7 @@ define(function(require, exports, module) { | |
var nl = this.getVariable('newline'); | ||
return typeof nl === 'string' ? nl : '\n'; | ||
}, | ||
|
||
/** | ||
* Sets new newline character that will be used in output | ||
* @param {String} str | ||
|
@@ -401,15 +401,11 @@ define(function(require, exports, module) { | |
(function(r) { | ||
if (typeof define === 'undefined' || !define.amd) { | ||
try { | ||
var fs = r('fs'); | ||
var path = r('path'); | ||
|
||
var defaultSnippets = fs.readFileSync(path.join(__dirname, '../snippets.json'), {encoding: 'utf8'}); | ||
exports.setVocabulary(JSON.parse(defaultSnippets), VOC_SYSTEM); | ||
exports.setVocabulary(r('../snippets.json'), VOC_SYSTEM); | ||
} catch (e) {} | ||
} | ||
})(require); | ||
|
||
|
||
return exports; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.