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

Method _consumeAttr

packages/compiler/src/ml_parser/parser.ts:547–619  ·  view source on GitHub ↗
(attrName: AttributeNameToken)

Source from the content-addressed store, hash-verified

545 }
546
547 private _consumeAttr(attrName: AttributeNameToken): html.Attribute {
548 const fullName = mergeNsAndName(attrName.parts[0], attrName.parts[1]);
549 if (attrName.parts[0].length === 0 && attrName.parts[1].startsWith(':')) {
550 this.errors.push(
551 TreeError.create(
552 attrName.parts[1],
553 attrName.sourceSpan,
554 `Invalid attribute name '${attrName.parts[1]}'. Attribute names cannot start with a colon.`,
555 ),
556 );
557 }
558 let attrEnd = attrName.sourceSpan.end;
559
560 // Consume any quote
561 if (this._peek.type === TokenType.ATTR_QUOTE) {
562 this._advance();
563 }
564
565 // Consume the attribute value
566 let value = '';
567 const valueTokens: InterpolatedAttributeToken[] = [];
568 let valueStartSpan: ParseSourceSpan | undefined = undefined;
569 let valueEnd: ParseLocation | undefined = undefined;
570 // NOTE: We need to use a new variable `nextTokenType` here to hide the actual type of
571 // `_peek.type` from TS. Otherwise TS will narrow the type of `_peek.type` preventing it from
572 // being able to consider `ATTR_VALUE_INTERPOLATION` as an option. This is because TS is not
573 // able to see that `_advance()` will actually mutate `_peek`.
574 const nextTokenType = this._peek.type as TokenType;
575 if (nextTokenType === TokenType.ATTR_VALUE_TEXT) {
576 valueStartSpan = this._peek.sourceSpan;
577 valueEnd = this._peek.sourceSpan.end;
578 while (
579 this._peek.type === TokenType.ATTR_VALUE_TEXT ||
580 this._peek.type === TokenType.ATTR_VALUE_INTERPOLATION ||
581 this._peek.type === TokenType.ENCODED_ENTITY
582 ) {
583 const valueToken = this._advance<InterpolatedAttributeToken>();
584 valueTokens.push(valueToken);
585 if (valueToken.type === TokenType.ATTR_VALUE_INTERPOLATION) {
586 // For backward compatibility we decode HTML entities that appear in interpolation
587 // expressions. This is arguably a bug, but it could be a considerable breaking change to
588 // fix it. It should be addressed in a larger project to refactor the entire parser/lexer
589 // chain after View Engine has been removed.
590 value += valueToken.parts.join('').replace(/&([^;]+);/g, decodeEntity);
591 } else if (valueToken.type === TokenType.ENCODED_ENTITY) {
592 value += valueToken.parts[0];
593 } else {
594 value += valueToken.parts.join('');
595 }
596 valueEnd = attrEnd = valueToken.sourceSpan.end;
597 }
598 }
599
600 // Consume any quote
601 if (this._peek.type === TokenType.ATTR_QUOTE) {
602 const quoteToken = this._advance<AttributeQuoteToken>();
603 attrEnd = quoteToken.sourceSpan.end;
604 }

Callers 1

_consumeAttributesMethod · 0.95

Calls 6

_advanceMethod · 0.95
mergeNsAndNameFunction · 0.90
createMethod · 0.65
joinMethod · 0.65
pushMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected