Skip to content

Commit 1bd09ee

Browse files
committed
Migrate CSSStyleDeclaration to WebIDL2JS
1 parent 62b3b1b commit 1bd09ee

15 files changed

+257
-66
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules
2+
lib/CSSStyleDeclaration.js
23
lib/implementedProperties.js
34
lib/properties.js
5+
lib/utils.js
46
jest.config.js

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
node_modules
22
npm-debug.log
3+
lib/CSSStyleDeclaration.js
34
lib/implementedProperties.js
45
lib/properties.js
6+
lib/utils.js
57
coverage
8+
src/CSSStyleDeclaration-properties.webidl

index.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const webidlWrapper = require('./webidl2js-wrapper.js');
3+
const webidlConversions = require('webidl-conversions');
4+
5+
const sharedGlobalObject = {};
6+
webidlWrapper.install(sharedGlobalObject);
7+
8+
const origCSSStyleDeclaration = sharedGlobalObject.CSSStyleDeclaration;
9+
function CSSStyleDeclaration() {
10+
if (new.target === undefined) {
11+
throw new TypeError('Illegal invocation');
12+
}
13+
14+
let onChangeCallback = arguments[0];
15+
if (onChangeCallback === null || onChangeCallback === undefined) {
16+
onChangeCallback = null;
17+
} else {
18+
onChangeCallback = webidlConversions.Function(onChangeCallback, {
19+
context: 'Failed to constructor CSSStyleDeclaration: parameter 1',
20+
});
21+
}
22+
23+
return webidlWrapper.create(sharedGlobalObject, undefined, {
24+
onChangeCallback: onChangeCallback,
25+
});
26+
}
27+
28+
sharedGlobalObject.CSSStyleDeclaration = CSSStyleDeclaration;
29+
Object.defineProperty(CSSStyleDeclaration, 'prototype', {
30+
value: origCSSStyleDeclaration.prototype,
31+
writable: false,
32+
});
33+
CSSStyleDeclaration.prototype.constructor = CSSStyleDeclaration;
34+
Object.setPrototypeOf(CSSStyleDeclaration, Object.getPrototypeOf(origCSSStyleDeclaration));
35+
36+
exports.CSSStyleDeclaration = CSSStyleDeclaration;

jest.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ module.exports = {
33
"collectCoverage": true,
44
"collectCoverageFrom": [
55
"lib/**/*.js",
6+
"!lib/CSSStyleDeclaration.js",
67
"!lib/implementedProperties.js",
78
"!lib/properties.js",
9+
"!lib/utils.js",
810
],
911
"coverageDirectory": "coverage",
1012
};

lib/CSSStyleDeclaration.js renamed to lib/CSSStyleDeclaration-impl.js

+36-51
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@ var allExtraProperties = require('./allExtraProperties');
99
var implementedProperties = require('./implementedProperties');
1010
var { dashedToCamelCase } = require('./parsers');
1111
var getBasicPropertyDescriptor = require('./utils/getBasicPropertyDescriptor');
12+
const idlUtils = require('./utils.js');
1213

13-
/**
14-
* @constructor
15-
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration
16-
*/
17-
var CSSStyleDeclaration = function CSSStyleDeclaration(onChangeCallback) {
18-
this._values = {};
19-
this._importants = {};
20-
this._length = 0;
21-
this._onChange =
22-
onChangeCallback ||
23-
function() {
24-
return;
25-
};
26-
};
27-
CSSStyleDeclaration.prototype = {
28-
constructor: CSSStyleDeclaration,
14+
class CSSStyleDeclaration {
15+
/**
16+
* @constructor
17+
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration
18+
*/
19+
constructor(globalObject, args, { onChangeCallback }) {
20+
this._globalObject = globalObject;
21+
this._values = {};
22+
this._importants = {};
23+
this._length = 0;
24+
this._onChange =
25+
onChangeCallback ||
26+
function() {
27+
return;
28+
};
29+
}
2930

3031
/**
3132
*
@@ -34,12 +35,12 @@ CSSStyleDeclaration.prototype = {
3435
* @return {string} the value of the property if it has been explicitly set for this declaration block.
3536
* Returns the empty string if the property has not been set.
3637
*/
37-
getPropertyValue: function(name) {
38+
getPropertyValue(name) {
3839
if (!this._values.hasOwnProperty(name)) {
3940
return '';
4041
}
4142
return this._values[name].toString();
42-
},
43+
}
4344

4445
/**
4546
*
@@ -48,7 +49,7 @@ CSSStyleDeclaration.prototype = {
4849
* @param {string} [priority=null] "important" or null
4950
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-setProperty
5051
*/
51-
setProperty: function(name, value, priority) {
52+
setProperty(name, value, priority) {
5253
if (value === undefined) {
5354
return;
5455
}
@@ -68,8 +69,9 @@ CSSStyleDeclaration.prototype = {
6869

6970
this[lowercaseName] = value;
7071
this._importants[lowercaseName] = priority;
71-
},
72-
_setProperty: function(name, value, priority) {
72+
}
73+
74+
_setProperty(name, value, priority) {
7375
if (value === undefined) {
7476
return;
7577
}
@@ -92,7 +94,7 @@ CSSStyleDeclaration.prototype = {
9294
this._values[name] = value;
9395
this._importants[name] = priority;
9496
this._onChange(this.cssText);
95-
},
97+
}
9698

9799
/**
98100
*
@@ -101,7 +103,7 @@ CSSStyleDeclaration.prototype = {
101103
* @return {string} the value of the property if it has been explicitly set for this declaration block.
102104
* Returns the empty string if the property has not been set or the property name does not correspond to a known CSS property.
103105
*/
104-
removeProperty: function(name) {
106+
removeProperty(name) {
105107
if (!this._values.hasOwnProperty(name)) {
106108
return '';
107109
}
@@ -123,47 +125,30 @@ CSSStyleDeclaration.prototype = {
123125

124126
this._onChange(this.cssText);
125127
return prevValue;
126-
},
128+
}
127129

128130
/**
129131
*
130132
* @param {String} name
131133
*/
132-
getPropertyPriority: function(name) {
134+
getPropertyPriority(name) {
133135
return this._importants[name] || '';
134-
},
135-
136-
getPropertyCSSValue: function() {
137-
//FIXME
138-
return;
139-
},
140-
141-
/**
142-
* element.style.overflow = "auto"
143-
* element.style.getPropertyShorthand("overflow-x")
144-
* -> "overflow"
145-
*/
146-
getPropertyShorthand: function() {
147-
//FIXME
148-
return;
149-
},
150-
151-
isPropertyImplicit: function() {
152-
//FIXME
153-
return;
154-
},
136+
}
155137

156138
/**
157139
* http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-item
158140
*/
159-
item: function(index) {
160-
index = parseInt(index, 10);
141+
item(index) {
161142
if (index < 0 || index >= this._length) {
162143
return '';
163144
}
164145
return this[index];
165-
},
166-
};
146+
}
147+
148+
[idlUtils.supportsPropertyIndex](index) {
149+
return index >= 0 && index < this._length;
150+
}
151+
}
167152

168153
Object.defineProperties(CSSStyleDeclaration.prototype, {
169154
cssText: {
@@ -257,4 +242,4 @@ allExtraProperties.forEach(function(property) {
257242
}
258243
});
259244

260-
exports.CSSStyleDeclaration = CSSStyleDeclaration;
245+
exports.implementation = CSSStyleDeclaration;

lib/CSSStyleDeclaration.test.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var { CSSStyleDeclaration } = require('./CSSStyleDeclaration');
3+
var { CSSStyleDeclaration } = require('../index.js');
44

55
var allProperties = require('./allProperties');
66
var allExtraProperties = require('./allExtraProperties');
@@ -9,6 +9,7 @@ var parsers = require('./parsers');
99

1010
var dashedProperties = [...allProperties, ...allExtraProperties];
1111
var allowedProperties = dashedProperties.map(parsers.dashedToCamelCase);
12+
allowedProperties.push('cssFloat');
1213
implementedProperties = Array.from(implementedProperties).map(parsers.dashedToCamelCase);
1314
var invalidProperties = implementedProperties.filter(prop => !allowedProperties.includes(prop));
1415

@@ -41,9 +42,6 @@ describe('CSSStyleDeclaration', () => {
4142
expect(typeof style.setProperty).toEqual('function');
4243
expect(typeof style.getPropertyPriority).toEqual('function');
4344
expect(typeof style.removeProperty).toEqual('function');
44-
45-
// TODO - deprecated according to MDN and not implemented at all, can we remove?
46-
expect(typeof style.getPropertyCSSValue).toEqual('function');
4745
});
4846

4947
test('has special properties', () => {
@@ -52,7 +50,6 @@ describe('CSSStyleDeclaration', () => {
5250
expect(style.__lookupGetter__('cssText')).toBeTruthy();
5351
expect(style.__lookupSetter__('cssText')).toBeTruthy();
5452
expect(style.__lookupGetter__('length')).toBeTruthy();
55-
expect(style.__lookupSetter__('length')).toBeTruthy();
5653
expect(style.__lookupGetter__('parentRule')).toBeTruthy();
5754
});
5855

lib/allExtraProperties.js

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ module.exports = new Set(
1616
'color-interpolation',
1717
'color-profile',
1818
'color-rendering',
19-
'css-float',
2019
'enable-background',
2120
'fill',
2221
'fill-opacity',

lib/allWebkitProperties.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,4 @@ module.exports = [
191191
'wrap-through',
192192
'writing-mode',
193193
'zoom',
194-
].map(prop => 'webkit-' + prop);
194+
].map(prop => '-webkit-' + prop);

lib/parsers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ exports.subImplicitSetter = function(prefix, part, isValid, parser) {
709709
};
710710

711711
var camel_to_dashed = /[A-Z]/g;
712-
var first_segment = /^\([^-]\)-/;
712+
var first_segment = /^([^-]+)-/;
713713
var vendor_prefixes = ['o', 'moz', 'ms', 'webkit'];
714714
exports.camelToDashed = function(camel_case) {
715715
var match;

lib/properties/borderSpacing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var parse = function parse(v) {
1010
if (v === '' || v === null) {
1111
return undefined;
1212
}
13-
if (v === 0) {
13+
if (v === '0' || v === 0) {
1414
return '0px';
1515
}
1616
if (v.toLowerCase() === 'inherit') {

package-lock.json

+57-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)