Skip to content

Commit

Permalink
Adding HTML contribution mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SphinxKnight committed Mar 17, 2018
1 parent bb41a89 commit 6961671
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
26 changes: 16 additions & 10 deletions bin/compat-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ commander
.description("A command line tool to test HTML and CSS compatibility between a browser scope and a website")
.option("-url, --url [https://mysite.html]", "Scans the remote web page and fetches/scans associated resources (JS scripts / CSS stylesheets)")
.option("-file, --file [index.html]", "Scans the local file and fetches/scans associated resources", "index.html")
.option("-contrib, --contribute [null|true|all]","Contribution mode to help fill data that isn't known into MDN browser-compat dataset. `null` addresses known features with unknown compatibility values and `true` addresses know features which are known to be compatible but without any detail","null")
.option("-scope, --scope [scope.json]","The browser scope in a JSON format", "scope.json")
.option("-html, --html [myFile.html]", "Only scans the HTML of myFile.html")
.option("-css, --css [myFile.css]", "Only scans the CSS of myFile.css")
.parse(process.argv);

// Exit gracefully, displaying help when no argument is given
if(!process.argv.slice(2).length){
commander.outputHelp(str=>str);
Expand All @@ -42,6 +42,12 @@ if(commander.args.length > 0){
}
}

// Convert different options into an object
const options = {
"contrib": commander.contribute
// Might add other secondary options to this
};

const scope = JSON.parse(fs.readFileSync(commander.scope, "utf-8"));

if(!commander.html && !commander.css && !commander.url){
Expand All @@ -56,7 +62,7 @@ if(!commander.html && !commander.css && !commander.url){
// report =[ {"browser " / "filename" / "line" / "column" / "featureName" / "minVer"]
report.sort(reportHelpers.sortReport);
report.map(reportHelpers.printReportLine);
});
}, options);


// Let's get the CSS inside the site
Expand All @@ -71,7 +77,7 @@ if(!commander.html && !commander.css && !commander.url){
console.log("CSS Report:");
report.sort(reportHelpers.sortReport);
report.map(reportHelpers.printReportLine);
});
}, options);
});
});

Expand All @@ -88,7 +94,7 @@ if(!commander.html && !commander.css && !commander.url){
// console.log("JS Report:");
report.sort(reportHelpers.sortReport);
report.map(reportHelpers.printReportLine);
});
}, options);
});
});
}
Expand All @@ -103,7 +109,7 @@ if(commander.html){
console.log("HTML Report:");
report.sort(reportHelpers.sortReport);
report.map(reportHelpers.printReportLine);
});
}, options);
}

if(commander.css){
Expand All @@ -117,7 +123,7 @@ if(commander.css){
console.log("CSS Report:");
report.sort(reportHelpers.sortReport);
report.map(reportHelpers.printReportLine);
});
}, options);
}

if(commander.js){
Expand All @@ -131,7 +137,7 @@ if(commander.js){
console.log("JavaScript analysis isn't yet implemented - JS Report:");
report.sort(reportHelpers.sortReport);
report.map(reportHelpers.printReportLine);
});
}, options);
}

if(commander.url){
Expand All @@ -147,7 +153,7 @@ if(commander.url){
// report =[ {"browser " / "filename" / "line" / "column" / "featureName" / "minVer"]
report.sort(reportHelpers.sortReport);
report.map(reportHelpers.printReportLine);
});
}, options);

// Let's get the CSS inside the site
cssExtracter.analyzeString(str, commander.url, (e, acc) => {
Expand All @@ -161,7 +167,7 @@ if(commander.url){
console.log("CSS Report:");
report.sort(reportHelpers.sortReport);
report.map(reportHelpers.printReportLine);
});
}, options);
});
});
// Let's get the JavaScript inside the site
Expand All @@ -176,7 +182,7 @@ if(commander.url){
// console.log("JS Report:");
report.sort(reportHelpers.sortReport);
report.map(reportHelpers.printReportLine);
});
}, options);
});
});
}
Expand Down
40 changes: 34 additions & 6 deletions src/html/compat-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ const semver = require("semver");
const readline = require("readline");

