Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Ensure the form attribute is set using setAttribute for tags where it… #352

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions packages/dom-helper/lib/prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@ export function isAttrRemovalValue(value) {
return value === null || value === undefined;
}

function UNDEFINED() {}
// TODO should this be an o_create kind of thing?
export var propertyCaches = {};


var FORM_REQUIRES_SET_ATTRIBUTE_BY_TAG = {
SELECT: true,
OPTION: true,
INPUT: true,
TEXTAREA: true,
BUTTON: true,
LABEL: true,
FIELDSET: true,
LEGEND: true,
OBJECT: true
};

export function normalizeProperty(element, attrName) {
var tagName = element.tagName;
var key;
var cache = propertyCaches[tagName];
if (!cache) {

if (cache === undefined) {
// TODO should this be an o_create kind of thing?
cache = {};

for (key in element) {
cache[key.toLowerCase()] = key;
if (key === 'form' && FORM_REQUIRES_SET_ATTRIBUTE_BY_TAG[tagName]) {
cache[key] = UNDEFINED;
} else {
cache[key.toLowerCase()] = key;
}
}
propertyCaches[tagName] = cache;
}

// presumes that the attrName has been lowercased.
return cache[attrName];
var value = cache[attrName];
return value === UNDEFINED ? undefined : value;
}
35 changes: 35 additions & 0 deletions packages/dom-helper/tests/prop-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { normalizeProperty } from 'dom-helper/prop';

QUnit.module('dom-helper prop');

function tag(tagName) {
return document.createElement(tagName);
}

var scenario = [
['div', 'id', 'id'],
['select', 'form', undefined],
['div', 'form', undefined],
['select', 'labels', 'labels'],
['select', 'options', 'options'],
['select', 'selectIndex', undefined],
['select', 'selectedOptions', undefined],
['select', 'type', 'type'],
['select', 'validationMessage', undefined],
['select', 'validity', 'validity'],
['select', 'willValidate', undefined]
];

if (typeof document === 'object') {
test('it throws when instantiated without document', function(){
scenario.forEach(function(test, index) {
var tagName = test[0];
var propertyName = test[1];
var expected = test[2];
var element = tag(tagName);
var actual = normalizeProperty(element, propertyName);

equal(actual, expected, '[scenario:' + index + '] expected: ' + tagName + '[' + propertyName+ '] to normalize to: ' + expected + ' but got: ' + actual);
});
});
}