2017-11-20 09:46:05 -05:00
|
|
|
const CssSelectorParser = require('css-selector-parser').CssSelectorParser;
|
2017-11-17 08:37:15 -05:00
|
|
|
const cssParser = new CssSelectorParser();
|
2017-09-25 15:00:05 -04:00
|
|
|
/**
|
|
|
|
* @dgProcessor addMetadataAliases
|
|
|
|
*
|
|
|
|
* Directives and components can also be referenced by their selectors,
|
|
|
|
* and Pipes can be referenced by their name.
|
|
|
|
* So let's add each selector as an alias to this doc.
|
|
|
|
*/
|
|
|
|
module.exports = function addMetadataAliasesProcessor() {
|
|
|
|
return {
|
|
|
|
$runAfter: ['extractDecoratedClassesProcessor'],
|
|
|
|
$runBefore: ['computing-ids'],
|
|
|
|
$process: function(docs) {
|
|
|
|
docs.forEach(doc => {
|
|
|
|
switch(doc.docType) {
|
|
|
|
case 'directive':
|
2019-04-02 17:24:29 -04:00
|
|
|
case 'component': {
|
|
|
|
const selector = doc[doc.docType + 'Options'].selector;
|
|
|
|
if (selector) {
|
|
|
|
doc.aliases = doc.aliases.concat(extractSelectors(selector));
|
|
|
|
}
|
2017-09-25 15:00:05 -04:00
|
|
|
break;
|
2019-04-02 17:24:29 -04:00
|
|
|
}
|
2017-09-25 15:00:05 -04:00
|
|
|
case 'pipe':
|
|
|
|
if (doc.pipeOptions.name) {
|
|
|
|
doc.aliases = doc.aliases.concat(stripQuotes(doc.pipeOptions.name));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function extractSelectors(selectors) {
|
2017-11-17 08:37:15 -05:00
|
|
|
const selectorAST = cssParser.parse(stripQuotes(selectors));
|
|
|
|
const rules = selectorAST.selectors ? selectorAST.selectors.map(ruleSet => ruleSet.rule) : [selectorAST.rule];
|
2017-11-20 09:46:05 -05:00
|
|
|
const aliases = {};
|
|
|
|
rules.forEach(rule => {
|
|
|
|
if (rule.tagName) {
|
|
|
|
aliases[rule.tagName] = true;
|
|
|
|
}
|
|
|
|
if (rule.attrs) {
|
|
|
|
rule.attrs.forEach(attr => aliases[attr.name] = true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return Object.keys(aliases);
|
2017-09-25 15:00:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function stripQuotes(value) {
|
|
|
|
return (typeof(value) === 'string') ? value.trim().replace(/^(['"])(.*)\1$/, '$2') : value;
|
|
|
|
}
|