TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Single-header C++11 HTML document constructor

87 pointsby tinfoilboyover 6 years ago

2 comments

tlbover 6 years ago
It&#x27;s worth getting the escaping right in a library like this. For example, at <a href="https:&#x2F;&#x2F;github.com&#x2F;tinfoilboy&#x2F;CTML&#x2F;blob&#x2F;master&#x2F;include&#x2F;ctml.hpp#L215" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tinfoilboy&#x2F;CTML&#x2F;blob&#x2F;master&#x2F;include&#x2F;ctml....</a><p><pre><code> for (const auto&amp; attr : m_attributes) output &lt;&lt; &quot; &quot; &lt;&lt; attr.first + &quot;=\&quot;&quot; &lt;&lt; attr.second + &quot;\&quot;&quot;; </code></pre> it&#x27;ll generate incorrect HTML if an attribute value has a &quot; character. Although early versions of HTML were kind of vague on how escaping was supposed to work, the HTML5 standard explains it in detail.
评论 #18695843 未加载
Negitivefragsover 6 years ago
I thought about this for a little while, and I think I don&#x27;t like the API for this library. There are two things in particular I don&#x27;t like. The first is that I think I would prefer to have helper types for each tag type so you don&#x27;t have to include them as strings all the time, and the second is that I don&#x27;t like the AppendChild approach that this library takes.<p>I would change it so that you pass the document to the constructor of each element and the scoping of each variable effectively determines the relationships. As a 10 second example of what I mean:<p><pre><code> HTML::document d; HTML::body b( d ); HTML::div div( d ); HTML::p p( d ); d.text( &quot;Hello world&quot; ); &lt;html&gt;&lt;body&gt;&lt;div&gt;&lt;p&gt;Hello world&lt;&#x2F;p&gt;&lt;&#x2F;div&gt;&lt;&#x2F;body&gt;&lt;&#x2F;html&gt; </code></pre> The reason I like this because it maps very well onto a C++ programmers natural understanding of the stack frame and RAII, and in addition it can be implemented with needing to store any state inside the node classes. This means that only HTML::document would need to actually allocate any memory, and it would just be a single text stream.<p>This wouldn&#x27;t create a node hierarchy in memory, so it&#x27;s not a DOM like this library creates, but if you are just looking to output HTML quickly, then I think it would be easier to use and more efficient.
评论 #18696025 未加载
评论 #18695874 未加载
评论 #18696532 未加载
评论 #18698193 未加载