Skip to content
This repository was archived by the owner on Sep 29, 2024. It is now read-only.

Commit d9ee746

Browse files
committed
0.2.0
1 parent f14c3db commit d9ee746

File tree

5 files changed

+61
-67
lines changed

5 files changed

+61
-67
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes to CSS Typed Object Model
22

3+
### 0.2.0 (April 4, 2018)
4+
5+
- Safely checks for existing methods before polyfilling
6+
- `CSSUnitValue` and `CSSKeywordValue` have realistic getters and setters
7+
38
### 0.1.0 (April 2, 2018)
49

510
- Default export is now `polyfill`

lib/CSSKeywordValue.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import CSSStyleValue from './CSSStyleValue';
1+
const _value = new WeakMap();
22

33
export default class CSSKeywordValue {
4+
get value() {
5+
return _value.get(this);
6+
}
7+
8+
set value(newValue) {
9+
_value.set(this, String(newValue));
10+
}
11+
412
toString() {
513
return `${this.value}`;
614
}
@@ -10,23 +18,10 @@ export default class CSSKeywordValue {
1018
throw new TypeError(`Failed to construct 'CSSKeywordValue': 1 arguments required, but only ${args.length} present.`);
1119
}
1220

13-
let [ value ] = args;
14-
15-
this.value = String(value);
16-
17-
Object.defineProperties(this, {
18-
value: {
19-
configurable: true,
20-
enumerable: true,
21-
get() {
22-
return value
23-
},
24-
set(newValue) {
25-
value = String(newValue);
26-
}
27-
}
28-
});
21+
_value.set(this, String(args[0]));
2922
}
3023
}
3124

32-
CSSKeywordValue.prototype = Object.create(CSSStyleValue.prototype);
25+
Object.defineProperties(CSSKeywordValue.prototype, {
26+
value: { enumerable: true }
27+
});

lib/CSSUnitValue.js

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,39 @@
1-
import CSSStyleValue from './CSSStyleValue';
21
import units from './units';
32

3+
const _value = new WeakMap();
4+
const _unit = new WeakMap();
5+
46
export default class CSSUnitValue {
7+
get value() {
8+
return _value.get(this);
9+
}
10+
11+
set value(newValue) {
12+
_value.set(this, getFiniteNumber(newValue));
13+
}
14+
15+
get unit() {
16+
return _unit.get(this);
17+
}
18+
19+
toString() {
20+
return `${this.value}${units[this.unit]}`;
21+
}
22+
523
constructor(...args) {
624
if (args.length < 2) {
725
throw new TypeError(`Failed to construct 'CSSUnitValue': 2 arguments required, but only ${args.length} present.`);
826
}
927

10-
let [ value, unit ] = args;
11-
12-
value = getFiniteNumber(value);
13-
unit = getUnit(unit);
14-
15-
Object.defineProperties(this, {
16-
value: {
17-
configurable: true,
18-
enumerable: true,
19-
get() {
20-
return value
21-
},
22-
set(newValue) {
23-
value = getFiniteNumber(newValue);
24-
}
25-
},
26-
unit: {
27-
configurable: true,
28-
enumerable: true,
29-
get() {
30-
return unit
31-
}
32-
},
33-
toString: {
34-
configurable: true,
35-
enumerable: false,
36-
value() {
37-
return this.unit === 'number' ? `${this.value}` : `${this.value}${units[this.unit]}`;
38-
}
39-
}
40-
});
28+
_value.set(this, getFiniteNumber(args[0]));
29+
_unit.set(this, getUnit(args[1]));
4130
}
4231
}
4332

44-
CSSUnitValue.prototype = Object.create(CSSStyleValue.prototype);
33+
Object.defineProperties(CSSUnitValue.prototype, {
34+
value: { enumerable: true },
35+
unit: { enumerable: true }
36+
});
4537

4638
function getFiniteNumber(value) {
4739
if (isNaN(value) || Math.abs(value) === Infinity) {

lib/polyfill.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ export default function polyfill(window) {
99

1010
Object.keys(units).forEach(
1111
unit => {
12-
if (!window.CSS[unit]) {
12+
if (!(unit in window.CSS)) {
1313
window.CSS[unit] = value => new CSSUnitValue(value, unit);
1414
}
1515
}
1616
);
1717

18-
if (!CSSRule.prototype.styleMap) defineProperty(
18+
defineProperty(
1919
CSSRule.prototype,
2020
'styleMap',
2121
context => context.style
2222
);
2323

24-
if (!CSSRule.prototype.attributeStyleMap) defineProperty(
24+
defineProperty(
2525
Element.prototype,
2626
'attributeStyleMap',
2727
context => context.style
2828
);
2929

30-
if (!Element.prototype.computedStyleMap) defineProperty(
30+
defineProperty(
3131
Element.prototype,
3232
'computedStyleMap',
3333
context => getComputedStyle(context)
@@ -39,16 +39,18 @@ export default function polyfill(window) {
3939
if (!window.StylePropertyMap) window.StylePropertyMap = StylePropertyMap;
4040

4141
function defineProperty(prototype, property, getStyle) {
42-
Object.defineProperty(prototype, property, {
43-
configurable: true,
44-
enumerable: true,
45-
get() {
46-
const computedStyleMap = Object.create(StylePropertyMap.prototype);
47-
48-
computedStyleMap.style = getStyle(this);
49-
50-
return computedStyleMap;
51-
}
52-
});
42+
if (!(property in prototype)) {
43+
Object.defineProperty(prototype, property, {
44+
configurable: true,
45+
enumerable: true,
46+
get() {
47+
const computedStyleMap = Object.create(StylePropertyMap.prototype);
48+
49+
computedStyleMap.style = getStyle(this);
50+
51+
return computedStyleMap;
52+
}
53+
});
54+
}
5355
}
5456
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "css-typed-om",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Use CSS Typed Object Model in the browser",
55
"author": "Jonathan Neal <[email protected]>",
66
"license": "CC0-1.0",

0 commit comments

Comments
 (0)