iOS 11 Safari HTML - 禁用 "Smart Punctuation"?

标签 ios unicode safari ios11 smart-quotes

是否有一种好方法可以禁用 iOS 11 Apple 键盘生成的“Smart 标点符号”——在 Safari 中的 HTML 登录表单中——尤其是用户名字段?

问题是我们的用户名中有撇号。在 iOS 11 上输入他们的用户名并且不知道 unicode 的微妙之处,他们无法登录。

理想情况下,我们可以指示此类用户禁用智能引号或通过按住撇号键键入正确的字符 - 但我正在为幼儿开发教育软件,这是不可能的。

事实上,还有一小部分用户在他们的用户名中使用了实际的 单引号,因此问题变得更加复杂,因此简单的替换映射将不起作用 - 我们可以'不要规范化用户名,因为它们来自许多我们无法控制/没有太多发言权的外部系统。

最佳答案

不幸的是根据this MDN page没有已知的 Webkit <input><textarea>禁用智能引号的属性,但您可以使用我编写的源自此 QA 的脚本对其进行修补:How to disable smart quotes for textarea fields in the browser?

window.addEventListener('DOMContentLoaded', attachFilterToInputs );

function attachFilterToInputs() {

    var textInputs = document.querySelectorAll( 'input[type=text], input[type=password], input[type=email], input[type=search], input[type=url], textarea, *[contenteditable=true]' );
    for( var i = 0; i < textInputs.length; i++ ) {
        textInputs[i].addEventListener( 'keypress', preventPretentiousPunctuation );
    } 
}

var conversionMap = createConversionMap();

function createConversionMap() {

    var map = {};
    // Open-quotes: http://www.fileformat.info/info/unicode/category/Pi/list.htm
    map[ 0x2018 ] = '\'';
    map[ 0x201B ] = '\'';
    map[ 0x201C ] = '"';
    map[ 0x201F ] = '"';

    // Close-quotes: http://www.fileformat.info/info/unicode/category/Pf/list.htm
    map[ 0x2019 ] = '\'';
    map[ 0x201D ] = '\"';

    // Primes: http://www.fileformat.info/info/unicode/category/Po/list.htm
    map[ 0x2032 ] = '\'';
    map[ 0x2033 ] = '"';
    map[ 0x2035 ] = '\'';
    map[ 0x2036 ] = '"';

    map[ 0x2014 ] = '-'; // iOS 11 also replaces dashes with em-dash
    map[ 0x2013 ] = '-'; // and "--" with en-dash

    return map;
}

function preventPretentiousPunctuation( event ) {

    if( event.key.length != 1 ) return;

    var code = event.key.codePointAt(0);
    var replacement = conversionMap[ code ];
    if( replacement ) {

        event.preventDefault();
        document.execCommand( 'insertText', 0, replacement );
    }
} 

关于iOS 11 Safari HTML - 禁用 "Smart Punctuation"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48678359/

相关文章:

javascript - HTML 选项不会仅在 Safari 中对 onmousedown 使用react

iOS Swift 始终以相同的 View Controller 开始

ios - 识别核心数据属性类型

ios - 在没有 iPhone6s 或更新版本的情况下测试 ARKit

c# - 为什么用于 Unicode 属性测试的 C# System.Char 方法有两个重载?

javascript - 在 MobileSafari 中点击状态栏在我的测试页面上不起作用

c# - 使用 protobuf-net 反序列化字典

string - 如何使用 Unicode::Normalize 创建最兼容的 windows-1252 编码字符串?

unicode - 什么是 UTF-8 数据的好终止符字节?

iphone - iPhone 上的 Safari : CSS Positioning off by 1px