Skip to content

Commit 1ebc38f

Browse files
committed
feat: add background-size, background-origin, background-clip
1 parent b1c3e37 commit 1ebc38f

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

lib/CSSStyleDeclaration.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,42 @@ describe('properties', () => {
358358
expect(style.cssText).toEqual('');
359359
});
360360
});
361+
describe('background-size', () => {
362+
test('invalid', () => {
363+
const style = new CSSStyleDeclaration();
364+
const invalid = ['full', '100viewport', '100px cover', 'cover auto', 'cover contain'];
365+
invalid.forEach(value => {
366+
style.backgroundSize = value;
367+
expect(style.backgroundSize).toBe('');
368+
});
369+
});
370+
test('valid', () => {
371+
const style = new CSSStyleDeclaration();
372+
const valid = ['100px', '100px 100%', 'auto', 'cover', 'contain'];
373+
valid.forEach(value => {
374+
style.backgroundSize = value;
375+
expect(style.backgroundSize).toBe(value);
376+
});
377+
});
378+
});
379+
describe('background-origin and background-clip', () => {
380+
test('invalid', () => {
381+
const style = new CSSStyleDeclaration();
382+
const invalid = ['0', '0px', 'left left', 'margin-box', 'border-box border-box'];
383+
invalid.forEach(value => {
384+
style.backgroundOrigin = value;
385+
expect(style.backgroundOrigin).toBe('');
386+
});
387+
});
388+
test('valid', () => {
389+
const style = new CSSStyleDeclaration();
390+
const valid = ['border-box', 'content-box', 'padding-box'];
391+
valid.forEach(value => {
392+
style.backgroundOrigin = value;
393+
expect(style.backgroundOrigin).toBe(value);
394+
});
395+
});
396+
});
361397
describe('border', () => {
362398
test('"none" should propagate', () => {
363399
const style = new CSSStyleDeclaration();

lib/properties/backgroundClip.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const { parseBox } = require('../parsers');
4+
5+
module.exports.parse = parseBox;
6+
7+
module.exports.definition = {
8+
set: function(v) {
9+
this._setProperty('background-clip', parseBox(v));
10+
},
11+
get: function() {
12+
return this.getPropertyValue('background-clip');
13+
},
14+
enumerable: true,
15+
configurable: true,
16+
};

lib/properties/backgroundOrigin.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const { parseBox } = require('../parsers');
4+
5+
module.exports.parse = parseBox;
6+
7+
module.exports.definition = {
8+
set: function(v) {
9+
this._setProperty('background-origin', parseBox(v));
10+
},
11+
get: function() {
12+
return this.getPropertyValue('background-origin');
13+
},
14+
enumerable: true,
15+
configurable: true,
16+
};

lib/properties/backgroundSize.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const { parseKeyword, parseLengthOrPercentage, splitTokens } = require('../parsers');
4+
5+
function parse(v) {
6+
const keyword = parseKeyword(v, ['auto', 'cover', 'contain']);
7+
if (keyword !== undefined) {
8+
return keyword;
9+
}
10+
const [components] = splitTokens(v);
11+
const { length: componentsLength } = components;
12+
if (componentsLength > 2) {
13+
return undefined;
14+
}
15+
const size = [];
16+
for (let i = 0; i < componentsLength; i++) {
17+
const component = components[i];
18+
const parsed = parseLengthOrPercentage(component);
19+
if (parsed === undefined) {
20+
return undefined;
21+
}
22+
size.push(parsed);
23+
}
24+
return size.join(' ');
25+
}
26+
27+
module.exports.parse = parse;
28+
29+
module.exports.definition = {
30+
set: function(v) {
31+
this._setProperty('background-size', parse(v));
32+
},
33+
get: function() {
34+
return this.getPropertyValue('background-size');
35+
},
36+
enumerable: true,
37+
configurable: true,
38+
};

0 commit comments

Comments
 (0)