最近我发现在 HTML5 中可能有一种新的评论方式。
而不是典型的 <!-- -->多行评论 我读过,我想 我注意到我的 IDE 做了一个常规的 <!div >注释掉了。所以我对其进行了测试,令我惊讶的是 Chrome 已经注释掉了该标签。它只注释掉了标签而不是div 的内容,所以我不得不注释掉越近的<!/div>。以避免关闭其他 div。
我测试了另一个,似乎一般在任何标签的开头都放一个感叹号,这个符号< , 使该标记被注释掉。
这真的是新的吗?这是不好的做法吗?它实际上非常方便,但是否实用(如果不是新的)?
额外的细节:
虽然语法错误或对这种特定语法的误解是一个很好的理由,但 Chrome 为什么实际上将它们呈现为完整注释?
代码写成:
<!div displayed> some text here that is still displayed <!/div>
然后呈现为:
<!--div displayed--> some text here that is still displayed <!--/div-->
最佳答案
HTML5 中没有新的注释标准。唯一有效的注释语法仍然是 <!-- --> .来自 section 8.1.6 of W3C HTML5 :
Comments must start with the four character sequence U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS, U+002D HYPHEN-MINUS (
<!--).
<!语法起源于 SGML DTD 标记,它不是 HTML5 的一部分。在 HTML5 中,它保留用于注释、CDATA 部分和 DOCTYPE 声明。因此,这种替代方法是否是不好的做法取决于您是否认为使用(或更糟的是,依赖)过时的标记是不好的做法。
Validator.nu 将您的内容称为“虚假评论”。 — 这意味着即使它不是有效评论,它也会被视为评论。这大概是为了与基于 SGML 的 HTML5 之前的版本向后兼容,并且具有采用 <!FOO> 形式的标记声明。 ,所以我不会称之为新的。它们被视为 注释的原因是因为 SGML 标记声明是不打算呈现的特殊声明,但由于它们在 HTML5 中毫无意义(除了上述异常(exception)),就 HTML5 DOM 而言担心他们只不过是评论。
以下步骤在section 8.2.4内得出这个结论,Chrome 似乎完全遵循了这个结论:
Consume the next input character:
"<" (U+003C)
Switch to the tag open state.
Consume the next input character:
"!" (U+0021)
Switch to the markup declaration open state.
8.2.4.45 Markup declaration open state :
If the next two characters are both "-" (U+002D) characters, consume those two characters, create a comment token whose data is the empty string, and switch to the comment start state.
Otherwise, if the next seven characters are an ASCII case-insensitive match for the word "DOCTYPE", then consume those characters and switch to the DOCTYPE state.
Otherwise, if there is an adjusted current node and it is not an element in the HTML namespace and the next seven characters are a case-sensitive match for the string "[CDATA[" (the five uppercase letters "CDATA" with a U+005B LEFT SQUARE BRACKET character before and after), then consume those characters and switch to the CDATA section state.
Otherwise, this is a parse error. Switch to the bogus comment state. The next character that is consumed, if any, is the first character that will be in the comment.
注意它说只有当遇到的字符序列是<!--时才切换到注释开始状态。 ,否则就是虚假评论。这反射(reflect)了上面第 8.1.6 节中所述的内容。
8.2.4.44 Bogus comment state :
Consume every character up to and including the first ">" (U+003E) character or the end of the file (EOF), whichever comes first. Emit a comment token whose data is the concatenation of all the characters starting from and including the character that caused the state machine to switch into the bogus comment state, up to and including the character immediately before the last consumed character (i.e. up to the character just before the U+003E or EOF character), but with any U+0000 NULL characters replaced by U+FFFD REPLACEMENT CHARACTER characters. (If the comment was started by the end of the file (EOF), the token is empty. Similarly, the token is empty if it was generated by the string "
<!>".)
用简单的英语来说,这变成了<!div displayed>进入<!--div displayed-->和 <!/div>进入<!--/div--> ,完全按照问题中的描述。
最后一点,您可能期望其他符合 HTML5 的解析器的行为与 Chrome 相同。
关于html - HTML5 是否改变了 HTML 评论的标准?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29405858/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI