This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
html_helper #16
Draft
BenKandelaars
wants to merge
5
commits into
master
Choose a base branch
from
html_helper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
html_helper #16
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| const wrapInArray = require('../lib/wrapInArray') | ||
|
|
||
| module.exports = function createElement ({ | ||
| type, | ||
| id, | ||
| classList, | ||
| onClick, | ||
| innerHTML | ||
| }) { | ||
| const element = document.createElement(type) | ||
|
|
||
| if (id) element.setAttribute('id', id) | ||
| if (classList) { | ||
| for (const className of wrapInArray(classList)) { | ||
| element.classList.add(className) | ||
| } | ||
| } | ||
| if (onClick) element.onclick = onClick | ||
| if (innerHTML) element.innerHTML = innerHTML | ||
|
|
||
| return element | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module.exports = function wrapInArray (value) { | ||
| return !Array.isArray(value) ? [value] : value | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* globals describe it */ | ||
| const createElement = require('../../html/createElement') | ||
| const { expect } = require('chai') | ||
|
|
||
| describe('HTML element create helpers', function () { | ||
| describe('createElement function creates a HTML element matching config object', function () { | ||
| ;[['div', 'DIV'], ['a', 'A'], ['p', 'P'], ['h1', 'H1']].forEach( | ||
| ([request, expected]) => { | ||
| const title = `creates requested ${request} HTML element` | ||
| it(title, function () { | ||
| const element = createElement({ | ||
| type: request | ||
| }) | ||
| expect(element.nodeName).equal(expected) | ||
| }) | ||
| } | ||
| ) | ||
|
|
||
| it('appends an id to the element', function () { | ||
| const ID = 'ajfoij2902cfv' | ||
| const element = createElement({ | ||
| type: 'div', | ||
| id: ID | ||
| }) | ||
| expect(element.id).equal(ID) | ||
| }) | ||
|
|
||
| it('appends a single class to an element', function () { | ||
| const CLASS_EXAMPLE = 'qp-badge' | ||
| const element = createElement({ | ||
| type: 'div', | ||
| classList: CLASS_EXAMPLE | ||
| }) | ||
| expect(element.classList.contains(CLASS_EXAMPLE)).to.equal(true) | ||
| }) | ||
|
|
||
| it('appends an array of classes to an element', function () { | ||
| const CLASS_EXAMPLE = ['qp-badge', 'qp-wrapper'] | ||
| const element = createElement({ | ||
| type: 'div', | ||
| classList: CLASS_EXAMPLE | ||
| }) | ||
| expect(element.classList.contains(CLASS_EXAMPLE[0])).to.equal(true) | ||
| expect(element.classList.contains(CLASS_EXAMPLE[1])).to.equal(true) | ||
| }) | ||
|
|
||
| it('appends an onclick handler to the element', function () { | ||
| const onClick = function () { | ||
| return 'ALPHA' | ||
| } | ||
| const element = createElement({ | ||
| type: 'div', | ||
| onClick | ||
| }) | ||
| expect(element.onclick).equal(onClick) | ||
| expect(element.onclick()).equal(onClick()) | ||
| }) | ||
|
|
||
| it('adds innerHTML to the element', function () { | ||
| const text = 'the cat jumped over the moon' | ||
| const textNode = document.createTextNode(text) | ||
| const div = document.createElement('p') | ||
| div.appendChild(textNode) | ||
| const innerHTML = '<p>the cat jumped over the moon</p>' | ||
| const element = createElement({ | ||
| type: 'div', | ||
| innerHTML: div.outerHTML | ||
| }) | ||
| expect(element.innerHTML).equal(innerHTML) | ||
| expect(element.children[0].nodeName).equal('P') | ||
| }) | ||
| }) | ||
| }) |
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| require('./html/createElement') | ||
| require('./dom/dom') | ||
| require('./lib/once') | ||
| require('./lib/promised') | ||
| require('./lib/withRestoreAll') | ||
| require('./lib/wrapInArray.js') |
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
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* globals describe it */ | ||
| const { expect } = require('chai') | ||
| const wrapInArray = require('../../lib/wrapInArray') | ||
|
|
||
| describe('wrapInArray', function () { | ||
| it('should wrap value in array', function () { | ||
| const wrapped = wrapInArray(5) | ||
| expect(wrapped).to.eql([5]) | ||
| }) | ||
|
|
||
| it('should wrap once', function () { | ||
| const wrapped = wrapInArray([42]) | ||
| expect(wrapped).to.eql([42]) | ||
| }) | ||
| }) |
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.
@qubit/htmlwould be a different package right?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 was wondering if it should be part of utils because it is directly related to html element creation
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.
Right but the name
@qubit/htmlimplies a separate package, did you mean@qubit/utils/html?