Skip to content

Commit

Permalink
Merge pull request #12606 from keymanapp/fix/developer/12455-no-uset-…
Browse files Browse the repository at this point in the history
…on-to
  • Loading branch information
srl295 authored Nov 4, 2024
2 parents 6144265 + ac99a4c commit ebf2222
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
7 changes: 7 additions & 0 deletions developer/src/kmc-ldml/src/compiler/ldml-compiler-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,11 @@ export class LdmlCompilerMessages {
m(this.ERROR_IllegalTransformAsterisk, `Invalid transform from="${def(o.from)}": Unescaped asterisk (*) is not valid transform syntax.`,
'**Hint**: Use `\\*` to match a literal asterisk.');

static ERROR_IllegalTransformToUset = SevErrorTransform | 0x05;
static Error_IllegalTransformToUset = (o: { to: string }) => m(
this.ERROR_IllegalTransformToUset,
`Invalid transform to="${def(o.to)}": Set variable (\\$[…]) cannot be used in 'to=' unless part of a map.`,
'**Hint**: If a map was meant, must use the form `<transform from="($[fromSet])" to="$[1:toSet]"/>`.'
);

}
18 changes: 18 additions & 0 deletions developer/src/kmc-ldml/src/compiler/tran.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ export abstract class TransformCompiler<T extends TransformCompilerType, TranBas
} else {
result.mapFrom = sections.strs.allocString('');
result.mapTo = sections.strs.allocString('');

// validate 'to' here
if (!this.isValidTo(transform.to || '')) {
return null;
}
}

if (cookedFrom === null) return null; // error
Expand Down Expand Up @@ -192,6 +197,19 @@ export abstract class TransformCompiler<T extends TransformCompilerType, TranBas
return result;
}

/**
* Validate the 'to' string.
* We have already checked that it's not a mapTo,
* so there should not be any illegal substitutions.
*/
private isValidTo(to: string) : boolean {
if (/(?<!\\)(?:\\\\)*\$\[/.test(to)) {
this.callbacks.reportMessage(LdmlCompilerMessages.Error_IllegalTransformToUset({ to }));
return false;
}
return true;
}

/**
* Validate the final regex
* @param cookedFrom the regex to use, missing the trailing '$'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<keyboard3 xmlns="https://schemas.unicode.org/cldr/45/keyboard3" locale="mt" conformsTo="45">
<info name="tran-minimal" />

<keys>
<key id="w" output="w"/>
</keys>

<variables>
<uset id="triisap" value="[\u{17CA}]" />
</variables>
<transforms type="simple">
<transformGroup>
<transform from="w" to="$[triisap]" /> <!-- uset should not be RHS -->
</transformGroup>
</transforms>
</keyboard3>
19 changes: 19 additions & 0 deletions developer/src/kmc-ldml/test/fixtures/sections/tran/ok-2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<keyboard3 xmlns="https://schemas.unicode.org/cldr/45/keyboard3" locale="mt" conformsTo="45">
<info name="tran-minimal" />

<keys>
<key id="w" output="w"/>
</keys>

<variables>
<uset id="triisap" value="[\u{17CA}]" /> <!-- unused. -->
</variables>
<transforms type="simple">
<transformGroup>
<!-- this test is identical to fail-IllegalTransformUsetRHS-1.xml except for the backslash before the dollarsign. -->
<transform from="w" to="\$[triisap]" /> <!-- Strange but OK- the dollarsign is escaped. -->
</transformGroup>
</transforms>
</keyboard3>
11 changes: 10 additions & 1 deletion developer/src/kmc-ldml/test/test-tran.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,17 @@ describe('tran', function () {
}
],
})),
// successful compile
...[1].map(n => ({
subpath: `sections/tran/fail-IllegalTransformUsetRHS-${n}.xml`,
errors: [
{
code: LdmlCompilerMessages.ERROR_IllegalTransformToUset,
matchMessage: /.*/,
}
],
})),
// successful compile
...[1, 2].map(n => ({
subpath: `sections/tran/ok-${n}.xml`,
errors: false,
})),
Expand Down

0 comments on commit ebf2222

Please sign in to comment.