Skip to content

Commit d83b784

Browse files
authored
Merge pull request #91 from Microsoft/octref/mdn
[WIP] Use MDN to enhance CSS LS. Part of #68
2 parents 33a29a9 + 5b2a127 commit d83b784

12 files changed

+1957
-367
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,8 @@ License
6161
(MIT License)
6262

6363
Copyright 2016, Microsoft
64+
65+
With the exceptions of,
66+
67+
- `build/mdn-documentation.js`, which is built upon content from [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web)
68+
and distributed under CC BY-SA 2.5.

build/generate_browserjs.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ function toSource(object, keyName) {
343343

344344
var parser = new xml2js.Parser({explicitArray : false});
345345
var schemaFileName= 'css-schema.xml';
346+
347+
var { buildPropertiesWithMDNData } = require('./mdn-data-importer')
348+
346349
fs.readFile(path.resolve(__dirname, schemaFileName), function(err, data) {
347350
parser.parseString(data, function (err, result) {
348351

@@ -360,7 +363,7 @@ fs.readFile(path.resolve(__dirname, schemaFileName), function(err, data) {
360363
atdirectives: atdirectives,
361364
pseudoclasses: pseudoclasses,
362365
pseudoelements: pseudoelements,
363-
properties: properties
366+
properties: buildPropertiesWithMDNData(properties)
364367
}
365368
};
366369

@@ -376,7 +379,7 @@ fs.readFile(path.resolve(__dirname, schemaFileName), function(err, data) {
376379
' * Copyright (c) Microsoft Corporation. All rights reserved.',
377380
' * Licensed under the MIT License. See License.txt in the project root for license information.',
378381
' *--------------------------------------------------------------------------------------------*/',
379-
'// file generated from ' + schemaFileName + ' using css-exclude_generate_browserjs.js',
382+
'// file generated from ' + schemaFileName + ' and https://github.com/mdn/data using css-exclude_generate_browserjs.js',
380383
'',
381384
'export const data : any = ' + toJavaScript(resultObject) + ';',
382385
'export const descriptions : any = ' + toJavaScript(descriptions) + ';',

build/mdn-data-importer.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
const path = require('path')
7+
const fs = require('fs')
8+
9+
const mdnDocumentations = require('./mdn-documentation')
10+
11+
const mdnExcludedProperties = [
12+
'--*', // custom properties
13+
'gap', // grid-gap
14+
'row-gap', // grid-row-gap
15+
'image-resolution', // https://www.w3.org/TR/css-images-4/#propdef-image-resolution
16+
]
17+
18+
function buildPropertiesWithMDNData(vscProperties) {
19+
const propertyMap = {}
20+
21+
const mdnProperties = require('mdn-data/css/properties.json')
22+
const mdnAtRules = require('mdn-data/css/at-rules.json')
23+
24+
// Flatten at-rule properties and put all properties together
25+
const allMDNProperties = mdnProperties
26+
for (const atRuleName of Object.keys(mdnAtRules)) {
27+
if (mdnAtRules[atRuleName].descriptors) {
28+
for (const atRulePropertyName of Object.keys(mdnAtRules[atRuleName].descriptors)) {
29+
allMDNProperties[atRulePropertyName] = mdnAtRules[atRuleName].descriptors[atRulePropertyName]
30+
}
31+
}
32+
}
33+
34+
mdnExcludedProperties.forEach(p => {
35+
delete allMDNProperties[p]
36+
})
37+
38+
/**
39+
* 1. Go through VSC properties. For each entry that has a matching entry in MDN, merge both entry.
40+
*/
41+
vscProperties.forEach(p => {
42+
if (p.name) {
43+
if (allMDNProperties[p.name]) {
44+
propertyMap[p.name] = {
45+
...p,
46+
...extractMDNProperties(allMDNProperties[p.name])
47+
}
48+
} else {
49+
propertyMap[p.name] = p
50+
}
51+
}
52+
})
53+
54+
/**
55+
* 2. Go through MDN properties. For each entry that hasn't been recorded, add it with empty description.
56+
*/
57+
for (const pn of Object.keys(allMDNProperties)) {
58+
if (!propertyMap[pn]) {
59+
propertyMap[pn] = {
60+
name: pn,
61+
desc: mdnDocumentations[pn] ? mdnDocumentations[pn] : '',
62+
restriction: 'none',
63+
...extractMDNProperties(allMDNProperties[pn])
64+
}
65+
}
66+
}
67+
68+
return Object.values(propertyMap)
69+
}
70+
71+
/**
72+
* Extract only the MDN data that we use
73+
*/
74+
function extractMDNProperties(mdnEntry) {
75+
return {
76+
status: mdnEntry.status,
77+
syntax: mdnEntry.syntax
78+
}
79+
}
80+
81+
module.exports = {
82+
buildPropertiesWithMDNData
83+
}

build/mdn-documentation.js

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@types/mocha": "^2.2.33",
1919
"@types/node": "^7.0.43",
2020
"istanbul": "^0.4.5",
21+
"mdn-data": "^1.1.1",
2122
"mkdirp": "^0.5.1",
2223
"mocha": "^5.0.4",
2324
"rimraf": "^2.6.2",

0 commit comments

Comments
 (0)