-
Notifications
You must be signed in to change notification settings - Fork 668
Refactor: refactor add-slots.js #556
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 all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,43 @@ | ||
// @flow | ||
|
||
import { compileToFunctions } from 'vue-template-compiler' | ||
import { throwError } from 'shared/util' | ||
import { validateSlots } from './validate-slots' | ||
import { toArray } from 'shared/util' | ||
|
||
function addSlotToVm (vm: Component, slotName: string, slotValue: Component | string | Array<Component> | Array<string>): void { | ||
let elem | ||
if (typeof slotValue === 'string') { | ||
if (!compileToFunctions) { | ||
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined') | ||
} | ||
if (typeof window === 'undefined') { | ||
throwError('the slots string option does not support strings in server-test-uitls.') | ||
} | ||
if (window.navigator.userAgent.match(/PhantomJS/i)) { | ||
throwError('the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.') | ||
} | ||
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 { | ||
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) | ||
function isSingleHTMLTag (template: string) { | ||
if (!template.startsWith('<') || !template.endsWith('>')) { | ||
return false | ||
} | ||
if (Array.isArray(elem)) { | ||
if (Array.isArray(vm.$slots[slotName])) { | ||
vm.$slots[slotName] = [...vm.$slots[slotName], ...elem] | ||
const _document = new window.DOMParser().parseFromString(template, 'text/html') | ||
return _document.body.childElementCount === 1 | ||
} | ||
|
||
function createElementFromAdvancedString (slotValue, vm) { | ||
const compiledResult = compileToFunctions(`<div>${slotValue}{{ }}</div>`) | ||
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns | ||
vm._renderProxy.$options.staticRenderFns = compiledResult.staticRenderFns | ||
const elem = compiledResult.render.call(vm._renderProxy, vm.$createElement).children | ||
vm._renderProxy.$options.staticRenderFns = _staticRenderFns | ||
return elem | ||
} | ||
|
||
function createElement (slotValue: string | Object, vm) { | ||
if (typeof slotValue === 'string') { | ||
slotValue = slotValue.trim() | ||
if (isSingleHTMLTag(slotValue)) { | ||
return vm.$createElement(compileToFunctions(slotValue)) | ||
} else { | ||
vm.$slots[slotName] = [...elem] | ||
return createElementFromAdvancedString(slotValue, vm) | ||
} | ||
} else { | ||
if (Array.isArray(vm.$slots[slotName])) { | ||
vm.$slots[slotName].push(elem) | ||
} else { | ||
vm.$slots[slotName] = [elem] | ||
} | ||
return vm.$createElement(slotValue) | ||
} | ||
} | ||
|
||
export function addSlots (vm: Component, slots: Object): void { | ||
validateSlots(slots) | ||
Object.keys(slots).forEach((key) => { | ||
if (Array.isArray(slots[key])) { | ||
slots[key].forEach((slotValue) => { | ||
addSlotToVm(vm, key, slotValue) | ||
}) | ||
} else { | ||
addSlotToVm(vm, key, slots[key]) | ||
} | ||
Object.keys(slots).forEach(name => { | ||
vm.$slots[name] = toArray(slots[name]) | ||
.map(slotValue => createElement(slotValue, vm)) | ||
}) | ||
} |
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,23 +1,26 @@ | ||
// @flow | ||
|
||
import { throwError } from 'shared/util' | ||
|
||
function isValidSlot (slot: any): boolean { | ||
return Array.isArray(slot) || (slot !== null && typeof slot === 'object') || typeof slot === 'string' | ||
} | ||
import { throwError, toArray, isObject } from 'shared/util' | ||
import { compileToFunctions } from 'vue-template-compiler' | ||
|
||
export function validateSlots (slots: Object): void { | ||
slots && Object.keys(slots).forEach((key) => { | ||
if (!isValidSlot(slots[key])) { | ||
throwError('slots[key] must be a Component, string or an array of Components') | ||
} | ||
Object.keys(slots).forEach(key => { | ||
toArray(slots[key]).forEach(slotValue => { | ||
if (!isObject(slotValue) && typeof slotValue !== 'string') { | ||
throwError('slots[key] must be a Component, string or an array of Components') | ||
} | ||
|
||
if (Array.isArray(slots[key])) { | ||
slots[key].forEach((slotValue) => { | ||
if (!isValidSlot(slotValue)) { | ||
throwError('slots[key] must be a Component, string or an array of Components') | ||
if (typeof slotValue === 'string') { | ||
if (!compileToFunctions) { | ||
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined') | ||
} | ||
if (typeof window === 'undefined') { | ||
throwError('the slots string option does not support strings in server-test-uitls.') | ||
} | ||
if (window.navigator.userAgent.match(/PhantomJS/i)) { | ||
throwError('the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.') | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
}) | ||
} |
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 |
---|---|---|
|
@@ -21,3 +21,11 @@ export const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.sli | |
*/ | ||
const hyphenateRE = /\B([A-Z])/g | ||
export const hyphenate = (str: string) => str.replace(hyphenateRE, '-$1').toLowerCase() | ||
|
||
export function toArray (value: any) { | ||
return Array.isArray(value) ? value : [value] | ||
} | ||
|
||
export function isObject (obj: mixed): boolean %checks { | ||
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. This method is copied from Vue |
||
return obj !== null && typeof obj === 'object' | ||
} |
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.
I removed this 'replacing' of
staticRenderFns
and didn't experienced unit-test failures - is it ok to remove, or I miss something ?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 don't think it's OK to remove, but we should add some tests to cover it. Can you write a test to check that a static slot is added correctly.
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.
Thanks @eddyerburgh for information - I'll add appropriate tests for that
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 moved back
staticRenderFns
related code in pull request. To be honest I don't know what is static slot - I'm not yet familiar with everything related with Vue.