Skip to content

feat(compiler): allow unicode characters for component name as described in #8564 #8666

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

Merged
merged 7 commits into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/compiler/parser/html-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@

import { makeMap, no } from 'shared/util'
import { isNonPhrasingTag } from 'web/compiler/util'
import { unicodeLetters } from '../../core/util/lang'

// Regular Expressions for parsing tags and attributes
const attribute = /^\s*([^\s"'<>\/=]+)(?:\s*(=)\s*(?:"([^"]*)"+|'([^']*)'+|([^\s"'=<>`]+)))?/
// use https://www.w3.org/TR/html53/semantics-scripting.html#potentialcustomelementname
// except \u10000-\uEFFFF because of performance problem
export const pcenchars = '[\\-\\.0-9_a-zA-Z\\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]'
const ncname = `[a-zA-Z_]${pcenchars}*`
const ncname = `[a-zA-Z_][\\-\\.0-9_a-zA-Z${unicodeLetters}]*`

Choose a reason for hiding this comment

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

'a-zA-Z' is repeat in unicodeLetters

const qnameCapture = `((?:${ncname}\\:)?${ncname})`
const startTagOpen = new RegExp(`^<${qnameCapture}`)
const startTagClose = /^\s*(\/?)>/
Expand Down
9 changes: 8 additions & 1 deletion src/core/util/lang.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/* @flow */

/**
* unicode letters used for parsing html tags, component names and property paths.
* use https://www.w3.org/TR/html53/semantics-scripting.html#potentialcustomelementname
* except \u10000-\uEFFFF because of performance problem
Copy link
Member

Choose a reason for hiding this comment

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

I made a benchmark for this and it turns out that in most modern browsers except Safari, it runs fastest if we include \u10000-\uEFFFF:

https://jsperf.com/unicode-regex-test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, in my computer, npm test failed because of timeout when including \u10000-\uEFFFF. It passed without the extra characters. I don't know why the performance difference exists, yet.

*/
export const unicodeLetters = 'a-zA-Z\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD'

/**
* Check if a string starts with $ or _
*/
Expand All @@ -23,7 +30,7 @@ export function def (obj: Object, key: string, val: any, enumerable?: boolean) {
/**
* Parse simple path.
*/
const bailRE = /[^\w.$]/
const bailRE = new RegExp(`[^${unicodeLetters}.$]`)
export function parsePath (path: string): any {
if (bailRE.test(path)) {
return
Expand Down
4 changes: 2 additions & 2 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import config from '../config'
import { warn } from './debug'
import { nativeWatch } from './env'
import { set } from '../observer/index'
import { pcenchars } from '../../compiler/parser/html-parser'
import { unicodeLetters } from '../../core/util/lang'

import {
ASSET_TYPES,
Expand Down Expand Up @@ -254,7 +254,7 @@ function checkComponents (options: Object) {
}

export function validateComponentName (name: string) {
if (!new RegExp(`^[a-zA-Z]${pcenchars}*$`).test(name)) {
if (!new RegExp(`^[a-zA-Z][\\-\\.0-9_a-zA-Z${unicodeLetters}]*$`).test(name)) {
warn(
'Invalid component name: "' + name + '". Component names ' +
'should conform to valid custom element name in html5 specification.'
Expand Down
12 changes: 12 additions & 0 deletions test/unit/features/instance/methods-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ describe('Instance methods data', () => {
data: {
a: {
b: 1
},
유니코드: {
なまえ: 'ok'
}
},
methods: {
Expand Down Expand Up @@ -108,6 +111,15 @@ describe('Instance methods data', () => {
expect(spy).toHaveBeenCalledWith(1)
})

it('handler option in string', () => {
vm.$watch('유니코드.なまえ', {
handler: 'foo',
immediate: true
})
expect(spy.calls.count()).toBe(1)
expect(spy).toHaveBeenCalledWith('ok')
})

it('warn expression', () => {
vm.$watch('a + b', spy)
expect('Watcher only accepts simple dot-delimited paths').toHaveBeenWarned()
Expand Down