1+ using System . Xml ;
2+ using System . Xml . Linq ;
3+ using System . Xml . XPath ;
4+ using Devlooped . Web ;
5+
6+ namespace Devlooped . Tests ;
7+
8+ public record HtmlTests ( ITestOutputHelper Output )
9+ {
10+ [ Fact ]
11+ public void Render ( )
12+ {
13+ var doc = HtmlDocument . Load ( File ( "sample.html" ) ) ;
14+
15+ Output . WriteLine ( doc . ToString ( ) ) ;
16+ }
17+
18+ [ Fact ]
19+ public void ExcludesScriptsByDefault ( )
20+ {
21+ var doc = HtmlDocument . Load ( File ( "wikipedia.html" ) ) ;
22+
23+ Assert . Empty ( doc . XPathSelectElements ( "//script" ) ) ;
24+ }
25+
26+ [ Fact ]
27+ public void IncludeScriptsExplicitSettings ( )
28+ {
29+ var doc = HtmlDocument . Load ( File ( "wikipedia.html" ) , new HtmlReaderSettings ( ) ) ;
30+
31+ Assert . NotEmpty ( doc . XPathSelectElements ( "//script" ) ) ;
32+ }
33+
34+ [ Fact ]
35+ public void ExcludesStylesByDefault ( )
36+ {
37+ var doc = HtmlDocument . Load ( File ( "wikipedia.html" ) ) ;
38+
39+ Assert . Empty ( doc . XPathSelectElements ( "//style" ) ) ;
40+ }
41+
42+ [ Fact ]
43+ public void IncludeStylesExplicitSettings ( )
44+ {
45+ var doc = HtmlDocument . Load ( File ( "wikipedia.html" ) , new HtmlReaderSettings ( ) ) ;
46+
47+ Assert . NotEmpty ( doc . XPathSelectElements ( "//style" ) ) ;
48+ }
49+
50+ [ Fact ]
51+ public void ExcludesXmlNamespacesByDefault ( )
52+ {
53+ var doc = HtmlDocument . Load ( File ( "sample.xhtml" ) ) ;
54+
55+ Assert . NotEmpty ( doc . XPathSelectElements ( "//h1" ) ) ;
56+ }
57+
58+ [ Fact ]
59+ public void IncludeXmlNamespacesExplicitly ( )
60+ {
61+ var doc = HtmlDocument . Load ( File ( "sample.xhtml" ) , new HtmlReaderSettings { IgnoreXmlNamespaces = false } ) ;
62+ var resolver = new XmlNamespaceManager ( new NameTable ( ) ) ;
63+ resolver . AddNamespace ( "xh" , "http://www.w3.org/1999/xhtml" ) ;
64+
65+ Assert . NotEmpty ( doc . XPathSelectElements ( "//xh:h1" , resolver ) ) ;
66+ // Won't match because the elements will have the XHTML namespace
67+ Assert . Empty ( doc . XPathSelectElements ( "//h1" ) ) ;
68+ }
69+
70+ [ Fact ]
71+ public void CanChangeToUpperCaseHtml ( )
72+ {
73+ var doc = HtmlDocument . Load ( File ( "wikipedia.html" ) ,
74+ new HtmlReaderSettings
75+ {
76+ CaseFolding = Sgml . CaseFolding . ToUpper ,
77+ } ) ;
78+
79+ // The source has lowercase elements
80+ var central = doc . XPathSelectElement ( "/HTML/BODY/DIV/H1/SPAN" ) ;
81+
82+ Assert . NotNull ( central ) ;
83+ }
84+
85+ [ Fact ]
86+ public void HtmlSettings ( )
87+ {
88+ var doc = HtmlDocument . Load ( File ( "wikipedia.html" ) ,
89+ new HtmlReaderSettings
90+ {
91+ TextWhitespace = Sgml . TextWhitespaceHandling . TrimBoth ,
92+ WhitespaceHandling = WhitespaceHandling . None
93+ } ) ;
94+
95+ var central = doc . XPathSelectElement ( "/html/body/div/h1/span" ) ;
96+
97+ // The source contains leading and trailing whitespaces.
98+ Assert . Equal ( "Wikipedia" , central ? . Value ) ;
99+ }
100+
101+ string File ( string path ) => new Uri ( "file://" + new FileInfo ( path ) . FullName ) . AbsoluteUri ;
102+ }
0 commit comments