Skip to content

Commit

Permalink
Empezamos a ordenar los IDL
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilio authored and Emilio committed Feb 11, 2016
1 parent a765cc0 commit 3e904e8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
15 changes: 15 additions & 0 deletions generators/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,24 @@ var html2idl = {
"valuetype": "valueType"
};

var htmlAttrsSpecials={
"class" :{ domName:'className', listName:'classList', rejectSpaces:true},
"for" :{ domName:'htmlFor' },
classList :{ synonym:'class' },
className :{ synonym:'class' },
htmlFor :{ synonym:'for' }
};


lista.forEach(function(linea){
var clave=linea[0];
mapa[clave]=mapa[clave]||{tags:{}, idl:html2idl[clave]||clave};
if(htmlAttrsSpecials[clave].rejectSpaces){
mapa[clave].rejectSpaces=htmlAttrsSpecials[clave].rejectSpaces;
}
if(htmlAttrsSpecials[clave].listName){
mapa[clave].listName=htmlAttrsSpecials[clave].listName;
}
linea[1].split('; ').forEach(function(tagName){
mapa[clave].tags[tagName]={
description:linea[2],
Expand Down
38 changes: 13 additions & 25 deletions js-to-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var validDirectProperties={
if(attrValue==null){
throw new Error('js-to-html: attributes must not contain null value');
}
if((attrName in jsToHtml.htmlAttrs) && (jsToHtml.htmlAttrs[attrName].rejectSpaces)){
if((attrName in jsToHtml.htmlAttributes) && (jsToHtml.htmlAttributes[attrName].rejectSpaces)){
var pattWhiteSpaces=new RegExp( "\\s");
if(pattWhiteSpaces.test(attrValue)){
throw new Error('js-to-html: ' + attrName + 'class attribute could not contain spaces. Use classList attribute.');
Expand All @@ -111,11 +111,10 @@ var validDirectProperties={
/*jshint forin:false */
for(var attrName in attributes){
/*jshint forin:true */
var htmlName = (jsToHtml.htmlAttrs[attrName]||{}).synonym||attrName;
var attrInfo=jsToHtml.htmlAttributes[htmlName];
if(/-/.test(htmlName)){
var attrInfo=jsToHtml.htmlAttributes[attrName];
if(/-/.test(attrName)){
}else if(!attrInfo){
throw new Error("inexistent attribute");
throw new Error("inexistent attribute "+JSON.stringify(attrName));
}else{
if(!attrInfo.tags[o.tagName] && !attrInfo.tags["HTML elements"]){
throw new Error("attribute "+JSON.stringify(attrName)+" does not match with tagName "+JSON.stringify(o.tagName)+"");
Expand Down Expand Up @@ -213,12 +212,8 @@ HtmlBase.prototype.attributesToHtmlText=function attributesToHtmlText(){
return Object.keys(this.attributes).map(function(attrName){
var attrVal=this.attributes[attrName];
var textAttrVal=attrVal;
var attrDefinition=jsToHtml.htmlAttrs[attrName] || {};
if(attrDefinition.synonym){
attrName=attrDefinition.synonym;
attrDefinition=jsToHtml.htmlAttrs[attrName];
}
if(attrDefinition.listType && typeof attrVal!=="string"){
var attrDefinition=jsToHtml.htmlAttributes[attrName] || {};
if(attrDefinition.listName && typeof attrVal!=="string"){
textAttrVal=attrVal.join(' ');
}
var escapedAttrVal=escapeChar(textAttrVal);
Expand Down Expand Up @@ -348,15 +343,6 @@ jsToHtml.indirect=function indirect(tagName,contentOrAttributes,contentIfThereAr
});
};

jsToHtml.htmlAttrs={
"class" :{ domName:'className', listType:'classList', rejectSpaces:true},
"for" :{ domName:'htmlFor' },
classList :{ synonym:'class' },
className :{ synonym:'class' },
htmlFor :{ synonym:'for' }
};


// https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
jsToHtml.htmlTags={
"a" :{type:'HTML4', description:"Defines a hyperlink"},
Expand Down Expand Up @@ -594,7 +580,9 @@ jsToHtml.htmlAttributes={
"tags": {
"HTML elements": {"description": "Classes to which the element belongs","value": "Set of space-separated tokens"}
},
"idl": "className"
"idl": "className",
"rejectSpaces": true,
"listName": "classList"
},
"cols": {
"tags": {
Expand Down Expand Up @@ -1401,13 +1389,13 @@ jsToHtml.Html.prototype.create = function create(){
if(/-/.test(attr)){
element.setAttribute(attr, value);
}else{
var defAttr=jsToHtml.htmlAttrs[attr]||{};
if(('listType' in defAttr) && (typeof value!=="string")){
var defAttr=jsToHtml.htmlAttributes[attr]||{};
if(('listName' in defAttr) && (typeof value!=="string")){
Array.prototype.forEach.call(value,function(subValue){
element[defAttr.listType].add(subValue);
element[defAttr.listName].add(subValue);
});
}else{
element[defAttr.domName||attr] = value;
element[defAttr.idl] = value;
}
}
},this);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-to-html",
"description": "Create HTML text from JS object",
"version": "0.7.0",
"version": "0.8.0",
"author": "Codenautas <codenautas@googlegroups.com>",
"license": "MIT",
"repository": "codenautas/js-to-html",
Expand Down
25 changes: 11 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ describe('js-to-html', function(){
// esto es inválido, no entiendo por qué lo pusimos
var input=direct({
tagName:'input',
attributes:{'class':'date'},
content:[direct({textNode: '3/12/2015'})]
attributes:{'class':'date', value:direct({textNode: '3/12/2015'})},
content:[]
});
expect(input).to.be.a(jsToHtml.Html);
var htmlText=input.toHtmlText();
expect(htmlText).to.eql("<input class=date>3/12/2015");
expect(htmlText).to.eql('<input class=date value="2015/12/3">');
});
it('should exclude null and undefined in content', function(){
var p=html.p(['sí', null, html.img(), undefined, 1, '', 'no', 0]);
Expand Down Expand Up @@ -198,16 +198,6 @@ describe('js-to-html', function(){
html.p({"class":['names', 'other']},'text').toHtmlText()
).to.eql("<p class='names other'>text</p>");
});
it('should understand synonyms with definition', function(){
expect(
html.p({"classList":['names', 'other']},'text').toHtmlText()
).to.eql("<p class='names other'>text</p>");
});
it('should understand synonyms without definition', function(){
expect(
html.p({"classList":['names', 'other']},'text').toHtmlText()
).to.eql("<p class='names other'>text</p>");
});
it('should accept numbers', function(){
expect(html.p([html.span(3),1.1]).toHtmlText()).to.eql(
"<p><span>3</span>1.1</p>"
Expand Down Expand Up @@ -391,7 +381,7 @@ describe('js-to-html', function(){
it('must reject inexistent attributes',function(){
expect(function(){
direct({tagName:'p', attributes:{thisnotexists:'one'}, content:[]})
}).to.throwError(/inexistent attribute/);
}).to.throwError(/inexistent attribute "thisnotexists"/);
})
it('must reject not matching attributes',function(){
expect(function(){
Expand Down Expand Up @@ -527,6 +517,13 @@ if(typeof document !== 'undefined'){
done
);
});
it('should translate HTML attributes to IDL attributes', function(done){
control(
html.td({colspan:3}),
{colSpan:3},
done
);
});
});
});
}

0 comments on commit 3e904e8

Please sign in to comment.