Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download attribute #1340

Merged
merged 6 commits into from
Apr 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/browser/ui/dom/DOMProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var DOMPropertyInjection = {
HAS_BOOLEAN_VALUE: 0x8,
HAS_NUMERIC_VALUE: 0x10,
HAS_POSITIVE_NUMERIC_VALUE: 0x20 | 0x10,
HAS_OVERLOADED_BOOLEAN_VALUE: 0x40,

/**
* Inject some specialized knowledge about the DOM. This takes a config object
Expand Down Expand Up @@ -115,6 +116,8 @@ var DOMPropertyInjection = {
propConfig & DOMPropertyInjection.HAS_NUMERIC_VALUE;
DOMProperty.hasPositiveNumericValue[propName] =
propConfig & DOMPropertyInjection.HAS_POSITIVE_NUMERIC_VALUE;
DOMProperty.hasOverloadedBooleanValue[propName] =
propConfig & DOMPropertyInjection.HAS_OVERLOADED_BOOLEAN_VALUE;

invariant(
!DOMProperty.mustUseAttribute[propName] ||
Expand All @@ -129,9 +132,11 @@ var DOMPropertyInjection = {
propName
);
invariant(
!DOMProperty.hasBooleanValue[propName] ||
!DOMProperty.hasNumericValue[propName],
'DOMProperty: Cannot have both boolean and numeric value: %s',
!!DOMProperty.hasBooleanValue[propName] +
!!DOMProperty.hasNumericValue[propName] +
!!DOMProperty.hasOverloadedBooleanValue[propName] <= 1,
'DOMProperty: Value can be one of boolean, overloaded boolean, or ' +
'numeric value, but not a combination: %s',
propName
);
}
Expand Down Expand Up @@ -231,6 +236,14 @@ var DOMProperty = {
*/
hasPositiveNumericValue: {},

/**
* Whether the property can be used as a flag as well as with a value. Removed
* when strictly equal to false; present without a value when strictly equal
* to true; present with a value otherwise.
* @type {Object}
*/
hasOverloadedBooleanValue: {},

/**
* All of the isCustomAttribute() functions that have been injected.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/browser/ui/dom/DOMPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function shouldIgnoreValue(name, value) {
return value == null ||
(DOMProperty.hasBooleanValue[name] && !value) ||
(DOMProperty.hasNumericValue[name] && isNaN(value)) ||
(DOMProperty.hasPositiveNumericValue[name] && (value < 1));
(DOMProperty.hasPositiveNumericValue[name] && (value < 1)) ||
(DOMProperty.hasOverloadedBooleanValue[name] && value === false);
}

var processAttributeNameAndPrefix = memoizeStringOnly(function(name) {
Expand Down Expand Up @@ -96,7 +97,8 @@ var DOMPropertyOperations = {
return '';
}
var attributeName = DOMProperty.getAttributeName[name];
if (DOMProperty.hasBooleanValue[name]) {
if (DOMProperty.hasBooleanValue[name] ||
(DOMProperty.hasOverloadedBooleanValue[name] && value === true)) {
return escapeTextForBrowser(attributeName);
}
return processAttributeNameAndPrefix(attributeName) +
Expand Down
4 changes: 3 additions & 1 deletion src/browser/ui/dom/DefaultDOMPropertyConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var HAS_SIDE_EFFECTS = DOMProperty.injection.HAS_SIDE_EFFECTS;
var HAS_NUMERIC_VALUE = DOMProperty.injection.HAS_NUMERIC_VALUE;
var HAS_POSITIVE_NUMERIC_VALUE =
DOMProperty.injection.HAS_POSITIVE_NUMERIC_VALUE;
var HAS_OVERLOADED_BOOLEAN_VALUE =
DOMProperty.injection.HAS_OVERLOADED_BOOLEAN_VALUE;

var DefaultDOMPropertyConfig = {
isCustomAttribute: RegExp.prototype.test.bind(
Expand Down Expand Up @@ -66,7 +68,7 @@ var DefaultDOMPropertyConfig = {
defer: HAS_BOOLEAN_VALUE,
dir: null,
disabled: MUST_USE_ATTRIBUTE | HAS_BOOLEAN_VALUE,
download: null,
download: HAS_OVERLOADED_BOOLEAN_VALUE,
draggable: null,
encType: null,
form: MUST_USE_ATTRIBUTE,
Expand Down
42 changes: 42 additions & 0 deletions src/browser/ui/dom/__tests__/DOMPropertyOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,48 @@ describe('DOMPropertyOperations', function() {
)).toBe('');
});

it('should create markup for booleanish properties', function() {
expect(DOMPropertyOperations.createMarkupForProperty(
'download',
'simple'
)).toBe('download="simple"');

expect(DOMPropertyOperations.createMarkupForProperty(
'download',
true
)).toBe('download');

expect(DOMPropertyOperations.createMarkupForProperty(
'download',
'true'
)).toBe('download="true"');

expect(DOMPropertyOperations.createMarkupForProperty(
'download',
false
)).toBe('');

expect(DOMPropertyOperations.createMarkupForProperty(
'download',
'false'
)).toBe('download="false"');

expect(DOMPropertyOperations.createMarkupForProperty(
'download',
undefined
)).toBe('');

expect(DOMPropertyOperations.createMarkupForProperty(
'download',
null
)).toBe('');

expect(DOMPropertyOperations.createMarkupForProperty(
'download',
0
)).toBe('download="0"');
});

it('should create markup for custom attributes', function() {
expect(DOMPropertyOperations.createMarkupForProperty(
'aria-label',
Expand Down