download_latest_properties.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. /*
  3. * W3C provides JSON list of all CSS properties and their status in the standard
  4. *
  5. * documentation: https://www.w3.org/Style/CSS/all-properties.en.html
  6. * JSON url: ( https://www.w3.org/Style/CSS/all-properties.en.json )
  7. *
  8. * Download that file, filter out duplicates and filter the properties based on the wanted standard level
  9. *
  10. * ED - Editors' Draft (not a W3C Technical Report)
  11. * FPWD - First Public Working Draft
  12. * WD - Working Draft
  13. * LC - Last Call Working Draft
  14. * CR - Candidate Recommendation
  15. * PR - Proposed Recommendation
  16. * REC - Recommendation
  17. * NOTE - Working Group Note
  18. */
  19. var fs = require('fs');
  20. var path = require('path');
  21. var request = require('request');
  22. const { camelToDashed } = require('../lib/parsers');
  23. var url = 'https://www.w3.org/Style/CSS/all-properties.en.json';
  24. console.log('Downloading CSS properties...');
  25. function toCamelCase(propName) {
  26. return propName.replace(/-([a-z])/g, function(g) {
  27. return g[1].toUpperCase();
  28. });
  29. }
  30. request(url, function(error, response, body) {
  31. if (!error && response.statusCode === 200) {
  32. var allCSSProperties = JSON.parse(body);
  33. // Filter out all properties newer than Working Draft
  34. var workingDraftAndOlderProperties = allCSSProperties.filter(function(cssProp) {
  35. // TODO: --* css Needs additional logic to this module, so filter it out for now
  36. return cssProp.status !== 'ED' && cssProp.status !== 'FPWD' && cssProp.property !== '--*';
  37. });
  38. // Remove duplicates, there can be many properties in different states of standard
  39. // and add only property names to the list
  40. var CSSpropertyNames = [];
  41. workingDraftAndOlderProperties.forEach(function(cssProp) {
  42. const camelCaseName = toCamelCase(cssProp.property);
  43. if (CSSpropertyNames.indexOf(camelCaseName) === -1) {
  44. CSSpropertyNames.push(camelCaseName);
  45. }
  46. });
  47. var out_file = fs.createWriteStream(path.resolve(__dirname, './../lib/allProperties.js'), {
  48. encoding: 'utf-8',
  49. });
  50. var date_today = new Date();
  51. out_file.write(
  52. "'use strict';\n\n// autogenerated - " +
  53. (date_today.getMonth() + 1 + '/' + date_today.getDate() + '/' + date_today.getFullYear()) +
  54. '\n\n'
  55. );
  56. out_file.write('/*\n *\n * https://www.w3.org/Style/CSS/all-properties.en.html\n */\n\n');
  57. out_file.write('var allProperties = new Set();\n');
  58. out_file.write('module.exports = allProperties;\n');
  59. CSSpropertyNames.forEach(function(property) {
  60. out_file.write('allProperties.add(' + JSON.stringify(camelToDashed(property)) + ');\n');
  61. });
  62. out_file.end(function(err) {
  63. if (err) {
  64. throw err;
  65. }
  66. console.log('Generated ' + Object.keys(CSSpropertyNames).length + ' properties.');
  67. });
  68. } else {
  69. throw error;
  70. }
  71. });