Skip to content

Commit

Permalink
Fixture de strings para ES 6. Close #7
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed May 9, 2016
1 parent d420746 commit 9b8118b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
30 changes: 20 additions & 10 deletions lib/js-from-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ jsFromHtml.parse = function parse(htmlText){
return res;
};

jsFromHtml.parseTextNode = function parseTextNode(text, pad) {
jsFromHtml.parseTextNode = function parseTextNode(text, pad, firstChild, versionES) {
//console.log("parseTextNode() <- ", JSON.stringify(text))
var lines = text.split(/\r?\n/);
if(versionES===6 && lines.length>1) { return '`'+((lines[0]==='' && firstChild) ?'\n':'')+text+'`'; }
//var lines = text.split(/\n/);
// remover dos \n seguidos al principio
// ver: https://www.w3.org/TR/html5/syntax.html#element-restrictions
Expand All @@ -110,8 +111,6 @@ jsFromHtml.parseTextNode = function parseTextNode(text, pad) {
}
};



function hasDisplayBlock(tagName) {
return jsToHtml.htmlTags[tagName]['display']==='block';
}
Expand All @@ -125,12 +124,14 @@ function hasDisplayBlock(tagName) {
jsFromHtml.cdoToSource = function cdoToSource(tag, pad, opts) {
var out = [];
var margen = ' ';
var fromPretty = opts && opts.fromPretty;
var opts = opts || {};
var fromPretty = opts.fromPretty;
var versionES = opts.versionES ? opts.versionES : 5;
if(tag instanceof jsToHtml.Html) {
if(pad.length!==0) {
out.push('\n');
}
var prettyPrint = fromPretty && hasDisplayBlock(tag.tagName);
var prettyParent = fromPretty && hasDisplayBlock(tag.tagName);
out.push(pad+'html.'+tag.tagName+'(');
var attrs = [];
var idPos = -1;
Expand Down Expand Up @@ -182,17 +183,26 @@ jsFromHtml.cdoToSource = function cdoToSource(tag, pad, opts) {
var obj = tag.content[elem];
var cont=[];
if(obj instanceof jsToHtml.Html || obj instanceof jsToHtml.HtmlComment) {
var prettyPrint = fromPretty && hasDisplayBlock(obj.tagName);
//console.log(" ES HTML")
++haveObjs;
cont.push(jsFromHtml.cdoToSource(obj, pad+margen)+',');
var prettyChild = fromPretty && hasDisplayBlock(obj.tagName);
var txt = jsFromHtml.cdoToSource(obj, pad+margen, opts)+',';
//console.log(" txt ["+txt+"]");
cont.push(/*prettyChild?txt.trim():*/txt);
} else {
if(tag.content.length !== 1) { cont.push('\n'+pad+margen); }
cont.push(jsFromHtml.parseTextNode(obj.textNode, pad));
if(tag.content.length !== 1) { cont.push(','); }
var txt = jsFromHtml.parseTextNode(obj.textNode, pad, (elem===0), versionES);
//console.log("prettyParent", prettyParent)
if(prettyParent) { txt = txt.trim(); }
if(txt !== '') {
if(tag.content.length > 1) { cont.push('\n'+pad+margen); }
cont.push(txt);
if(tag.content.length > 1) { cont.push(','); }
}
}
allCont.push(cont.join(''));
}
if(allCont.length>0) {
//console.log("out: ", out, "allCont.length:", allCont.length, "attrs.length:", attrs.length)
if(attrs.length) { out.push(', '); }
if(haveObjs) {
out.push('[');
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/pseudo-pp-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ html.body([
`:
`,
]),
html.pre(`
Array.prototype.forEach.call(document.body.childNodes, function(node){
html.pre(`Array.prototype.forEach.call(document.body.childNodes, function(node){
console.log(JSON.stringify(node.textContent));
});
`),
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("jsFromHtml from fixtures", function(){
{fileName: 'fixture1c.js' , },
{fileName: 'ejemplo.html' , },
{fileName: 'pseudo-pp.html'},
{fileName: 'pseudo-pp.html', skip: '#7', jsName: 'pseudo-pp-es6.js', versionES:6},
{fileName: 'pseudo-pp.html', jsName: 'pseudo-pp-es6.js', versionES:6},
{fileName: 'from-pp.html' , skip: '#8', fromPretty:true},
].forEach(function(fixtureInfo){
var fileName = fixtureInfo.fileName;
Expand Down

0 comments on commit 9b8118b

Please sign in to comment.