Support IDN (#1629)

* Support IDN

* use js

* Optimize code

* Optimize code

* fix URL script

* remove unnecessary use

---------

Co-authored-by: joyqi <joyqi@segmentfault.com>
This commit is contained in:
Lu Fei 2023-12-04 00:07:20 -06:00 committed by GitHub
parent e89b1bbcf6
commit a1f62e1bcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 96 additions and 7 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,7 @@
<script>
(function () {
$(document).ready(function () {
var error = $('.typecho-option .error:first');
const error = $('.typecho-option .error:first');
if (error.length > 0) {
$('html,body').scrollTop(error.parents('.typecho-option').offset().top);
@ -23,10 +23,29 @@
});
$('label input[type=text]').click(function (e) {
var check = $('#' + $(this).parents('label').attr('for'));
const check = $('#' + $(this).parents('label').attr('for'));
check.prop('checked', true);
return false;
});
$('.main form input[type="url"]').each(function () {
const self = $(this);
const input = $('<input type="hidden" />').attr('name', self.attr('name'));
function setInput() {
const url = self.val();
try {
const urlObj = new URL(url);
input.val(urlObj.toString());
} catch {
// ignore
}
}
self.removeAttr('name').after(input).on('input', setInput);
setInput();
});
});
})();
</script>

View File

@ -11,7 +11,7 @@ $stat = \Widget\Stat::alloc();
<?php include 'page-title.php'; ?>
<div class="row typecho-page-main">
<div class="col-mb-12 col-tb-3">
<p><a href="https://gravatar.com/emails/"
<p><a href="https://gravatar.com/"
title="<?php _e('在 Gravatar 上修改头像'); ?>"><?php echo '<img class="profile-avatar" src="' . \Typecho\Common::gravatarUrl($user->mail, 220, 'X', 'mm', $request->isSecure()) . '" alt="' . $user->screenName . '" />'; ?></a>
</p>
<h2><?php $user->screenName(); ?></h2>

View File

@ -2,7 +2,7 @@
* Forms
*/
input[type=text], input[type=password], input[type=email],
input[type=text], input[type=url], input[type=password], input[type=email],
textarea {
background: #FFF;
border: 1px solid #D9D9D6;

View File

@ -1477,5 +1477,21 @@ EOF;
return $result;
}
/**
* IDN转UTF8
*
* @param string $url
* @return string
*/
public static function idnToUtf8(string $url): string
{
if (function_exists('idn_to_utf8') && !empty($url)) {
$host = parse_url($url, PHP_URL_HOST);
$url = str_replace($host, idn_to_utf8($host), $url);
}
return $url;
}
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Common;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* Url 表单项帮手类
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Url extends Element
{
/**
* 初始化当前输入项
*
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout|null
*/
public function input(?string $name = null, ?array $options = null): ?Layout
{
$input = new Layout('input', ['id' => $name . '-0-' . self::$uniqueId,
'name' => $name, 'type' => 'url', 'class' => 'text']);
$this->container($input);
$this->label->setAttribute('for', $name . '-0-' . self::$uniqueId);
$this->inputs[] = $input;
return $input;
}
/**
* 设置表单项默认值
*
* @param mixed $value 表单项默认值
*/
protected function inputValue($value)
{
if (isset($value)) {
$this->input->setAttribute('value', htmlspecialchars(Common::idnToUtf8($value)));
} else {
$this->input->removeAttribute('value');
}
}
}

View File

@ -153,7 +153,7 @@ class General extends Options implements ActionInterface
/** 站点地址 */
if (!defined('__TYPECHO_SITE_URL__')) {
$siteUrl = new Form\Element\Text(
$siteUrl = new Form\Element\Url(
'siteUrl',
null,
$this->options->originalSiteUrl,

View File

@ -222,7 +222,7 @@ class Profile extends Users implements ActionInterface
$form->addInput($screenName);
/** 个人主页地址 */
$url = new Form\Element\Text('url', null, null, _t('个人主页地址'), _t('此用户的个人主页地址, 请用 <code>https://</code> 开头.'));
$url = new Form\Element\Url('url', null, null, _t('个人主页地址'), _t('此用户的个人主页地址, 请用 <code>https://</code> 开头.'));
$form->addInput($url);
/** 电子邮箱地址 */