Skip to content

Commit

Permalink
Allow HTML elements to be attached to DOM (#28)
Browse files Browse the repository at this point in the history
* Allow HTML elements to be attached to DOM
  • Loading branch information
michaeltintiuc committed Sep 5, 2018
1 parent b51065b commit 44bad3c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
18 changes: 18 additions & 0 deletions src/framework-delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ export default class Delegate {

// Attach the passed Vue component to DOM
attachViewToDom(parentElement, component, opts, classes) {
// Handle HTML elements
if (isElement(component)) {
// Add any classes to the element
addClasses(component, classes)

// Append the element to DOM
parentElement.appendChild(component)
return Promise.resolve(component)
}

// Get the Vue controller
return this.vueController(component).then(controller => {
const vueComponent = this.vueComponent(controller, opts)
Expand Down Expand Up @@ -43,12 +53,20 @@ export default class Delegate {
}
}

// Check Symbol support
const hasSymbol = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'

// Check if object is an ES module
function isESModule(obj) {
return obj.__esModule || (hasSymbol && obj[Symbol.toStringTag] === 'Module')
}

// Check if value is an Element
function isElement(el) {
return typeof Element !== 'undefined' && el instanceof Element
}

// Add an array of classes to an element
function addClasses(element, classes = []) {
for (const cls of classes) {
element.classList.add(cls)
Expand Down
9 changes: 0 additions & 9 deletions test/framework-delegate.node.spec.js

This file was deleted.

9 changes: 9 additions & 0 deletions test/framework-delegate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ describe('Framework delegation', () => {
})
})

it('Attaches HTML elements to DOM', () => {
expect.assertions(1)
const element = document.createElement('p')

return delegate.attachViewToDom(app, element, null, ['foo']).then(el => {
return expect(el.classList.contains('foo')).toBeTruthy()
})
})

it('Removes from DOM', () => {
expect.assertions(2)
const div = document.querySelector('p')
Expand Down

0 comments on commit 44bad3c

Please sign in to comment.