Skip to content

Commit 44bad3c

Browse files
Allow HTML elements to be attached to DOM (#28)
* Allow HTML elements to be attached to DOM
1 parent b51065b commit 44bad3c

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/framework-delegate.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ export default class Delegate {
55

66
// Attach the passed Vue component to DOM
77
attachViewToDom(parentElement, component, opts, classes) {
8+
// Handle HTML elements
9+
if (isElement(component)) {
10+
// Add any classes to the element
11+
addClasses(component, classes)
12+
13+
// Append the element to DOM
14+
parentElement.appendChild(component)
15+
return Promise.resolve(component)
16+
}
17+
818
// Get the Vue controller
919
return this.vueController(component).then(controller => {
1020
const vueComponent = this.vueComponent(controller, opts)
@@ -43,12 +53,20 @@ export default class Delegate {
4353
}
4454
}
4555

56+
// Check Symbol support
4657
const hasSymbol = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'
4758

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

64+
// Check if value is an Element
65+
function isElement(el) {
66+
return typeof Element !== 'undefined' && el instanceof Element
67+
}
68+
69+
// Add an array of classes to an element
5270
function addClasses(element, classes = []) {
5371
for (const cls of classes) {
5472
element.classList.add(cls)

test/framework-delegate.node.spec.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/framework-delegate.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ describe('Framework delegation', () => {
5252
})
5353
})
5454

55+
it('Attaches HTML elements to DOM', () => {
56+
expect.assertions(1)
57+
const element = document.createElement('p')
58+
59+
return delegate.attachViewToDom(app, element, null, ['foo']).then(el => {
60+
return expect(el.classList.contains('foo')).toBeTruthy()
61+
})
62+
})
63+
5564
it('Removes from DOM', () => {
5665
expect.assertions(2)
5766
const div = document.querySelector('p')

0 commit comments

Comments
 (0)