Issue with unclosed img tag

夙愿已清 提交于 2019-12-12 12:46:58

问题


data presented in HTML format and submitted to server, that does some preprocessing.

It operates with "src" attribute of "img" tag.

After preprocessing and saving, all the preprocessed "img" tags are not self-closed.

For example, if "img" tag was following:

<img src="image.png" />

after preprocessing with Nokogiri or Hpricot, it will be:

<img src="/preprocessed_path/image.png">

The code is pretty simple:

doc = Hpricot(self.content)
doc.search("img").each do |tag|
  preprocess tag
end
self.content = doc.to_html

For Nokorigi, it looks the same.

How to resolve this issue ?


Update 1

Forget to mention - i have HTML 5 page, which i'm trying to validate with W3C Validator.

When "img" tag is inside a div, it complaints about following:

required character (found d) (expected i)
</div>

For example, try to validate following code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8" />
</head>
<body>
    <div>
        <img src="image.png">
    </div>
</body>
</html>

You will get the same error:

Line 9, Column 4: required character (found d) (expected i)
</div>

回答1:


I think the problem is with your <html> where it delcares the xmlns attribute as XHTML. This seems like it would be contradictory to the fact that it's not an XHTML document. If you remove this xmlns attribute, it should be valid.

<!DOCTYPE html>
<html>
  <head>
  <meta charset="utf-8" />
  <title>something here</title>
</head>
<body>
  <div>
    <img src="image.png">
  </div>
</body>
</html>



回答2:


The problem is that your libraries are generating correct HTML, and the trailing "/" is not correct in HTML. Unless you're serving application/xhtml+xml, there's no point in having it there at all — the IMG tag is self-closing in all versions of HTML, and the "/" is meaningless. If you are serving application/xhtml+xml, I think you'll need to explicitly use Nokogiri's to_xhtml.




回答3:


In the preprocess function you are delegating to, do you not have control over each img tag? Can you not return what it is already return and append an explicit close tag?



来源:https://stackoverflow.com/questions/4220795/issue-with-unclosed-img-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!