Skip to content

Commit

Permalink
Clean up a few things and update version to 0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber623 committed Mar 27, 2018
1 parent b15daa5 commit 4ff7914
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 40 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ TemplateTemplate is also really tiny:
<tbody>
<tr>
<th>Uncompressed</th>
<td>1,252 bytes</td>
<td>1,529 bytes</td>
</tr>
<tr>
<th>Minified</th>
<td>767 bytes</td>
<td>878 bytes</td>
</tr>
<tr>
<th>Minified and gzipped</th>
<td>446 bytes</td>
<td>493 bytes</td>
</tr>
</tbody>
</table>
Expand Down
38 changes: 21 additions & 17 deletions dist/templatetemplate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* TemplateTemplate 0.1.2
* TemplateTemplate 0.1.3
*
* A very small JavaScript <template> manipulation library.
*
Expand All @@ -16,21 +16,25 @@
return function(template, insertions) {
template = template instanceof HTMLElement ? template : document.querySelector(template);
insertions = insertions || {};
var fragment = document.importNode(template.content, true);
Object.entries(insertions).forEach(function(insertionArray) {
var node = fragment.querySelector(insertionArray[0]), insertion = insertionArray[1];
if (insertion instanceof Array) {
Object.entries(insertion[1]).forEach(function(attributesArray) {
node.setAttribute(attributesArray[0], attributesArray[1]);
});
insertion = insertion[0];
}
if (insertion instanceof DocumentFragment || insertion instanceof HTMLElement) {
node.appendChild(insertion);
} else {
node.textContent = insertion;
}
});
return fragment;
if (template instanceof HTMLTemplateElement) {
var importedNode = document.importNode(template.content, true);
Object.entries(insertions).forEach(function(insertionArray) {
var currentNode = importedNode.querySelector(insertionArray[0]), insertionValue = insertionArray[1];
if (insertionValue instanceof Array) {
Object.entries(insertionValue[1]).forEach(function(attributesArray) {
currentNode.setAttribute(attributesArray[0], attributesArray[1]);
});
insertionValue = insertionValue[0];
}
if (insertionValue instanceof DocumentFragment || insertionValue instanceof HTMLElement) {
currentNode.appendChild(insertionValue);
} else {
currentNode.textContent = insertionValue;
}
});
return importedNode;
} else {
throw new TypeError(template.constructor.name + " is not an HTMLTemplateElement.");
}
};
});
4 changes: 2 additions & 2 deletions dist/templatetemplate.min.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* TemplateTemplate 0.1.2
* TemplateTemplate 0.1.3
*
* A very small JavaScript <template> manipulation library.
*
Expand All @@ -10,4 +10,4 @@
* TemplateTemplate may be freely distributed under the MIT license.
*/

!function(e,t){e.TemplateTemplate=function(){return function(e,t){e=e instanceof HTMLElement?e:document.querySelector(e),t=t||{};var n=document.importNode(e.content,!0);return Object.entries(t).forEach(function(e){var t=n.querySelector(e[0]),o=e[1];o instanceof Array&&(Object.entries(o[1]).forEach(function(e){t.setAttribute(e[0],e[1])}),o=o[0]),o instanceof DocumentFragment||o instanceof HTMLElement?t.appendChild(o):t.textContent=o}),n}}()}(this);
!function(e,t){e.TemplateTemplate=function(){return function(e,t){if(e=e instanceof HTMLElement?e:document.querySelector(e),t=t||{},e instanceof HTMLTemplateElement){var n=document.importNode(e.content,!0);return Object.entries(t).forEach(function(e){var t=n.querySelector(e[0]),o=e[1];o instanceof Array&&(Object.entries(o[1]).forEach(function(e){t.setAttribute(e[0],e[1])}),o=o[0]),o instanceof DocumentFragment||o instanceof HTMLElement?t.appendChild(o):t.textContent=o}),n}throw new TypeError(e.constructor.name+" is not an HTMLTemplateElement.")}}()}(this);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jgarber/templatetemplate",
"version": "0.1.2",
"version": "0.1.3",
"description": "A very small JavaScript <template> manipulation library.",
"main": "dist/templatetemplate.js",
"scripts": {
Expand Down
38 changes: 21 additions & 17 deletions src/templatetemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@
template = template instanceof HTMLElement ? template : document.querySelector(template);
insertions = insertions || {};

var fragment = document.importNode(template.content, true);
if (template instanceof HTMLTemplateElement) {
var importedNode = document.importNode(template.content, true);

Object.entries(insertions).forEach(function(insertionArray) {
var node = fragment.querySelector(insertionArray[0]),
insertion = insertionArray[1];
Object.entries(insertions).forEach(function(insertionArray) {
var currentNode = importedNode.querySelector(insertionArray[0]),
insertionValue = insertionArray[1];

if (insertion instanceof Array) {
Object.entries(insertion[1]).forEach(function(attributesArray) {
node.setAttribute(attributesArray[0], attributesArray[1]);
});
if (insertionValue instanceof Array) {
Object.entries(insertionValue[1]).forEach(function(attributesArray) {
currentNode.setAttribute(attributesArray[0], attributesArray[1]);
});

insertion = insertion[0];
}
insertionValue = insertionValue[0];
}

if (insertion instanceof DocumentFragment || insertion instanceof HTMLElement) {
node.appendChild(insertion);
} else {
node.textContent = insertion;
}
});
if (insertionValue instanceof DocumentFragment || insertionValue instanceof HTMLElement) {
currentNode.appendChild(insertionValue);
} else {
currentNode.textContent = insertionValue;
}
});

return fragment;
return importedNode;
} else {
throw new TypeError(template.constructor.name + ' is not an HTMLTemplateElement.');
}
};
}));

0 comments on commit 4ff7914

Please sign in to comment.