Keep JSPX from creating self closing tags (<div></div> != <div/>)

假如想象 提交于 2019-12-18 12:38:33

问题


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?


回答1:


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




回答2:


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




回答3:


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(&#x200B;).




回答4:


You can also create a custom taglib or encapsulate your <div></div> in CDATA. More here: How to produce valid HTML with JSPX? (not XHTML)




回答5:


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);
}


来源:https://stackoverflow.com/questions/3907744/keep-jspx-from-creating-self-closing-tags-div-div-div

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