防止 JSPX 创建自关闭标签 (
!=
)

发布于 2024-09-26 20:03:10 字数 415 浏览 3 评论 0原文

JSPX 有一个可爱的副作用:变成

<div class="magic"></div>

<div class="magic" />

对于许多浏览器来说,这会导致布局混乱,即使它是有效的 XHTML。 因此,我求助于使用 groovy 脚本通过以下正则表达式查找所有可能的错误 HTML:

def m = html =~ /<(\w+)[^>]*?><\/(\w+)>/
def bad = m.findAll { it[1] == it[2]  };

有办法让 JSPX XML 处理器不自动关闭标签吗?

JSPX has the lovely side effect of turning:

<div class="magic"></div>

Into:

<div class="magic" />

For many browsers this causes pandemonium and mayhem with layout even though it is valid XHTML.
Consequently I have resorted to using a groovy script to find all possible bad HTML with the following regex:

def m = html =~ /<(\w+)[^>]*?><\/(\w+)>/
def bad = m.findAll { it[1] == it[2]  };

Is there way to have the JSPX XML processor not to self close tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

记忆里有你的影子 2024-10-03 20:03:10

我一直在使用

I've been using <div><jsp:text/></div>

清风无影 2024-10-03 20:03:10

AFAIK,对此没有优雅的解决方案(阅读:在容器级别可配置)。检查“GlassFish v3 上的 jspx 脚本元素”以获取可能的解决方法。

AFAIK, there is no elegant solution to this (read: configurable on container level). Check "jspx script element on GlassFish v3" for possible workarounds.

骄傲 2024-10-03 20:03:10

您可以尝试在元素内指定不会影响 HTML 呈现方式的内容,但会阻止 XHTML 被序列化为自关闭元素;例如注释、处理指令或不间断空白字符()。

You can try specifying content inside the element that won't affect how the HTML renders, but will prevent the XHTML from being serialized as a self-closing element; like a comment, processing instruction, or non-breaking white space character().

若水微香 2024-10-03 20:03:10

正如 Neeme 所指出的,这个问题似乎没有解决方案。
不过,我编写了一个 Groovy 脚本,您可以让 Maven 调用 (GMaven) 来检查可能会自动关闭的 XHTML 标记。

该脚本可能需要更好的错误消息,并且不能处理所有情况,但到目前为止已经缓解了问题。

#!/usr/bin/env groovy


def srcdir = project.properties['srcdir'];
def badFiles = [];

def checkFile(badFiles, file) {
    def htmlLines = file.readLines();
    def found = [];
    int i = 0;
    for (html in htmlLines) {
        ++i;
        //print html;
        def m = html =~ /<(\w+)[^>]*?><\/(\w+)>/
        def bad = m.findAll { it[1] == it[2]  };
        if (bad)
            found.add(['bad' : bad, 'line' : i]);
    }
    if (found) {
        badFiles << file;
        println "File had bad HTML: " + file.canonicalPath;
        println found;
    }

}

def ant = new AntBuilder();
scanner = ant.fileScanner {
    fileset(dir:srcdir) {
        include(name:"**/*.jspx")
    }
}

for (f in scanner) {
    //println "Checking file: " + f.canonicalPath;
    checkFile(badFiles, f);
}
if (badFiles) { 
    println "Bad files: " + badFiles;
    fail('Bad files: ' + badFiles);
}

As noted by Neeme there seems to be no solution to this problem.
However I have written a Groovy script that you can have Maven call (GMaven) to check for possible XHTML tags that will get self closed.

This script probably needs better error messages and does not handle all cases but has so far mitigated the problem.

#!/usr/bin/env groovy


def srcdir = project.properties['srcdir'];
def badFiles = [];

def checkFile(badFiles, file) {
    def htmlLines = file.readLines();
    def found = [];
    int i = 0;
    for (html in htmlLines) {
        ++i;
        //print html;
        def m = html =~ /<(\w+)[^>]*?><\/(\w+)>/
        def bad = m.findAll { it[1] == it[2]  };
        if (bad)
            found.add(['bad' : bad, 'line' : i]);
    }
    if (found) {
        badFiles << file;
        println "File had bad HTML: " + file.canonicalPath;
        println found;
    }

}

def ant = new AntBuilder();
scanner = ant.fileScanner {
    fileset(dir:srcdir) {
        include(name:"**/*.jspx")
    }
}

for (f in scanner) {
    //println "Checking file: " + f.canonicalPath;
    checkFile(badFiles, f);
}
if (badFiles) { 
    println "Bad files: " + badFiles;
    fail('Bad files: ' + badFiles);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文