Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: 在sethtml方法中 大空格在有样式的文本中会失效 #339

Open
along521 opened this issue Nov 13, 2024 · 1 comment
Open
Assignees
Labels
bug Something isn't working

Comments

@along521
Copy link

along521 commented Nov 13, 2024

bug 描述

在sethtml方法中 大空格在有样式的文本中会转化为小空格

你预期的样子是?

正确显示大空格

系统和浏览器及版本号

  • 操作系统:macos15
  • 浏览器和版本:谷歌最新

wangEditor-next 版本

"@wangeditor-next/editor": "^5.6.10",
"@wangeditor-next/editor-for-vue": "^5.1.14",

demo 能否复现该 bug ?

最小成本的复现步骤

代码如下


<p>测试大空格</p>
<p>1.111&emsp; 222</p>
<p>2.<span style="font-family: MS-Mincho;">111&emsp;222</span></p>
<p>3.<span style="font-family: MS-Mincho;">111</span>&emsp;<span>222</span></p>

第一个正常
第二个会失败,会转化为小空格。
第三个额外分开标签。会识别成功 ,但是需要单独处理

image
@along521
Copy link
Author

along521 commented Nov 13, 2024

针对第二种 先简单的做了一个小方法来改善 第二种大空格错误的问题

function splitSpanOnEmsp(inputStr) {
  const flag = '\u2003'
  // 使用正则表达式查找包含 style 的 <span> 标签,并检查内容
  return inputStr.replace(/<span([^>]*)>(.*?)<\/span>/g, (match, attrs, content) => {
    // 检查 <span> 是否含有 style 属性,且内容包含 &emsp;
    if (attrs.includes('style') && content.includes(flag)) {
      const parts = content.split(flag);
      let newSpans = [];

      // 遍历分割的内容,将每段内容封装成新的 <span> 元素并保留样式
      parts.forEach((part, index) => {
        if (part || index === 0 || index === parts.length - 1) { // 保留空白符位置
          newSpans.push(`<span${attrs}>${part}</span>`);
        }
        if (index < parts.length - 1) { // 添加 &emsp; 作为分隔符
          newSpans.push(flag);
        }
      });

      return newSpans.join('');
    }
    return match; // 没有 &emsp;,保持原样
  });
}

@cycleccc cycleccc added the bug Something isn't working label Nov 13, 2024
@cycleccc cycleccc self-assigned this Nov 13, 2024
@wjw020206 wjw020206 changed the title bug 在sethtml方法中 大空格在有样式的文本中会失效 Bug: 在sethtml方法中 大空格在有样式的文本中会失效 Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants