UNPKG

ngast

Version:

Parsing tools for Angular. The project is an abstraction over the Angular compiler which provides friendly interface.

142 lines 5.84 kB
import { TmplAstElement, TmplAstTemplate, SelectorMatcher, CssSelector, } from '@angular/compiler'; var TransformTemplateVisitor = /** @class */ (function () { function TransformTemplateVisitor(_componentScope, _workspace) { var _this = this; this._componentScope = _componentScope; this._workspace = _workspace; this._matcher = new SelectorMatcher(); this._matcher = new SelectorMatcher(); this._componentScope.compilation.directives.forEach(function (directive) { var selector = directive.selector; if (!selector) { return; } _this._matcher.addSelectables(CssSelector.parse(selector), function () { return _this._workspace.getSymbol(directive.ref.node); }); }); } TransformTemplateVisitor.prototype.visit = function (node) { return node.visit(this); }; TransformTemplateVisitor.prototype.visitElement = function (element) { return this._visitElementOrTemplate(element); }; TransformTemplateVisitor.prototype.visitTemplate = function (template) { return this._visitElementOrTemplate(template); }; TransformTemplateVisitor.prototype.visitContent = function (content) { var _this = this; return { component: null, directives: [], name: 'ng-content', attributes: content.attributes.map(function (attribute) { return attribute.visit(_this); }), children: [], references: [], variables: [], }; }; TransformTemplateVisitor.prototype.visitVariable = function (variable) { return variable.name; }; TransformTemplateVisitor.prototype.visitReference = function (reference) { return reference.name; }; TransformTemplateVisitor.prototype.visitTextAttribute = function (attribute) { return attribute.name; }; TransformTemplateVisitor.prototype.visitBoundAttribute = function (attribute) { return attribute.name; }; TransformTemplateVisitor.prototype.visitBoundEvent = function (attribute) { return attribute.name; }; TransformTemplateVisitor.prototype.visitText = function (_) { return; }; TransformTemplateVisitor.prototype.visitBoundText = function (_) { return; }; TransformTemplateVisitor.prototype.visitIcu = function (_) { return; }; TransformTemplateVisitor.prototype._visitElementOrTemplate = function (element) { var _this = this; var _a; var name = element instanceof TmplAstElement ? element.name : 'ng-template'; var directives = this._getDirectives(name, element); var component = ((_a = directives.find(function (dir) { return dir.annotation === 'Component'; })) !== null && _a !== void 0 ? _a : null); return { name: name, component: component, directives: directives.filter(function (dir) { return dir.annotation === 'Directive'; }), attributes: element.attributes.map(function (attribute) { return attribute.visit(_this); }), children: element.children.map(function (child) { return child.visit(_this); }).filter(function (el) { return !!el; }), references: element.references.map(function (ref) { return ref.visit(_this); }), variables: [], }; }; TransformTemplateVisitor.prototype._getDirectives = function (name, element) { var selector = createCssSelector(name, getAttrsForDirectiveMatching(element)); var result = []; this._matcher.match(selector, function (_, callback) { result.push(callback()); }); return result; }; return TransformTemplateVisitor; }()); export { TransformTemplateVisitor }; var splitNsName = function (elementName) { if (elementName[0] != ':') { return [null, elementName]; } var colonIndex = elementName.indexOf(':', 1); if (colonIndex == -1) { throw new Error("Unsupported format \"" + elementName + "\" expecting \":namespace:name\""); } return [elementName.slice(1, colonIndex), elementName.slice(colonIndex + 1)]; }; var createCssSelector = function (elementName, attributes) { var cssSelector = new CssSelector(); var elementNameNoNs = splitNsName(elementName)[1]; cssSelector.setElement(elementNameNoNs); Object.getOwnPropertyNames(attributes).forEach(function (name) { var nameNoNs = splitNsName(name)[1]; var value = attributes[name]; cssSelector.addAttribute(nameNoNs, value); if (name.toLowerCase() === 'class') { var classes = value.trim().split(/\s+/); classes.forEach(function (className) { return cssSelector.addClassName(className); }); } }); return cssSelector; }; var getAttrsForDirectiveMatching = function (elOrTpl) { var attributesMap = {}; if (elOrTpl instanceof TmplAstTemplate && elOrTpl.tagName !== 'ng-template') { elOrTpl.templateAttrs.forEach(function (a) { return (attributesMap[a.name] = ''); }); } else { elOrTpl.attributes.forEach(function (a) { if (!isI18nAttribute(a.name)) { attributesMap[a.name] = a.value; } }); elOrTpl.inputs.forEach(function (i) { attributesMap[i.name] = ''; }); elOrTpl.outputs.forEach(function (o) { attributesMap[o.name] = ''; }); } return attributesMap; }; /** Name of the i18n attributes **/ var I18N_ATTR = 'i18n'; var I18N_ATTR_PREFIX = 'i18n-'; var isI18nAttribute = function (name) { return name === I18N_ATTR || name.startsWith(I18N_ATTR_PREFIX); }; //# sourceMappingURL=template-transform.visitor.js.map