有没有办法让wysithtml5'的链接编辑器允许用户向href标记添加他们喜欢的任何内容

Is there a way to let wysithtml5's link editor let the user add anything they like to the href tag?

本文关键字:href 用户 任何内 喜欢 他们 添加 许用户 wysithtml5 有没有 编辑器 链接      更新时间:2023-09-26

我使用的是wysithtml5编辑器,但用户报告在添加链接时遇到了很多问题。用户希望将他们想要的任何内容添加到href属性中。然而,当点击"确定"添加链接时,它会通过某种方式回调修改后的链接。

例如,用户想要添加一个锚标签:

#moo

将要输入的链接是后面跟着ancors的文档url,所以它最终会看起来像这样:

http://stackoverflow.com/#moo

如果他们试图添加液体标签,也会发生同样的情况,例如:

{{name}}

变成…

http://stackoverflow.com/84748/%7B%7Bname%7D%7D

是否有修改wysithtml5的方法,使其不经过修改href属性的回调?我尝试过从语法分析器规则中删除/修改checkAttributes,但没有效果。还有其他东西在处理href。

谢谢!

wysihtml5没有显式执行此转换。这是它为表示锚标记而创建的DOM对象的奇怪行为的结果。基本上,anchor.hrefanchor.getAttribute('href')不一定返回相同的东西。

下面是一个可以在Javascript控制台上执行的示例:

var anchor = document.createElement('a');
anchor.setAttribute('href', '#foo');
console.log(anchor.href); //prints anchor.baseURI + '#foo'
console.log(anchor.getAttribute('href')); //prints '#foo'

无论如何,我认为这是wysihtml5中的一个错误。据我所知,你只需要更改源代码中的两行就可以修复它。请参阅我在Github上的fork:https://github.com/b3nj4m/wysihtml5/commit/c14d746b2b192b043673d97f79f3f61c23908f8d

编辑:关于在原始html->composer视图转换过程中剥离的href属性,这是由于解析器规则。我认为处理此问题的最佳方法是添加一个新规则,如not_empty,并将其用于href

例如

"a": {
  "check_attributes": {
    "href": "url"
  },
  //...
}

成为

"a": {
  "check_attributes": {
    "href": "not_empty"
  },
  //...
}

然后在src/dom/parse.js 中添加not_empty规则

var attributeCheckMethods = { 
  not_empty: function(attributeValue) {
    return attributeValue || null;
  },  
  //...
};

请在此处查看对src/dom/parse.js的更改:https://github.com/b3nj4m/wysihtml5/commit/0ef0dad5f0457266057d7e14df42dafe987bdb69#L2R374