Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom element in <head> #1035

Closed
LukeTOBrien opened this issue Jan 21, 2022 · 21 comments
Closed

Custom element in <head> #1035

LukeTOBrien opened this issue Jan 21, 2022 · 21 comments

Comments

@LukeTOBrien
Copy link

LukeTOBrien commented Jan 21, 2022

I came upon this today, I am developing a Static Website Generator and I want to allow for layout templates like:

<html>
  <head>
    <site-include type="partial" name="scripts"></site-include>
  </head>
</html>

So here you see the tag site-include denotes where a partial page should be included (in this case the scripts partial page in the head).
In my C# code I then QuerySelector the site-include and ReplaceWith the partial.
But or course the problem is that when I use the HTMLParser the above template gets turne into:

<html>
  <head>
  </head>
  <body>
   <site-include type="partial" name="scripts"></site-include>
  </body>
</html>

Is there any option I can set to the HTMLParser to allow custom elements inside the <head>?
I believe your parser is working as is correct for browsers, but it is not what I need for my purposes.

@FlorianRappl
Copy link
Contributor

Potentially we could add such an option, but then I'd ask why you use an HTML5 conform parser if what you input is not HTML5 confirm. Maybe XML is more what you look for?

Anyway - is there any chance to either use an existing (head-allowed) element or some notation such as the processing elements or comments?

(I don't want to change your SSG - just want to see if there is any other option besides changing a lib that is used underneath)

@LukeTOBrien
Copy link
Author

why you use an HTML5 conform parser if what you input is not HTML5

I guess I didn't realise it would do this, I have used AngleSharp in the past and had the idea of using it with this project.
An option to make the parser non HTML5 conformant would be the solution... But I could look at using XML like you say, the System.Xml.Linq namespace has similar manipulation methods, so I will give this a try.

(I don't want to change your SSG - just want to see if there is any other option besides changing a lib that is used underneath)

At this stage I am just doing unit tests and experimenting to see what direction to go in.

@LukeTOBrien
Copy link
Author

Hello, just wanted to update here.
XML didn't work because some HTML elements don't have closing tags which are required for XML.
However I did find that HTML Agility Pack worked in my case (I guess they don't validate for HTML5). The only thing missing is your fancy CSS selectors, but in this project I don't need them. (In my previous project I switched to AngleSharp because I was using CSS3 selectors like nth-of-type).

I will close he issue. Thanks

@LukeTOBrien
Copy link
Author

I'm going to open this again and humbly request that an option could be added to the HTMLParserOptions so that custom elements be allowed in the <head>.
I am using AngleSharp for my other projects and I think I would like to be consisted and use AngleSharp for this one too.

My suggestion would be similar to IsStrictMode:

var parser = new HtmlParser(new HtmlParserOptions
{
    IsHtmlConfirm = false
});
var html = "<html><head><site-include type="partial" name="scripts"></site-include></head><body></body></html>";
var document = parser.ParseDocument(html);
Console.WriteLine(document.DocumentElement.ToHtml());

What do you think?

@LukeTOBrien LukeTOBrien reopened this Feb 8, 2022
@FlorianRappl
Copy link
Contributor

Well, in general something like that would be feasible I think. I wouldn't call it IsHtmlConform (I think Confirm means Conform, right?) or so, but rather IsAcceptingCustomElementsEverywhere. Then it's clear that this flag only corresponds to such behavior.

I would also add this then also to other parts of the document where custom elements would right now not be allowed / shifted away.

@LukeTOBrien
Copy link
Author

LukeTOBrien commented Feb 8, 2022

I wouldn't call it IsHtmlConform (I think Confirm means Conform, right?)

Yeah sorry I mis-spelt

but rather IsAcceptingCustomElementsEverywhere

Custom elements are allowed in the <body> just not the <head>, so I think this option would only apply to the <head>.
Only the following are allowed in the <head>:

  • <title>
  • <style>
  • <base>
  • <link>
  • <meta>
  • <script>
  • <noscript>

I don't see why I (or others in the future) can't use a custom element in the <head> which is then going to be transformed and replaced.

@FlorianRappl
Copy link
Contributor

No it would not. This option would also allow custom elements to be used, e.g., in <html> or in <tbody> etc. (as written - in all places - not just certain allowed places).

I don't see why I (or others in the future) can't use a custom element in the which is then going to be transformed and replaced.

Hm? Maybe we are talking in different directions. AngleSharp follows the HTML5 spec. Custom elements are only allowed in certain areas (e.g., as descendant of body).

@FlorianRappl
Copy link
Contributor

So are you willing to work on this or sponsor this? @LukeTOBrien

@LukeTOBrien
Copy link
Author

I don't have the time at the moment to work on this, perhaps in the future. I could sponsor it for sure. But perhaps I will

Maybe we are talking in different directions. AngleSharp follows the HTML5 spec. Custom elements are only allowed in certain areas (e.g., as descendant of body).

Having a HTML document that doesn't follow the spec is a use case I think, like here I am using a place-holder element and replacing it.

Perhaps I will close this issue again and continue using HAP. I can always come back.

@FlorianRappl
Copy link
Contributor

I am really confused here. I propose to add something and then you just leave again?

And having something spec violating in a parser that has "spec compliant" as one of the core values is ... well - but we add things where it makes sense. Again, I am not sure what your problem is with my proposal? All I did was to give it a more accurate name and to expand it from "like now + 1 more position" to "all positions".

@LukeTOBrien
Copy link
Author

I am really confused here. I propose to add something and then you just leave again?

I didn't mean to sound ungrateful, sorry, I do like your proposal but I just don't have time to work on this right now.
I am working on other things and probably won't get around to using this feature for a long while.... I'm just thinking ahead

@dmartensson
Copy link

I just found this when looking for a library to read HTML and while I have not reached as far as custom elements I have a similar problem with custom attributes.

And seeing this I can see that custom elements in the head could come.

Its for a template solution so some custom markup will be present and while a previous solution used only data-attributes, it was a bit cumbersome, custom attributes within tables or head for that matter would help make the usage of the templates much easier, so is this still something that might come?

@FlorianRappl
Copy link
Contributor

I have a similar problem with custom attributes.

Any attribute is valid as long as its name is following the right syntax. For attributes there are no such checks.

And seeing this I can see that custom elements in the head could come.

As mentioned we can have the option to produce an invalid DOM / allow custom/any elements in the head (or actually anywhere). Open here either for sponsorship or community contributions.

@LukeTOBrien
Copy link
Author

Hello, I just did the sponsorship thing.

I was wondering perhaps you could add a HtmlParserOptions.DocType?
This DocType would be an enum:

enum HtmlDocType
{
  None,
  Html5,
  Html4.01,
  SVG // Maybe??
}

A DocType == HtmlDocType.None would mean no check, so therefore you could place custom elements inside the <head>.
What do you think?

This is not a high priority so no worries.

@FlorianRappl
Copy link
Contributor

Hello, I just did the sponsorship thing.

Great - what did you do? Because usually I get a GitHub notification or at least something is shown on GitHub. I don't see anything; but maybe you did something else or sponsored somebody else?

I was wondering perhaps you could add a HtmlParserOptions.DocType?

Well, since this would pretty much hide what is actually going on I'd still go with IsAcceptingCustomElementsEverywhere - this just makes it easy to decipher for the user and obvious for the implementer.

@LukeTOBrien
Copy link
Author

Great - what did you do? Because usually I get a GitHub notification or at least something is shown on GitHub.

Yes it seems something went wrong because it seems you still have 6 sponsor and I don't appear.
I used an organisation called Jollify Software. I will try again.

@LukeTOBrien
Copy link
Author

I just did it again using my personal account, and this time I received an email saying there was a problem with billing.
I will try again..,

@LukeTOBrien
Copy link
Author

3rd time lucky ;)

@FlorianRappl
Copy link
Contributor

Yeah that worked - thanks, much appreciated!

@FlorianRappl
Copy link
Contributor

Landed in devel. The option is called IsAcceptingCustomElementsEverywhere. By defaults its false. Activating it allows placing custom elements at positions such head or table.

Just remember that children of the custom element still follow the rules of the outer context, e.g., in head only certain elements may be placed. That does not make a difference in case of of a directly closed / empty element is in the OP.

@LukeTOBrien
Copy link
Author

LukeTOBrien commented Jun 1, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants