I’d like to use the HTMLElement class to format my code for export using the toString() method. But the code that I want to export may or may not be valid HTML. I’d also like to be able to export with unsupported attributes.
The reason to do this is to get the formatting tabs and indentation correct and to allow the DOM to validate the HTML for everything except my use cases mentioned here.
Is it possible to use that class to export code that may not be valid HTML? The last time I checked it would not allow me to do that but I have reason to believe it may allow it now.
Examples of exported tags:
- <abc:div>
- <mycustomtagname>
- <?php
- <abc:mytag unsupportedattribute="10">
The lines above represent:
- A tag with a namespace.
- A custom tag not defined in the HTML spec
- A php tag so that php can be wrapped around blocks of HTML.
- A custom attribute
What I’m trying to do is use the HTMLElement class to export to different languages. I like this class and using it would solve many issues around formatting and creating valid HTML. Having custom php tags might make it invalid HTML so I don’t know if the .toString()
method would export it anyway. In the past incorrect or unsupported HTML was corrected
when using toString().
Example of HTML to export:
<div>
<input/>
</div>
Example of PHP / HTML to export:
<div>
<?php
<input/>
?>
</div>
Example of XML to export:
<a:div>
<b:input></b:input>
</a:div>
Example of HTML with user attributes (not using data names):
<div myattribute="10">
</div>
But if it can’t export those types of tags is there any recommendations for alternatives? It needs to be a generic export type of class. For example, there is the XMLDocument class that supports namespaces but that would export XML not HTML. There is the DOM and the DOMDocument objects and I’ve even seen some classes like shadow DOM.
My own suggestion to this problem would be export bodyElement.toString()
but using valid tag name as tokens and then do a global search and replace. So there would be a valid HTML token for php like, <php>
and it would be replaced with an actual php tag later, <?php
.