draft.js : getPlainText 对换行符的修改 (填坑篇)

直接给出结论:

contentState.getPlainText()拿出的字符串,所有的换行符都是\n

原始字符串中包含的\r也会被转成\n

导致前后两个字符串不相等


偶然发现 同一个字符串str,传入editorState,再传出来,str.length就变了,

逻辑如下:

传入inputText得到新的editorState:

const contentState = ContentState.createFromText(inputText) 
const editorState = EditorState.createWithContent(contentState)

再从这个editorState中得到outputText:

const contentState = editorState.getCurrentContent()
const outputText = contentState.getPlainText()

对比

inputText === outputText //false

文字内容肉眼一看没差,而字符长度变长了10几个单位

原始字符串中出现了\r
结果都被转成了\n
所以判断的时候 两个字符串不相等

想要对字符串做判断的话,
就一致使用从edtorState中拿出来的值

over

猜你喜欢

转载自blog.csdn.net/sinat_24070543/article/details/80373155