diff --git a/app/utils/markdown.js b/app/utils/markdown.js index 9d5a80333..c5c60f20b 100644 --- a/app/utils/markdown.js +++ b/app/utils/markdown.js @@ -184,22 +184,25 @@ export function escapeRegex(text) { } export function switchKeyboardForCodeBlocks(value, cursorPosition) { - const regexForCodeBlock = /^```$(.*?)^```$|^```$(.*)/gms; + if (Platform.OS === 'ios' && parseInt(Platform.Version, 10) >= 12) { + const regexForCodeBlock = /^```$(.*?)^```$|^```$(.*)/gms; - const matches = []; - let nextMatch; - while ((nextMatch = regexForCodeBlock.exec(value)) !== null) { - matches.push({ - startOfMatch: regexForCodeBlock.lastIndex - nextMatch[0].length, - endOfMatch: regexForCodeBlock.lastIndex + 1, - }); + const matches = []; + let nextMatch; + while ((nextMatch = regexForCodeBlock.exec(value)) !== null) { + matches.push({ + startOfMatch: regexForCodeBlock.lastIndex - nextMatch[0].length, + endOfMatch: regexForCodeBlock.lastIndex + 1, + }); + } + + const cursorIsInsideCodeBlock = matches.some((match) => cursorPosition >= match.startOfMatch && cursorPosition <= match.endOfMatch); + + // 'email-address' keyboardType prevents iOS emdash autocorrect + if (cursorIsInsideCodeBlock) { + return 'email-address'; + } } - const cursorIsInsideCodeBlock = matches.some((match) => cursorPosition >= match.startOfMatch && cursorPosition <= match.endOfMatch); - - // 'email-address' keyboardType prevents iOS emdash autocorrect - if (cursorIsInsideCodeBlock) { - return 'email-address'; - } return 'default'; } diff --git a/app/utils/markdown.test.js b/app/utils/markdown.test.js index 079547549..cafd4d72c 100644 --- a/app/utils/markdown.test.js +++ b/app/utils/markdown.test.js @@ -1,6 +1,7 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import {Platform} from 'react-native'; import {switchKeyboardForCodeBlocks} from './markdown'; describe('switchKeyboardForCodeBlocks', () => { @@ -67,3 +68,11 @@ describe('switchKeyboardForCodeBlocks', () => { }); } }); + +describe('switchKeyboardForCodeBlocks for iOS 11', () => { + it('Should return default keyboard', () => { + Platform.Version = 11; + expect(switchKeyboardForCodeBlocks('```\ntest\n```test', 12)).toEqual('default'); + Platform.Version = 12; + }); +}); diff --git a/test/setup.js b/test/setup.js index 0bf8d9a49..f13b31e1c 100644 --- a/test/setup.js +++ b/test/setup.js @@ -94,7 +94,11 @@ jest.doMock('react-native', () => { }; return Object.setPrototypeOf({ - Platform, + Platform: { + ...Platform, + OS: 'ios', + Version: 12, + }, StyleSheet, ViewPropTypes, PermissionsAndroid,