Do you know a HTML-Snippet Validator?

前端 未结 2 836
眼角桃花
眼角桃花 2021-02-06 21:52

I\'m searching for a tool that would allow me to check whether a certain snippet of HTML would be valid in it\'s proper context.

I\'d input something like



        
相关标签:
2条回答
  • 2021-02-06 22:09

    W3C does currently (May 2020) have a fragment validator, but it seems to have bitrot (no HTML5, at least). Here's a couple of simple scripts that attach a header and a footer to your fragment, and run the result through a local copy of the Nu checker. The fragment can be anything which is valid at the top level of a <body> tag - modify the header and footer if you need something else.

    validate1.sh takes a single filename argument and checks it, while validate2.sh cycles through all HTML files in a directory. It has a simple exclusion list mechanism which you'll need to change. You'll need to modify both to point to your copy of vnu.jar.

    validate1.sh:

    #!/bin/bash
    #
    # validate1.sh
    #
    # Run the nu validator on the HTML fragment in the supplied filename.
    # This script adds a header and trailer around the fragment, and supplies
    # the result to 'vnu.jar'. You'll need to modify it to correctly locate
    # vnu.jar.
    
    if test "$#" -ne 1; then
        echo "Usage: '$0 fname', where 'fname' is the HTML file to be linted"
        exit 1
    fi
    
    var="<!doctype html>
         <html lang=\"en\">
         <head>
         <title>foo</title>
         </head>
         <body>
         $(< "$1")
         </body>
         </html>"
    echo "Checking '$1'... subtract 6 from any reported line numbers"
    echo "$var" | java -jar vnu.jar -
    

    validate2.sh:

    #!/bin/bash
    #
    # validate2.sh
    #
    # Run the nu validator on the HTML fragments in the supplied directory.  This
    # script adds a header and footer around each fragment in the directory, and
    # supplies the result to 'vnu.jar'. You'll need to modify it to correctly
    # locate vnu.jar.
    
    if test "$#" -ne 1; then
        echo "Usage: '$0 fname', where 'fname' is the HTML directory to be linted"
        exit 1
    fi
    
    for filename in $1/*.html; do
        case $filename in
            # simple exclusion list example:
            "$1/root.html" | "$1/sitedown.html")
                echo "Skipping '$filename'"
                continue
                ;;
            *)
                ;;
        esac
    
        var="<!doctype html>
             <html lang=\"en\">
             <head>
             <title>foo</title>
             </head>
             <body>
             $(< "$filename")
             </body>
             </html>"
        echo "Checking '$filename'... subtract 6 from any reported line numbers"
        echo "$var" | java -jar vnu.jar -
    done
    
    0 讨论(0)
  • 2021-02-06 22:19

    You can actually use the W3C validator to check a snippet.

    Choose the 'Validate by Direct Input' tab and select More Options. In there there is a radio button to 'Validate HTML fragment'. http://validator.w3.org/#validate_by_input+with_options

    It will wrap your page in valid html so the errors which you see are only due to your snippet.

    0 讨论(0)
提交回复
热议问题