Remove environment charset handling

This commit is contained in:
Anton Liaposhchenko 2024-12-29 12:39:51 +02:00
parent 8b23f3c82d
commit e2918c883d
21 changed files with 20 additions and 42 deletions
src
main/lib
test/tests
integration
unit/lib/environment

@ -47,10 +47,6 @@ export type TwingEnvironmentOptions = {
*/
cache?: TwingCache;
/**
* The default charset. Defaults to "UTF-8".
*/
charset?: string;
dateFormat?: string;
dateIntervalFormat?: string;
globals?: Record<string, any>;
@ -69,7 +65,6 @@ export type TwingSynchronousEnvironmentOptions = Omit<TwingEnvironmentOptions, "
export interface TwingSynchronousEnvironment {
readonly cache: TwingSynchronousCache | null;
readonly charset: string;
readonly dateFormat: string;
readonly dateIntervalFormat: string;
readonly escapingStrategyHandlers: Record<TwingEscapingStrategy, TwingEscapingStrategyHandler>;
@ -176,7 +171,6 @@ export const createSynchronousEnvironment = (
extensionSet.addExtension(createSynchronousCoreExtension());
const cache: TwingSynchronousCache | null = options?.cache || null;
const charset = options?.charset || 'UTF-8';
const dateFormat = options?.dateFormat || 'F j, Y H:i';
const dateIntervalFormat = options?.dateIntervalFormat || '%d days';
const numberFormat: TwingNumberFormat = options?.numberFormat || {
@ -194,9 +188,6 @@ export const createSynchronousEnvironment = (
get cache() {
return cache;
},
get charset() {
return charset;
},
get dateFormat() {
return dateFormat;
},

@ -1,3 +1,3 @@
export type TwingEscapingStrategyResolver = (templateName: string) => string | null;
export type TwingEscapingStrategy = "css" | "html" | "html_attr" | "js" | "url" | string; // todo: remove true
export type TwingEscapingStrategyHandler = (value: string, charset: string, templateName: string) => string;
export type TwingEscapingStrategyHandler = (value: string, templateName: string) => string;

@ -17,10 +17,10 @@ export const escapeSynchronously: TwingSynchronousCallable<[
const {template, environment} = executionContext;
// todo: probably we need to use traceable method
const escapedValue = escapeValueSynchronously(template, environment, value, strategy, environment.charset);
const escapedValue = escapeValueSynchronously(template, environment, value, strategy);
if (typeof escapedValue === "string") {
return createMarkup(escapedValue, environment.charset);
return createMarkup(escapedValue);
}
return escapedValue;

@ -96,5 +96,5 @@ export const includeSynchronously: TwingSynchronousCallable<[
const result = outputBuffer.getAndClean();
return createMarkup(result, environment.charset);
return createMarkup(result);
}

@ -8,7 +8,6 @@ export const escapeValueSynchronously = (
environment: TwingSynchronousEnvironment,
value: string | boolean | TwingMarkup | null | undefined,
strategy: TwingEscapingStrategy | string,
charset: string | null
): string | boolean | TwingMarkup => {
if (typeof value === "boolean") {
return value;
@ -30,7 +29,7 @@ export const escapeValueSynchronously = (
throw new Error(`Invalid escaping strategy "${strategy}" (valid ones: ${Object.keys(environment.escapingStrategyHandlers).sort().join(', ')}).`);
}
result = strategyHandler(value.toString(), charset || environment.charset, template.name);
result = strategyHandler(value.toString(), template.name);
}
return result;

@ -1,5 +1,4 @@
export const getContextValueSynchronously = (
charset: string,
templateName: string,
isStrictVariables: boolean,
context: Map<string, any>,
@ -12,7 +11,6 @@ export const getContextValueSynchronously = (
const specialNames = new Map<string, any>([
['_self', templateName],
['_context', context],
['_charset', charset]
]);
const isSpecial = () => {

@ -1,5 +1,4 @@
export interface TwingMarkup {
readonly charset: string;
readonly content: string;
readonly count: number;
@ -10,24 +9,17 @@ export interface TwingMarkup {
export const isAMarkup = (candidate: any): candidate is TwingMarkup => {
return candidate !== null
&& candidate !== undefined
&& (candidate as TwingMarkup).charset !== undefined
&& (candidate as TwingMarkup).content !== undefined
&& (candidate as TwingMarkup).count !== undefined // todo: we should not test getter values but actual property existence
&& (candidate as TwingMarkup).toJSON !== undefined
&& (candidate as TwingMarkup).toString !== undefined;
};
export const createMarkup = (
content: string,
charset: string = 'UTF-8'
): TwingMarkup => {
export const createMarkup = (content: string): TwingMarkup => {
return {
get content() {
return content;
},
get charset() {
return charset;
},
get count() {
return content.length;
},

@ -12,5 +12,5 @@ export const executeEscapeNodeSynchronously: TwingSynchronousNodeExecutor<TwingE
const traceableEscape = getSynchronousTraceableMethod(escapeValueSynchronously, node, template.source);
return traceableEscape(template, environment, value, strategy, null);
return traceableEscape(template, environment, value, strategy);
};

@ -18,7 +18,6 @@ export const executeNameNodeSynchronously: TwingSynchronousNodeExecutor<TwingNam
);
return traceableGetContextValue(
environment.charset,
template.name,
strict,
// todo: this is needed for the for loop to work properly when the sequence is the context, but this should not be needed

@ -163,7 +163,7 @@ export const createSynchronousTemplate = (
for (const [name, macroNode] of Object.entries(macrosNode.children)) {
const macroHandler: TwingSynchronousTemplateMacroHandler = (executionContent, ...args) => {
const {environment, nodeExecutor, outputBuffer} = executionContent;
const {nodeExecutor, outputBuffer} = executionContent;
const {body, arguments: macroArguments} = macroNode.children;
const keyValuePairs = getKeyValuePairs(macroArguments);
@ -207,7 +207,7 @@ export const createSynchronousTemplate = (
const content = outputBuffer.getContents();
return createMarkup(content, environment.charset);
return createMarkup(content);
} finally {
outputBuffer.endAndClean();
}

@ -8,6 +8,6 @@ runTest({
},
expectation: '<br/>',
synchronousContext: {
foo: createMarkup('<br/>', "UTF-8")
foo: createMarkup('<br/>')
}
});

@ -39,7 +39,7 @@ class Test extends TestBase {
getSynchronousContext() {
return {
foo: createMarkup(('foo'), 'UTF-8'),
foo: createMarkup('foo'),
map: new Map([['message', 'Hello, world!']])
};
}

@ -25,7 +25,7 @@ class Test extends TestBase {
getSynchronousContext() {
return {
string: 'été',
markup: createMarkup(('foo'), 'UTF-8')
markup: createMarkup('foo')
};
}
}

@ -14,6 +14,6 @@ foo
foo
`,
synchronousContext: {
markup: createMarkup(('FoO'), "UTF-8")
markup: createMarkup('FoO')
}
});

@ -16,6 +16,6 @@ content
<div><span>content</span></div>
`,
synchronousContext: {
markup: createMarkup(('FoO'), "UTF-8")
markup: createMarkup('FoO')
}
});

@ -14,6 +14,6 @@ Foo
Foo
`,
synchronousContext: {
markup: createMarkup(('FoO'), "UTF-8")
markup: createMarkup('FoO')
}
});

@ -14,6 +14,6 @@ FOO
FOO
`,
synchronousContext: {
markup: createMarkup(('FoO'), "UTF-8")
markup: createMarkup('FoO')
}
});

@ -24,7 +24,7 @@ class Test extends TestBase {
getSynchronousContext() {
return {
markup: createMarkup('Foo', 'utf-8')
markup: createMarkup('Foo')
}
}
}

@ -76,8 +76,8 @@ ok
countable_not_empty: new Map([[1, 2]]),
tostring_empty: new ToStringStub(''),
tostring_not_empty: new ToStringStub('0' /* edge case of using "0" as the string */),
markup_empty: createMarkup((''), 'UTF-8'),
markup_not_empty: createMarkup(('test'), 'UTF-8'),
markup_empty: createMarkup(''),
markup_not_empty: createMarkup('test'),
};
}
}

@ -147,7 +147,7 @@ OK
dir_object: new SplFileInfo(),
object: {},
resource: opendir(),
safe: createMarkup(('foo'), 'UTF-8')
safe: createMarkup('foo')
}
}
}

@ -13,7 +13,6 @@ tape('createSynchronousEnvironment ', ({test}) => {
const environment = createSynchronousEnvironment(createSynchronousArrayLoader({}));
same(environment.charset, 'UTF-8');
same(environment.dateFormat, 'F j, Y H:i');
same(environment.numberFormat, {
decimalPoint: '.',