HTML Mode 下 <video> <source> 的缩进问题

这样一个 HTML5 Video 元素:

<video controls>
  <source src="rabbit320.webm" type="video/webm">
  <source src="rabbit320.mp4" type="video/mp4">
  <p>Your browser doesn't support HTML5 video.</p>
</video>

C-x h TAB 会缩进成:

<video controls>
  <source src="rabbit320.webm" type="video/webm">
    <source src="rabbit320.mp4" type="video/mp4">
      <p>Your browser doesn't support HTML5 video.</p>
</video>

显然 Emacs 认为 <source> 标签必须要闭合,但其实不闭合也行。不知道有没有办法让 Emacs 的 HTML Mode 知道这里的 <source> 不闭合也行?就像 <img> <br>

目前为了能让 Emacs 正确地缩进,我不得不做这样的修改:

-<source src="rabbit320.webm" type="video/webm">
+<source src="rabbit320.webm" type="video/webm"/>

把 source 加进去就可以了:

(setq-local sgml-empty-tags (append sgml-empty-tags '("source")))

也可以用 web-mode,避开这个问题。

1 个赞

找到解决方法了,把 source 加入 sgml-empty-tags 即可。

sgml-empty-tags is a variable defined in sgml-mode.el.

Its value is ("area" "base" "basefont" "br" "col" "frame" "hr" "img" "input" "isindex" "link" "meta" "param" "wbr")

Local in buffer foo.html; global value is nil

Documentation:

List of tags whose !ELEMENT definition says EMPTY.

(defun chunyang-html-mode-setup ()
  (add-to-list 'sgml-empty-tags "source"))

(add-hook 'html-mode-hook #'chunyang-html-mode-setup)

web-mode 更好一点