1

I'm making a framework for building simple html websites for an embedded system and I want to make it bulletproof in a way that a user can't make mistakes in building the html document. As I've learned, it is hard to make and maintain an html markup inside c++ code. In one framework that I found (only one I could find) you could add any tag and add any attribute to one node. I want to control that. To do so I need to have a lot of data which I've gathered from w3schools.com. My next step would be putting the data into the code.

To explain briefly:

  • I have a list of allowed tags
  • Each tag supports a list of global attributes
  • Each tag supports a list of event attributes
  • Some of the tags have a tag-specific attributes
  • Each attribute has a list of allowed values

The problem I'm having is how to properly connect those lists. I could put everything into enums, but that would defeat the purpose of this framework. I could also have a class for each of the tags but would result in a lot of redundant code.

The end result I wish to have:

//tag temp(tag.type); 
// OR
//tag.type temp; 
tag.a temp;
//temp.addAttribute(attribute.type.value)
temp.addAttribute(att.href("http://www.w3schools.com"));
temp.addAttribute(att.target.blank);
temp.addAttribute(att.onclick("alert('You are goint away :(')"));
temp.value("Visit W3Schools!");
temp.ToString()
//document.addNode(temp);
//this will produce the following tag
//<a href="http://www.w3schools.com" target="_blank" onclick="alert('You are goint away :(')">Visit W3Schools!</a>

Any idea on how to structure the data to achieve this or something similarly?

  • I'm a little confused. 1) Your example seems to contradict the claim that "Each attribute has a list of allowed values", since you're allowing arbitrary URLs, Javascript into the href and onclick attributes. 2) Why would using enums for the whitelisted values "defeat the purpose of this framework"? 3) A bunch of const std::maps from strings to lists of strings seems like an obvious and adequate way to store most of your "this tag allows these attributes" information (assuming you have C++11 to initialize them properly). Any reason you haven't considered that? – Ixrec Mar 24 '16 at 0:03
  • 1) some attributes have specific values ( target="_blank | _self | _parent | _top") and some allow arbitrary strings (id, class, name). 2) if I would to do that I would have to make a constructor for each individual tag and adding additional code to handle the differences between them. But now that I think of it, it wouldn't be that bad. 3) I did think of that for storing the data of individual node. But how would I limit what gets put in? enum class tag { div, a, h1}; map<tag, ?> ? – CodeBreaker Mar 24 '16 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.