From bbdb7112dadfdd892835c278f36b878e2aba8be3 Mon Sep 17 00:00:00 2001 From: LiOn Date: Sun, 7 Apr 2024 22:55:48 +0300 Subject: [PATCH 1/2] ref: Simplify and modern domain validation --- src/index.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 1073c17..08e144e 100644 --- a/src/index.js +++ b/src/index.js @@ -100,25 +100,21 @@ $('.badge__dns-mobile').addEventListener('click', () => { // SET DOMAIN +const validateDomainChars = (domain) => { + return !!domain.match(/^[0-9A-Za-z-]+$/) // '-' or 0-9 or a-z ; abcdefghijklmnopqrstuvwxyz-0123456789 +} + const validateDomain = (domain) => { if (domain.length < 4 || domain.length > 126) { return store.localeDict.error_length } - for (let i = 0; i < domain.length; i++) { - if (domain.charAt(i) === '.') { - return store.localeDict.subdomains_not_allowed - } - const char = domain.charCodeAt(i) - const isHyphen = char === 45 - const isValidChar = - (isHyphen && i > 0 && i < domain.length - 1) || - (char >= 48 && char <= 57) || - (char >= 97 && char <= 122) // '-' or 0-9 or a-z ; abcdefghijklmnopqrstuvwxyz-0123456789 - - if (!isValidChar) { - return store.localeDict.invalid_chars - } + if (domain.includes('.')) { + return store.localeDict.subdomains_not_allowed + } + + if (!validateDomainChars(domain)) { + return store.localeDict.invalid_chars } } From cdb309de878929703f5cc1ff5f44d61f60305586 Mon Sep 17 00:00:00 2001 From: LiOn Date: Sun, 7 Apr 2024 22:57:55 +0300 Subject: [PATCH 2/2] feature: Punycode support --- css/dns.css | 10 +++++++++- index.html | 3 +++ src/index.js | 9 +++++++-- src/libs/idna-uts46-hx-2.3.1.js | 1 + src/utils.js | 13 +++++++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/libs/idna-uts46-hx-2.3.1.js diff --git a/css/dns.css b/css/dns.css index c01450d..cca34f7 100644 --- a/css/dns.css +++ b/css/dns.css @@ -909,7 +909,6 @@ nav .container-inner { font-size: 40px; line-height: 48px; color: var(--text-primary); - margin-bottom: 24px; word-break: break-all; text-align: center; } @@ -918,6 +917,15 @@ nav .container-inner { font-size: 32px; } +#domainPunyName { + font-weight: 600; + line-height: 48px; + color: var(--text-primary); + margin-bottom: 24px; + word-break: break-all; + text-align: center; +} + .status { flex-direction: row; justify-content: center; diff --git a/index.html b/index.html index dce7155..25fb0ad 100644 --- a/index.html +++ b/index.html @@ -224,6 +224,8 @@

Get .ton domains

+ +

@@ -910,6 +912,7 @@

Get .ton domains

+ diff --git a/src/index.js b/src/index.js index 08e144e..560b5c3 100644 --- a/src/index.js +++ b/src/index.js @@ -272,7 +272,8 @@ const setDomain = (domain, isTimerMounted) => { $('.start-input').value = '' setCareeteHelperValue('') - setDomainName(domain, $('#domainName')) + setDomainName(normalizeDomain(domain), $('#domainName')) + $('#domainPunyName').innerText = isPunycode(domain) ? `(${domain}.ton)` : '' setScreen('main') clearInterval(updateIntervalId) @@ -310,6 +311,9 @@ const onInput = (e) => { if (domain.endsWith('.ton')) { domain = domain.substring(0, domain.length - 4) } + if (!validateDomainChars(domain)) { + domain = idnaUts46.toAscii(domain) + } const error = validateDomain(domain) if (error) { setError($('.start-error'), error) @@ -556,6 +560,7 @@ const renderSearchHistory = (node) => { try { const historyMarkup = getHistoryFromStorage().map( (historyRecord) => { + const normalizedDomain = normalizeDomain(historyRecord) const sanitizedValue = encodeHTML(historyRecord) return `