also looking at this
fix(loops): an @empty(expr) conditional in a @forelse row is not the separator
#2104
2 files
+24
-2
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -48,7 +48,7 @@ import type { StxOptions } from './types' | ||
| 48 | 48 | import { processConditionals } from './conditionals' |
| 49 | 49 | import { ErrorCodes, inlineError } from './error-handling' |
| 50 | 50 | import { processExpressions } from './expressions' |
| 51 | import { findDirectiveBlocks, findMatchingDelimiter } from './parser' | |
| 51 | import { findDirectiveBlocks, findMatchingDelimiter, findMatchingEndTag } from './parser' | |
| 52 | 52 | import { createSafeFunction, freeIdentifiers, isExpressionSafe, isForExpressionSafe, SANITIZE_CACHE, sanitizeExpression, safeEvaluate, safeEvaluateObject } from './safe-evaluator' |
| 53 | 53 | |
| 54 | 54 | // Default loop configuration |
| @@ -577,7 +577,9 @@ else if (remaining.startsWith('@endforelse')) { | ||
| 577 | 577 | searchI = atPos + '@endforelse'.length |
| 578 | 578 | } |
| 579 | 579 | else if (nestedDepth === 0 && remaining.startsWith('@empty')) { |
| 580 | if (fullContent.length <= atPos + 6 || !/[a-z]/i.test(fullContent[atPos + 6])) { | |
| 580 | // @empty(expr) ... @endempty in a row is the conditional, not this separator | |
| 581 | const isConditional = /^@empty[ \t]*\(/.test(remaining) && findMatchingEndTag(fullContent, 'empty', 'endempty', atPos + 6) !== -1 | |
| 582 | if ((fullContent.length <= atPos + 6 || !/[a-z]/i.test(fullContent[atPos + 6])) && !isConditional) { | |
| 581 | 583 | emptyPos = atPos |
| 582 | 584 | break |
| 583 | 585 | } |
| @@ -575,6 +575,26 @@ describe('Loops Comprehensive', () => { | ||
| 575 | 575 | expect(result).toContain('x') |
| 576 | 576 | expect(result).not.toContain('empty') |
| 577 | 577 | }) |
| 578 | it('should not take an @empty(expr) conditional in a row for the @empty separator', () => { | |
| 579 | const template = '@forelse(items as item)<p>@empty(item.name)missing@else{{ item.name }}@endempty</p>@empty<p>No items</p>@endforelse' | |
| 580 | ||
| 581 | const full = run(template, { items: [{ name: 'Ann' }, { name: '' }] }) | |
| 582 | expect(full).toContain('<p>Ann</p>') | |
| 583 | expect(full).toContain('<p>missing</p>') | |
| 584 | expect(full).not.toContain('No items') | |
| 585 | ||
| 586 | const empty = run(template, { items: [] }) | |
| 587 | expect(empty).toContain('<p>No items</p>') | |
| 588 | expect(empty).not.toContain('@empty') | |
| 589 | expect(empty).not.toContain('@endempty') | |
| 590 | }) | |
| 591 | ||
| 592 | it('should keep @empty followed by text in parentheses as the separator', () => { | |
| 593 | const template = '@forelse(items as item)<li>{{ item }}</li>@empty (none)@endforelse' | |
| 594 | expect(run(template, { items: [] }).trim()).toBe('(none)') | |
| 595 | expect(run(template, { items: ['a'] })).toContain('<li>a</li>') | |
| 596 | expect(run(template, { items: ['a'] })).not.toContain('(none)') | |
| 597 | }) | |
| 578 | 598 | }) |
| 579 | 599 | |
| 580 | 600 | // =========================================================================== |