Skip to content
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

Refactor: refactor add-slots.js #556

Merged
merged 1 commit into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 27 additions & 44 deletions packages/create-instance/add-slots.js
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
Copy link
Contributor Author

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 ?

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

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))
})
}
33 changes: 18 additions & 15 deletions packages/create-instance/validate-slots.js
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.')
}
})
}
}
})
})
}
8 changes: 8 additions & 0 deletions packages/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is copied from Vue

return obj !== null && typeof obj === 'object'
}