TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

87 点作者 tinfoilboy超过 6 年前

2 条评论

tlb超过 6 年前
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 未加载
Negitivefrags超过 6 年前
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 未加载