From 0781f820eebe433c84ce6eda2c96931670b0778c Mon Sep 17 00:00:00 2001 From: Behind The Math Date: Tue, 19 Dec 2017 15:56:48 -0500 Subject: [PATCH] Add switchElementsAlt() to the default switches Fixes #83. --- lib/proto/parse-options.js | 6 ++++-- lib/switches.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/proto/parse-options.js b/lib/proto/parse-options.js index 5384e5b..706a466 100644 --- a/lib/proto/parse-options.js +++ b/lib/proto/parse-options.js @@ -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() {} diff --git a/lib/switches.js b/lib/switches.js index 9f5fcec..d87ae0b 100644 --- a/lib/switches.js +++ b/lib/switches.js @@ -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 = []