diff --git a/docs/i18n/detail/options.md b/docs/i18n/detail/options.md index 04395313..d5731a1f 100644 --- a/docs/i18n/detail/options.md +++ b/docs/i18n/detail/options.md @@ -54,9 +54,9 @@ i18n.set({ now: '现在', reset: '重置' }, - timeout: '结束时间不能早于开始时间
请重新选择', + timeout: '结束时间不能早于开始时间\n请重新选择', invalidDate: '不在有效日期或时间范围内', - formatError: ['日期格式不合法
必须遵循下述格式:
', '
已为你重置'], + formatError: ['日期格式不合法\n必须遵循下述格式:\n', '\n已为你重置'], preview: '当前选中的结果' }, layer: { @@ -78,7 +78,7 @@ i18n.set({ }, viewPicture: '查看原图', urlError: { - prompt: '当前图片地址异常,
是否继续查看下一张?', + prompt: '当前图片地址异常,\n是否继续查看下一张?', confirm: '下一张', cancel: '不看了' } diff --git a/examples/i18n/en.js b/examples/i18n/en.js index db414c03..fabfe301 100644 --- a/examples/i18n/en.js +++ b/examples/i18n/en.js @@ -54,9 +54,9 @@ export default { now: 'Now', reset: 'Reset' }, - timeout: 'End time cannot be less than start Time
Please re-select', + timeout: 'End time cannot be less than start Time\nPlease re-select', invalidDate: 'Invalid date', - formatError: ['The date format error
Must be followed:
', '
It has been reset'], + formatError: ['The date format error\nMust be followed:\n', '\nIt has been reset'], preview: 'The selected result' }, layer: { @@ -78,7 +78,7 @@ export default { }, viewPicture: 'View Original', urlError: { - prompt: 'Image URL is invalid
Continue to next one?', + prompt: 'Image URL is invalid, \nContinue to next one?', confirm: 'Next', cancel: 'Cancel' } diff --git a/examples/i18n/fr.js b/examples/i18n/fr.js index 7d0a0f72..7516e230 100644 --- a/examples/i18n/fr.js +++ b/examples/i18n/fr.js @@ -54,9 +54,9 @@ export default { now: 'Ajd.', reset: 'Réinit.' }, - timeout: 'L’heure de fin ne peut pas être antérieure à l’heure de début
Veuillez recommencer', + timeout: 'L’heure de fin ne peut pas être antérieure à l’heure de début\nVeuillez recommencer', invalidDate: 'Date ou heure hors plage valide', - formatError: ['Format de date invalide
Le format attendu est :
', '
Il a été réinitialisé pour vous'], + formatError: ['Format de date invalide\nLe format attendu est :\n', '\nIl a été réinitialisé pour vous'], preview: 'Résultat sélectionné actuel' }, layer: { @@ -78,7 +78,7 @@ export default { }, viewPicture: 'Voir l’image originale', urlError: { - prompt: 'L’adresse de l’image est invalide
Continuer avec la suivante ?', + prompt: 'L’adresse de l’image est invalide,\nContinuer avec la suivante ?', confirm: 'Suivante', cancel: 'Annuler' } diff --git a/examples/i18n/zh-HK.js b/examples/i18n/zh-HK.js index 1b3cc7ba..3ddc362f 100644 --- a/examples/i18n/zh-HK.js +++ b/examples/i18n/zh-HK.js @@ -53,9 +53,9 @@ export default { now: '現在', reset: '重設' }, - timeout: '結束時間不能早於開始時間
請重新選擇', + timeout: '結束時間不能早於開始時間\n請重新選擇', invalidDate: '不在有效日期或時間範圍內', - formatError: ['日期格式不合法
必須遵循以下格式:
', '
已為你重設'], + formatError: ['日期格式不合法\n必須遵循以下格式:\n', '\n已為你重設'], preview: '當前選中的結果' }, layer: { @@ -77,7 +77,7 @@ export default { }, viewPicture: '查看原圖', urlError: { - prompt: '當前圖片地址異常,
是否繼續查看下一張?', + prompt: '當前圖片地址異常,\n是否繼續查看下一張?', confirm: '下一張', cancel: '不看了' } diff --git a/src/css/modules/laydate.css b/src/css/modules/laydate.css index 1d7facca..9019c6d0 100644 --- a/src/css/modules/laydate.css +++ b/src/css/modules/laydate.css @@ -96,7 +96,7 @@ html #layuicss-laydate{display: none; position: absolute; width: 1989px;} .layui-laydate .laydate-time-list-hide-2 ol li{padding-left: 117px;} /* 提示 */ -.layui-laydate-hint{position: absolute; top: 115px; left: 50%; width: 250px; margin-left: -125px; line-height: 20px; padding: 15px; text-align: center; font-size: 12px; color: #FF5722;} +.layui-laydate-hint{position: absolute; top: 115px; left: 50%; width: 250px; margin-left: -125px; line-height: 20px; padding: 15px; text-align: center; font-size: 12px; color: #FF5722;white-space: pre-line;} /* 双日历 */ diff --git a/src/layui.js b/src/layui.js index c99b5c06..b4498ea0 100644 --- a/src/layui.js +++ b/src/layui.js @@ -120,6 +120,28 @@ }; } + // Array.prototype.map + // 引用自 https://github.com/inexorabletash/polyfill/blob/716a3f36ca10fad032083014faf1a47c638e2502/es5.js#L300-L345 + if (!Array.prototype.map) { + Array.prototype.map = function (fun /*, thisp */) { + if (this === void 0 || this === null) { throw TypeError(); } + + var t = Object(this); + var len = t.length >>> 0; + if (typeof fun !== "function") { throw TypeError(); } + + var res = []; res.length = len; + var thisp = arguments[1], i; + for (i = 0; i < len; i++) { + if (i in t) { + res[i] = fun.call(thisp, t[i], i, t); + } + } + + return res; + }; + } + /** * 节点加载事件 * @param {HTMLElement} node - script 或 link 节点 diff --git a/src/modules/i18n.js b/src/modules/i18n.js index 718a3afb..bb8d665f 100644 --- a/src/modules/i18n.js +++ b/src/modules/i18n.js @@ -66,9 +66,9 @@ layui.define('lay', function(exports) { now: '现在', reset: '重置' }, - timeout: '结束时间不能早于开始时间
请重新选择', + timeout: '结束时间不能早于开始时间\n请重新选择', invalidDate: '不在有效日期或时间范围内', - formatError: ['日期格式不合法
必须遵循下述格式:
', '
已为你重置'], + formatError: ['日期格式不合法\n必须遵循下述格式:\n', '\n已为你重置'], preview: '当前选中的结果' }, layer: { @@ -90,7 +90,7 @@ layui.define('lay', function(exports) { }, viewPicture: '查看原图', urlError: { - prompt: '当前图片地址异常,
是否继续查看下一张?', + prompt: '当前图片地址异常,\n是否继续查看下一张?', confirm: '下一张', cancel: '不看了' } @@ -217,6 +217,30 @@ layui.define('lay', function(exports) { return result; } + /** + * 对传入的值进行转义处理 + * 若值为字符串,直接进行转义;若为函数,对函数返回的字符串进行转义;若为数组,对数组中的字符串元素进行转义 + * @param {any} value - 需要进行转义处理的值 + * @returns {any} - 转义后的结果 + */ + function escape(value) { + if(typeof value === 'string'){ + value = lay.escape(value); + }else if(typeof value === 'function'){ + var origFn = value; + value = function(){ + var val = origFn.apply(this, arguments) + return typeof val === 'string' ? lay.escape(val) : val; + } + }else if(layui.type(value) === 'array'){ + value = value.map(function(v){ + return typeof v === 'string' ? lay.escape(v) : v; + }); + } + + return value + } + var i18n = { config: config, set: function(options) { @@ -271,19 +295,19 @@ layui.define('lay', function(exports) { var opts = args[1]; // 第二个参数为对象或数组,替换占位符 {key} 或 {0}, {1}... if (opts !== null && typeof opts === 'object') { - return result.replace(OBJECT_REPLACE_REGEX, function(match, key) { + result = result.replace(OBJECT_REPLACE_REGEX, function(match, key) { return opts[key] !== undefined ? opts[key] : match; }); + }else{ + // 处理可变参数,替换占位符 {0}, {1}... + result = result.replace(INDEX_REPLACE_REGEX, function(match, index) { + var arg = args[index + 1]; + return arg !== undefined ? arg : match; + }); } - - // 处理可变参数,替换占位符 {0}, {1}... - return result.replace(INDEX_REPLACE_REGEX, function(match, index) { - var arg = args[index + 1]; - return arg !== undefined ? arg : match; - }); } - return result; + return escape(result); }; /** diff --git a/src/modules/lay.js b/src/modules/lay.js index d19abc0e..8a95f278 100644 --- a/src/modules/lay.js +++ b/src/modules/lay.js @@ -796,10 +796,46 @@ }; var hasOwnProperty = Object.prototype.hasOwnProperty; + /** + * 检查对象是否具有指定的属性 + * @param {Record} obj 要检查的对象 + * @param {string} prop 要检查的属性名 + * @returns {boolean} 如果对象具有指定的属性,则为 true;否则为 false + */ lay.hasOwn = function(obj, prop){ return hasOwnProperty.call(obj, prop); }; + /** + * 转义 HTML 字符串中的特殊字符 + * @param {string} html 要转义的 HTML 字符串 + * @returns {string} 转义后的 HTML 字符串 + */ + lay.escape = function (html) { + var exp = /[<"'>]|&(?=#?[a-zA-Z0-9]+)/g; + if (html === undefined || html === null) return ''; + + html += ''; + if (!exp.test(html)) return html; + + return html.replace(/&(?=#?[a-zA-Z0-9]+;?)/g, '&') + .replace(//g, '>') + .replace(/'/g, ''').replace(/"/g, '"'); + }; + + /** + * 还原转义的 HTML 字符串中的特殊字符 + * @param {string} html 要还原转义的 HTML 字符串 + * @returns {string} 还原转义后的 HTML 字符串 + */ + lay.unescape = function (html) { + if (html === undefined || html === null) return ''; + + return String(html).replace(/\"/g, '"').replace(/\'/g, '\'') + .replace(/\>/g, '>').replace(/\</g, '<') + .replace(/\&/g, '&'); + }; + /* * lay 元素操作 diff --git a/src/modules/laydate.js b/src/modules/laydate.js index e38a0d2f..85943c67 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -199,9 +199,9 @@ layui.define(['lay', 'i18n'], function(exports) { now: 'Now', reset: 'Reset' }, - timeout: 'End time cannot be less than start Time
Please re-select', + timeout: 'End time cannot be less than start Time\nPlease re-select', invalidDate: 'Invalid date', - formatError: ['The date format error
Must be followed:
', '
It has been reset'], + formatError: ['The date format error\nMust be followed:\n', '\nIt has been reset'], preview: 'The selected result' } }; diff --git a/src/modules/layer.js b/src/modules/layer.js index 6a484d8e..7e774764 100644 --- a/src/modules/layer.js +++ b/src/modules/layer.js @@ -50,7 +50,7 @@ var ready = { }, viewPicture: '查看原图', urlError: { - prompt: '当前图片地址异常,
是否继续查看下一张?', + prompt: '当前图片地址异常,\n是否继续查看下一张?', confirm: '下一张', cancel: '不看了' } @@ -1921,7 +1921,7 @@ layer.photos = function(options, loop, key){ }, options)); }, function(){ layer.close(dict.loadi); - layer.msg(i18n.$t('layer.photos.urlError.prompt'), { + layer.msg('' + i18n.$t('layer.photos.urlError.prompt') + '', { time: 30000, btn: [i18n.$t('layer.photos.urlError.confirm'), i18n.$t('layer.photos.urlError.cancel')], yes: function(){ diff --git a/src/modules/util.js b/src/modules/util.js index b64102ea..0ddf2a19 100644 --- a/src/modules/util.js +++ b/src/modules/util.js @@ -2,12 +2,13 @@ * util 工具组件 */ -layui.define(['i18n', 'jquery'], function(exports) { +layui.define(['lay', 'i18n', 'jquery'], function(exports) { "use strict"; var $ = layui.$; var hint = layui.hint(); var i18n = layui.i18n; + var lay = layui.lay; // 引用自 dayjs // https://github.com/iamkun/dayjs/blob/v1.11.9/src/constant.js#L30 @@ -335,26 +336,10 @@ layui.define(['i18n', 'jquery'], function(exports) { }, // 转义 html - escape: function(html){ - var exp = /[<"'>]|&(?=#?[a-zA-Z0-9]+)/g; - if (html === undefined || html === null) return ''; - - html += ''; - if (!exp.test(html)) return html; - - return html.replace(/&(?=#?[a-zA-Z0-9]+;?)/g, '&') - .replace(//g, '>') - .replace(/'/g, ''').replace(/"/g, '"'); - }, + escape: lay.escape, // 还原转义的 html - unescape: function(html){ - if (html === undefined || html === null) return ''; - - return String(html).replace(/\"/g, '"').replace(/\'/g, '\'') - .replace(/\>/g, '>').replace(/\</g, '<') - .replace(/\&/g, '&'); - }, + unescape: lay.unescape, // 打开新窗口 openWin: function(options){