Skip to content

Commit

Permalink
Valida los nombres de los atributos.
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilio authored and Emilio committed Jan 6, 2016
1 parent 8396248 commit 58354f4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
21 changes: 19 additions & 2 deletions js-to-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,24 @@ var validDirectProperties={
}
}
return true;
}}
}},
{check:function(attributes, o){
/*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)){
}else if(!attrInfo){
throw new Error("inexistent attribute");
}else{
if(!attrInfo[o.tagName] && !attrInfo["HTML elements"]){
throw new Error("attribute does not match with tagName");
}
}
}
return true;
}},
]},
content:{checks:[
{check:function(x){ return typeof x==="object" && x instanceof Array; }, text:"must be an Array"},
Expand Down Expand Up @@ -313,7 +330,7 @@ jsToHtml.direct=function direct(directObject){
jsToHtml.indirect=function indirect(tagName,contentOrAttributes,contentIfThereAreAttributes){
var thereAreAttributes=isPlainObject(contentOrAttributes);
if(!thereAreAttributes && contentOrAttributes instanceof Object && !(contentOrAttributes instanceof Array)){
throw new Error('jsToHmlt.'+tagName+' expects plain object of attributes or array of content');
throw new Error('jsToHtml.'+tagName+' expects plain object of attributes or array of content');
}
var attributes = thereAreAttributes?contentOrAttributes:{};
var content = thereAreAttributes?contentIfThereAreAttributes:contentOrAttributes;
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ describe('js-to-html', function(){
});
it('should delimite with simple quotes attribute value if contains some not alphabetic chars', function(){
expect(
html.p({"class":'names', title:'this title', empty:''},'text').toHtmlText()
).to.eql("<p class=names title='this title' empty=''>text</p>");
html.p({"class":'names', title:'this title', lang:''},'text').toHtmlText()
).to.eql("<p class=names title='this title' lang=''>text</p>");
});
it('should escape text', function(){
expect(direct({textNode:'esto < esto & > aquello \'sí\' y "no"'}).toHtmlText()).to.eql(
Expand Down Expand Up @@ -267,7 +267,7 @@ describe('js-to-html', function(){
});
it('should reject content for void elements', function(){
expect(function(){
direct({tagName:'link', attributes:{src: "http://correct.com"}, content:["content"]})
direct({tagName:'link', attributes:{href: "http://correct.com"}, content:["content"]})
}).to.throwError(/void elements must not have content/);
});
});
Expand Down

0 comments on commit 58354f4

Please sign in to comment.