| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- var config = require('../config')
- /**
- * Check if a node is in the document.
- * Note: document.documentElement.contains should work here
- * but always returns false for comment nodes in phantomjs,
- * making unit tests difficult. This is fixed byy doing the
- * contains() check on the node's parentNode instead of
- * the node itself.
- *
- * @param {Node} node
- * @return {Boolean}
- */
- exports.inDoc = function (node) {
- var doc = document.documentElement
- var parent = node && node.parentNode
- return doc === node ||
- doc === parent ||
- !!(parent && parent.nodeType === 1 && (doc.contains(parent)))
- }
- /**
- * Extract an attribute from a node.
- *
- * @param {Node} node
- * @param {String} attr
- */
- exports.attr = function (node, attr) {
- attr = config.prefix + attr
- var val = node.getAttribute(attr)
- if (val !== null) {
- node.removeAttribute(attr)
- }
- return val
- }
- /**
- * Insert el before target
- *
- * @param {Element} el
- * @param {Element} target
- */
- exports.before = function (el, target) {
- target.parentNode.insertBefore(el, target)
- }
- /**
- * Insert el after target
- *
- * @param {Element} el
- * @param {Element} target
- */
- exports.after = function (el, target) {
- if (target.nextSibling) {
- exports.before(el, target.nextSibling)
- } else {
- target.parentNode.appendChild(el)
- }
- }
- /**
- * Remove el from DOM
- *
- * @param {Element} el
- */
- exports.remove = function (el) {
- el.parentNode.removeChild(el)
- }
- /**
- * Prepend el to target
- *
- * @param {Element} el
- * @param {Element} target
- */
- exports.prepend = function (el, target) {
- if (target.firstChild) {
- exports.before(el, target.firstChild)
- } else {
- target.appendChild(el)
- }
- }
- /**
- * Replace target with el
- *
- * @param {Element} target
- * @param {Element} el
- */
- exports.replace = function (target, el) {
- var parent = target.parentNode
- if (parent) {
- parent.replaceChild(el, target)
- }
- }
- /**
- * Add event listener shorthand.
- *
- * @param {Element} el
- * @param {String} event
- * @param {Function} cb
- */
- exports.on = function (el, event, cb) {
- el.addEventListener(event, cb)
- }
- /**
- * Remove event listener shorthand.
- *
- * @param {Element} el
- * @param {String} event
- * @param {Function} cb
- */
- exports.off = function (el, event, cb) {
- el.removeEventListener(event, cb)
- }
- /**
- * Add class with compatibility for IE & SVG
- *
- * @param {Element} el
- * @param {Strong} cls
- */
- exports.addClass = function (el, cls) {
- if (el.classList) {
- el.classList.add(cls)
- } else {
- var cur = ' ' + (el.getAttribute('class') || '') + ' '
- if (cur.indexOf(' ' + cls + ' ') < 0) {
- el.setAttribute('class', (cur + cls).trim())
- }
- }
- }
- /**
- * Remove class with compatibility for IE & SVG
- *
- * @param {Element} el
- * @param {Strong} cls
- */
- exports.removeClass = function (el, cls) {
- if (el.classList) {
- el.classList.remove(cls)
- } else {
- var cur = ' ' + (el.getAttribute('class') || '') + ' '
- var tar = ' ' + cls + ' '
- while (cur.indexOf(tar) >= 0) {
- cur = cur.replace(tar, ' ')
- }
- el.setAttribute('class', cur.trim())
- }
- }
- /**
- * Extract raw content inside an element into a temporary
- * container div
- *
- * @param {Element} el
- * @param {Boolean} asFragment
- * @return {Element}
- */
- exports.extractContent = function (el, asFragment) {
- var child
- var rawContent
- /* istanbul ignore if */
- if (
- exports.isTemplate(el) &&
- el.content instanceof DocumentFragment
- ) {
- el = el.content
- }
- if (el.hasChildNodes()) {
- rawContent = asFragment
- ? document.createDocumentFragment()
- : document.createElement('div')
- /* jshint boss:true */
- while (child = el.firstChild) {
- rawContent.appendChild(child)
- }
- }
- return rawContent
- }
- /**
- * Check if an element is a template tag.
- * Note if the template appears inside an SVG its tagName
- * will be in lowercase.
- *
- * @param {Element} el
- */
- exports.isTemplate = function (el) {
- return el.tagName &&
- el.tagName.toLowerCase() === 'template'
- }
|