Skip to content

Commit

Permalink
add option 'getElementAttributes'
Browse files Browse the repository at this point in the history
  • Loading branch information
Riim committed May 14, 2016
1 parent 40dffe1 commit 0fd5370
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 49 deletions.
1 change: 1 addition & 0 deletions dist/morphElement.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare function morphElement(el: HTMLElement, toEl: HTMLElement, options?: {
contentOnly?: boolean;
getElementAttributes?: (el: HTMLElement) => NamedNodeMap;
getElementKey?: (el: HTMLElement) => string;
isCompatibleElements?: (el1: HTMLElement, el2: HTMLElement) => boolean;
onBeforeMorphElement?: (el: HTMLElement, toEl: HTMLElement) => boolean;
Expand Down
6 changes: 5 additions & 1 deletion dist/morphElement.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"use strict";
var specialElementHandlers = require('./specialElementHandlers');
var morphElementAttributes = require('./morphElementAttributes');
function defaultGetElementAttributes(el) {
return el.attributes;
}
function defaultGetElementKey(el) {
return el.getAttribute('key') || void 0;
}
Expand All @@ -12,6 +15,7 @@ function morphElement(el, toEl, options) {
options = {};
}
var contentOnly = !!options.contentOnly;
var getElementAttributes = options.getElementAttributes || defaultGetElementAttributes;
var getElementKey = options.getElementKey || defaultGetElementKey;
var isCompatibleElements = options.isCompatibleElements || defaultIsCompatibleElements;
var onBeforeMorphElement = options.onBeforeMorphElement;
Expand Down Expand Up @@ -92,7 +96,7 @@ function morphElement(el, toEl, options) {
if (onBeforeMorphElement && onBeforeMorphElement(el, toEl) === false) {
return;
}
morphElementAttributes(el, toEl);
morphElementAttributes(el, toEl, getElementAttributes(el));
if (onBeforeMorphElementContent && onBeforeMorphElementContent(el, toEl) === false) {
return;
}
Expand Down
27 changes: 13 additions & 14 deletions dist/morphElement.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ return /******/ (function(modules) { // webpackBootstrap
"use strict";
var specialElementHandlers = __webpack_require__(1);
var morphElementAttributes = __webpack_require__(2);
function defaultGetElementAttributes(el) {
return el.attributes;
}
function defaultGetElementKey(el) {
return el.getAttribute('key') || void 0;
}
Expand All @@ -68,6 +71,7 @@ return /******/ (function(modules) { // webpackBootstrap
options = {};
}
var contentOnly = !!options.contentOnly;
var getElementAttributes = options.getElementAttributes || defaultGetElementAttributes;
var getElementKey = options.getElementKey || defaultGetElementKey;
var isCompatibleElements = options.isCompatibleElements || defaultIsCompatibleElements;
var onBeforeMorphElement = options.onBeforeMorphElement;
Expand Down Expand Up @@ -148,7 +152,7 @@ return /******/ (function(modules) { // webpackBootstrap
if (onBeforeMorphElement && onBeforeMorphElement(el, toEl) === false) {
return;
}
morphElementAttributes(el, toEl);
morphElementAttributes(el, toEl, getElementAttributes(el));
if (onBeforeMorphElementContent && onBeforeMorphElementContent(el, toEl) === false) {
return;
}
Expand Down Expand Up @@ -315,24 +319,19 @@ return /******/ (function(modules) { // webpackBootstrap
/***/ function(module, exports) {

"use strict";
function morphElementAttributes(el, toEl) {
function morphElementAttributes(el, toEl, elAttributes) {
var toElAttributes = toEl.attributes;
var foundAttributes = {};
for (var i = toElAttributes.length; i;) {
var attr = toElAttributes[--i];
var attrName = attr.name;
var attrValue = attr.value;
foundAttributes[attrName] = foundAttributes;
if (el.getAttribute(attrName) !== attrValue) {
el.setAttribute(attrName, attrValue);
var toElAttr = toElAttributes.item(--i);
var elAttr = elAttributes.getNamedItem(toElAttr.name);
if (!elAttr || elAttr.value != toElAttr.value) {
el.setAttribute(toElAttr.name, toElAttr.value);
}
}
var elAttributes = el.attributes;
for (var i = elAttributes.length; i;) {
var attr = elAttributes[--i];
var attrName = attr.name;
if (foundAttributes[attrName] !== foundAttributes) {
el.removeAttribute(attrName);
var elAttr = elAttributes.item(--i);
if (!toElAttributes.getNamedItem(elAttr.name)) {
el.removeAttribute(elAttr.name);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/morphElementAttributes.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare function morphElementAttributes(el: HTMLElement, toEl: HTMLElement): void;
declare function morphElementAttributes(el: HTMLElement, toEl: HTMLElement, elAttributes: NamedNodeMap): void;
export = morphElementAttributes;
21 changes: 8 additions & 13 deletions dist/morphElementAttributes.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
"use strict";
function morphElementAttributes(el, toEl) {
function morphElementAttributes(el, toEl, elAttributes) {
var toElAttributes = toEl.attributes;
var foundAttributes = {};
for (var i = toElAttributes.length; i;) {
var attr = toElAttributes[--i];
var attrName = attr.name;
var attrValue = attr.value;
foundAttributes[attrName] = foundAttributes;
if (el.getAttribute(attrName) !== attrValue) {
el.setAttribute(attrName, attrValue);
var toElAttr = toElAttributes.item(--i);
var elAttr = elAttributes.getNamedItem(toElAttr.name);
if (!elAttr || elAttr.value != toElAttr.value) {
el.setAttribute(toElAttr.name, toElAttr.value);
}
}
var elAttributes = el.attributes;
for (var i = elAttributes.length; i;) {
var attr = elAttributes[--i];
var attrName = attr.name;
if (foundAttributes[attrName] !== foundAttributes) {
el.removeAttribute(attrName);
var elAttr = elAttributes.item(--i);
if (!toElAttributes.getNamedItem(elAttr.name)) {
el.removeAttribute(elAttr.name);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "morph-element",
"version": "0.2.0",
"version": "0.2.1",
"description": "",
"main": "dist/morphElement.js",
"typings": "dist/morphElement.d.ts",
Expand Down Expand Up @@ -31,14 +31,14 @@
"karma-chai": "^0.1.0",
"karma-coverage": "^1.0.0",
"karma-mocha": "^1.0.1",
"karma-mocha-reporter": "^2.0.2",
"karma-mocha-reporter": "^2.0.3",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sinon": "^1.0.4",
"karma-sinon": "^1.0.5",
"mocha": "^2.4.5",
"phantomjs-prebuilt": "^2.1.7",
"sinon": "^1.17.4",
"typescript": "^1.8.10",
"webpack": "^1.13.0",
"yargs": "^4.6.0"
"yargs": "^4.7.0"
}
}
8 changes: 7 additions & 1 deletion src/morphElement.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import specialElementHandlers = require('./specialElementHandlers');
import morphElementAttributes = require('./morphElementAttributes');

function defaultGetElementAttributes(el: HTMLElement): NamedNodeMap {
return el.attributes;
}

function defaultGetElementKey(el: HTMLElement): string {
return el.getAttribute('key') || void 0;
}
Expand All @@ -11,6 +15,7 @@ function defaultIsCompatibleElements(el1: HTMLElement, el2: HTMLElement): boolea

function morphElement(el: HTMLElement, toEl: HTMLElement, options?: {
contentOnly?: boolean,
getElementAttributes?: (el: HTMLElement) => NamedNodeMap,
getElementKey?: (el: HTMLElement) => string,
isCompatibleElements?: (el1: HTMLElement, el2: HTMLElement) => boolean,
onBeforeMorphElement?: (el: HTMLElement, toEl: HTMLElement) => boolean,
Expand All @@ -22,6 +27,7 @@ function morphElement(el: HTMLElement, toEl: HTMLElement, options?: {
}

let contentOnly = !!options.contentOnly;
let getElementAttributes = options.getElementAttributes || defaultGetElementAttributes;
let getElementKey = options.getElementKey || defaultGetElementKey;
let isCompatibleElements = options.isCompatibleElements || defaultIsCompatibleElements;
let onBeforeMorphElement = options.onBeforeMorphElement;
Expand Down Expand Up @@ -115,7 +121,7 @@ function morphElement(el: HTMLElement, toEl: HTMLElement, options?: {
return;
}

morphElementAttributes(el, toEl);
morphElementAttributes(el, toEl, getElementAttributes(el));

if (onBeforeMorphElementContent && onBeforeMorphElementContent(el, toEl) === false) {
return;
Expand Down
23 changes: 8 additions & 15 deletions src/morphElementAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
function morphElementAttributes(el: HTMLElement, toEl: HTMLElement): void {
function morphElementAttributes(el: HTMLElement, toEl: HTMLElement, elAttributes: NamedNodeMap): void {
let toElAttributes = toEl.attributes;
let foundAttributes: { [key: string]: Object; } = {};

for (let i = toElAttributes.length; i;) {
let attr = toElAttributes[--i];
let attrName = attr.name;
let attrValue = attr.value;
let toElAttr = toElAttributes.item(--i);
let elAttr = elAttributes.getNamedItem(toElAttr.name);

foundAttributes[attrName] = foundAttributes;

if (el.getAttribute(attrName) !== attrValue) {
el.setAttribute(attrName, attrValue);
if (!elAttr || elAttr.value != toElAttr.value) {
el.setAttribute(toElAttr.name, toElAttr.value);
}
}

let elAttributes = el.attributes;

for (let i = elAttributes.length; i;) {
let attr = elAttributes[--i];
let attrName = attr.name;
let elAttr = elAttributes.item(--i);

if (foundAttributes[attrName] !== foundAttributes) {
el.removeAttribute(attrName);
if (!toElAttributes.getNamedItem(elAttr.name)) {
el.removeAttribute(elAttr.name);
}
}
}
Expand Down

0 comments on commit 0fd5370

Please sign in to comment.