是否有一种很好的方法来禁用iOS 11 Apple Keyboard生成的“ Smart标点符号-在Safari中以HTML登录表单-特别是用户名字段?
问题是,我们在用户名中使用撇号的用户。在iOS 11上输入他们的用户名,并且不知道他们无法登录的unicode的细微之处。
理想情况下,我们可以指示这些用户禁用智能引号或通过按住撇号键来键入正确的字符-但我正在为幼儿设计教育软件,这是不可能的。
问题更加复杂,因为用户名中还有一小部分具有实际curly单引号的用户,因此简单的替换图将不起作用-我们无法规范化用户名因为它们来自许多外部系统,我们无法控制/不能说太多。
[不幸的是,根据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 );
}
}
感谢Dai的概述。不幸的是,当我们实施他们的解决方案并在iOS 11设备上进行测试时,我们发现keypress
事件监听器根本没有触发。我们使用input
事件和正则表达式字符串替换对他们的解决方案进行了重新设计,发现这确实对我们有用。
这是我们的变体,使用jQuery作为选择器和较短的替换列表。
<script type="text/javascript">
// Here are the reference to Unicode Characters in the Punctuation
// Open-quotes: http://www.fileformat.info/info/unicode/category/Pi/list.htm
// Close-quotes: http://www.fileformat.info/info/unicode/category/Pf/list.htm
window.addEventListener('DOMContentLoaded', attachFilterToInputs);
// Patch the iOS Smart Punctuation functionality on restricted field(s),
// so that the user types acceptable characters.
function attachFilterToInputs() {
try {
$("[name$='MyProtectedFieldName']").on('input', preventPretentiousPunctuation);
}
catch (err) {
// do nothing
}
}
function preventPretentiousPunctuation(event) {
try {
var str = $(this).val();
var replacedStr = "";
replacedStr = str.replace(/[\u2018\u2019\u201C\u201D]/g,
(c) => '\'\'""'.substr('\u2018\u2019\u201C\u201D'.indexOf(c), 1));
if (str !== replacedStr) {
$(this).val(replacedStr);
}
}
catch (err) {
// do nothing
}
}
</script>
添加为答案,因为我没有评论的代表。