简体   繁体   English

清洗和合并内联 CSS styles

[英]Cleaning and Merging Inline CSS styles

I am trying to clean up user entered HTML code that has a lot of inline CSS especially spans with styles and I'm not sure where to start.我正在尝试清理用户输入的 HTML 代码,该代码有很多内联 CSS 尤其是与 styles 的跨度,我不知道从哪里开始。 Does anyone know of a way to merge the spans and the styling using JavaScript?有谁知道使用 JavaScript 合并跨度和样式的方法? I have found ways to convert inline styles by moving all the styling to a style sheet, but that is not yet possible to store the CSS page in our system as it is right now.我已经找到了通过将所有样式移动到样式表来转换内联 styles 的方法,但是现在还不可能将 CSS 页面存储在我们的系统中。 (Hopefully that will be a goal in the near future, but not my call) (希望这将是在不久的将来的一个目标,但不是我的电话)

An example - turn this mess:一个例子 - 把这个烂摊子:

<span style="font-size: large;"><span style="font-family: arial black,avant garde;"><span style="font-size: xx-large;"><span style="font-size: large;"><span style="font-family: arial black,avant garde;"><span style="font-size: xx-large;"><span style="color: #000000;"><span style="font-size: x-large;"><span style="font-size: large;"><span style="font-family: arial black,avant garde;"><span style="font-size: xx-large;"><span style="color: #000000;"><span style="font-size: x-large;"><a href="index.php?p=75">Home</a></span></span></span></span></span></span></span></span></span></span></span></span></span>

into this:进入这个:

<span style="font-size: x-large;color: #000000;font-family: arial black,avant garde;"><a href="index.php?p=75">Home</a></span>

How about this?这个怎么样?

window.onload = function(){
    var spans = document.querySelectorAll('span');

    for (var i = 0; i < spans.length; i++)
    {
        if (spans[i].hasChildNodes() && spans[i].childNodes.length == 1
            && spans[i].firstChild.tagName == 'SPAN')
        {
            var parent = spans[i];
            var child = spans[i].firstChild;

            // Copy new properties to child.
            for (var j = 0; j < parent.style.length; j++)
            {
                var prop = parent.style[j];
                if (child.style.getPropertyValue(prop) === "")
                {
                    child.style.setProperty(
                        prop,
                        parent.style.getPropertyValue(prop),
                        ''
                    );
                }
            }

            // Replace parent with child.
            parent.parentNode.insertBefore(child, parent);
            parent.parentNode.removeChild(parent);
        }
    }
}

This will find all spans, and merges those which only have a single span child.这将找到所有跨度,并合并那些只有一个跨度子级的跨度。

This will disregard the use of !important , but I presume that's not being used anyway.这将忽略!important的使用,但我认为无论如何都不会使用它。

Also, it won't return exactly the same property values since getPropertyValue normalizes them.此外,它不会返回完全相同的属性值,因为getPropertyValue对它们进行了规范化。

Edit:编辑:

The above example code reduces the 13 spans in your example down to just one for me, though, Safari throws away some of the styles in the process.上面的示例代码将示例中的 13 个跨度减少到只有一个,但是,Safari 在此过程中丢弃了一些 styles。

It's fairly simplistic, of course, and just based on one example, since I have no idea what kind of span soup you might be running into, or what kind of results exactly you might be looking for.当然,这相当简单,并且仅基于一个示例,因为我不知道您可能会遇到什么样的跨度汤,或者您可能正在寻找什么样的结果。

For example, you might want to merge spans like this too: <p>foo<span style="font-size: x-large"> <span style="font-size: small">bar</span></span></p> ?例如,您可能也希望像这样合并跨度: <p>foo<span style="font-size: x-large"> <span style="font-size: small">bar</span></span></p> ? You'd end up losing the font size on that space, but that'll only make a tiny difference.您最终会丢失该空间的字体大小,但这只会产生很小的差异。

I'm sure there are plenty more cases like that, so it'll come down to how clean you want the end result to be, and how much time you want to spend on fixing all the corner cases.我敢肯定还有很多这样的案例,所以这将归结为您希望最终结果有多干净,以及您想花多少时间修复所有角落案例。

You can find more information about the DOM methods I used in Mozilla's DOM reference for CSSStyleDeclaration and element , for example.例如,您可以在Mozilla 的CSSStyleDeclarationelement的 DOM 参考中找到有关我使用的 DOM 方法的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM