-
Notifications
You must be signed in to change notification settings - Fork 667
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
Improve passing text to slots #274
Changes from 13 commits
e397ef2
76f14f6
99c0f7a
dec437c
912fbe1
3bd9da6
3c3abf8
c3750a8
66ecd96
3b99090
a07c622
192c5ac
2302443
d2a5d65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
// @flow | ||
|
||
import Vue from 'vue' | ||
import { compileToFunctions } from 'vue-template-compiler' | ||
import { throwError } from './util' | ||
|
||
|
@@ -10,27 +9,40 @@ function isValidSlot (slot: any): boolean { | |
|
||
function addSlotToVm (vm: Component, slotName: string, slotValue: Component | string | Array<Component> | Array<string>): void { | ||
let elem | ||
const vueVersion = Number(`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`) | ||
if (typeof slotValue === 'string') { | ||
if (!compileToFunctions) { | ||
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined') | ||
} | ||
if (slotValue.trim()[0] === '<') { | ||
if (window.navigator.userAgent.match(/PhantomJS/i)) { | ||
throwError('option.slots does not support strings PhantomJS. Please use Puppeteer, or pass a component') | ||
} | ||
const domParser = new window.DOMParser() | ||
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. DOMParser isn't supported in phantomJs, so we need an alternative that will work in phantom 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. Thank you for reviewing. 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. A lot of users still use phantom. We could throw an error, or a warning that phantom doesn't support markup in slots, and handle it gracefully 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. Thank you for reply. 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. Can you change the error message to— 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 am sorry. |
||
const document = domParser.parseFromString(slotValue, 'text/html') | ||
const _slotValue = slotValue.trim() | ||
if (_slotValue[0] === '<' && _slotValue[_slotValue.length - 1] === '>' && document.body.childElementCount === 1) { | ||
elem = vm.$createElement(compileToFunctions(slotValue)) | ||
} else { | ||
if (vueVersion >= 2.2) { | ||
elem = vm._v(slotValue) | ||
} else { | ||
throwError('vue-test-utils support for passing text to slots at vue@2.2+') | ||
} | ||
const compiledResult = compileToFunctions(`<div>${slotValue}{{ }}</div>`) | ||
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns | ||
vm._renderProxy.$options.staticRenderFns = compiledResult.staticRenderFns | ||
elem = compiledResult.render.call(vm._renderProxy, vm.$createElement).children | ||
vm._renderProxy.$options.staticRenderFns = _staticRenderFns | ||
} | ||
} else { | ||
elem = vm.$createElement(slotValue) | ||
} | ||
if (Array.isArray(vm.$slots[slotName])) { | ||
vm.$slots[slotName].push(elem) | ||
if (Array.isArray(elem)) { | ||
if (Array.isArray(vm.$slots[slotName])) { | ||
vm.$slots[slotName] = [...vm.$slots[slotName], ...elem] | ||
} else { | ||
vm.$slots[slotName] = [...elem] | ||
} | ||
} else { | ||
vm.$slots[slotName] = [elem] | ||
if (Array.isArray(vm.$slots[slotName])) { | ||
vm.$slots[slotName].push(elem) | ||
} else { | ||
vm.$slots[slotName] = [elem] | ||
} | ||
} | ||
} | ||
|
||
|
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.
to
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 changed it.