// eslint-disable-next-line no-unused-vars
exports.analyzeString = function analyzeString (str, browserScope, lineShift = 0, fileName, callback){
exports.analyzeString = function analyzeString (str, browserScope, lineShift = 0, fileName, callback, options){
const report = [];
let numLine = 1;
const lines = str.split("\n");
const parser = initParser(browserScope, fileName, numLine, report, callback);
const parser = initParser(browserScope, fileName, numLine, report, callback, options);
for(const line of lines){
parser.write(line);
numLine++;
}
parser.end();
};

exports.analyzeFile = function analyzeFile (fileName, browserScope, callback){
exports.analyzeFile = function analyzeFile (fileName, browserScope, callback, options){
const report = [];
const rl = readline.createInterface({
input: fs.createReadStream(fileName),
crlfDelay: Infinity
});
let numLine = 1;

const parser = initParser(browserScope, fileName, numLine, report, callback);
const parser = initParser(browserScope, fileName, numLine, report, callback, options);

rl.on("line", (line) => {
parser.write(line);
Expand All @@ -38,7 +38,7 @@ exports.analyzeFile = function analyzeFile (fileName, browserScope, callback){
};


function initParser (browserScope, fileName, numLine, report, callback){
function initParser (browserScope, fileName, numLine, report, callback, options){
return new htmlParser.Parser({
onopentag: function (name, attribs){
if(bcd.html.elements[name]){
Expand All @@ -54,8 +54,18 @@ function initParser (browserScope, fileName, numLine, report, callback){
"featureVersion":versionAddedElem
});
}
// Intercept any feature that isn't properly filled into MDN according to
// the contribute option
if(options.contrib === "true" && versionAddedElem === true){
// eslint-disable-next-line no-console
console.log("Element <" + name + "> with true in BCD for " + browser + ": https://github.com/mdn/browser-compat-data/blob/master/html/elements/" + name + ".json to fix that 🤘");
}
if(options.contrib === "null" && versionAddedElem === null){
// eslint-disable-next-line no-console
console.log("Element <" + name + "> with null in BCD for " + browser + ": https://github.com/mdn/browser-compat-data/blob/master/html/elements/" + name + ".json to fix that 🤘");
}
Object.keys(attribs).map((attrib)=>{
let versionAddedAttr = null;
let versionAddedAttr;
let featureName = "";
if(bcd.html.elements[name][attrib] && bcd.html.elements[name][attrib].__compat){
versionAddedAttr = bcd.html.elements[name][attrib].__compat.support[browser].version_added;
Expand All @@ -76,6 +86,24 @@ function initParser (browserScope, fileName, numLine, report, callback){
});
}
}
if(options.contrib === "true" && versionAddedAttr === true){
if(featureName.startsWith("global attribute")){
// eslint-disable-next-line no-console
console.log(featureName + " with true in BCD for " + browser + ": https://github.com/mdn/browser-compat-data/blob/master/html/global_attributes.json to fix that 🤘");
} else {
// eslint-disable-next-line no-console
console.log(featureName + " with true in BCD for " + browser + ": https://github.com/mdn/browser-compat-data/blob/master/html/elements/" + name + ".json to fix that 🤘");
}
}
if(options.contrib === "null" && versionAddedAttr === null){
if(featureName.startsWith("global attribute")){
// eslint-disable-next-line no-console
console.log(featureName + " with null in BCD for " + browser + ": https://github.com/mdn/browser-compat-data/blob/master/html/global_attributes.json to fix that 🤘");
} else {
// eslint-disable-next-line no-console
console.log(featureName + " with null in BCD for " + browser + " https://github.com/mdn/browser-compat-data/blob/master/html/elements/" + name + ".json to fix that 🤘");
}
}
});
});

Expand Down

0 comments on commit 6961671

Please sign in to comment.