Skip to content

Commit

Permalink
Merge pull request #69 from tbranyen/transform
Browse files Browse the repository at this point in the history
Work to support a Babel transform
  • Loading branch information
tbranyen committed Jun 9, 2016
2 parents 7d4c248 + f2d4cff commit f149d60
Show file tree
Hide file tree
Showing 28 changed files with 754 additions and 515 deletions.
623 changes: 380 additions & 243 deletions dist/diffhtml.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions karma.conf.js
Expand Up @@ -16,6 +16,12 @@ module.exports = function(config) {
{ pattern: 'test/integration/**/*.js', watched: false }
],

client: {
mocha: {
bail: true
}
},

preprocessors: {
'lib/**/*.js': ['browserify'],
'test/unit/**/*.js': ['browserify'],
Expand Down
15 changes: 0 additions & 15 deletions lib/element/get.js

This file was deleted.

12 changes: 6 additions & 6 deletions lib/element/make.js
Expand Up @@ -13,8 +13,8 @@ export default function make(descriptor) {
var isSvg = false;

// If the element descriptor was already created, reuse the existing element.
if (makeNode.nodes[descriptor.uuid]) {
return makeNode.nodes[descriptor.uuid];
if (makeNode.nodes.has(descriptor)) {
return makeNode.nodes.get(descriptor);
}

if (descriptor.nodeName === '#text') {
Expand All @@ -39,10 +39,10 @@ export default function make(descriptor) {

// If not a dynamic type, set as an attribute, since it's a valid
// attribute value.
if (!isObject && !isFunction) {
if (attr.name && !isObject && !isFunction) {
element.setAttribute(attr.name, attr.value);
}
else if (typeof attr.value !== 'string') {
else if (attr.name && typeof attr.value !== 'string') {
// Necessary to track the attribute/prop existence.
element.setAttribute(attr.name, '');

Expand Down Expand Up @@ -75,8 +75,8 @@ export default function make(descriptor) {
}
}

// Add to the nodes cache using the designated uuid as the lookup key.
makeNode.nodes[descriptor.uuid] = element;
// Add to the nodes cache.
makeNode.nodes.set(descriptor, element);

return element;
}
1 change: 0 additions & 1 deletion lib/html.js
@@ -1,7 +1,6 @@
import { parse } from './util/parser';
import escape from './util/escape';

// Make a parser.
const isPropEx = /(=|'|")/;

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/index.js
Expand Up @@ -13,6 +13,9 @@ import { TransitionStateError, DOMException } from './errors';
export { html } from './html';
import { html } from './html';

// Export the VTree/Attribute helpers.
export { createElement, createAttribute } from './util/transform';

/**
* Used to diff the outerHTML contents of the passed element with the markup
* contents. Very useful for applying a global diff on the
Expand Down
43 changes: 16 additions & 27 deletions lib/node/make.js
Expand Up @@ -3,7 +3,7 @@ import { pools } from '../util/pools';
const empty = {};

// Cache created nodes inside this object.
make.nodes = {};
make.nodes = new Map();

/**
* Converts a live node into a virtual node.
Expand All @@ -23,45 +23,34 @@ export default function make(node) {
// diff and patch.
let entry = pools.elementObject.get();

// Associate this newly allocated uuid with this Node.
make.nodes[entry.uuid] = node;
// Associate this newly allocated descriptor with this Node.
make.nodes.set(entry, node);

// Set a lowercased (normalized) version of the element's nodeName.
entry.nodeName = node.nodeName.toLowerCase();

// If the element is a text node set the nodeValue.
if (nodeType === 3) {
entry.nodeValue = node.textContent;
entry.nodeType = 3;
}
else {
entry.nodeValue = '';
entry.nodeType = 1;
}

entry.nodeValue = nodeType === 3 ? node.textContent : '';
entry.nodeType = nodeType;
entry.childNodes.length = 0;
entry.attributes.length = 0;

// Collect attributes.
let attributes = node.attributes;

// If the element has no attributes, skip over.
if (attributes) {
let attributesLength = attributes.length;
if (attributes && attributes.length) {
for (let i = 0; i < attributes.length; i++) {
let attr = pools.attributeObject.get();

if (attributesLength) {
for (let i = 0; i < attributesLength; i++) {
let attr = pools.attributeObject.get();
attr.name = attributes[i].name;
attr.value = attributes[i].value;

attr.name = attributes[i].name;
attr.value = attributes[i].value;

if (attr.name === 'key') {
entry.key = attr.value;
}

entry.attributes[entry.attributes.length] = attr;
if (attr.name === 'key') {
entry.key = attr.value;
}

entry.attributes.push(attr);
}
}

Expand All @@ -70,12 +59,12 @@ export default function make(node) {
let childNodesLength = childNodes.length;

// If the element has child nodes, convert them all to virtual nodes.
if (nodeType !== 3 && childNodesLength) {
if (childNodesLength) {
for (let i = 0; i < childNodesLength; i++) {
let newNode = make(childNodes[i]);

if (newNode) {
entry.childNodes[entry.childNodes.length] = newNode;
entry.childNodes.push(newNode);
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions lib/node/patch.js
Expand Up @@ -62,7 +62,7 @@ export default function patchNode(element, newHTML, options) {
var oldTree = elementMeta.oldTree;

if (oldTree) {
unprotectElement(oldTree, makeNode);
unprotectElement(oldTree);
}

elementMeta.oldTree = protectElement(makeNode(element));
Expand All @@ -79,22 +79,26 @@ export default function patchNode(element, newHTML, options) {

if (typeof newHTML === 'string') {
let childNodes = parse(newHTML).childNodes;
newTree = options.inner ? childNodes : childNodes[0];
newTree = isInner ? childNodes : childNodes[0];
}
else if (newHTML.ownerDocument) {
newTree = makeNode(newHTML);

if (newTree.nodeType === 11) {
pools.elementObject.unprotect(newTree);
newTree = newTree.childNodes;
}
}
else {
newTree = newHTML;
}

if (options.inner) {
let childNodes = Array.isArray(newTree) ? newTree : [newTree];
pools.elementObject.unprotect(newTree);

newTree = {
childNodes,
childNodes: [].concat(newTree),
attributes: elementMeta.oldTree.attributes,
uuid: elementMeta.oldTree.uuid,
nodeName: elementMeta.oldTree.nodeName,
nodeValue: elementMeta.oldTree.nodeValue
};
Expand Down
5 changes: 2 additions & 3 deletions lib/node/release.js
@@ -1,5 +1,4 @@
import { TreeCache } from './tree';
import makeNode from './make';
import { cleanMemory, unprotectElement } from '../util/memory';
import { pools } from '../util/pools';

Expand All @@ -17,12 +16,12 @@ export default function releaseNode(element) {
if (elementMeta) {
// If there was a tree set up, recycle the memory allocated for it.
if (elementMeta.oldTree) {
unprotectElement(elementMeta.oldTree, makeNode);
unprotectElement(elementMeta.oldTree);
}

// Remove this element's meta object from the cache.
TreeCache.delete(element);
}

cleanMemory(makeNode);
cleanMemory();
}

0 comments on commit f149d60

Please sign in to comment.