-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.go
More file actions
36 lines (30 loc) · 1.24 KB
/
tokenizer.go
File metadata and controls
36 lines (30 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package twig
// Processes whitespace control modifiers in the template
// Applies whitespace trimming to adjacent text tokens based on the token types
// This is called after tokenization to handle the whitespace around trimming tokens
func processWhitespaceControl(tokens []Token) []Token {
if len(tokens) == 0 {
return tokens
}
// Modify tokens in-place to avoid allocation
// This works because we're only changing token values, not adding/removing tokens
// Process each token to apply whitespace trimming
for i := 0; i < len(tokens); i++ {
token := tokens[i]
// Handle opening tags that trim whitespace before them
if token.Type == TOKEN_VAR_START_TRIM || token.Type == TOKEN_BLOCK_START_TRIM {
// If there's a text token before this, trim its trailing whitespace
if i > 0 && tokens[i-1].Type == TOKEN_TEXT {
tokens[i-1].Value = trimTrailingWhitespace(tokens[i-1].Value)
}
}
// Handle closing tags that trim whitespace after them
if token.Type == TOKEN_VAR_END_TRIM || token.Type == TOKEN_BLOCK_END_TRIM {
// If there's a text token after this, trim its leading whitespace
if i+1 < len(tokens) && tokens[i+1].Type == TOKEN_TEXT {
tokens[i+1].Value = trimLeadingWhitespace(tokens[i+1].Value)
}
}
}
return tokens
}