Skip to content

Commit e3bf750

Browse files
committed
Learning to use GitHub...
1 parent 2e74fe9 commit e3bf750

6 files changed

Lines changed: 61 additions & 4 deletions

File tree

js/almcss3/almcss.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ var ALMCSS = function() {
9191
info = logger.info,
9292
templates = ALMCSS.template.templates,
9393
createTemplateElements = ALMCSS.template.dom.createTemplateElements,
94+
positionedElements = ALMCSS.template.positionedElements,
95+
moveElementsIntoSlots = ALMCSS.template.dom.moveElementsIntoSlots,
9496
i;
9597

9698
info('Starting the main function of ALMCSS3...');
@@ -108,6 +110,7 @@ var ALMCSS = function() {
108110
info('No templates were found');
109111
}
110112
createTemplateElements(templates);
113+
moveElementsIntoSlots(positionedElements);
111114
};
112115

113116
var loadModules = function() {

js/almcss3/parser.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,9 +1648,12 @@ ALMCSS.parser = function() {
16481648
info('A template has been matched:\n' + template);
16491649

16501650
// Is it a `position` property with the name of a slot as a value?
1651-
} else if (property === 'position' && currentToken.isIdent()) {
1651+
} else if (property === 'position' && currentToken.isIdent() &&
1652+
!currentToken.isIdent('absolute') && !currentToken.isIdent('relative') &&
1653+
!currentToken.isIdent('static') && !currentToken.isIdent('fixed')) {
16521654
info("Found a 'position' property with a identifier " +
1653-
'as a value: assuming it is the name of a slot');
1655+
'as a value: assuming it is the name of a slot ' +
1656+
'(' + rule.selectorText + ' => at slot ' + currentToken.name + ')');
16541657
addPositionedElement(rule.selectorText, currentToken.name);
16551658
}
16561659

js/almcss3/template.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ ALMCSS.template = function() {
313313
return templateId;
314314
},
315315
hasSlot: function(slotName) {
316-
return slots.hasSlot(slotName);
316+
return slots.contains(slotName);
317317
},
318318
getSlot: function(slotName) {
319319
return slots.get(slotName);
@@ -495,6 +495,7 @@ ALMCSS.template = function() {
495495

496496
return {
497497
templates: templates,
498+
positionedElements: positionedElements,
498499
TemplateError: TemplateError,
499500
Slot: Slot,
500501
Length: Length,

js/almcss3/template_dom.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ALMCSS.template.dom = function() {
3232
slotLabel.innerHTML = slot.slotId;
3333
slotElement.appendChild(slotLabel);
3434
template.htmlElement.appendChild(slotElement);
35+
slot.htmlElement = slotElement;
3536
}
3637
};
3738

@@ -48,7 +49,9 @@ ALMCSS.template.dom = function() {
4849
// Currently, it is assumed that templates are defined using a single selector per CSS rule
4950
var containerElement = document.querySelector(template.getSelectorText());
5051
containerElement.appendChild(templateElement);
52+
// The HtmlElement object of the DOM **is modified**
5153
containerElement.isTemplate = true;
54+
containerElement.template = template;
5255
template.htmlElement = containerElement;
5356
// Create the slots
5457
createSlotElements(template);
@@ -61,8 +64,44 @@ ALMCSS.template.dom = function() {
6164
}
6265
};
6366

67+
var getTemplateAncestor = function(element, slotName) {
68+
var template;
69+
log("Looking for a template ancestor for the slot '" + slotName + "'...")
70+
while (element.parentNode) {
71+
element = element.parentNode;
72+
if (element.isTemplate) {
73+
template = element.template;
74+
if (template.hasSlot(slotName)) {
75+
info('A template ancestor was found');
76+
log('The ancestor template is:\n' + template);
77+
return template;
78+
} else {
79+
log("A template ancestor was found, but it did not contain a slot named '" +
80+
slotName + "', so we continue traversing up the DOM tree...");
81+
}
82+
}
83+
}
84+
warn("No template ancestor with a slot '" + slotName + "' was found");
85+
};
86+
87+
var moveElementsIntoSlots = function(positionedElements) {
88+
var i, j, elements, slotName, templateAncestor, slotElement;
89+
for (i = 0; i < positionedElements.length; i++) {
90+
slotName = positionedElements[i].slotName;
91+
elements = document.querySelectorAll(positionedElements[i].selectorText);
92+
for (j = 0; j < elements.length; j++) {
93+
templateAncestor = getTemplateAncestor(elements[j], slotName);
94+
if (templateAncestor) {
95+
slotElement = templateAncestor.getSlot(slotName).htmlElement;
96+
slotElement.appendChild(elements[j].parentNode.removeChild(elements[j]));
97+
}
98+
}
99+
}
100+
};
101+
64102
return {
65-
createTemplateElements: createTemplateElements
103+
createTemplateElements: createTemplateElements,
104+
moveElementsIntoSlots: moveElementsIntoSlots
66105
};
67106

68107
}();

js/almcss3/template_sizing.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var ALMCSS = ALMCSS || {};
2+
3+
ALMCSS.template.dom = function() {
4+
5+
}();

tests/template-000.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ body {
44
"c";
55
}
66

7+
#header { position: c; }
8+
#content { position: a; }
9+
#footer { position: b; }
10+
11+
ul { position: c; }
12+
713
body {
814
background: #fbf1cd;
915
margin: 0;

0 commit comments

Comments
 (0)