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

Function parseHostBindings

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

Source from the content-addressed store, hash-verified

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

Callers 4

extractHostBindingsFunction · 0.90
extractHostBindingsFunction · 0.90
buildHostMetadataFunction · 0.90

Calls 2

keysMethod · 0.65
sliceMethod · 0.65

Tested by

no test coverage detected