【问题标题】:Implementing custom script types in the browser?在浏览器中实现自定义脚本类型?
【发布时间】:2014-08-20 06:50:42
【问题描述】:

当某个自定义语言的脚本出现在网页中时,这些脚本是如何执行的?

<script type='text/mighty-mouse'>
 // logic expressed in mighty mouse language
</script>

我猜应该是这样的:

  1. Mighty Mouse 解释器在加载页面时查询mighty-mouse 脚本。
  2. 解释器使用标准的词法分析/编译策略解析脚本并转译为 JavaScript。
  3. 生成的 JavaScript 会动态注入到页面上的标准脚本标记中,甚至可能会消除原来的强大鼠标标记。

(我意识到我们可以在运行前进行转编译以避免加载我们的解释器。)

这是一个准确完整的理解吗?

【问题讨论】:

  • 您可以先阅读HTML5 specification:Note: The exact processing details for these attributes are, for mostly historical reasons, somewhat non-trivial, involving a number of aspects of HTML. The implementation requirements are therefore by necessity scattered throughout the specification. The algorithms below (in this section) describe the core of this processing, but these algorithms reference and are referenced by the parsing rules for script start and end tags in HTML, in foreign content...
  • 底线是有很多与脚本元素和javascript相关的历史行为。任何新的脚本语言都可以选择利用这一点,或者朝着完全不同的方向发展(例如 Java 插件和 Flash 对象)。
  • 基本上,浏览器处理非标准脚本语言的方式将取决于实现。

标签: javascript compiler-construction dynamic-compilation


【解决方案1】:

据我所知有两种方法。浏览器插件或更可能是用 JavaScript 编写的解释器。它本质上是扫描 DOM 以寻找正确的类型并解释内容。

CoffeeScript 是后者的一个很好的例子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 2012-12-03
    • 1970-01-01
    • 2022-10-17
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多