Skip to content

Commit

Permalink
remove type field from view nodes
Browse files Browse the repository at this point in the history
This removes the type field from view nodes and removes normalization
from nodes during compilation. It prevents inconsistencies when using
`data` fields in default components because they won't support the
"class" -> "className" or "for" -> "htmlFor" transformation. Instead of
that, the whole concept of types was removed because it was mainly to
distinguish between components and nodes, but now it isn't needed.
Instead, the type field is gone and the patch checks for name === "text"
instead. Also, there are individual checks for "class" and "for" that
set, update, or remove their corresponding properties.
  • Loading branch information
kbrsh committed Aug 5, 2019
1 parent 8165998 commit ad18b76
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 196 deletions.
42 changes: 5 additions & 37 deletions packages/moon-browser/dist/moon-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,10 @@
return charPrevious !== "\\" && (_char === "\"" || _char === "'" || _char === "`");
}

/**
* View node types.
*/
var types = {
element: 0,
text: 1
};
/**
* Logs an error message to the console.
* @param {string} message
*/

function error(message) {
console.error("[Moon] ERROR: " + message);
}
Expand All @@ -55,14 +47,6 @@
*/

var globals = ["Infinity", "NaN", "break", "case", "catch", "class", "const", "continue", "default", "delete", "do", "else", "extends", "false", "finally", "for", "function", "if", "in", "instanceof", "let", "new", "null", "return", "super", "switch", "this", "throw", "true", "try", "typeof", "undefined", "var", "void", "while", "window"];
/*
* Map from attribute keys to equivalent DOM properties.
*/

var normalizeAttributeKeyMap = {
"class": "className",
"for": "htmlFor"
};
/**
* Map from special characters to a safe format for JavaScript string literals.
*/
Expand Down Expand Up @@ -312,13 +296,6 @@
} else {
attributeKey += charAttribute;
}
} // Normalize the attribute key. Moon attribute keys should
// follow camelCase by convention instead of using standard HTML
// attribute keys.


if (attributeKey in normalizeAttributeKeyMap) {
attributeKey = normalizeAttributeKeyMap[attributeKey];
} // Match an attribute value if it exists.


Expand Down Expand Up @@ -705,15 +682,14 @@
* @param {number} variable
* @returns {Object} prelude code, view function code, static status, and variable
*/

function generateNodeElement(element, variable) {
var attributes = element.attributes;
var name = attributes.name;
var data = attributes.data;
var children = attributes.children;
return {
prelude: "",
node: "Moon.view.m(" + types.element + "," + name.value + "," + data.value + "," + children.value + ")",
node: "Moon.view.m(" + name.value + "," + data.value + "," + children.value + ")",
isStatic: name.isStatic && data.isStatic && children.isStatic,
variable: variable
};
Expand Down Expand Up @@ -835,7 +811,7 @@


if (emptyElseClause) {
var staticPart = generateStaticPart("", "Moon.view.m(" + types.text + ",\"text\",{\"\":\"\"},[])", variable, staticParts, staticPartsMap);
var staticPart = generateStaticPart("", "Moon.view.m(\"text\",{\"\":\"\"},[])", variable, staticParts, staticPartsMap);
variable = staticPart.variable;
prelude += "else{" + variableIf + "=" + staticPart.variableStatic + ";}";
}
Expand Down Expand Up @@ -914,7 +890,7 @@

return {
prelude: variableForChildren + "=[];" + childFunction + loop + variableForChildren + ".push(" + variableForChild + "(" + args + "));}",
node: "Moon.view.m(" + types.element + "," + ("name" in attributes ? attributes.name.value : "\"span\"") + "," + data + "," + variableForChildren + ")",
node: "Moon.view.m(" + ("name" in attributes ? attributes.name.value : "\"span\"") + "," + data + "," + variableForChildren + ")",
isStatic: false,
variable: variable
};
Expand Down Expand Up @@ -1034,15 +1010,7 @@
variable: variable
};
} else {
var type;

if (name === "text") {
type = types.text;
} else {
type = types.element;
} // Add braces around the data.


// Add braces around the data.
data = "{" + data + "}";

if (staticData && !staticChildren) {
Expand All @@ -1060,7 +1028,7 @@

return {
prelude: prelude,
node: "Moon.view.m(" + type + ",\"" + name + "\"," + data + "," + children + ")",
node: "Moon.view.m(\"" + name + "\"," + data + "," + children + ")",
isStatic: staticData && staticChildren,
variable: variable
};
Expand Down
2 changes: 1 addition & 1 deletion packages/moon-browser/dist/moon-browser.min.js

Large diffs are not rendered by default.

42 changes: 5 additions & 37 deletions packages/moon-compiler/dist/moon-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,10 @@
return charPrevious !== "\\" && (_char === "\"" || _char === "'" || _char === "`");
}

/**
* View node types.
*/
var types = {
element: 0,
text: 1
};
/**
* Logs an error message to the console.
* @param {string} message
*/

function error(message) {
console.error("[Moon] ERROR: " + message);
}
Expand All @@ -61,14 +53,6 @@
*/

var globals = ["Infinity", "NaN", "break", "case", "catch", "class", "const", "continue", "default", "delete", "do", "else", "extends", "false", "finally", "for", "function", "if", "in", "instanceof", "let", "new", "null", "return", "super", "switch", "this", "throw", "true", "try", "typeof", "undefined", "var", "void", "while", "window"];
/*
* Map from attribute keys to equivalent DOM properties.
*/

var normalizeAttributeKeyMap = {
"class": "className",
"for": "htmlFor"
};
/**
* Map from special characters to a safe format for JavaScript string literals.
*/
Expand Down Expand Up @@ -318,13 +302,6 @@
} else {
attributeKey += charAttribute;
}
} // Normalize the attribute key. Moon attribute keys should
// follow camelCase by convention instead of using standard HTML
// attribute keys.


