Weird behavour wysiwyg?

安稳与你 提交于 2019-12-25 10:16:09

问题


I have a code to wrap elements around text it works fine until i try the following format in my editor:

<u><strong>T</strong>es<strong>t</strong></u>

It automatic adds two empty strong elements before the underlined element and after like this:

<strong></strong>
<u><strong>T</strong>es<strong>t</strong></u>
<strong></strong>

Here's the code that i use and i have buttons that have actions like wrap('strong'):

function wrap(tagName)
{
var selection;
var elements = [];
var ranges = [];
var rangeCount = 0;

if (window.getSelection)
{
    selection = window.getSelection();
    if (selection.rangeCount)
    {
        rangeCount = selection.rangeCount;
        for (var i=0; i<rangeCount; i++)
        {
            ranges[i] = selection.getRangeAt(i).cloneRange();
            elements[i] = document.createElement(tagName);
            elements[i].appendChild(ranges[i].extractContents());
            ranges[i].insertNode(elements[i]);
            ranges[i].selectNode(elements[i]);
        }
        selection.removeAllRanges();
        for (var i=0; i<ranges.length; i++)
        {
            selection.addRange(ranges[i]);
        }
    }
}
}

回答1:


WYSIWYG is hard. Especially with HTML, which is nothing what it looks like. I'm just guessing here, but if you start with

<u>Test</u>

ant you select T in WYSIWYG then the actual selected code will probably be <u>T. Since you can't wrap that in strong (because <strong><u>T</strong> is not valid markup then the editor will wrap everything before the tag in strong and everything after tag in strong, which results in

<strong></strong>
<u><strong>T</strong>es<strong>t</strong></u>
<strong></strong>

that you are getting.

I to avoid that you could check if the text that you are wrapping has the lenght of 0, end then if so - not wrap it with anything.




回答2:


I'd suggest using DOM manipulation to wrap text nodes within the selection individually. My Rangy library can help a little with this, providing splitBoundaries() and getNodes() extension methods to its Range objects.

Live demo: http://jsfiddle.net/5cdMn/

Code:

function isNodeInsideElementWithTagName(node, tagName) {
    tagName = tagName.toLowerCase();
    while (node) {
        if (node.nodeType == 1 && node.tagName.toLowerCase() == tagName) {
            return true;
        }
        node = node.parentNode;
    }
    return false;
}

function wrapSelection(tagName) {
    var range, textNode, i, len, j, jLen, el;
    var ranges = rangy.getSelection().getAllRanges();
    for (i = 0, len = ranges.length; i < len; ++i) {
        range = ranges[i];
        range.splitBoundaries();
        textNodes = range.getNodes([3]/* Array of node types to retrieve */);
        for (j = 0, jLen = textNodes.length; j < jLen; ++j) {
            textNode = textNodes[j];
            if (!isNodeInsideElementWithTagName(textNode, tagName)) {
                el = document.createElement(tagName);
                textNode.parentNode.insertBefore(el, textNode);
                el.appendChild(textNode);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/11422786/weird-behavour-wysiwyg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!