CKeditor 删除开头的多余空格 - trim

CKeditor remove extra whitespace at beginning - trim

我正在尝试删除 ckeditor 生成的文本的 beginningend 处多余的白色 space但不是文本本身之间的 whtiespaces。

我在开头写了很多 space,然后是实际的文本。

所以我正在尝试为 ckeditor 生成的文本编写一个 trim 函数,传统的 trim() 不起作用,因为文本包含 <p></p> &nbsp; 有时 <br> 所以我不能只写一个正则表达式。

生成的文本示例

<p><br><br><br><br><br><br><br><br><br><br><br>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p><br><br><br><br><br><br>here text begins</p><p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>another line</p><p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>third line </p><p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><strong>fourth line</strong></p><p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><span class="text-huge"><strong>sdsdsdsdsd</strong></span></p><h2><span class="text-huge"><i><strong><u>sdsdsdsdsdsdsd</u></strong></i></span><br><br><br><br><br><br><span class="text-huge"><i><strong><u>csdsdsdsdsdsds</u></strong></i></span><br><br><br><br><br><br><br><br><br>&nbsp;</h2>

我想删除每个标签,直到这里的文本开始,但是这个文本可能会有所不同,

有什么建议吗?

好的,我已经设法通过解决方法解决了这个问题,想法是:

1- 将我们从 ckeditor content.getData() 获得的全部字符串解析为 HTMLCollection,

2- 遍历 html 标签并通过检查 属性 textContent[ 删除任何 NOT 有文字的标签=27=],一旦标签有单词就退出循环。

3- 在包含单词的标签内循环,通过拆分 str.split(' ') 将字符串转换为单词数组,并遍历数组并删除每个 <br>&nbsp;直到你到达这些标签和实体以外的任何东西,然后退出循环。

4- 您需要从(从 ckeditor 获得的全部文本)的开头和结尾完成此过程。

        trimedCkEditorText() {
            let contentStr = this.content.getData();
            // Remove Extra whitespaces at the begining of the text
            contentStr = this.trimedCkEditorTextAt(contentStr, true);

            // Remove Extra whitespaces at the end of the text
            contentStr = this.trimedCkEditorTextAt(contentStr, false);

            return contentStr;
        },
        trimedCkEditorTextAt(contentStr, startOfText) {
            const parser = new DOMParser();
            const doc = parser.parseFromString(contentStr, "text/html")

            // Check first child 
            while(doc.body.children.length) {
                const index = startOfText ? 0 : doc.body.children.length - 1; 
                const child = doc.body.children[index];
                
                if(child.textContent.replace(/\s/g, '').length) {
                    // Remove <br> tags
                    while(child.children.length) {
                        const index = startOfText ? 0 : child.children.length - 1; 
                        const grandechild = child.children[index];
                        if(grandechild.localName === 'br') grandechild.remove();
                        else break;
                    }

                    // Remove &nbsp;
                    const childTextArray = child.innerHTML.split(' ');
                    while(childTextArray.length) {
                        const index = startOfText ? 0 : childTextArray.length - 1; 
                        if(childTextArray[index] === '&nbsp;') childTextArray.splice(index, 1);
                        else break;
                    }
                    child.innerHTML = childTextArray.join(' ');
                    break;
                } else {
                    child.remove();
                }
            }

            return doc.body.innerHTML;
        }