【问题标题】:How can I remove the first doctype tooltip of the ace-editor in my html-editor?如何在我的 html-editor 中删除 ace-editor 的第一个 doctype 工具提示?
【发布时间】:2015-10-20 09:16:00
【问题描述】:

我们要求用户在这里定义 html,所以添加一个 div 或一个部分或类似的东西。所以,我在编辑我的 HTML 时想要验证工具提示。但不想出现 doc 类型的警告。

【问题讨论】:

  • in this thread 所述,似乎无法过滤某些错误,但您可以将它们全部禁用。

标签: javascript html ace-editor


【解决方案1】:

试试这个

var session = editor.getSession();
session.on("changeAnnotation", function() {
  var annotations = session.getAnnotations()||[], i = len = annotations.length;
  while (i--) {
    if(/doctype first\. Expected/.test(annotations[i].text)) {
      annotations.splice(i, 1);
    }
  }
  if(len>annotations.length) {
    session.setAnnotations(annotations);
  }
});

【讨论】:

  • 几乎可以完美运行。如果第一行中出现格式错误的标签,除了“标签名称中的文件意外结束”之外,我们还会收到Unexpected End of File. Expected DOCTYPE. - 这并不是什么大问题,但如果你更新它会很棒你的 sn-p 来反映这一点。 :)
  • Uncaught ReferenceError: assignment to undeclared variable len
  • 未声明的变量 len 很可能是因为您处于严格模式。明确声明这些变量应该清除错误。 var annotations = this.aceEditor.session.getAnnotations()||[];让 i = annotations.length;让 len = i;
【解决方案2】:

带有“意外的文件结尾。预期的 DOCTYPE。”已过滤警告。

var session = editor.getSession();
session.on("changeAnnotation", function () {
    var annotations = session.getAnnotations() || [], i = len = annotations.length;
    while (i--) {
        if (/doctype first\. Expected/.test(annotations[i].text)) {
            annotations.splice(i, 1);
        }
        else if (/Unexpected End of file\. Expected/.test(annotations[i].text)) {
            annotations.splice(i, 1);
        }
    }
    if (len > annotations.length) {
        session.setAnnotations(annotations);
    }
});

【讨论】:

    【解决方案3】:

    如果您直接对annotations 进行操作并直接调用编辑器onChangeAnnotation 方法来更新页面上的注释,则可以防止触发另一个changeAnnotation 事件并像克里斯的回答那样调用此事件处理程序两次。

    var editor = Application.ace.edit(element),
        session = editor.getSession();
    
    session.on('changeAnnotation', function () {
      session.$annotations = session.$annotations.filter(function(annotation){
        return !(/doctype first\. Expected/.test(annotation.text) || /Unexpected End of file\. Expected/.test(annotation.text))
      });
      editor.$onChangeAnnotation();
    });
    

    【讨论】:

      猜你喜欢
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多