Skip to content

Commit

Permalink
Get rid of some node deps for better Webpack budle
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche authored and ramya-rao-a committed Feb 1, 2017
1 parent 96996d4 commit 6bd5715
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 73 deletions.
110 changes: 53 additions & 57 deletions lib/assets/resources.js
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])
Expand All @@ -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();

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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];
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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];
},

Expand All @@ -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
Expand All @@ -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;
});
});
1 change: 0 additions & 1 deletion lib/emmet.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ define(function(require, exports, module) {
profile.create(name, normalizeProfile(profiles[name]));
});
},
require: require,

// expose some useful data for plugin authors
actions: actions,
Expand Down
Loading

0 comments on commit 6bd5715

Please sign in to comment.