We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27708 MODX Staff
    • 2,502 Posts
    Note:
    This thread was originally entitled, "meta tags in document.parser.class.inc.php and HTML 4.01" and appeared in Development & Coding>Core Code. I wanted to find a way to remove the XHTML/XML required trailing slashes from self closing tags--which are not required and in the document head will fail validation--without having to dip into the core code. The plugin as resolved with the help of sottwell is here: http://modxcms.com/forums/index.php/topic,9331.msg65041.html#msg65041and based on Susan’s post here: http://modxcms.com/forums/index.php/topic,9331.msg65001.html#msg65001. Thanks again Susan -- Jay.


    I develop pages in HTML 4.01 most of the time and I want to ensure that the output for the files validates according to WC3 spec. In order to get this to work with keywords in ver 9.2.2 I had to mod the document.parser.class.inc.php at lines 1143 and 1161 to remove the trailing slash from the meta element which will fail validation as HTML 4.01 but is required in XHTML.

    Is there a way to have the document.parser.class.inc.php check to see which doctype is in use and to select whether thier should be a trailing slash or not.?

    OR

    Is there a way to override the document.parser.class.inc.php via a file, snippet or TV so that I don’t have to mod the core?

    I have a bunch of sites coming up and I don’t like messing with the core at all if I want to be able to sanely drop on patches and upgrades.

    Any help would be appreciated.

    All the best,

    Jay
      Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You could write a plugin to scan the finished document using the OnWebPagePrerender event and replace all " />" with ">" tags.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 27708 MODX Staff
        • 2,502 Posts
        Susan,

        While I understand the concept clearly and see that it will work. As I have mentioned elsewhere, my php skills are quite limited. I don’t have a grasp of PCRE or preg_replace() enough to write the string. I will certainly give a try. Any good resources for learning REgular Expressions other than PHP.net?

        All the best,

        Jay
          Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You could use this as a basis, since all you want to do is replace the string " />" (space slash right-angle) with the string ">" (right-angle) in the finished document.
          $modx->documentOutput=preg_replace(" />",">",$modx->documentOutput);

          Put that in a plugin and set its System Events to "onWebPagePrerender".
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 27708 MODX Staff
            • 2,502 Posts
            Susan,

            Thanks. I will try it. It seems to be fairly clear.

            ATB,
            Jay
              Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
              • 27708 MODX Staff
              • 2,502 Posts
              Susan,

              The code you offered helped get me started but didn’t quite do the trick. Here is what worked to get the MODx generated tags to be HTML 4.01 compliant:

              $modx->documentOutput=preg_replace("% />%",">",$modx->documentOutput);
              


              I called the plugin "HTML_Valid_Tags" and it works like a charm.

              All the best,

              Jay

              Note to Mod: Perhaps this should be moved to the Plugins or How To forums.
                Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
                • 1341
                • 20 Posts
                Note that this quick cosmetic change does not guarantee that your page will validate, since it doesn’t stop people from doing silly things like wrapping <p> tags with <b> tags (<b><div>block inside inline is forbidden</div></b>).

                A more fully-featured MODx and HTML Purifier plugin we worked on a while ago in this forum can be configured to output HTML 4.01 compliant HTML by replacing $purifier = new HTMLPurifier(); with:

                $config = HTMLPurifier_Config::createDefault();
                $config->set('Core', 'XHTML', false);
                $purifier = new HTMLPurifier($config);
                
                  • 27708 MODX Staff
                  • 2,502 Posts
                  Ambush Commander,

                  Yes you are correct it can’t stop people from writing bad code and it isn’t designed to fix that.

                  Since I am a web designer, who writes HTML 4.01 and standards based markup and not an end user, this works fine for me. The fewer lines of code to be parsed by PHP or MODx, the better.

                  My snippet was merely to convert the specific elements containing the XML required, tag-closing, trailing slash in the MODx Core from their current XHTML 1.0 compliant state to a state that complies with HTML 4.01, all without touching the core and causing problems with upgrades.

                  It may be wise to change the name of my snippet from "HTML_Valid_Tags" to "HTML401NoCloseSlash" to make things more clear.

                  Thanks for the tip though on the code cleaner.

                  All the best,

                  Jay
                    Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
                    • 21257 MODX Staff
                    • 730 Posts
                    This is great - as a developer who has just recently migrated away from xhtml in favour of 4.01, I was looking for something similar. Not only does the core output a couple of things xhtml, but many snippets and other contributions do as well. (I didn’t realize that xhtml-style self-closing tags were considered valid in the body of an html 4.01 document.. interesting.)

                    I’m still finding my way through MODx - getting easier every day thanks to the wonderful community. But what I do have is lots of php experience. So after I found this post, I thought I should make a couple of small optimizations.

                    This is the code for my version of the plugin, based on the plugin "tutorial" in the Documentation section of the main site, and based on the plugin discussed above.

                    $e = &$modx->Event;
                    switch ($e->name) {
                    	case 'OnWebPagePrerender':
                    	$modx->documentOutput = str_replace(' />', '>',$modx->documentOutput);
                    	break;
                    
                    	default :
                    	return; // stop here - this is very important.
                    	break;
                    }
                    


                    As far as I understand it, str_replace is going to be significantly faster than using regular expressions. (Using regex is faster when you’re running a complicated search/replace that can otherwise only be obtained by a sequence of str_replace commands.)

                    Also.. and I know it’s costing me more to write this sentence than the CPU cycles that this "tip" will save.. but *in some configurations* using single quotes uses fewer cycles than double quotes. When double quotes are used, the php parser scans the string for the existence of any variables so it can do variable replacement. In this plugin, it only happens three times, so it really is highly insignificant, but for some reason, I have felt compelled to write this paragraph anyway. Must be the egg nog ;-)

                    The only problem that I can forsee this plugin causing is that if you want to cite some xhtml-style code on a valid html 4.01 page, it’s going to get replaced. You can avoid this by escaping the angle brackets:

                    <!-- instead of this -->
                    <p>
                     Waffles for dinner?
                     <br /><!-- if you're citing code on a valid html 4.01 page, you may not want the plugin to alter this self-closing tag -->
                     WAFFLES???
                    </p>
                    
                    <!-- write your xhtml-style citations like this -->
                    <p>
                     Waffles for dinner?
                     <br /><!-- now the plugin won't replace <br /> with <br> -->
                     WAFFLES???
                    </p>
                    
                    


                    It’s definitely the nog.
                      Mike Schell
                      Lead Developer, MODX Cloud
                      Email: [email protected]
                      GitHub: https://github.com/netProphET/
                      Twitter: @mkschell
                      • 27708 MODX Staff
                      • 2,502 Posts
                      netProphET,

                      Your code is much more effective. I wouldn’t ever promote myself as a php programmer. I am a web designer and a business person. I can code valid HTML with CSS in my sleep but my use of PHP is hacking at best. I may switch to your version.

                      Cheers,

                      Jay
                        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub