diff --git a/.gitignore b/.gitignore index b512c09d..7346a64b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +lib/properties.js diff --git a/lib/CSSStyleDeclaration.js b/lib/CSSStyleDeclaration.js index 28c6c2aa..02768ff2 100644 --- a/lib/CSSStyleDeclaration.js +++ b/lib/CSSStyleDeclaration.js @@ -209,36 +209,6 @@ Object.defineProperties(CSSStyleDeclaration.prototype, { } }); -var LazyDefinition = function (property, modulePath) { - this.get = function () { - var definition = require(modulePath).definition; - Object.defineProperty(this, property, definition); - return this[property]; - }; - - this.set = function (v) { - var definition = require(modulePath).definition; - Object.defineProperty(this, property, definition); - this[property] = v; - }; - - this.enumerable = true; - this.configurable = true; -}; - -/* - * - * http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties - */ -var property_files = fs.readdirSync(__dirname + '/properties'); -property_files.forEach(function (property) { - var dashed; - if (property.substr(-3) === '.js') { - property = path.basename(property, '.js'); - dashed = camelToDashed(property); - Object.defineProperty(CSSStyleDeclaration.prototype, property, new LazyDefinition(property, './properties/' + property)); - Object.defineProperty(CSSStyleDeclaration.prototype, dashed, new LazyDefinition(property, './properties/' + property)); - } -}); +require('./properties')(CSSStyleDeclaration.prototype); exports.CSSStyleDeclaration = CSSStyleDeclaration; diff --git a/package.json b/package.json index 64871c54..8736b64a 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "nodeunit": "~0.8.0" }, "scripts": { - "test": "nodeunit tests" + "test": "./scripts/run_tests.sh", + "prepublish": "node ./scripts/generateTestFiles.js" }, "licenses": [ { diff --git a/scripts/generate_properties.js b/scripts/generate_properties.js new file mode 100644 index 00000000..4ce7f6cc --- /dev/null +++ b/scripts/generate_properties.js @@ -0,0 +1,58 @@ +'use strict'; + +var fs = require('fs'); +var path = require('path'); + +var camelToDashed = require('../lib/parsers').camelToDashed; + +var property_files = fs.readdirSync(path.resolve(__dirname, '../lib/properties')); +var out_file = fs.createWriteStream(path.resolve(__dirname, '../lib/properties.js'), {encoding: 'utf-8'}); + +out_file.write('\'use strict\';\n\n// autogenerated\n\n'); +out_file.write('/*\n *\n * http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties\n */\n\n'); +out_file.write('module.exports = function (prototype) {\n'); + +property_files.forEach(function (property) { + var dashed; + if (property.substr(-3) === '.js') { + property = path.basename(property, '.js'); + dashed = camelToDashed(property); + out_file.write(' Object.defineProperty(prototype, \'' + property + '\', {\n'); + out_file.write(' get: function () {\n'); + out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n'); + out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n'); + out_file.write(' return this.' + property + ';\n'); + out_file.write(' },\n'); + out_file.write(' set: function (v) {\n'); + out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n'); + out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n'); + out_file.write(' this.' + property + ' = v;\n'); + out_file.write(' },\n'); + out_file.write(' enumerable: true,\n'); + out_file.write(' configurable: true\n'); + out_file.write(' });\n'); + if (property !== dashed) { + out_file.write(' Object.defineProperty(prototype, \'' + dashed + '\', {\n'); + out_file.write(' get: function () {\n'); + out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n'); + out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n'); + out_file.write(' return this.' + property + ';\n'); + out_file.write(' },\n'); + out_file.write(' set: function (v) {\n'); + out_file.write(' var definition = require(\'./properties/' + property + '\').definition;\n'); + out_file.write(' Object.defineProperty(prototype, \'' + property + '\', definition);\n'); + out_file.write(' this.' + property + ' = v;\n'); + out_file.write(' },\n'); + out_file.write(' enumerable: true,\n'); + out_file.write(' configurable: true\n'); + out_file.write(' });\n'); + } + } +}); + +out_file.write('};\n'); +out_file.end(function (err) { + if (err) { + throw err; + } +}); diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh new file mode 100755 index 00000000..dc32fc3c --- /dev/null +++ b/scripts/run_tests.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +node ./scripts/generate_properties.js +nodeunit tests