Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($interpolate): use the first end symbol that forms a valid expression #8744

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lgalfaso
Copy link
Contributor

When an interpolation string has multiple end symbol choices, pick the
occurrence that forms a valid expression

Closes #8642

@caitp
Copy link
Contributor

caitp commented Aug 23, 2014

Then we spend potentially a lot more time parsing =\ also try/catch blocks --- it looks like it will prevent us from throwing in the case of invalid expressions, am i wrong?

@lgalfaso
Copy link
Contributor Author

Updated the PR with another approach that keeps track of (){}[] and strings. It uses some extra bytes but does not have a try..catch

break;
}
index++;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was kind of a hard read, I think I would refactor this to something like:

var brackets = [[0, 0, 0], [0, 0, 0]], bracketChars = '({[)}]', ch, quoteCh;
index = startIndex + startSymbolLength;
while (index !== endIndex || quoteCh || !equals(brackets[0], brackets[1])) {
  ch = text[index];
  if (quoteCh) {
    if (ch === '\\') {
      index++;
    } else if (ch === quoteCh) {
      quoteCh = undefined;
    }
  } else if (ch === "'" || ch === '"') {
    quoteCh = ch;
  } else if ((i = bracketChars.indexOf(ch)) != -1) {
    brackets[i/3][i%3]++;
  }
  index++;

  if (index > endIndex) {
    endIndex = text.indexOf(endSymbol, index);
    if (endIndex === -1) {
      break;
    }
  }
}

(haven't tested, I might have some bug there)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggested code will fail with $interpolate('{{"\\}}"}}'), anyhow it is possible to fix it, but it does add some other oddities

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, that's just a suggestion for a direction to get rid of this pretty cryptic switch statement, I didn't really test it. Anyway, I modified it a bit so it will work with the failure you mentioned.

@shahata
Copy link
Contributor

shahata commented Aug 24, 2014

I think I like the try/catch better since my guess is that this implementation is much less performant in "sunny day" cases (where the expression does not contain }}).

@caitp
Copy link
Contributor

caitp commented Aug 24, 2014

The try/catch case is broken because it causes us to silently fail when there are legitimate syntax errors.

I don't really expect this bug to actually get fixed, since it's been around for so long and people rarely get bit by it

@shahata
Copy link
Contributor

shahata commented Aug 24, 2014

Not sure what you mean, looking at lgalfaso@2c64dc4 it seems to me that it will re-throw the last exception when it runs out of endSymbols ahead. Regarding the chances of this getting fixed, you're probably right :)

@lgalfaso
Copy link
Contributor Author

The PR is in place to start the discussion on if this should be fixed and if that is the case, the solution that should be implemented

@lgalfaso
Copy link
Contributor Author

@caitp not sure why you say that the PR silently fails on syntax errors, it will still throw when these are found
Also updated the PR so it it cleaner and saves a few bytes

…sion

When an interpolation string has multiple end symbol choices, pick the
occurrence that forms a valid expression

Closes angular#8642
@caitp
Copy link
Contributor

caitp commented Aug 24, 2014

I saw that it rethrows the second time I looked at it, but I still wouldn't expect it to land

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interpolated expressions cannot contain }}
5 participants