|
8 | 8 |
|
9 | 9 | import * as chars from '../chars'; |
10 | 10 | import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../ml_parser/interpolation_config'; |
| 11 | +import {InterpolatedAttributeToken, InterpolatedTextToken, TokenType as MlParserTokenType} from '../ml_parser/tokens'; |
11 | 12 |
|
12 | 13 | import {AbsoluteSourceSpan, AST, ASTWithSource, Binary, BindingPipe, Call, Chain, Conditional, EmptyExpr, ExpressionBinding, ImplicitReceiver, Interpolation, KeyedRead, KeyedWrite, LiteralArray, LiteralMap, LiteralMapKey, LiteralPrimitive, NonNullAssert, ParserError, ParseSpan, PrefixNot, PropertyRead, PropertyWrite, Quote, RecursiveAstVisitor, SafeCall, SafeKeyedRead, SafePropertyRead, TemplateBinding, TemplateBindingIdentifier, ThisReceiver, Unary, VariableBinding} from './ast'; |
13 | 14 | import {EOF, isIdentifier, Lexer, Token, TokenType} from './lexer'; |
@@ -167,9 +168,10 @@ export class Parser { |
167 | 168 |
|
168 | 169 | parseInterpolation( |
169 | 170 | input: string, location: string, absoluteOffset: number, |
| 171 | + interpolatedTokens: InterpolatedAttributeToken[]|InterpolatedTextToken[]|null, |
170 | 172 | interpolationConfig: InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG): ASTWithSource|null { |
171 | 173 | const {strings, expressions, offsets} = |
172 | | - this.splitInterpolation(input, location, interpolationConfig); |
| 174 | + this.splitInterpolation(input, location, interpolatedTokens, interpolationConfig); |
173 | 175 | if (expressions.length === 0) return null; |
174 | 176 |
|
175 | 177 | const expressionNodes: AST[] = []; |
@@ -223,10 +225,13 @@ export class Parser { |
223 | 225 | */ |
224 | 226 | splitInterpolation( |
225 | 227 | input: string, location: string, |
| 228 | + interpolatedTokens: InterpolatedAttributeToken[]|InterpolatedTextToken[]|null, |
226 | 229 | interpolationConfig: InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG): SplitInterpolation { |
227 | 230 | const strings: InterpolationPiece[] = []; |
228 | 231 | const expressions: InterpolationPiece[] = []; |
229 | 232 | const offsets: number[] = []; |
| 233 | + const inputToTemplateIndexMap = |
| 234 | + interpolatedTokens ? getIndexMapForOriginalTemplate(interpolatedTokens) : null; |
230 | 235 | let i = 0; |
231 | 236 | let atInterpolation = false; |
232 | 237 | let extendLastString = false; |
@@ -264,7 +269,9 @@ export class Parser { |
264 | 269 | `at column ${i} in`, location); |
265 | 270 | } |
266 | 271 | expressions.push({text, start: fullStart, end: fullEnd}); |
267 | | - offsets.push(exprStart); |
| 272 | + const startInOriginalTemplate = inputToTemplateIndexMap?.get(fullStart) ?? fullStart; |
| 273 | + const offset = startInOriginalTemplate + interpStart.length; |
| 274 | + offsets.push(offset); |
268 | 275 |
|
269 | 276 | i = fullEnd; |
270 | 277 | atInterpolation = false; |
@@ -1334,3 +1341,38 @@ class SimpleExpressionChecker extends RecursiveAstVisitor { |
1334 | 1341 | this.errors.push('pipes'); |
1335 | 1342 | } |
1336 | 1343 | } |
| 1344 | +/** |
| 1345 | + * Computes the real offset in the original template for indexes in an interpolation. |
| 1346 | + * |
| 1347 | + * Because templates can have encoded HTML entities and the input passed to the parser at this stage |
| 1348 | + * of the compiler is the _decoded_ value, we need to compute the real offset using the original |
| 1349 | + * encoded values in the interpolated tokens. Note that this is only a special case handling for |
| 1350 | + * `MlParserTokenType.ENCODED_ENTITY` token types. All other interpolated tokens are expected to |
| 1351 | + * have parts which exactly match the input string for parsing the interpolation. |
| 1352 | + * |
| 1353 | + * @param interpolatedTokens The tokens for the interpolated value. |
| 1354 | + * |
| 1355 | + * @returns A map of index locations in the decoded template to indexes in the original template |
| 1356 | + */ |
| 1357 | +function getIndexMapForOriginalTemplate(interpolatedTokens: InterpolatedAttributeToken[]| |
| 1358 | + InterpolatedTextToken[]): Map<number, number> { |
| 1359 | + let offsetMap = new Map<number, number>(); |
| 1360 | + let consumedInOriginalTemplate = 0; |
| 1361 | + let consumedInInput = 0; |
| 1362 | + let tokenIndex = 0; |
| 1363 | + while (tokenIndex < interpolatedTokens.length) { |
| 1364 | + const currentToken = interpolatedTokens[tokenIndex]; |
| 1365 | + if (currentToken.type === MlParserTokenType.ENCODED_ENTITY) { |
| 1366 | + const [decoded, encoded] = currentToken.parts; |
| 1367 | + consumedInOriginalTemplate += encoded.length; |
| 1368 | + consumedInInput += decoded.length; |
| 1369 | + } else { |
| 1370 | + const lengthOfParts = currentToken.parts.reduce((sum, current) => sum + current.length, 0); |
| 1371 | + consumedInInput += lengthOfParts; |
| 1372 | + consumedInOriginalTemplate += lengthOfParts; |
| 1373 | + } |
| 1374 | + offsetMap.set(consumedInInput, consumedInOriginalTemplate); |
| 1375 | + tokenIndex++; |
| 1376 | + } |
| 1377 | + return offsetMap; |
| 1378 | +} |
0 commit comments