8000 warn against reserved element tags as component ids · Hsinwe/vue@a51be5f · GitHub
[go: up one dir, main page]

Skip to content

Commit a51be5f

Browse files
committed
warn against reserved element tags as component ids
1 parent 99bf444 commit a51be5f

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

src/instance/api/global.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
classify,
77
toArray,
88
commonTagRE,
9+
reservedTagRE,
910
warn,
1011
isPlainObject
1112
} from '../../util/index'
@@ -167,9 +168,12 @@ export default function (Vue) {
167168
} else {
168169
/* istanbul ignore if */
169170
if (process.env.NODE_ENV !== 'production') {
170-
if (type === 'component' && commonTagRE.test(id)) {
171+
if (
172+
type === 'component' &&
173+
(commonTagRE.test(id) || reservedTagRE.test(id))
174+
) {
171175
warn(
172-
'Do not use built-in HTML elements as component ' +
176+
'Do not use built-in or reserved HTML elements as component ' +
173177
'id: ' + id
174178
)
175179
}

src/util/component.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getAttr, getBindAttr } from './dom'
44
import { isArray, isPlainObject } from './lang'
55

66
export const commonTagRE = /^(div|p|span|img|a|b|i|br|ul|ol|li|h1|h2|h3|h4|h5|h6|code|pre|table|th|td|tr|form|label|input|select|option|nav|article|section|header|footer)$/
7+
export const reservedTagRE = /^(slot|partial|component)$/
78

89
/**
910
* Check if an element is a component, if yes return its
@@ -17,7 +18,7 @@ export const commonTagRE = /^(div|p|span|img|a|b|i|br|ul|ol|li|h1|h2|h3|h4|h5|h6
1718
export function checkComponentAttr (el, options) {
1819
var tag = el.tagName.toLowerCase()
1920
var hasAttrs = el.hasAttributes()
20-
if (!commonTagRE.test(tag) && tag !== 'component') {
21+
if (!commonTagRE.test(tag) && !reservedTagRE.test(tag)) {
2122
if (resolveAsset(options, 'components', tag)) {
2223
return { id: tag }
2324
} else {

src/util/options.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
camelize
1111
} from './lang'
1212
import { warn } from './debug'
13-
import { commonTagRE } from './component'
13+
import { commonTagRE, reservedTagRE } from './component'
1414

1515
/**
1616
* Option overwriting strategies are functions that handle
@@ -233,9 +233,9 @@ function guardComponents (options) {
233233
var ids = Object.keys(components)
234234
for (var i = 0, l = ids.length; i < l; i++) {
235235
var key = ids[i]
236-
if (commonTagRE.test(key)) {
236+
if (commonTagRE.test(key) || reservedTagRE.test(key)) {
237237
process.env.NODE_ENV !== 'production' && warn(
238-
'Do not use built-in HTML elements as component ' +
238+
'Do not use built-in or reserved HTML elements as component ' +
239239
'id: ' + key
240240
)
241241
continue

test/unit/specs/util/options_spec.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,15 @@ describe('Util - Option merging', function () {
161161
a: { template: 'hi' }
162162
}
163163
})
164-
expect(hasWarned('Do not use built-in HTML elements')).toBe(true)
164+
expect(hasWarned('Do not use built-in or reserved HTML elements as component id: a')).toBe(true)
165+
merge({
166+
components: null
167+
}, {
168+
components: {
169+
slot: { template: 'hi' }
170+
}
171+
})
172+
expect(hasWarned('Do not use built-in or reserved HTML elements as component id: slot')).toBe(true)
165173
})
166174

167175
it('should ignore non-function el & data in class merge', function () {

0 commit comments

Comments
 (0)
0