Making/finding html5 validator bookmarklet

后端 未结 4 578
独厮守ぢ
独厮守ぢ 2021-01-18 23:53

I want to find or make a bookmarklet that will validate the html content of a currently viewed page using the W3C HTML 5 validator.

I have found two bookmarklets and

相关标签:
4条回答
  • 2021-01-19 00:30

    The previous answers didn't work form me. I'm using the "Check serialized DOM of Current Page" bookmarklet at https://validator.w3.org/nu/about.html. This seems to work wonderfully, picking up dynamically generated HTML.

    0 讨论(0)
  • 2021-01-19 00:32

    Marta's answer helped me out. Here is the updated bookmarklet.

    javascript:(function(){function c(a,b){var c=document.createElement("textarea");c.name=a;c.value=b;d.appendChild(c)}var e=function(a){for(var b="",a=a.firstChild;a;){switch(a.nodeType){case Node.ELEMENT_NODE:b+=a.outerHTML;break;case Node.TEXT_NODE:b+=a.nodeValue;break;case Node.CDATA_SECTION_NODE:b+="<![CDATA["+a.nodeValue+"]]\>";break;case Node.COMMENT_NODE:b+="<\!--"+a.nodeValue+"--\>";break;case Node.DOCUMENT_TYPE_NODE:b+="<!DOCTYPE "+a.name+(a.publicId?' PUBLIC "'+a.publicId+'"':"")+(!a.publicId&&a.systemId? " SYSTEM":"")+(a.systemId?' "'+a.systemId+'"':"")+">\n"}a=a.nextSibling}return b}(document),d=document.createElement("form");d.method="POST";d.action="http://validator.w3.org/check";d.enctype="multipart/form-data";d.target="_blank";d.acceptCharset="utf-8";c("fragment",e);c("doctype","HTML5");c("group","0");document.body.appendChild(d);d.submit()})();
    
    0 讨论(0)
  • 2021-01-19 00:38

    I was also getting the 'Sorry! This document cannot be checked.' error, resolved it by adding an accept-charset "utf-8" to the form attributes.

    In the function that creates the form element add the following line: form.acceptCharset = "utf-8";

    It worked for me.

    0 讨论(0)
  • 2021-01-19 00:40

    First, you need an exact copy of the HTML document (including Doctype etc). For this purpose, I have written the following function:

    function DOMtoString(document_root) {
        var html = '',
            node = document_root.firstChild;
        while (node) {
            switch (node.nodeType) {
                case Node.ELEMENT_NODE:
                    html += node.outerHTML;
                break;
                case Node.TEXT_NODE:
                    html += node.nodeValue;
                break;
                case Node.CDATA_SECTION_NODE:
                    html += '<![CDATA[' + node.nodeValue + ']]>';
                break;
                case Node.COMMENT_NODE:
                    html += '<!--' + node.nodeValue + '-->';
                break;
                case Node.DOCUMENT_TYPE_NODE:
                    // (X)HTML documents are identified by public identifiers
                    html += "<!DOCTYPE "
                         + node.name
                         + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
                         + (!node.publicId && node.systemId ? ' SYSTEM' : '') 
                         + (node.systemId ? ' "' + node.systemId + '"' : '')
                         + '>\n';
                break;
            }
            node = node.nextSibling;
        }
        return html;
    }
    

    Then, a form has to be created and submitted. After inspecting the form submission to http://validator.w3.org/check, I've created the following function, which submits the significant key-value pairs:

    javascript:(function() {
        var html_to_validate = DOMtoString(document);
    
        /* Paste the DOMtoString function here */
    
        function append(key, value) {
            var input = document.createElement('textarea');
            input.name = key;
            input.value = value;
            form.appendChild(input);
        }
        var form = document.createElement('form');
        form.method = 'POST';
        form.action = 'http://validator.w3.org/check';
        form.enctype = 'multipart/form-data';         // Required for this validator
        form.target = '_blank';                       // Open in new tab
        append('fragment', html_to_validate);         // <-- Code to validate
        append('doctype', 'HTML5');                   // Validate as HTML 5
        append('group', '0');
        document.body.appendChild(form);
        form.submit();
    })();
    

    Bookmarklet

    Copy the previous two blocks to Google's closure compiler. Do not forget to prefix javascript: again.

    javascript:(function(){function c(a,b){var c=document.createElement("textarea");c.name=a;c.value=b;d.appendChild(c)}var e=function(a){for(var b="",a=a.firstChild;a;){switch(a.nodeType){case Node.ELEMENT_NODE:b+=a.outerHTML;break;case Node.TEXT_NODE:b+=a.nodeValue;break;case Node.CDATA_SECTION_NODE:b+="<![CDATA["+a.nodeValue+"]]\>";break;case Node.COMMENT_NODE:b+="<\!--"+a.nodeValue+"--\>";break;case Node.DOCUMENT_TYPE_NODE:b+="<!DOCTYPE "+a.name+(a.publicId?' PUBLIC "'+a.publicId+'"':"")+(!a.publicId&&a.systemId? " SYSTEM":"")+(a.systemId?' "'+a.systemId+'"':"")+">\n"}a=a.nextSibling}return b}(document),d=document.createElement("form");d.method="POST";d.action="http://validator.w3.org/check";d.enctype="multipart/form-data";d.target="_blank";c("fragment",e);c("doctype","HTML5");c("group","0");document.body.appendChild(d);d.submit()})();
    
    0 讨论(0)
提交回复
热议问题