Add switchElementsAlt() to the default switches #93

Merged
BehindTheMath merged 1 commits from bugfix/add-switchElementsAlt into master 2017-12-20 15:58:30 -05:00
2 changed files with 18 additions and 2 deletions

View File

@@ -1,5 +1,7 @@
/* global _gaq: true, ga: true */
var defaultSwitches = require("../switches")
module.exports = function(options){
this.options = options
this.options.elements = this.options.elements || "a[href], form[action]"
@@ -27,10 +29,10 @@ module.exports = function(options){
// if you set head.outerHTML, a new body tag is appended, so the dom get 2 body
// & it break the switchFallback which replace head & body
if (!this.options.switches.head) {
this.options.switches.head = this.switchElementsAlt
this.options.switches.head = defaultSwitches.switchElementsAlt
}
if (!this.options.switches.body) {
this.options.switches.body = this.switchElementsAlt
this.options.switches.body = defaultSwitches.switchElementsAlt
}
if (typeof options.analytics !== "function") {
options.analytics = function() {}

View File

@@ -15,6 +15,20 @@ module.exports = {
this.onSwitch()
},
switchElementsAlt: function(oldEl, newEl) {
oldEl.innerHTML = newEl.innerHTML
// Copy attributes from the new element to the old one
if (newEl.hasAttributes()) {
const attrs = newEl.attributes;
for (var i = 0; i < attrs.length; i++) {
oldEl.attributes.setNamedItem(attrs[i])
}
}
this.onSwitch()
},
sideBySide: function(oldEl, newEl, options, switchOptions) {
var forEach = Array.prototype.forEach
var elsToRemove = []