if (attributeKey in normalizeAttributeKeyMap) {
attributeKey = normalizeAttributeKeyMap[attributeKey];
} // Match an attribute value if it exists.


Expand Down Expand Up @@ -711,15 +688,14 @@
* @param {number} variable
* @returns {Object} prelude code, view function code, static status, and variable
*/

function generateNodeElement(element, variable) {
var attributes = element.attributes;
var name = attributes.name;
var data = attributes.data;
var children = attributes.children;
return {
prelude: "",
node: "Moon.view.m(" + types.element + "," + name.value + "," + data.value + "," + children.value + ")",
node: "Moon.view.m(" + name.value + "," + data.value + "," + children.value + ")",
isStatic: name.isStatic && data.isStatic && children.isStatic,
variable: variable
};
Expand Down Expand Up @@ -841,7 +817,7 @@


if (emptyElseClause) {
var staticPart = generateStaticPart("", "Moon.view.m(" + types.text + ",\"text\",{\"\":\"\"},[])", variable, staticParts, staticPartsMap);
var staticPart = generateStaticPart("", "Moon.view.m(\"text\",{\"\":\"\"},[])", variable, staticParts, staticPartsMap);
variable = staticPart.variable;
prelude += "else{" + variableIf + "=" + staticPart.variableStatic + ";}";
}
Expand Down Expand Up @@ -920,7 +896,7 @@

return {
prelude: variableForChildren + "=[];" + childFunction + loop + variableForChildren + ".push(" + variableForChild + "(" + args + "));}",
node: "Moon.view.m(" + types.element + "," + ("name" in attributes ? attributes.name.value : "\"span\"") + "," + data + "," + variableForChildren + ")",
node: "Moon.view.m(" + ("name" in attributes ? attributes.name.value : "\"span\"") + "," + data + "," + variableForChildren + ")",
isStatic: false,
variable: variable
};
Expand Down Expand Up @@ -1040,15 +1016,7 @@
variable: variable
};
} else {
var type;

if (name === "text") {
type = types.text;
} else {
type = types.element;
} // Add braces around the data.


// Add braces around the data.
data = "{" + data + "}";

if (staticData && !staticChildren) {
Expand All @@ -1066,7 +1034,7 @@

return {
prelude: prelude,
node: "Moon.view.m(" + type + ",\"" + name + "\"," + data + "," + children + ")",
node: "Moon.view.m(\"" + name + "\"," + data + "," + children + ")",
isStatic: staticData && staticChildren,
variable: variable
};
Expand Down
Loading

0 comments on commit ad18b76

Please sign in to comment.