jamietre / CsQuery

CsQuery is a complete CSS selector engine, HTML parser, and jQuery port for C# and .NET 4.
Other
1.15k stars 249 forks source link

Working with XHTML #94

Open cmwoods opened 11 years ago

cmwoods commented 11 years ago

Is there any way to specify that the Save or Render methods should generate XHTML output? I'm reading in XHTML input document but the output is generated is no longer XHTML (tags like meta aren't being self-closed which I think is a requirement of XHTML?).

Req commented 11 years ago

Also the BR tag seems to get rendered without it's self closing slash, which breaks XHTML compliance.

jamietre commented 11 years ago

I'm just getting back into the swing of things after my daughter was born (:first-child) but I'm planning to get caught up this week! Thanks for hanging in there.

304NotModified commented 9 years ago

+1

This sounds (no harm intended) like a small change?

jamietre commented 9 years ago

This works as far as I can tell, see CsQuery.Tests\HtmlParser\DocType.cs - as long as the document type is XHTML, it should render the correct tag format.

    [Test]
    public void Issue94()
    {
        string html = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN""      
             ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
            <html><head></head><body><br></body></html>";
        var dom = CQ.Create(html);
        var br = dom["br"];
        Assert.AreEqual(br.RenderSelection(), "<br />");
    }

You can change the doctype by replacing the DocType node:

dom.Document.DocTypeNode = dom.Document.CreateDocumentType(DocType.XHTML);