StencilSwiftKit icon indicating copy to clipboard operation
StencilSwiftKit copied to clipboard

Replace filter: unescape backslashes

Open djbe opened this issue 7 years ago • 7 comments

In a template, if I try: value|replace:"\n","\\n"

The search and replace strings become "\\n" and "\\\\n" respectively. I think we should un-escape these backslashes, so that users correctly can search for newlines (for example). Or is this something that we should handle in Stencil itself?

djbe avatar Oct 13 '18 12:10 djbe

Mmmh good question. Is it:

  • The job of the replace filter itself
  • The job of Stencil itself (which would allow to fix that issue for all tags, but might be a breaking change too)
  • The job of SwiftGenKit (during parsing of the YAML/JSON/PLIST files)?

AliSoftware avatar Oct 13 '18 12:10 AliSoftware

Note: SGK correctly parses the files (the string value is as it should be, unchanged).

djbe avatar Oct 13 '18 12:10 djbe

Yeah the parsing itself shouldn't be changed, but should we or should we not convert newlines to \n when we build the context from those parsed content, or is that the job for Stencil or the template, that's the real question :wink:

AliSoftware avatar Oct 13 '18 13:10 AliSoftware

Keep in mind that if we do that in SGK, those strings may be somewhere quite deep in some array/dictionary structure, and we'd need to check all strings. Seems quite the overhead on the context builder, where it's honestly an issue in Stencil(SwiftKit).

djbe avatar Oct 13 '18 13:10 djbe

Note: we also have a contains filter where this applies as well. Note 2: and hasPrefix and hasSuffix also.

djbe avatar Oct 13 '18 13:10 djbe

Yeah which is why I'm starting to wonder if it shouldn't be Stencil's job to do that un-escape, instead of each filter individually…

AliSoftware avatar Oct 13 '18 13:10 AliSoftware

Right, so the culprit would be this line: https://github.com/stencilproject/Stencil/blob/521a599a60206b61b1ac76dd0f01d20f95d3d588/Sources/Variable.swift#L53

Anyone know what's the best way to unescape something like "\\n" or "\\u5404\\u500b\\u90fd" (we may as well do this for unicode sequences)?

djbe avatar Oct 13 '18 13:10 djbe