-
-
Notifications
You must be signed in to change notification settings - Fork 861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⭐ new: HTML locale message warning option #567
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0414a22
:star: new: allow html foratting option
kazupon 55e37a9
fix new feature API name
kazupon af563bd
implement logic
kazupon 48bdde0
fix: change to 'off' from 'allow'
kazupon 46a4c1b
fix: flow error
kazupon 563b3f5
fix: tweak console message
kazupon 2f87050
refactor
kazupon 008cb87
fix: regex miss-take
kazupon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import { install, Vue } from './install' | ||
import { | ||
warn, | ||
error, | ||
isNull, | ||
parseArgs, | ||
isPlainObject, | ||
|
@@ -17,6 +18,7 @@ import I18nPath from './path' | |
|
||
import type { PathValue } from './path' | ||
|
||
const htmlTagMatcher = /<\/?[\w\s="/.':;#-\/]+>/gi | ||
const linkKeyMatcher = /(?:@(?:\.[a-z]+)?:(?:[\w\-_|.]+|\([\w\-_|.]+\)))/g | ||
const linkKeyPrefixMatcher = /^@(?:\.([a-z]+))?:/ | ||
const bracketsMatcher = /[()]/g | ||
|
@@ -46,6 +48,7 @@ export default class VueI18n { | |
_path: I18nPath | ||
_dataListeners: Array<any> | ||
_preserveDirectiveContent: boolean | ||
_warnHtmlInMessage: WarnHtmlInMessageLevel | ||
pluralizationRules: { | ||
[lang: string]: (choice: number, choicesLength: number) => number | ||
} | ||
|
@@ -87,6 +90,7 @@ export default class VueI18n { | |
? false | ||
: !!options.preserveDirectiveContent | ||
this.pluralizationRules = options.pluralizationRules || {} | ||
this._warnHtmlInMessage = options.warnHtmlInMessage || 'off' | ||
|
||
this._exist = (message: Object, key: Path): boolean => { | ||
if (!message || !key) { return false } | ||
|
@@ -96,6 +100,12 @@ export default class VueI18n { | |
return false | ||
} | ||
|
||
if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') { | ||
Object.keys(messages).forEach(locale => { | ||
this._checkLocaleMessage(locale, this._warnHtmlInMessage, messages[locale]) | ||
}) | ||
} | ||
|
||
this._initVM({ | ||
locale, | ||
fallbackLocale, | ||
|
@@ -105,6 +115,41 @@ export default class VueI18n { | |
}) | ||
} | ||
|
||
_checkLocaleMessage (locale: Locale, level: WarnHtmlInMessageLevel, message: LocaleMessageObject): void { | ||
const stack: Array<string> = [] | ||
|
||
const fn = (message: mixed, stack: Array<string>) => { | ||
if (isPlainObject(message)) { | ||
Object.keys(message).forEach(key => { | ||
const val = message[key] | ||
const hasObject = isPlainObject(val) | ||
stack.push(key) | ||
hasObject && stack.push('.') | ||
fn(val, stack) | ||
hasObject && stack.pop() | ||
}) | ||
} else if (Array.isArray(message)) { | ||
message.forEach((item, index) => { | ||
stack.push(`[${index}]`) | ||
fn(item, stack) | ||
stack.pop() | ||
}) | ||
} else if (typeof message === 'string') { | ||
const ret = htmlTagMatcher.test(message) | ||
if (ret) { | ||
const msg = `Detect unsafe locale message '${message}' of keypath '${stack.join('')}' at '${locale}', suggest use component interpolation with '<i18n>'` | ||
if (level === 'warn') { | ||
warn(msg) | ||
} else if (level === 'error') { | ||
error(msg) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn(message, stack) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be helpful if error message suggest users to read Formatting and use component interpolation |
||
|
||
_initVM (data: { | ||
locale: Locale, | ||
fallbackLocale: Locale, | ||
|
@@ -184,6 +229,18 @@ export default class VueI18n { | |
get preserveDirectiveContent (): boolean { return this._preserveDirectiveContent } | ||
set preserveDirectiveContent (preserve: boolean): void { this._preserveDirectiveContent = preserve } | ||
|
||
get warnHtmlInMessage (): WarnHtmlInMessageLevel { return this._warnHtmlInMessage } | ||
set warnHtmlInMessage (level: WarnHtmlInMessageLevel): void { | ||
const orgLevel = this._warnHtmlInMessage | ||
this._warnHtmlInMessage = level | ||
if (orgLevel !== level && (level === 'warn' || level === 'error')) { | ||
const messages = this._getMessages() | ||
Object.keys(messages).forEach(locale => { | ||
this._checkLocaleMessage(locale, this._warnHtmlInMessage, messages[locale]) | ||
}) | ||
} | ||
} | ||
|
||
_getMessages (): LocaleMessages { return this._vm.messages } | ||
_getDateTimeFormats (): DateTimeFormats { return this._vm.dateTimeFormats } | ||
_getNumberFormats (): NumberFormats { return this._vm.numberFormats } | ||
|
@@ -499,10 +556,18 @@ export default class VueI18n { | |
} | ||
|
||
setLocaleMessage (locale: Locale, message: LocaleMessageObject): void { | ||
if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') { | ||
this._checkLocaleMessage(locale, this._warnHtmlInMessage, message) | ||
if (this._warnHtmlInMessage === 'error') { return } | ||
} | ||
this._vm.$set(this._vm.messages, locale, message) | ||
} | ||
|
||
mergeLocaleMessage (locale: Locale, message: LocaleMessageObject): void { | ||
if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') { | ||
this._checkLocaleMessage(locale, this._warnHtmlInMessage, message) | ||
if (this._warnHtmlInMessage === 'error') { return } | ||
} | ||
this._vm.$set(this._vm.messages, locale, merge(this._vm.messages[locale] || {}, message)) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
describe('warnHtmlInMessage', () => { | ||
let spyWarn | ||
let spyError | ||
beforeEach(() => { | ||
spyWarn = sinon.spy(console, 'warn') | ||
spyError = sinon.spy(console, 'error') | ||
}) | ||
afterEach(() => { | ||
spyWarn.restore() | ||
spyError.restore() | ||
}) | ||
|
||
describe('constructor option', () => { | ||
it('should be worked', () => { | ||
const messages = { | ||
en: { | ||
message: { | ||
foo: { | ||
buz: '<p>buz</p>' | ||
}, | ||
bar: [1, '<p>bar</p>'], | ||
buz: 22 | ||
} | ||
}, | ||
ja: { message: '<p>こんにちは</p>' } | ||
} | ||
|
||
// `off` | ||
new VueI18n({ | ||
warnHtmlInMessage: 'off', | ||
messages | ||
}) | ||
assert(spyWarn.callCount === 0) | ||
assert(spyError.callCount === 0) | ||
|
||
// `warn` | ||
new VueI18n({ | ||
warnHtmlInMessage: 'warn', | ||
messages | ||
}) | ||
assert(spyWarn.callCount === 2) | ||
assert(spyError.callCount === 0) | ||
|
||
// `error` | ||
new VueI18n({ | ||
warnHtmlInMessage: 'error', | ||
messages | ||
}) | ||
assert(spyWarn.callCount === 2) | ||
assert(spyError.callCount === 2) | ||
}) | ||
}) | ||
|
||
describe('property', () => { | ||
it('should be worked', () => { | ||
const messages = { | ||
en: { | ||
message: { | ||
foo: { | ||
buz: '<p>buz</p>' | ||
}, | ||
bar: [1, '<p>bar</p>'], | ||
buz: 22 | ||
} | ||
}, | ||
ja: { message: '<p>こんにちは</p>' } | ||
} | ||
|
||
const i18n = new VueI18n({ | ||
warnHtmlInMessage: 'off', | ||
messages | ||
}) | ||
|
||
// `warn` | ||
i18n.warnHtmlInMessage = 'warn' | ||
assert(spyWarn.callCount === 2) | ||
assert(spyError.callCount === 0) | ||
|
||
// `error` | ||
i18n.warnHtmlInMessage = 'error' | ||
assert(spyWarn.callCount === 2) | ||
assert(spyError.callCount === 2) | ||
|
||
// `off` | ||
i18n.warnHtmlInMessage = 'off' | ||
assert(spyWarn.callCount === 2) | ||
assert(spyError.callCount === 2) | ||
}) | ||
}) | ||
|
||
describe('setLocaleMessage', () => { | ||
it('should be worked', () => { | ||
const i18n = new VueI18n({ | ||
warnHtmlInMessage: 'warn', | ||
messages: { | ||
en: {}, | ||
ja: {} | ||
} | ||
}) | ||
|
||
i18n.setLocaleMessage('en', { | ||
hello: '<p>hello</p>' | ||
}) | ||
assert(spyWarn.callCount === 1) | ||
assert(spyError.callCount === 0) | ||
|
||
i18n.warnHtmlInMessage = 'error' | ||
i18n.setLocaleMessage('ja', { | ||
hello: '<p>こんにちは</p>' | ||
}) | ||
assert(spyWarn.callCount === 1) | ||
assert(spyError.callCount === 1) | ||
|
||
i18n.warnHtmlInMessage = 'off' | ||
assert(spyWarn.callCount === 1) | ||
assert(spyError.callCount === 1) | ||
}) | ||
}) | ||
|
||
describe('mergeLocaleMessage', () => { | ||
it('should be worked', () => { | ||
const i18n = new VueI18n({ | ||
warnHtmlInMessage: 'warn', | ||
messages: { | ||
en: {}, | ||
ja: {} | ||
} | ||
}) | ||
|
||
i18n.mergeLocaleMessage('en', { | ||
hello: '<p>hello</p>' | ||
}) | ||
assert(spyWarn.callCount === 1) | ||
assert(spyError.callCount === 0) | ||
|
||
i18n.warnHtmlInMessage = 'error' | ||
i18n.mergeLocaleMessage('ja', { | ||
hello: '<p>こんにちは</p>' | ||
}) | ||
assert(spyWarn.callCount === 1) | ||
assert(spyError.callCount === 1) | ||
|
||
i18n.warnHtmlInMessage = 'off' | ||
assert(spyWarn.callCount === 1) | ||
assert(spyError.callCount === 1) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to add followings to give more advidses to users.
HTML
XSS
(technical term)URL could be shortend by https://bitly.com/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot!