初始提交,未完全测试
This commit is contained in:
34
template/ext/directive/EscapeDirective.py
Normal file
34
template/ext/directive/EscapeDirective.py
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3.9
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
JFinal EscapeDirective - Escape Directive
|
||||
"""
|
||||
|
||||
from ...Directive import Directive
|
||||
from ...Env import Env
|
||||
from ...stat.Scope import Scope
|
||||
|
||||
class EscapeDirective(Directive):
|
||||
"""Escape directive for HTML escaping"""
|
||||
|
||||
def exec(self, env: Env, scope: Scope, writer) -> None:
|
||||
"""
|
||||
Execute escape directive
|
||||
|
||||
Args:
|
||||
env: Template environment
|
||||
scope: Execution scope
|
||||
writer: Output writer
|
||||
"""
|
||||
if self.expr_list:
|
||||
# Get value to escape
|
||||
value = self.expr_list.eval(scope)
|
||||
|
||||
if value:
|
||||
# Escape HTML special characters
|
||||
escaped = str(value).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')
|
||||
if hasattr(writer, 'write'):
|
||||
writer.write(escaped)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "EscapeDirective()"
|
||||
Reference in New Issue
Block a user