简体   繁体   English

HTML 5、内联 SVG 和 SVG DOM 的命名空间感知

[英]HTML 5, inline SVG, and namespace awareness for SVG DOM

The following questions are confusing me.以下问题让我感到困惑。 I know they are related, but...我知道他们是相关的,但是...

  1. Is HTML 5 namespace aware (for including tags of SVG/other XML dialects)?是否支持 HTML 5 命名空间(用于包含 SVG/其他 XML 方言的标签)?
  2. If it is not, then what about this -如果不是,那这个怎么办——

    I have read this old link, but I am totally confused... because Mozilla says "to dynamically modify inline SVG, scripting needs to be done this way" - so finally, how can I dynamically modify inline SVG (if HTML 5 is not namespace aware)?我已经阅读了这个链接,但我很困惑......因为 Mozilla 说“要动态修改内联 SVG,脚本需要以这种方式完成” - 所以最后,我怎样才能动态修改内联 SVG(如果 HTML 5 不是命名空间感知)?

  3. Or the page needs to be served as (X)HTML 5?或者页面需要作为 (X)HTML 5 提供?


Details - 细节 -

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>hello-SVG</title> </head> <body> <svg width="200" height="200"> <rect x="0" y="0" width="100" height="100" fill="blue"></rect> </svg> </body> </html>

The above code is the correct way to render a rect (using SVG) in HTML 5. Now, to modify the SVG using JavaScript, Mozilla recommends using this API.上面的代码是在 HTML 5 中渲染rect (使用 SVG)的正确方法。现在,要使用 JavaScript 修改 SVG,Mozilla 建议使用API。 And my question is, what is the point of doing so if HTML 5 is not namespace aware?我的问题是,如果 HTML 5 不支持命名空间,那么这样做有什么意义? For such cases do browsers automatically switch to (X)HTML 5?对于这种情况,浏览器会自动切换到 (X)HTML 5 吗?

I read this comment on SO, and I find it closest to the answer I'm looking for -我在 SO 上阅读了此评论,我发现它最接近我正在寻找的答案 -

I guess the HTML 5 SVG situation is basically "SVG without a namespace gets the namespace added during parsing (but after that it's just like (X)HTML before)".我猜 HTML 5 SVG 的情况基本上是“没有命名空间的 SVG 在解析期间添加了命名空间(但之后就像之前的 (X)HTML)”。

HTML5 defines HTML, XHTML and the DOM. HTML5 定义了 HTML、XHTML 和 DOM。

The DOM is namespace aware. DOM 是命名空间感知的。 When you use DOM methods you must take into account which namespace each element is in, but the default is the HTML ( http://www.w3.org/1999/xhtml ) namespace.当您使用 DOM 方法时,您必须考虑每个元素所在的命名空间,但默认为 HTML ( http://www.w3.org/1999/xhtml ) 命名空间。

HTML and XHTML are serializations that are converted into DOMs by parsing . HTML 和 XHTML 是通过解析转换为 DOM 的序列化。

XHTML is namespace aware and XHTML documents apply namespaces according to the rules of XML, so all namespaces must be assigned to each element explicitly . XHTML 是名称空间感知的,XHTML 文档根据 XML 规则应用名称空间,因此所有名称空间必须显式分配给每个元素 XHTML is converted to a DOM using an XML parser.使用 XML 解析器将 XHTML 转换为 DOM。

HTML is also namespace aware, but namespaces are assigned implicitly . HTML 也是命名空间感知的,但命名空间是隐式分配的 HTML is converted to a DOM using an HTML parser, which knows which elements go in which namespace.使用 HTML 解析器将 HTML 转换为 DOM,该解析器知道哪些元素位于哪个命名空间中。 That is, it knows that <div> goes in the http://www.w3.org/1999/xhtml namespace and that <svg> goes in the http://www.w3.org/2000/svg namespace.也就是说,它知道<div>进入http://www.w3.org/1999/xhtml命名空间,而<svg>进入http://www.w3.org/2000/svg命名空间。 Elements like <script> can go in either the http://www.w3.org/1999/xhtml or the http://www.w3.org/2000/svg namespace depending on the context in which they appear in the HTML code. <script>之类的元素可以进入http://www.w3.org/1999/xhtmlhttp://www.w3.org/2000/svg命名空间,具体取决于它们在 HTML 中出现的上下文代码。

The HTML parser knows about HTML elements , SVG elements , and MathML elements and no others . HTML 解析器只知道HTML elementsSVG elementsMathML elements不知道其他元素。 There is no option to use elements from other namespaces, neither implicitly nor explicitly .没有使用来自其他名称空间的元素的选项,无论是隐式的还是显式的。 That is, xmlns attributes have no effect.也就是说, xmlns属性没有任何作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM