这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@sirrahd
Copy link

@sirrahd sirrahd commented Jan 7, 2018

This change fixes an issue where the Datetime Format GUI fails to load with the error 'Failed to convert locale string to UTF8: Invalid byte sequence in conversion input' if unicode characters are entered in any of the format strings.

Date().toLocaleFormat(format) only accepts ascii characters in the format string. This change works around that limitation by escaping non-ascii characters in the format string before passing the string to toLocaleFormat(), then escaping the result in case non-ascii characters are in the localized string, and finally unescaping the resulting string by passing it to JSON.parse.

This change fixes an issue where the Datetime Format GUI fails to load with the error 'Failed to convert locale string to UTF8: Invalid byte sequence in conversion input' if unicode characters are entered in any of the format strings.

Date().toLocaleFormat(format) only accepts ascii characters in the format string. This change works around that limitation by escaping non-ascii characters in the format string before passing the string to toLocaleFormat(), then escaping the result in case non-ascii characters are in the localized string, and finally unescaping the resulting string by passing it to JSON.parse.
src/Utilities.js Outdated
///
function dateTimeFormat(format, defaultFormat) {
return (format && new Date().toLocaleFormat(format) || format) || defaultFormat && new Date().toLocaleFormat(defaultFormat) || "";
function escapeUnicode(str) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this function out and add comments like the other functions in this file? And could you also rename the 'str' parameter to 'string'?

src/Utilities.js Outdated
});
}

return JSON.parse('"' + escapeUnicode((format && new Date().toLocaleFormat(escapeUnicode(format)) || format) || defaultFormat && new Date().toLocaleFormat(escapeUnicode(defaultFormat)) || "").replace('"', '\\"') + '"');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to test this before I merge it, want to make sure all edge cases are accounted for.

@sirrahd
Copy link
Author

sirrahd commented Jan 8, 2018

Thanks Daniel. I did some edge case testing just now on this and found this doesn't handle " and \ characters well, need to escape them to make JSON.parse happy. I will push an update today with these fixes and your requested changes.

This change expands on the previous unicode fix to also escape \ and " characters that are problematic for JSON.parse, and make formatting changes.

escapeJson() includes escaping problematic ascii characters and unicode, while escapeUnicode() escapes only unicode characters. escapeJson() should be run only once on the raw format string to avoid over-escaping, and the escapeUnicode() must be run after toLocaleFormat() because toLocaleFormat() can return additional unicode characters in languages like Japanese.
@sirrahd
Copy link
Author

sirrahd commented Jan 8, 2018

Just pushed an update that addresses your feedback and fixes the " and \ issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants