-
Notifications
You must be signed in to change notification settings - Fork 668
Improve passing text to slots #274
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
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e397ef2
Improve passing text to slots
38elements 76f14f6
Add test
38elements 99c0f7a
Add document
38elements dec437c
Add throw error
38elements 912fbe1
Add link
38elements 3bd9da6
Skip
38elements 3c3abf8
Lint
38elements c3750a8
Fix document
38elements 66ecd96
Fix link
38elements 3b99090
Fix update()
38elements a07c622
Improve
38elements 192c5ac
Fix error message
38elements 2302443
Update test
38elements d2a5d65
Fix error message
38elements 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
// @flow | ||
|
||
import Vue from 'vue' | ||
import { compileToFunctions } from 'vue-template-compiler' | ||
import { throwError } from './util' | ||
|
||
|
@@ -10,19 +9,21 @@ 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] === '<') { | ||
const domParser = new window.DOMParser() | ||
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 [email protected]+') | ||
} | ||
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) | ||
|
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -2,7 +2,6 @@ import { compileToFunctions } from 'vue-template-compiler' | |
import { mount } from '~vue-test-utils' | ||
import Component from '~resources/components/component.vue' | ||
import ComponentWithSlots from '~resources/components/component-with-slots.vue' | ||
import { vueVersion } from '~resources/test-utils' | ||
|
||
describe('mount.slots', () => { | ||
it('mounts component with default slot if passed component in slot object', () => { | ||
|
@@ -27,14 +26,16 @@ describe('mount.slots', () => { | |
}) | ||
|
||
it('mounts component with default slot if passed string in slot object', () => { | ||
if (vueVersion >= 2.2) { | ||
const wrapper = mount(ComponentWithSlots, { slots: { default: 'foo' }}) | ||
expect(wrapper.find('main').text()).to.equal('foo') | ||
} else { | ||
const message = '[vue-test-utils]: vue-test-utils support for passing text to slots at [email protected]+' | ||
const fn = () => mount(ComponentWithSlots, { slots: { default: 'foo' }}) | ||
expect(fn).to.throw().with.property('message', message) | ||
} | ||
const wrapper1 = mount(ComponentWithSlots, { slots: { default: 'foo<span>123</span>{{ foo }}' }}) | ||
expect(wrapper1.find('main').html()).to.equal('<main>foo<span>123</span>bar</main>') | ||
const wrapper2 = mount(ComponentWithSlots, { slots: { default: '<p>1</p>{{ foo }}2' }}) | ||
expect(wrapper2.find('main').html()).to.equal('<main><p>1</p>bar2</main>') | ||
const wrapper3 = mount(ComponentWithSlots, { slots: { default: '<p>1</p>{{ foo }}<p>2</p>' }}) | ||
expect(wrapper3.find('main').html()).to.equal('<main><p>1</p>bar<p>2</p></main>') | ||
const wrapper4 = mount(ComponentWithSlots, { slots: { default: '123' }}) | ||
expect(wrapper4.find('main').html()).to.equal('<main>123</main>') | ||
const wrapper5 = mount(ComponentWithSlots, { slots: { default: '1{{ foo }}2' }}) | ||
expect(wrapper5.find('main').html()).to.equal('<main>1bar2</main>') | ||
}) | ||
|
||
it('throws error if passed string in default slot object and vue-template-compiler is undefined', () => { | ||
|
@@ -59,14 +60,8 @@ describe('mount.slots', () => { | |
}) | ||
|
||
it('mounts component with default slot if passed string in slot text array object', () => { | ||
if (vueVersion >= 2.2) { | ||
const wrapper = mount(ComponentWithSlots, { slots: { default: ['foo', 'bar'] }}) | ||
expect(wrapper.find('main').text()).to.equal('foobar') | ||
} else { | ||
const message = '[vue-test-utils]: vue-test-utils support for passing text to slots at [email protected]+' | ||
const fn = () => mount(ComponentWithSlots, { slots: { default: ['foo', 'bar'] }}) | ||
expect(fn).to.throw().with.property('message', message) | ||
} | ||
const wrapper = mount(ComponentWithSlots, { slots: { default: ['{{ foo }}<span>1</span>', 'bar'] }}) | ||
expect(wrapper.find('main').html()).to.equal('<main>bar<span>1</span>bar</main>') | ||
}) | ||
|
||
it('throws error if passed string in default slot array vue-template-compiler is undefined', () => { | ||
|
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reviewing.
Is there a choice not to support PhantomJS ?
IMHO, I think it is better to use Puppeteer.
It is better to introduce Puppeteer at document.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reply.
I will look for other ways.
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.
Can you change the error message to—
option.slots does not support strings PhantomJS. Please use Puppeteer, or pass a component
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 am sorry.
I did not notice your comment.
I fixed the error message.