-
Notifications
You must be signed in to change notification settings - Fork 101
/
transform.js
53 lines (41 loc) · 1.28 KB
/
transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Simple HTML transformations, in HTML!
* Author: Lea Verou
*/
Bliss.ready().then(function($, $$){
var actions = ["before", "after", "start", "end", "around", "attr"];
var methods = {"end": "inside"}
actions.forEach(function(action){
var method = methods[action] || action;
$$("template[data-" + action + "]").forEach(function(template){
var context = template.parentNode;
var selector = template.getAttribute("data-" + action);
$$(selector, context).forEach(function(element){
if (element.classList.contains("transform-ignore")) {
return;
}
var clone = document.importNode(template.content, true);
if (method == "attr" || method == "around") {
var content = $$("content", clone);
if (content.length > 0) {
// Copy attributes from <content> onto target
$$(content[0].attributes).forEach(function(attribute){
if (attribute.name != "select") {
element.setAttribute(attribute.name, attribute.value);
}
});
if (method == "around") {
$.before(clone, element);
// TODO handle multiple <content> and <content select>
$.before(element, content[0]);
$.remove(content[0]);
}
return;
}
}
$[method](clone, element);
element.normalize();
});
});
});
}.bind(null, Bliss, Bliss.$));