浏览器中的 Include 指令处理

本文档描述了在浏览器环境中(xmlhttprequest IO 模块)如何处理 include 指令。这种处理方式与文件系统中的处理略有不同。以下是规则的应用顺序:

在这些定义中,有几个常见的术语与 include 指令相关:

  • target:文档中指定的 include 指令的目标。

  • include path:已解析的 include 路径;通常是绝对路径或 URL。

  • relative path:相对于根文档的 include 路径,除非 include path 是绝对路径。

  • base dir:处理器定义的基础目录;通常与根文档所在的目录相同。

  • parent dir:包含嵌套 include 的文件的目录;嵌套的 include 相对于包含它的文件进行解析。

由于浏览器无法知道文档的预期环境,路径解析器会初始化为考虑包含正斜杠和反斜杠的路径。

顶级 include

  1. 如果目标以 file:// 开头,目标既是 include path 也是相对路径。在这种情况下,base dir 的值没有影响。

  2. 如果目标是绝对 URL(即以 http://https:// 开头),则目标必须是 base dir 的子路径,或者文档必须设置 allow-uri-read 属性。如果其中之一成功,目标既是 include path 也是相对路径。如果这两者都失败,include 被替换为一个链接。

  3. 如果目标是绝对路径(即以 POSIX 根、Windows 驱动器字母或 UNC 路径开头),则 file:// 协议(如果路径以 Windows 驱动器字母开头,则为 file:///)会被加到目标路径前,结果既是 include path 也是相对路径。在这种情况下,base dir 的值没有影响。

  4. 如果目标是相对路径,并且 base dir 等于 .,目标既是 include path 也是相对路径。include path 会相对于 window.location.href 解析(遵循 XMLHttpRequest 的行为)。

  5. 如果目标是相对路径,并且 base dirfile:// 开头,则 base dir 会加到目标路径前形成 include path。目标作为相对路径使用。

  6. 如果目标是相对路径,并且 base dir 是相对路径,则 base dir 会加到目标路径前形成 include pathinclude path 会相对于 window.location.href 解析(遵循 XMLHttpRequest 的行为)。目标作为相对路径使用。

  7. 如果目标是相对路径,并且 base dir 是绝对 URL(即以 http://https:// 开头),则 base dir 会加到目标路径前并作为 include path 使用,目标作为相对路径使用。

嵌套 include

  • 如果目标以 file:// 开头,目标既是 include path 也是相对路径。在这种情况下,base dir 的值没有影响。

  • 如果目标是 URL(以 http://https:// 开头),则目标必须是 base dir 的子路径,或者文档必须设置 allow-uri-read 属性。如果这两者都失败,include 被替换为一个链接。如果其中之一成功,目标既是 include path 也是相对路径。

  • 如果目标是绝对路径(即以 POSIX 根、Windows 驱动器字母或 UNC 路径开头),则 file:// 协议(如果路径以 Windows 驱动器字母开头,则为 file:///)会被加到目标路径前,结果既是 include path 也是相对路径。在这种情况下,base dir 的值没有影响。

  • 如果目标是相对路径,并且 parent dir 等于 .,目标既是 include path 也是相对路径。include path 会相对于 window.location.href 解析(遵循 XMLHttpRequest 的行为)。

  • 如果目标是相对路径,并且 parent dirfile:// 开头,则 parent dir 会加到目标路径前形成 include path。如果 base dir 等于 .,或者 include path 不属于 base dir,则 include path 被作为相对路径使用。如果 include path 属于 base dir,则相对路径为路径差。

  • 如果目标是相对路径,并且 parent dir 是相对路径,则 parent dir 会加到目标路径前形成 include pathinclude path 会相对于 window.location.href 解析(遵循 XMLHttpRequest 的行为)。如果 base dir 等于 .,或者 include path 不属于 base dir,则 include path 被作为相对路径使用。如果 include path 属于 base dir,则相对路径为路径差。

  • 如果目标是相对路径,并且 parent dir 是绝对 URL(即以 http://https:// 开头),则 parent dir 必须是 base dir 的子路径,或者文档必须设置 allow-uri-read 属性。如果其中之一成功,则 parent dir 会加到目标路径前并作为 include path 使用。如果 parent dir 属于 base dir,则相对路径为路径差。否则,相对路径为目标路径。如果这两者都失败,include 被替换为一个链接。