Skip to content

Commit

Permalink
#54 - Always use lower case for tag names.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Feb 27, 2012
1 parent 6cd4f95 commit 3734c4f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/binding/defaultBindings.js
Expand Up @@ -156,7 +156,7 @@ ko.bindingHandlers['value'] = {
});
},
'update': function (element, valueAccessor) {
var valueIsSelectOption = ko.utils.tagNameUpper(element) === "SELECT";
var valueIsSelectOption = ko.utils.tagNameLower(element) === "select";
var newValue = ko.utils.unwrapObservable(valueAccessor());
var elementValue = ko.selectExtensions.readValue(element);
var valueHasChanged = (newValue != elementValue);
Expand Down Expand Up @@ -187,12 +187,12 @@ ko.bindingHandlers['value'] = {

ko.bindingHandlers['options'] = {
'update': function (element, valueAccessor, allBindingsAccessor) {
if (ko.utils.tagNameUpper(element) !== "SELECT")
if (ko.utils.tagNameLower(element) !== "select")
throw new Error("options binding applies only to SELECT elements");

var selectWasPreviouslyEmpty = element.length == 0;
var previousSelectedValues = ko.utils.arrayMap(ko.utils.arrayFilter(element.childNodes, function (node) {
return node.tagName && (ko.utils.tagNameUpper(node) === "OPTION") && node.selected;
return node.tagName && (ko.utils.tagNameLower(node) === "option") && node.selected;
}), function (node) {
return ko.selectExtensions.readValue(node) || node.innerText || node.textContent;
});
Expand Down Expand Up @@ -275,7 +275,7 @@ ko.bindingHandlers['selectedOptions'] = {
var result = [];
var nodes = selectNode.childNodes;
for (var i = 0, j = nodes.length; i < j; i++) {
var node = nodes[i], tagName = node.tagName.toLowerCase();
var node = nodes[i], tagName = ko.utils.tagNameLower(node);
if (tagName == "option" && node.selected)
result.push(ko.selectExtensions.readValue(node));
else if (tagName == "optgroup") {
Expand All @@ -298,15 +298,15 @@ ko.bindingHandlers['selectedOptions'] = {
});
},
'update': function (element, valueAccessor) {
if (element.tagName.toLowerCase() != "select")
if (ko.utils.tagNameLower(element) != "select")
throw new Error("values binding applies only to SELECT elements");

var newValue = ko.utils.unwrapObservable(valueAccessor());
if (newValue && typeof newValue.length == "number") {
var nodes = element.childNodes;
for (var i = 0, j = nodes.length; i < j; i++) {
var node = nodes[i];
if (ko.utils.tagNameUpper(node) === "OPTION")
if (ko.utils.tagNameLower(node) === "option")
ko.utils.setOptionNodeSelectionState(node, ko.utils.arrayIndexOf(newValue, ko.selectExtensions.readValue(node)) >= 0);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/binding/selectExtensions.js
Expand Up @@ -6,21 +6,21 @@
// that are arbitrary objects. This is very convenient when implementing things like cascading dropdowns.
ko.selectExtensions = {
readValue : function(element) {
switch (ko.utils.tagNameUpper(element)) {
case 'OPTION':
switch (ko.utils.tagNameLower(element)) {
case 'option':
if (element[hasDomDataExpandoProperty] === true)
return ko.utils.domData.get(element, ko.bindingHandlers.options.optionValueDomDataKey);
return element.getAttribute("value");
case 'SELECT':
case 'select':
return element.selectedIndex >= 0 ? ko.selectExtensions.readValue(element.options[element.selectedIndex]) : undefined;
default:
return element.value;
}
},

writeValue: function(element, value) {
switch (ko.utils.tagNameUpper(element)) {
case 'OPTION':
switch (ko.utils.tagNameLower(element)) {
case 'option':
switch(typeof value) {
case "string":
ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, undefined);
Expand All @@ -39,7 +39,7 @@
break;
}
break;
case 'SELECT':
case 'select':
for (var i = element.options.length - 1; i >= 0; i--) {
if (ko.selectExtensions.readValue(element.options[i]) == value) {
element.selectedIndex = i;
Expand Down
6 changes: 3 additions & 3 deletions src/templating/templateSources.js
Expand Up @@ -32,9 +32,9 @@
}

ko.templateSources.domElement.prototype['text'] = function(/* valueToWrite */) {
var tagNameUpper = ko.utils.tagNameUpper(this.domElement),
elemContentsProperty = tagNameUpper === "SCRIPT" ? "text"
: tagNameUpper === "TEXTAREA" ? "value"
var tagNameLower = ko.utils.tagNameLower(this.domElement),
elemContentsProperty = tagNameLower === "script" ? "text"
: tagNameLower === "textarea" ? "value"
: "innerHTML";

if (arguments.length == 0) {
Expand Down
15 changes: 8 additions & 7 deletions src/utils.js
Expand Up @@ -29,9 +29,9 @@ ko.utils = new (function () {
isIe7 = ieVersion === 7;

function isClickOnCheckableElement(element, eventType) {
if ((ko.utils.tagNameUpper(element) !== "INPUT") || !element.type) return false;
if ((ko.utils.tagNameLower(element) !== "input") || !element.type) return false;
if (eventType.toLowerCase() != "click") return false;
var inputType = element.type.toLowerCase();
var inputType = element.type;
return (inputType == "checkbox") || (inputType == "radio");
}

Expand Down Expand Up @@ -204,10 +204,11 @@ ko.utils = new (function () {
return ko.utils.domNodeIsContainedBy(node, document);
},

tagNameUpper: function(element) {
// Possible future optimization: If we know it's an element from an HTML document (not XHTML),
// we don't need to do the .toUpperCase() as it will always be uppercase anyway
return element.tagName.toUpperCase();
tagNameLower: function(element) {
// For HTML elements, tagName will always be upper case; for XHTML elements, it'll be lower case.
// Possible future optimization: If we know it's an element from an XHTML document (not HTML),
// we don't need to do the .toLowerCase() as it will always be lower case anyway.
return element.tagName.toLowerCase();
},

registerEventHandler: function (element, eventType, handler) {
Expand Down Expand Up @@ -371,7 +372,7 @@ ko.utils = new (function () {
var url = urlOrForm;

// If we were given a form, use its 'action' URL and pick out any requested field values
if((typeof urlOrForm == 'object') && (ko.utils.tagNameUpper(urlOrForm) === "FORM")) {
if((typeof urlOrForm == 'object') && (ko.utils.tagNameLower(urlOrForm) === "form")) {
var originalForm = urlOrForm;
url = originalForm.action;
for (var i = includeFields.length - 1; i >= 0; i--) {
Expand Down
2 changes: 1 addition & 1 deletion src/virtualElements.js
Expand Up @@ -155,7 +155,7 @@
// Workaround for https://github.com/SteveSanderson/knockout/issues/155
// (IE <= 8 or IE 9 quirks mode parses your HTML weirdly, treating closing </li> tags as if they don't exist, thereby moving comment nodes
// that are direct descendants of <ul> into the preceding <li>)
if (!htmlTagsWithOptionallyClosingChildren[elementVerified.tagName.toLowerCase()])
if (!htmlTagsWithOptionallyClosingChildren[ko.utils.tagNameLower(elementVerified)])
return;

// Scan immediate children to see if they contain unbalanced comment tags. If they do, those comment tags
Expand Down

0 comments on commit 3734c4f

Please sign in to comment.