|
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, RecursiveAstVisitor, SafeCall, SafeKeyedRead, SafePropertyRead, TemplateBinding, TemplateBindingIdentifier, ThisReceiver, Unary, VariableBinding} from './ast'; |
13 | 14 | import {EOF, Lexer, Token, TokenType} from './lexer'; |
@@ -147,9 +148,10 @@ export class Parser { |
147 | 148 |
|
148 | 149 | parseInterpolation( |
149 | 150 | input: string, location: string, absoluteOffset: number, |
| 151 | + interpolatedTokens: InterpolatedAttributeToken[]|InterpolatedTextToken[]|null, |
150 | 152 | interpolationConfig: InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG): ASTWithSource|null { |
151 | 153 | const {strings, expressions, offsets} = |
152 | | - this.splitInterpolation(input, location, interpolationConfig); |
| 154 | + this.splitInterpolation(input, location, interpolatedTokens, interpolationConfig); |
153 | 155 | if (expressions.length === 0) return null; |
154 | 156 |
|
155 | 157 | const expressionNodes: AST[] = []; |
@@ -203,10 +205,13 @@ export class Parser { |
203 | 205 | */ |
204 | 206 | splitInterpolation( |
205 | 207 | input: string, location: string, |
| 208 | + interpolatedTokens: InterpolatedAttributeToken[]|InterpolatedTextToken[]|null, |
206 | 209 | interpolationConfig: InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG): SplitInterpolation { |
207 | 210 | const strings: InterpolationPiece[] = []; |
208 | 211 | const expressions: InterpolationPiece[] = []; |
209 | 212 | const offsets: number[] = []; |
| 213 | + const inputToTemplateIndexMap = |
| 214 | + interpolatedTokens ? getIndexMapForOriginalTemplate(interpolatedTokens) : null; |
210 | 215 | let i = 0; |
211 | 216 | let atInterpolation = false; |
212 | 217 | let extendLastString = false; |
@@ -244,7 +249,9 @@ export class Parser { |
244 | 249 | `at column ${i} in`, location); |
245 | 250 | } |
246 | 251 | expressions.push({text, start: fullStart, end: fullEnd}); |
247 | | - offsets.push(exprStart); |
| 252 | + const startInOriginalTemplate = inputToTemplateIndexMap?.get(fullStart) ?? fullStart; |
| 253 | + const offset = startInOriginalTemplate + interpStart.length; |
| 254 | + offsets.push(offset); |
248 | 255 |
|
249 | 256 | i = fullEnd; |
250 | 257 | atInterpolation = false; |
@@ -1314,3 +1321,38 @@ class SimpleExpressionChecker extends RecursiveAstVisitor { |
1314 | 1321 | this.errors.push('pipes'); |
1315 | 1322 | } |
1316 | 1323 | } |
| 1324 | +/** |
| 1325 | + * Computes the real offset in the original template for indexes in an interpolation. |
| 1326 | + * |
| 1327 | + * Because templates can have encoded HTML entities and the input passed to the parser at this stage |
| 1328 | + * of the compiler is the _decoded_ value, we need to compute the real offset using the original |
| 1329 | + * encoded values in the interpolated tokens. Note that this is only a special case handling for |
| 1330 | + * `MlParserTokenType.ENCODED_ENTITY` token types. All other interpolated tokens are expected to |
| 1331 | + * have parts which exactly match the input string for parsing the interpolation. |
| 1332 | + * |
| 1333 | + * @param interpolatedTokens The tokens for the interpolated value. |
| 1334 | + * |
| 1335 | + * @returns A map of index locations in the decoded template to indexes in the original template |
| 1336 | + */ |
| 1337 | +function getIndexMapForOriginalTemplate(interpolatedTokens: InterpolatedAttributeToken[]| |
| 1338 | + InterpolatedTextToken[]): Map<number, number> { |
| 1339 | + let offsetMap = new Map<number, number>(); |
| 1340 | + let consumedInOriginalTemplate = 0; |
| 1341 | + let consumedInInput = 0; |
| 1342 | + let tokenIndex = 0; |
| 1343 | + while (tokenIndex < interpolatedTokens.length) { |
| 1344 | + const currentToken = interpolatedTokens[tokenIndex]; |
| 1345 | + if (currentToken.type === MlParserTokenType.ENCODED_ENTITY) { |
| 1346 | + const [decoded, encoded] = currentToken.parts; |
| 1347 | + consumedInOriginalTemplate += encoded.length; |
| 1348 | + consumedInInput += decoded.length; |
| 1349 | + } else { |
| 1350 | + const lengthOfParts = currentToken.parts.reduce((sum, current) => sum + current.length, 0); |
| 1351 | + consumedInInput += lengthOfParts; |
| 1352 | + consumedInOriginalTemplate += lengthOfParts; |
| 1353 | + } |
| 1354 | + offsetMap.set(consumedInInput, consumedInOriginalTemplate); |
| 1355 | + tokenIndex++; |
| 1356 | + } |
| 1357 | + return offsetMap; |
| 1358 | +} |
0 commit comments