Skip to content

Commit

Permalink
fix: multiple define event crash in some browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Dec 6, 2017
1 parent 3997aca commit 28cf266
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
["env", {
"spec": true,
"useBuiltIns": false
}], ["stage-0"]
}], ["stage-0"], ["es3"]
],
"plugins": [
[
Expand Down
8 changes: 1 addition & 7 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ module.exports = function (config) {
'./node_modules/es5-polyfill/dist/polyfill.js',
'browsers/ie8.js',
'browsers/polyfill.js',
'packages/nerv/__tests__/event.spec.js',
'packages/*/__tests__/**/*spec.js?(x)'
],

Expand Down Expand Up @@ -171,7 +170,7 @@ module.exports = function (config) {

// Concurrency level
// how many browser should be started simultaneous
concurrency: 2,
concurrency: 1,

webpack: {
devtool: 'inline-source-map',
Expand Down Expand Up @@ -205,11 +204,6 @@ module.exports = function (config) {
module: 'commonjs'
}
}
},
{
enforce: 'post',
test: /.js$/,
loader: 'es3ify-loader'
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"rollup-plugin-uglify": "^2.0.1",
"sinon": "^2.3.8",
"ts-jest": "^21.1.2",
"ts-loader": "^2.3.4",
"ts-loader": "^3.2.0",
"tslint": "^5.7.0",
"typescript": "^2.4.2",
"webpack": "^3.7.1"
Expand Down
31 changes: 28 additions & 3 deletions packages/nerv/__tests__/event.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,52 @@ describe('Events', () => {
rerender()
scratch.childNodes[0].focus()
await nextTick()
// @TODO: IMPORTANT
expect(removeEventListenerSpy.called).toBeTruthy()
})

// @TODO
it('should change/fix event name', () => {
it('should change/fix onchange event name', () => {
const container = document.createElement('div')
document.body.appendChild(container)
const onchange = function () {}
const proto = document.constructor.prototype
const addEventListenerSpy = sinon.spy(proto, 'addEventListener')
// const ondbclick = function () {}
// const ontouchtap = function () {}
class Outer extends Component {
render () {
return (
<div>
<input onChange={onchange} />
<button />
</div>
)
}
}
const app = <Outer />
render(app, container)
expect(addEventListenerSpy.called).toBe(true)
})

it('should change/fix onDoubleClick/onTouchTap event name', () => {
const container = document.createElement('div')
document.body.appendChild(container)
const proto = document.constructor.prototype
const addEventListenerSpy = sinon.spy(proto, 'addEventListener')
const ondbclick = function () {}
const ontouchtap = function () {}
class Outer extends Component {
render () {
return (
<div>
<button onDoubleClick={ondbclick} />
<button onTouchTap={ontouchtap} />
</div>
)
}
}
const app = <Outer />
render(app, container)
expect(addEventListenerSpy.called).toBe(true)
expect(addEventListenerSpy.callCount).toBe(2)
})
})
27 changes: 16 additions & 11 deletions packages/nerv/src/hooks/event-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function fixEvent (node: Element, eventName: string) {
eventName = 'ondblclick'
} else if (eventName === 'onTouchTap') {
eventName = 'onclick'
// tslint:disable-next-line:prefer-conditional-expression
// tslint:disable-next-line:prefer-conditional-expression
} else if (eventName === 'onChange' && detectCanUseOnInputNode(node)) {
eventName = ONINPUT in window ? ONINPUT : ONPROPERTYCHANGE
} else {
Expand Down Expand Up @@ -281,17 +281,22 @@ function attachEventToDocument (d, eventName, delegatedRoots) {
currentTarget: event.target
}
/* istanbul ignore next */
Object.defineProperties(event, {
currentTarget: {
configurable: true,
get () {
return eventData.currentTarget
try {
Object.defineProperties(event, {
currentTarget: {
configurable: true,
get () {
return eventData.currentTarget
}
},
stopPropagation: {
value: stopPropagation
}
},
stopPropagation: {
value: stopPropagation
}
})
})
} catch (error) {
// some browsers crashed
// see: https://stackoverflow.com/questions/44052813/why-cannot-redefine-property
}
dispatchEvent(event, event.target, delegatedRoots.items, count, eventData)
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/nerv/src/vdom/create-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ function mountChild (
}
}

function setProps (domNode: Element, vnode, isSvg) {
function setProps (domNode: Element, vnode: VNode, isSvg) {
const props = vnode.props
// set type property first for input element
if ('type' in props && domNode.tagName === 'INPUT') {
if ('type' in props && vnode.type === 'input') {
domNode.setAttribute('type', props['type'])
}
for (const p in props) {
Expand Down
16 changes: 12 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,14 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0:
escape-string-regexp "^1.0.5"
supports-color "^4.0.0"

chalk@^2.3.0:
version "2.3.0"
resolved "https://registry.npmjs.org/chalk/-/chalk-2.3.0.tgz#b5ea48efc9c1793dccc9b4767c93914d3f2d52ba"
dependencies:
ansi-styles "^3.1.0"
escape-string-regexp "^1.0.5"
supports-color "^4.0.0"

chokidar@^1.4.1, chokidar@^1.7.0:
version "1.7.0"
resolved "https://registry.npmjs.org/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468"
Expand Down Expand Up @@ -6547,11 +6555,11 @@ ts-jest@^21.1.2:
source-map-support "^0.5.0"
yargs "^9.0.1"

ts-loader@^2.3.4:
version "2.3.4"
resolved "https://registry.npmjs.org/ts-loader/-/ts-loader-2.3.4.tgz#904f82f6812406f3f073c1c114eea2759e27f80a"
ts-loader@^3.2.0:
version "3.2.0"
resolved "https://registry.npmjs.org/ts-loader/-/ts-loader-3.2.0.tgz#23211922179b81f7448754b7fdfca45b8374a15a"
dependencies:
chalk "^2.0.1"
chalk "^2.3.0"
enhanced-resolve "^3.0.0"
loader-utils "^1.0.2"
semver "^5.0.1"
Expand Down

0 comments on commit 28cf266

Please sign in to comment.