1
+ using System ;
2
+ using System . IO ;
3
+ using System . Linq ;
4
+ using System . Xml . XPath ;
5
+ using Xunit ;
6
+
7
+ namespace HtmlAgilityPack . Tests . NetStandard2_0
8
+ {
9
+ public class HtmlDocumentPreserveOriginalTest
10
+ {
11
+ [ Fact ]
12
+ public void PreserveEmptyAttributesTest ( )
13
+ {
14
+ var d = new HtmlDocument ( ) ;
15
+ d . OptionEmptyCollection = true ;
16
+
17
+ d . GlobalAttributeValueQuote = AttributeValueQuote . Initial ;
18
+
19
+ d . OptionDefaultUseOriginalName = true ;
20
+ //d.OptionPreserveEmptyAttributes = true;
21
+
22
+ var html = @"
23
+ <form #editForm='ngForm' (ngSubmit)='OnSaveData()' name='globalForm' [dataSourceContext]='detailDataSourceContext'>
24
+ <page editable (beginEdit)='OnBeginEdit()' (cancelEdit)='OnCancelEdit($event)'>
25
+
26
+ <page-header pageHeaderIcon='_Lea_ mdi mdi-code-json'>
27
+
28
+ <page-header-actions>
29
+ <list-counter formErrorsCounter></list-counter>
30
+
31
+ <button mat-button *hasEveryPermission='permissionConstants.LeaProdutos.edit' enterEditButton>
32
+ </button>
33
+
34
+ <button mat-button *hasEveryPermission='permissionConstants.LeaProdutos.edit' cancelEditButton>
35
+ </button>
36
+
37
+ <button mat-button *hasEveryPermission='permissionConstants.LeaProdutos.edit' type='submit' confirmEditButton>
38
+ </button>
39
+
40
+ <refresh-button></refresh-button>
41
+ </page-header-actions>
42
+
43
+ </page-header>
44
+
45
+ <page-body>
46
+ </page-body>
47
+
48
+ </page>
49
+ </form>
50
+ " ;
51
+ d . LoadHtml ( html ) ;
52
+
53
+ var outer = d . DocumentNode . OuterHtml ;
54
+
55
+ var xpath = XPathExpression . Compile ( "/form/page/page-header/page-header-actions/button[@enterEditButton]" ) ;
56
+ var nodes = d . DocumentNode . SelectNodes ( xpath ) ;
57
+
58
+ Assert . Equal ( 1 , nodes ? . Count ) ;
59
+ Assert . Equal ( html , d . DocumentNode . OuterHtml ) ;
60
+ }
61
+
62
+ [ Fact ]
63
+ public void PreserveOriginalCasingTest ( )
64
+ {
65
+ var d = new HtmlDocument ( ) ;
66
+ d . OptionEmptyCollection = true ;
67
+
68
+ d . GlobalAttributeValueQuote = AttributeValueQuote . Initial ;
69
+
70
+ d . OptionDefaultUseOriginalName = true ;
71
+ //d.OptionPreserveEmptyAttributes = true;
72
+
73
+ var html = @"
74
+ <form #editForm='ngForm' (ngSubmit)='OnSaveData()' name='globalForm' [dataSourceContext]='detailDataSourceContext'>
75
+ <page editable (beginEdit)='OnBeginEdit()' (cancelEdit)='OnCancelEdit($event)'>
76
+
77
+ <page-Header pageHeaderIcon='_Lea_ mdi mdi-code-json'>
78
+
79
+ <page-Header-Actions>
80
+ <list-counter formErrorsCounter></list-counter>
81
+
82
+ <button mat-button *hasEveryPermission='permissionConstants.LeaProdutos.edit' enterEditButton>
83
+ </button>
84
+
85
+ <button mat-button *hasEveryPermission='permissionConstants.LeaProdutos.edit' cancelEditButton>
86
+ </button>
87
+
88
+ <button mat-button *hasEveryPermission='permissionConstants.LeaProdutos.edit' type='submit' confirmEditButton>
89
+ </button>
90
+
91
+ <refresh-button></refresh-button>
92
+ </page-Header-Actions>
93
+
94
+ </page-Header>
95
+
96
+ <page-body>
97
+ </page-body>
98
+
99
+ </page>
100
+ </form>
101
+ " ;
102
+ d . LoadHtml ( html ) ;
103
+
104
+ var xpath = XPathExpression . Compile ( "/form/page/page-Header/page-Header-Actions/button[@enterEditButton]" ) ;
105
+ var nodes = d . DocumentNode . SelectNodes ( xpath ) ;
106
+
107
+ Assert . Equal ( 1 , nodes ? . Count ) ;
108
+ Assert . Equal ( html , d . DocumentNode . OuterHtml ) ;
109
+ }
110
+
111
+
112
+ [ Fact ]
113
+ public void PreserveOriginalQuoteTest ( )
114
+ {
115
+ var contentDirectory = Path . GetDirectoryName ( typeof ( HtmlDocumentPreserveOriginalTest ) . Assembly . Location ) . ToString ( ) + "\\ files\\ " ;
116
+
117
+ var d = new HtmlDocument ( ) ;
118
+ d . OptionEmptyCollection = true ;
119
+
120
+ d . GlobalAttributeValueQuote = AttributeValueQuote . Initial ;
121
+ d . OptionDefaultUseOriginalName = true ;
122
+
123
+ var filePath = Path . Combine ( contentDirectory , "attr_quote.html" ) ;
124
+ var filePathExpected = Path . Combine ( contentDirectory , "attr_quote_expected.html" ) ;
125
+ d . Load ( filePath ) ;
126
+
127
+ var xpath = XPathExpression . Compile ( "/form/page/page-body/card/data-group/mat-form-field[@*[local-name() = 'xdt:Transform' and . = \" InsertAfter(mat-form-field[mat-select/@name = 'tipoProdutoId'])\" ]]/mat-select/mat-option" ) ;
128
+ var nodes = d . DocumentNode . SelectNodes ( xpath ) ;
129
+
130
+ Assert . Equal ( 1 , nodes ? . Count ) ;
131
+
132
+ nodes [ 0 ] . ParentNode . Attributes [ "required" ] . Value = "true" ;
133
+
134
+ Assert . Equal ( File . ReadAllText ( filePathExpected ) , d . DocumentNode . OuterHtml ) ;
135
+
136
+ var clone = nodes [ 0 ] . CloneNode ( true ) ;
137
+
138
+ var expectedOuterNode = @"<mat-option *ngFor=""let dimension of lea546CATBEM$ | async | filterDimensionByValue:( simulationRequest.tipoProdutoId | commonData:lea508SUBPROD$:'id':'code')"" [value]=""dimension.dimensionId"">
139
+
140
+
141
+ {{ dimension.code }} - {{ dimension.description }}
142
+ </mat-option>" ;
143
+
144
+ Assert . Equal ( expectedOuterNode , clone . OuterHtml ) ;
145
+
146
+ }
147
+
148
+
149
+ [ Fact ]
150
+ public void PreserveClonedEmptyAttributesTest ( )
151
+ {
152
+ var d = new HtmlDocument ( ) ;
153
+ d . GlobalAttributeValueQuote = AttributeValueQuote . Initial ;
154
+ d . OptionDefaultUseOriginalName = true ;
155
+
156
+ var html = @"<list-counter formErrorsCounter></list-counter>" ;
157
+ d . LoadHtml ( html ) ;
158
+
159
+ var cloned = d . DocumentNode . CloneNode ( true ) ;
160
+
161
+ Assert . Equal ( @"<list-counter formErrorsCounter></list-counter>" , cloned . OuterHtml ) ;
162
+ }
163
+
164
+ [ Fact ]
165
+ public void PreserveQuoteTypeForLoadedAttributes ( )
166
+ {
167
+ var input = HtmlNode . CreateNode ( "<input checked></input>" ) ;
168
+ var checkedAttribute = input . Attributes . First ( ) ;
169
+
170
+ // Result is: Value: '' (empty string)
171
+ Assert . Equal ( "" , checkedAttribute . Value ) ;
172
+
173
+ // Result is: QuoteType: WithoutValue
174
+ Assert . Equal ( AttributeValueQuote . WithoutValue , checkedAttribute . QuoteType ) ;
175
+ }
176
+ }
177
+ }
0 commit comments