Skip to content

Commit

Permalink
add fragments and children property
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 27, 2019
1 parent 844ff1a commit e2c1ebe
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 74 deletions.
92 changes: 57 additions & 35 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
var types = {
element: 0,
text: 1,
component: 2
component: 2,
fragment: 3
};
/**
* Checks if a given character is a quote.
Expand Down Expand Up @@ -542,32 +543,43 @@
*/

function generate(element) {
var type;
var name = element.type;
var type;

if (name === "text") {
type = types.text;
} else if (name === "fragment") {
type = types.fragment;
} else if (name[0] === name[0].toLowerCase()) {
type = types.element;
} else {
type = types.component;
}

var attributes = element.attributes;
var data = "{";
var separator = "";

for (var attribute in element.attributes) {
data += "\"" + attribute + "\":" + element.attributes[attribute] + ",";
for (var attribute in attributes) {
data += separator + "\"" + attribute + "\":" + attributes[attribute];
separator = ",";
}

data += "children:[";
var separator = "";
if (attributes.children === undefined) {
// Generate children if they are not in the element data.
var children = element.children;
data += separator + "children:[";
separator = "";

for (var i = 0; i < element.children.length; i++) {
data += separator + generate(element.children[i]);
separator = ",";
for (var i = 0; i < children.length; i++) {
data += separator + generate(children[i]);
separator = ",";
}

data += "]";
}

return "{type:" + type + ",name:\"" + name + "\",data:" + data + "]}}";
return "{type:" + type + ",name:\"" + name + "\",data:" + data + "}}";
}

function compile(input) {
Expand Down Expand Up @@ -635,7 +647,7 @@
replaceElement: 4
};
/**
* Creates a DOM element from a view node.
* Creates an old reference node from a view node.
*
* @param {Object} node
* @returns {Object} node to be used as an old node
Expand All @@ -648,21 +660,34 @@
var nodeChildren = [];
var nodeNode;

if (nodeType === types.element) {
nodeNode = document.createElement(node.name); // Set data, events, and attributes.
if (nodeType === types.text) {
// Get text content using the default data key.
var textContent = node.data[""]; // Create a text node using the text content.

nodeNode = document.createTextNode(textContent); // Set only the default data key.

nodeData[""] = textContent;
} else {
var _data = node.data;

for (var key in _data) {
var value = _data[key];
if (nodeType === types.element) {
// Create a DOM element.
nodeNode = document.createElement(node.name); // Set data, events, and attributes.

if (key[0] === "@") {
nodeData[key] = value;
nodeNode.addEventListener(key.slice(1), value);
} else if (key !== "children") {
nodeData[key] = value;
nodeNode.setAttribute(key, value);
for (var key in _data) {
var value = _data[key];

if (key[0] === "@") {
nodeData[key] = value;
nodeNode.addEventListener(key.slice(1), value);
} else if (key !== "children") {
nodeData[key] = value;
nodeNode.setAttribute(key, value);
}
}
} else {
// Create a DOM fragment.
nodeNode = document.createDocumentFragment();
} // Recursively append children.


Expand All @@ -673,13 +698,6 @@
nodeChildren.push(child);
nodeNode.appendChild(child.node);
}
} else {
// Get text content using the default data key.
var textContent = node.data[""]; // Create a text node using the text content.

nodeNode = document.createTextNode(textContent); // Set only the default data key.

nodeData[""] = textContent;
} // Set the children of the new old node.


Expand Down Expand Up @@ -784,13 +802,17 @@
});
} else {
// If they both are normal elements, then set attributes and diff the
// children for appends, deletes, or recursive updates.
patches.push({
type: patchTypes.setAttributes,
nodeOld: nodeOld,
nodeNew: nodeNew,
nodeParent: null
});
// children for appends, deletes, or recursive updates. This skips
// updating attributes for fragments.
if (nodeOld.type === types.element) {
patches.push({
type: patchTypes.setAttributes,
nodeOld: nodeOld,
nodeNew: nodeNew,
nodeParent: null
});
}

var childrenOld = nodeOld.data.children;
var childrenNew = nodeNew.data.children;
var childrenOldLength = childrenOld.length;
Expand Down
2 changes: 1 addition & 1 deletion packages/moon/dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 20 additions & 9 deletions packages/moon/src/compiler/generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,41 @@ import { types } from "../../util/util";
* @returns {string} View function code
*/
export function generate(element) {
let type;
const name = element.type;
let type;

if (name === "text") {
type = types.text;
} else if (name === "fragment") {
type = types.fragment;
} else if (name[0] === name[0].toLowerCase()) {
type = types.element;
} else {
type = types.component;
}

const attributes = element.attributes;
let data = "{";
let separator = "";

for (let attribute in element.attributes) {
data += `"${attribute}":${element.attributes[attribute]},`;
for (let attribute in attributes) {
data += `${separator}"${attribute}":${attributes[attribute]}`;
separator = ",";
}

data += "children:[";
if (attributes.children === undefined) {
// Generate children if they are not in the element data.
const children = element.children;
data += separator + "children:[";

let separator = "";
for (let i = 0; i < element.children.length; i++) {
data += separator + generate(element.children[i]);
separator = ",";
separator = "";
for (let i = 0; i < children.length; i++) {
data += separator + generate(children[i]);
separator = ",";
}

data += "]";
}

return `{type:${type},name:"${name}",data:${data}]}}`;
return `{type:${type},name:"${name}",data:${data}}}`;
}
Loading

0 comments on commit e2c1ebe

Please sign in to comment.