[NoQA] Validate HTML string for ChatGPT Translator#64973
Conversation
This comment was marked as off-topic.
This comment was marked as off-topic.
|
@mollfpr can you add the missing strings to cspell.json to fix spellcheck please? |
| /** | ||
| * Validate that the HTML structure is the same before and after translation. | ||
| */ | ||
| private validateTemplateHTML(original: string, translated: string): boolean { |
There was a problem hiding this comment.
Can we put this in the Translator parent class?
There was a problem hiding this comment.
Let's move validateTemplatePlaceholders as well. That way, if we want to swap out models to a different provider we don't need to port or duplicate this logic.
There was a problem hiding this comment.
I think we can safely ignore this file in cspell.json
| const TRANSLATABLE_ATTRIBUTES = ['alt', 'title', 'placeholder', 'aria-label', 'aria-describedby', 'aria-labelledby', 'value']; | ||
|
|
||
| const parseHTMLStructure = (s: string) => { | ||
| const tags = Array.from(s.matchAll(/<([^>]+)>/g)); |
There was a problem hiding this comment.
NAB but we do have https://www.npmjs.com/package/htmlparser2 in the repo already, so we could use that here rather than manually implementing it with regex parsing
| */ | ||
| public validateTemplateHTML(original: string, translated: string): boolean { | ||
| // Attributes that are allowed to be translated | ||
| const TRANSLATABLE_ATTRIBUTES = ['alt', 'title', 'placeholder', 'aria-label', 'aria-describedby', 'aria-labelledby', 'value']; |
There was a problem hiding this comment.
Since we're just checking whether elements are in this set and not iterating over it, a Set would be more appropriate imo. This isn't performance-critical and is likely fast enough, so NAB:
| const TRANSLATABLE_ATTRIBUTES = ['alt', 'title', 'placeholder', 'aria-label', 'aria-describedby', 'aria-labelledby', 'value']; | |
| const TRANSLATABLE_ATTRIBUTES = new Set(['alt', 'title', 'placeholder', 'aria-label', 'aria-describedby', 'aria-labelledby', 'value']); |
| // Extract attributes, excluding translatable ones | ||
| const attributes: string[] = []; | ||
| if (element.attribs) { | ||
| Object.entries(element.attribs).forEach(([attrName, attrValue]) => { |
There was a problem hiding this comment.
NAB because we haven't standardized, but I've been trying to push us to start using plain loops rather than forEach, map, reduce, particularly in scripts, because it's more performant, flexible, and imo more readable:
const attributes: string[] = [];
for (const [attrName, attrValue] of Object.entries(element.attribs ?? {})) {
const normalizedAttrName = attrName.toLowerCase();
if (!TRANSLATABLE_ATTRIBUTES.includes(normalizedAttrName)) {
attributes.push(`${normalizedAttrName}="${attrValue ?? ''}"`);
}
}
return {
tagName,
attributes: attributes.sort(),
};
Reviewer Checklist
Screenshots/VideosAndroid: HybridAppAndroid: mWeb ChromeiOS: HybridAppiOS: mWeb SafariMacOS: Chrome / SafariMacOS: Desktop |
|
Great NAB's! Updated! Thanks Rory! Let me know if there anything else to get this merge. |
|
Thanks @mollfpr! |
|
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
|
🚀 Deployed to staging by https://github.com/roryabraham in version: 9.1.78-0 🚀
|
|
🚀 Deployed to production by https://github.com/mountiny in version: 9.1.78-4 🚀
|
Explanation of Change
Fixed Issues
$ #64185
PROPOSAL:
Tests
Add a new test case on
ChatGPTTranslatorTest.Offline tests
N/A
QA Steps
N/A
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectioncanBeMissingparam foruseOnyxtoggleReportand notonIconClick)src/languages/*files and using the translation methodSTYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.ScrollViewcomponent to make it scrollable when more elements are added to the page.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
MacOS: Desktop