Skip to content

Commit

Permalink
Make a template factory helper to handle all template cases
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Jul 24, 2021
1 parent 2bdbb42 commit 0303338
Show file tree
Hide file tree
Showing 6 changed files with 490 additions and 98 deletions.
6 changes: 3 additions & 3 deletions .bundlewatch.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@
},
{
"path": "./dist/js/bootstrap.bundle.js",
"maxSize": "41.5 kB"
"maxSize": "42 kB"
},
{
"path": "./dist/js/bootstrap.bundle.min.js",
"maxSize": "22.25 kB"
},
{
"path": "./dist/js/bootstrap.esm.js",
"maxSize": "27 kB"
"maxSize": "27.5 kB"
},
{
"path": "./dist/js/bootstrap.esm.min.js",
"maxSize": "18.25 kB"
},
{
"path": "./dist/js/bootstrap.js",
"maxSize": "27.5 kB"
"maxSize": "28 kB"
},
{
"path": "./dist/js/bootstrap.min.js",
Expand Down
12 changes: 6 additions & 6 deletions js/src/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ class Popover extends Tooltip {
return this.getTitle() || this._getContent()
}

setContent() {
const tip = this.getTipElement()
// Private

this._sanitizeAndSetContent(tip, this.getTitle(), SELECTOR_TITLE)
this._sanitizeAndSetContent(tip, this._getContent(), SELECTOR_CONTENT)
_getContentForTemplate() {
return {
[SELECTOR_TITLE]: this.getTitle(),
[SELECTOR_CONTENT]: this._getContent()
}
}

// Private

_getContent() {
return this._resolvePossibleFunction(this._config.content)
}
Expand Down
90 changes: 29 additions & 61 deletions js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ import {
findShadowRoot,
getElement,
getUID,
isElement,
isRTL,
noop,
typeCheckConfig
} from './util/index'
import { DefaultAllowlist, sanitizeHtml } from './util/sanitizer'
import { DefaultAllowlist } from './util/sanitizer'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine'
import BaseComponent from './base-component'
import TemplateFactory from './util/template-factory'

/**
* ------------------------------------------------------------------------
Expand Down Expand Up @@ -134,6 +133,7 @@ class Tooltip extends BaseComponent {
this._hoverState = ''
this._activeTrigger = {}
this._popper = null
this._templateFactory = null

// Protected
this._config = this._getConfig(config)
Expand Down Expand Up @@ -240,8 +240,6 @@ class Tooltip extends BaseComponent {
tip.setAttribute('id', tipId)
this._element.setAttribute('aria-describedby', tipId)

this.setContent()

if (this._config.animation) {
tip.classList.add(CLASS_NAME_FADE)
}
Expand Down Expand Up @@ -269,11 +267,6 @@ class Tooltip extends BaseComponent {

tip.classList.add(CLASS_NAME_SHOW)

const customClass = this._resolvePossibleFunction(this._config.customClass)
if (customClass) {
tip.classList.add(...customClass.split(' '))
}

// If this is a touch-enabled device we add extra
// empty mouseover listeners to the body's immediate children;
// only needed because of broken event delegation on iOS
Expand Down Expand Up @@ -359,66 +352,46 @@ class Tooltip extends BaseComponent {
return Boolean(this.getTitle())
}

setContent(content) {
this.tip = this._getTemplateFactory(content).toHtml()
}

getTipElement() {
if (this.tip) {
return this.tip
}

const element = document.createElement('div')
element.innerHTML = this._config.template
const templateFactory = this._getTemplateFactory(this._getContentForTemplate())

const tip = element.children[0]
const tip = templateFactory.toHtml()
tip.classList.remove(CLASS_NAME_FADE, CLASS_NAME_SHOW)

this.tip = tip
return this.tip
}

setContent() {
const tip = this.getTipElement()
this._sanitizeAndSetContent(tip, this.getTitle(), SELECTOR_TOOLTIP_INNER)
}

_sanitizeAndSetContent(template, content, selector) {
const templateElement = SelectorEngine.findOne(selector, template)
if (!content) {
templateElement.remove()
return
_getTemplateFactory(content) {
// content = { selector : text }
if (this._templateFactory) {
this._templateFactory.changeContent(content)
} else {
this._templateFactory = new TemplateFactory({
extraClass: this._resolvePossibleFunction(this._config.customClass),
template: this._config.template,
content,
html: this._config.html,
sanitize: this._config.sanitize,
sanitizeFn: this._config.sanitizeFn,
allowList: this._config.allowList
})
}

// we use append for html objects to maintain js events
this.setElementContent(templateElement, content)
return this._templateFactory
}

setElementContent(element, content) {
if (element === null) {
return
}

if (isElement(content)) {
content = getElement(content)

// content is a DOM node or a jQuery
if (this._config.html) {
if (content.parentNode !== element) {
element.innerHTML = ''
element.appendChild(content)
}
} else {
element.textContent = content.textContent
}

return
}

if (this._config.html) {
if (this._config.sanitize) {
content = sanitizeHtml(content, this._config.allowList, this._config.sanitizeFn)
}

element.innerHTML = content
} else {
element.textContent = content
_getContentForTemplate() {
return {
[SELECTOR_TOOLTIP_INNER]: this.getTitle()
}
}

Expand Down Expand Up @@ -460,8 +433,8 @@ class Tooltip extends BaseComponent {
return offset
}

_resolvePossibleFunction(content) {
return typeof content === 'function' ? content.call(this._element) : content
_resolvePossibleFunction(arg) {
return typeof arg === 'function' ? arg.call(this._element) : arg
}

_getPopperConfig(attachment) {
Expand Down Expand Up @@ -674,11 +647,6 @@ class Tooltip extends BaseComponent {
}

typeCheckConfig(NAME, config, this.constructor.DefaultType)

if (config.sanitize) {
config.template = sanitizeHtml(config.template, config.allowList, config.sanitizeFn)
}

return config
}

Expand Down
151 changes: 151 additions & 0 deletions js/src/util/template-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.2): util/template-factory.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/

import { DefaultAllowlist, sanitizeHtml } from './sanitizer'
import { getElement, isElement, typeCheckConfig } from '../util/index'
import SelectorEngine from '../dom/selector-engine'

const NAME = 'TemplateFactory'
const Default = {
extraClass: '',
template: '<div></div>',
content: {}, // { selector : text , selector2 : text2 , }
html: false,
sanitize: true,
sanitizeFn: null,
allowList: DefaultAllowlist
}

const DefaultType = {
extraClass: '(string|function)',
template: 'string',
content: 'object',
html: 'boolean',
sanitize: 'boolean',
sanitizeFn: '(null|function)',
allowList: 'object'
}
const DefaultContentType = {
selector: '(string|element)',
entry: '(string|element|function|null)'
}

class TemplateFactory {
constructor(config) {
this._config = this._getConfig(config)
}

// Getters

static get NAME() {
return NAME
}

static get Default() {
return Default
}

// Public

getContent() {
return Object.values(this._config.content).map(this._resolvePossibleFunction).filter(Boolean)
}

changeContent(content) {
this._config.content = content
return this
}

hasContent() {
return this.getContent().length > 0
}

toHtml() {
const templateWrapper = document.createElement('div')
templateWrapper.innerHTML = this._maybeSanitize(this._config.template)

for (const [selector, text] of Object.entries(this._config.content)) {
this._setContent(templateWrapper, text, selector)
}

const template = templateWrapper.children[0]
const extraClass = this._resolvePossibleFunction(this._config.extraClass)
if (extraClass) {
template.classList.add(...extraClass.split(' '))
}

return template
}

// Private
_getConfig(config) {
config = {
...Default,
...(typeof config === 'object' ? config : {})
}

typeCheckConfig(NAME, config, DefaultType)

for (const [selector, content] of Object.entries(config.content)) {
typeCheckConfig(NAME, { selector, entry: content }, DefaultContentType)
}

return config
}

_setContent(template, content, selector) {
const templateElement = SelectorEngine.findOne(selector, template)

if (templateElement === null) {
return
}

content = this._resolvePossibleFunction(content)

if (!content) {
templateElement.remove()
return
}

if (isElement(content)) {
this._putElementInTemplate(getElement(content), templateElement)
return
}

if (this._config.html) {
content = this._maybeSanitize(content)
templateElement.innerHTML = content
return
}

templateElement.textContent = content
}

_maybeSanitize(arg) {
return this._config.sanitize ? sanitizeHtml(arg, this._config.allowList, this._config.sanitizeFn) : arg
}

_resolvePossibleFunction(arg) {
return typeof arg === 'function' ? arg(this) : arg
}

_putElementInTemplate(element, templateElement) {
if (!this._config.html) {
templateElement.textContent = element.textContent
return
}

if (templateElement.outerHTML === element.outerHTML) { // is the same
return
}

templateElement.innerHTML = ''
templateElement.appendChild(element)
}
}

export default TemplateFactory
Loading

0 comments on commit 0303338

Please sign in to comment.