5

Let's say I'm crazy and I want to implement an basic HTML rendering engine with c#. I would like to use WPF Controls to display the HTML Layout.

While there is nothing to gain with such an idea, I still want to try it out! So what libraries, projects and documentation would help me get this done?

Alex Maker
  • 1,529
  • 2
  • 19
  • 27
  • 3
    This will be more difficult than you think. – Will Vousden Feb 12 '10 at 17:47
  • Alt+Prnt screen, same the image and use it in the opposite world...that's about the only remotely sane way this can happen :) – Nick Craver Feb 12 '10 at 17:48
  • Is your goal to learn how to parse HTML, or use WPF? There are probably other projects you can tackle to learn things quickly (and in a more directed manner) – Dave Feb 12 '10 at 17:50
  • @Dave - If I understand correctly, he wants to parse an HTML page, then render it using WPF controls as closely as possible. – Nick Craver Feb 12 '10 at 17:52
  • @Nick - seems to me that he wants to write a WPF application that spits out HTML instead of Windows API calls when rendering controls. – Ed Power Feb 12 '10 at 18:03
  • @Zakalwe - I agree. Kind of like hitting your finger with a hammer because it feels so good when you stop. – Ed Power Feb 12 '10 at 18:09
  • While I agree it's difficult I don't think there's nothing to gain. A WPF/Silverlight rendering engine would benefit from their rendering engines, which means you can apply their transformations/effects, etc. on the html content. IE's rendering engine isn't, that's why you can't do that with content from the WebBrowser control. Such a project would make html a first-class citizen in the world of WPF/Silverlight, something I would love to see. – John Nov 21 '13 at 15:14

4 Answers4

3

Since WPF XAML's capabilities are a superset of HTML's capabilities, pretty much all you will need to do is map the one to the other.

If your input is XHTML you can simply use XSLT or LINQ-to-XML to get the data.

If you use XSLT you will end up with a XAML that you parse with XamlReader.Parse() to get a UIElement you can add to your Window or other WPF container. This is probably more work than using LINQ-to-XML because XSLT is relatively limited.

If you use LINQ-to-XML you can choose to produce XAML and then parse it as in the XSLT case, or you can just create the object tree directly. If you create XAML using LINQ-to-XML, I would write the actual translation code in VB.NET instead of C# which is my normal preferred language. VB.NET's "XML Literal" syntax makes that part of the job easier.

If your input is arbitrary HTML and not XHTML, I would use a third party library to convert it to XHTML and work from there. Handling all of HTML's parsing strangeness is a lot of work and probably mostly unrelated to your actual goal.

If you just want to display HTML without turning it into WPF objects, you can use the Frame control.

Ray Burns
  • 62,163
  • 12
  • 140
  • 141
1

Rendering html will be easy just convert to xaml file and render should be quite fast. You will get a HW accelerated browser with beautiful zoom and fonts.

But how do you handle css - you will need to create styles ( Yuck) ? DHTML , HTML5 and Java script .. So many pages now require these thinsg you cant even view it.

The easy way for Java script is to use the parser everyone uses , there may be a better way ( and Java script to CIL compilers ?)

ben
  • 223
  • 2
  • 2
0

html parsers, with a typed mapping to xaml alternatives, interesting... perhaps you can use antlr for the parser lexer http://www.antlr.org/, more specifically heres an interesting codeproject, project http://www.codeproject.com/KB/cs/TagBasedHtmlParser.aspx, you could perhaps try transform your html into xaml, which would be quite interesting :)

almog.ori
  • 7,839
  • 1
  • 35
  • 49
0

I think this project is exactly what you aim to achieve:
https://htmlrenderer.codeplex.com/
Maybe you could work on it and add HTML 5 and CSS 3 support!

SepehrM
  • 1,087
  • 2
  • 20
  • 44