Skip to content

Commit 3e83c13

Browse files
committed
use htmlparser instead the regex
1 parent b85117d commit 3e83c13

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

scripts/utils/Translator/Translator.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {DomUtils, parseDocument} from 'htmlparser2';
12
import type {TranslationTargetLocale} from '@src/CONST/LOCALES';
23

34
/**
@@ -55,23 +56,21 @@ abstract class Translator {
5556
const TRANSLATABLE_ATTRIBUTES = ['alt', 'title', 'placeholder', 'aria-label', 'aria-describedby', 'aria-labelledby', 'value'];
5657

5758
const parseHTMLStructure = (s: string) => {
58-
const tags = Array.from(s.matchAll(/<([^>]+)>/g));
59-
return tags.map((match) => {
60-
const tagContent = match[1];
61-
const tagName = tagContent.split(/\s/).at(0)?.toLowerCase();
59+
const doc = parseDocument(s);
60+
const elements = DomUtils.getElementsByTagName(() => true, doc, true);
61+
62+
return elements.map((element) => {
63+
const tagName = element.name.toLowerCase();
6264

6365
// Extract attributes, excluding translatable ones
6466
const attributes: string[] = [];
65-
const attrMatches = Array.from(tagContent.matchAll(/(\w+)(?:=["']([^"']*)["'])?/g));
66-
67-
for (const attrMatch of attrMatches) {
68-
const attrName = attrMatch[1].toLowerCase();
69-
const attrValue = attrMatch[2] || '';
70-
71-
// Only include non-translatable attributes in comparison
72-
if (!TRANSLATABLE_ATTRIBUTES.includes(attrName)) {
73-
attributes.push(`${attrName}="${attrValue}"`);
74-
}
67+
if (element.attribs) {
68+
Object.entries(element.attribs).forEach(([attrName, attrValue]) => {
69+
const normalizedAttrName = attrName.toLowerCase();
70+
if (!TRANSLATABLE_ATTRIBUTES.includes(normalizedAttrName)) {
71+
attributes.push(`${normalizedAttrName}="${attrValue ?? ''}"`);
72+
}
73+
})
7574
}
7675

7776
return {

0 commit comments

Comments
 (0)