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

Support calculated values in sizes and heights #3078

Merged
merged 1 commit into from
May 5, 2016
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
72 changes: 63 additions & 9 deletions src/size-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,72 @@ export function parseSizeList(s, opt_allowPercentAsLength) {

let mediaStr;
let sizeStr;
const spaceIndex = sSize.lastIndexOf(' ');
if (spaceIndex != -1) {
mediaStr = sSize.substring(0, spaceIndex).trim();
sizeStr = sSize.substring(spaceIndex + 1).trim();

// Process the expression from the end.
const lastChar = sSize.charAt(sSize.length - 1);
let div;
let func = false;
if (lastChar == ')') {
// Value is the CSS function, e.g. `calc(50vw + 10px)`.
func = true;

// First, skip to the opening paren.
let parens = 1;
div = sSize.length - 2;
for (; div >= 0; div--) {
const c = sSize.charAt(div);
if (c == '(') {
parens--;
} else if (c == ')') {
parens++;
}
if (parens == 0) {
break;
}
}

// Then, skip to the begining to the function's name.
const funcEnd = div - 1;
if (div > 0) {
div--;
for (; div >= 0; div--) {
const c = sSize.charAt(div);
if (!(c == '%' || c == '-' || c == '_' ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'))) {
break;
}
}
}
user.assert(div < funcEnd, 'Invalid CSS function in "%s"', sSize);
} else {
// Value is the length or a percent: accept a wide range of values,
// including invalid values - they will be later asserted to conform
// to exact CSS length or percent value.
div = sSize.length - 2;
for (; div >= 0; div--) {
const c = sSize.charAt(div);
if (!(c == '%' || c == '.' ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'))) {
break;
}
}
}
if (div >= 0) {
mediaStr = sSize.substring(0, div + 1).trim();
sizeStr = sSize.substring(div + 1).trim();
} else {
sizeStr = sSize;
mediaStr = undefined;
}
sizes.push({mediaQuery: mediaStr,
size: opt_allowPercentAsLength ?
assertLengthOrPercent(sizeStr) :
assertLength(sizeStr)});
size: func ? sizeStr :
opt_allowPercentAsLength ?
assertLengthOrPercent(sizeStr) :
assertLength(sizeStr)});
});
return new SizeList(sizes);
};
Expand Down Expand Up @@ -109,7 +163,7 @@ export class SizeList {
*
* See http://www.w3.org/html/wg/drafts/html/master/semantics.html#attr-img-sizes
* @param {!Window} win
* @return {!Length}
* @return {!LengthDef|string}
*/
select(win) {
for (let i = 0; i < this.sizes_.length - 1; i++) {
Expand All @@ -123,7 +177,7 @@ export class SizeList {

/**
* Returns the last size in the SizeList, which is the default.
* @return {!Length}
* @return {!LengthDef|string}
*/
getLast() {
return this.sizes_[this.sizes_.length - 1].size;
Expand Down
48 changes: 46 additions & 2 deletions test/functional/test-size-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,59 @@ describe('SizeList parseSizeList', () => {
expect(res.sizes_[0].size).to.equal('111vw');
});

it('should accept different length units including percent', () => {
it('should accept fractional numbers', () => {
const res = parseSizeList(' \n 11.1vw \n ');
expect(res.sizes_.length).to.equal(1);
expect(res.sizes_[0].mediaQuery).to.equal(undefined);
expect(res.sizes_[0].size).to.equal('11.1vw');
});

it('should accept CSS functions', () => {
const res = parseSizeList('screen calc(111vw + 10px) \n' +
', ca_1-C((50vw+20px) / 2) ');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slightly unrelated but just curious, why do we not allow absolute lengths (in, q, cm, pt etc)? is it supposed to be not supported? https://github.com/ampproject/amphtml/blob/master/src/layout.js#L172

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Good question. Please file the bug - we'll see. I think we should allow them.

expect(res.sizes_.length).to.equal(2);
expect(res.sizes_[0].mediaQuery).to.equal('screen');
expect(res.sizes_[0].size).to.equal('calc(111vw + 10px)');
expect(res.sizes_[1].mediaQuery).to.be.undefined;
expect(res.sizes_[1].size).to.equal('ca_1-C((50vw+20px) / 2)');
});

it('should tolerate right paren', () => {
const res = parseSizeList('(min-width:2000px)calc(11px)' +
',(min-width:1000px)11px,12px');
expect(res.sizes_.length).to.equal(3);
expect(res.sizes_[0].mediaQuery).to.equal('(min-width:2000px)');
expect(res.sizes_[0].size).to.equal('calc(11px)');
expect(res.sizes_[1].mediaQuery).to.equal('(min-width:1000px)');
expect(res.sizes_[1].size).to.equal('11px');
expect(res.sizes_[2].mediaQuery).to.be.undefined;
expect(res.sizes_[2].size).to.equal('12px');
});

it('should fail on invalid CSS functions', () => {
// Spaces are not allowed between function name and `(`.
expect(() => {
parseSizeList('screen calc (111vw + 10px) \n, 10px ');
}).to.throw(/Invalid CSS function/);

// Parens don't match.
expect(() => {
parseSizeList('screen calc(111vw + 10px)) \n, 10px ');
}).to.throw(/Invalid CSS function/);
expect(() => {
parseSizeList('screen calc((111vw + 10px) \n, 10px ');
}).to.throw(/Invalid CSS function/);
});

it('should accept percent when allowed', () => {
const res = parseSizeList(' \n 111% \n ',
/* opt_allowPercentAsLength */ true);
expect(res.sizes_.length).to.equal(1);
expect(res.sizes_[0].mediaQuery).to.equal(undefined);
expect(res.sizes_[0].size).to.equal('111%');
});

it('should fail bad length', () => {
it('should not accept percent', () => {
expect(() => {
parseSizeList(' \n 111% \n ', /* opt_allowPercentAsLength */ false);
}).to.throw(/Invalid length value/);
Expand Down