MCPcopy Create free account
hub / github.com/angular/angular / parseHostBindings

Function parseHostBindings

packages/compiler/src/render3/view/compiler.ts:521–574  ·  view source on GitHub ↗
(host: {
  [key: string]: string | o.Expression;
})

Source from the content-addressed store, hash-verified

519}
520
521export function parseHostBindings(host: {
522 [key: string]: string | o.Expression;
523}): ParsedHostBindings {
524 const attributes: Record<string, o.Expression> = {};
525 const listeners: Record<string, string> = {};
526 const properties: Record<string, string> = {};
527 const specialAttributes: {styleAttr?: string; classAttr?: string} = {};
528
529 for (const key of Object.keys(host)) {
530 const value = host[key];
531
532 if (key.startsWith('(') && key.endsWith(')')) {
533 if (typeof value !== 'string') {
534 // TODO(alxhub): make this a diagnostic.
535 throw new Error(`Event binding must be string`);
536 }
537 listeners[key.slice(1, -1)] = value;
538 } else if (key.startsWith('[') && key.endsWith(']')) {
539 if (typeof value !== 'string') {
540 // TODO(alxhub): make this a diagnostic.
541 throw new Error(`Property binding must be string`);
542 }
543 // synthetic properties (the ones that have a `@` as a prefix)
544 // are still treated the same as regular properties. Therefore
545 // there is no point in storing them in a separate map.
546 properties[key.slice(1, -1)] = value;
547 } else {
548 switch (key) {
549 case 'class':
550 if (typeof value !== 'string') {
551 // TODO(alxhub): make this a diagnostic.
552 throw new Error(`Class binding must be string`);
553 }
554 specialAttributes.classAttr = value;
555 break;
556 case 'style':
557 if (typeof value !== 'string') {
558 // TODO(alxhub): make this a diagnostic.
559 throw new Error(`Style binding must be string`);
560 }
561 specialAttributes.styleAttr = value;
562 break;
563 default:
564 if (typeof value === 'string') {
565 attributes[key] = o.literal(value);
566 } else {
567 attributes[key] = value;
568 }
569 }
570 }
571 }
572
573 return {attributes, listeners, properties, specialAttributes};
574}
575
576/**
577 * Verifies host bindings and returns the list of errors (if any). Empty array indicates that a

Callers 3

extractHostBindingsFunction · 0.90
extractHostBindingsFunction · 0.90

Calls 1

keysMethod · 0.65

Tested by

no test coverage detected