views:

45

answers:

3

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  A: 

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

Neeme Praks
A: 

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

Mads Hansen
Yeah I have been using <!-- -->. So <div><!-- --></div> seems to work.
Adam Gent
A: 

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