Skip to content

Commit

Permalink
prefer to use DOM properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jun 9, 2019
1 parent 7e4ea03 commit c206837
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
38 changes: 36 additions & 2 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,45 @@
* Escape text to make it usable in a JavaScript string literal.
*
* @param {string} text
* @returns {string} Escaped text
*/

function escapeText(text) {
return text.replace(textRE, function (match) {
return escapeTextMap[match];
});
}
/**
* Normalize an attribute key to a DOM property.
*
* Moon attribute keys should follow camelCase by convention instead of using
* standard HTML attribute keys. However, standard HTML attributes are
* supported. They should typically be used for custom attributes, data-*
* attributes, or aria-* attributes.
*
* @param {string} key
* @returns {string} Normalized key
*/


function normalizeAttributeKey(key) {
switch (key) {
case "class":
return "className";

case "for":
return "htmlFor";

default:
// Other keys should ideally be camelCased.
return key;
}
}
/**
* Scope an expression to use variables within the `data` object.
*
* @param {string} expression
* @returns {Object} Scoped expression and static status
*/


Expand Down Expand Up @@ -297,7 +325,7 @@
while ((attributeExec = attributeRE.exec(attributesText)) !== null) {
// Store the match and captured groups.
var attributeMatch = attributeExec[0];
var attributeKey = attributeExec[1];
var attributeKey = normalizeAttributeKey(attributeExec[1]);
var attributeValue = attributeExec[2];
var attributeExpression = attributeExec[3];

Expand Down Expand Up @@ -981,7 +1009,11 @@
info[0](event, info[1]);
});
} else if (key !== "children" && value !== false) {
element.setAttribute(key, value);
if (key in element) {
element[key] = value;
} else {
element.setAttribute(key, value);
}
}
};

Expand Down Expand Up @@ -1209,6 +1241,8 @@
// otherwise.
if (value === false) {
nodeOldElement.removeAttribute(key);
} else if (key in nodeOldElement) {
nodeOldElement[key] = value;
} else {
nodeOldElement.setAttribute(key, value);
}
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.

27 changes: 26 additions & 1 deletion packages/moon/src/compiler/lexer/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,40 @@ const escapeTextMap = {
* Escape text to make it usable in a JavaScript string literal.
*
* @param {string} text
* @returns {string} Escaped text
*/
function escapeText(text) {
return text.replace(textRE, (match) => escapeTextMap[match]);
}

/**
* Normalize an attribute key to a DOM property.
*
* Moon attribute keys should follow camelCase by convention instead of using
* standard HTML attribute keys. However, standard HTML attributes are
* supported. They should typically be used for custom attributes, data-*
* attributes, or aria-* attributes.
*
* @param {string} key
* @returns {string} Normalized key
*/
function normalizeAttributeKey(key) {
switch (key) {
case "class":
return "className";
case "for":
return "htmlFor";
default:
// Other keys should ideally be camelCased.
return key;
}
}

/**
* Scope an expression to use variables within the `data` object.
*
* @param {string} expression
* @returns {Object} Scoped expression and static status
*/
function scopeExpression(expression) {
let isStatic = true;
Expand Down Expand Up @@ -244,7 +269,7 @@ export function lex(input) {
) {
// Store the match and captured groups.
const attributeMatch = attributeExec[0];
const attributeKey = attributeExec[1];
const attributeKey = normalizeAttributeKey(attributeExec[1]);
const attributeValue = attributeExec[2];
const attributeExpression = attributeExec[3];

Expand Down
Loading

0 comments on commit c206837

Please sign in to comment.