-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
383 lines (332 loc) · 15.1 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TidyManaged;
using HtmlAgilityPack;
using System.Threading;
using System.IO;
using System.Xml.XPath;
using Microsoft.Web.WebView2;
using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Core;
namespace Html_Css
{
public partial class Form1 : Form
{
readonly RichTextBox rtb1 = new RichTextBox();
readonly WebView2 view = new WebView2();
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
public Form1()
{
InitializeComponent();
}
private void htmlSourceToolStripMenuItem_Click(object sender, EventArgs e)
{
splitContainer1.BringToFront();
richTextBox1.Clear();
richTextBox1.LoadFile(Application.StartupPath + "/tempHtml.txt", RichTextBoxStreamType.PlainText);
ColorHtmlTag();
treeView1.Nodes.Clear();
document.Load(Application.StartupPath + "/tempHtml.txt");
HtmlNode node = document.DocumentNode;
treeView1.Nodes.Add(node.Name);
TreeViewMethods.BindTreeView(node, treeView1.Nodes[0]);
treeView1.ExpandAll();
htmlElementToolStripMenuItem1.Enabled = false;
}
private async void htmlToolStripMenuItem3_Click(object sender, EventArgs e)
{
view.Dock = DockStyle.Fill;
this.Controls.Add(view);
view.BringToFront();
await view.EnsureCoreWebView2Async(null);
view.CoreWebView2.NavigateToString(richTextBox1.Text);
htmlElementToolStripMenuItem1.Enabled = false;
}
private void webBrowserToolStripMenuItem_Click(object sender, EventArgs e)
{
rtb1.Dock = DockStyle.Fill;
this.Controls.Add(rtb1);
rtb1.BringToFront();
htmlElementToolStripMenuItem1.Enabled = false;
}
private void htmlToolStripMenuItem_Click(object sender, EventArgs e)
{
HtmlCssData hs = new HtmlCssData();
document = hs.GetHtmlStructure;
string filePath = Application.StartupPath;
document.Save(filePath + "/tempHtml.txt");
richTextBox1.LoadFile(filePath + "/tempHtml.txt", RichTextBoxStreamType.PlainText);
using (Document d = Document.FromString(richTextBox1.Text))
{
d.IndentBlockElements = AutoBool.Yes;
d.IndentSpaces = 2;
d.AddTidyMetaElement = false;
d.CleanAndRepair();
d.Save(filePath + "/tempHtml.txt");
}
document.Load(filePath + "/tempHtml.txt");
richTextBox1.Clear();
richTextBox1.LoadFile(filePath + "/tempHtml.txt", RichTextBoxStreamType.PlainText);
this.ColorHtmlTag();
//view.NavigateToString(richTextBox1.Text);
treeView1.Nodes.Clear();
treeView1.Nodes.Add(document.DocumentNode.Name);
TreeViewMethods.BindTreeView(document.DocumentNode, treeView1.Nodes[0]);
htmlElementToolStripMenuItem1.Enabled = false;
builder.Dispose();
builder = new CssBulderControl1();
//builder.Dock = DockStyle.Fill ;
// this.Controls.Add(builder);
// builder.BringToFront();
}
readonly HtmlelementControl ctl = new HtmlelementControl();
private void htmlElementToolStripMenuItem_Click(object sender, EventArgs e)
{
ctl.Dock = DockStyle.Fill;
this.Controls.Add(ctl);
ctl.BringToFront();
htmlElementToolStripMenuItem1.Enabled = true;
}
private void HtmlelementControl_Leave(object sender, EventArgs e)
{
htmlElementToolStripMenuItem1.Enabled = false;
}
private void htmlElementToolStripMenuItem1_Click(object sender, EventArgs e)
{
HtmlNode nd = ctl.node;
Control[] control = ctl.Controls.Find("checkedListBox1", true);
document.Load(Application.StartupPath + "/tempHtml.txt");
CheckedListBox checkedListBox1 = (CheckedListBox)control[0];
lock (checkedListBox1.CheckedItems)
{
foreach (var item in checkedListBox1.CheckedItems)
{
string displayname = item.ToString();
int index = checkedListBox1.FindStringExact(displayname);
string name = "textBox" + (index + 1).ToString();
Control[] c = checkedListBox1.Controls.Find(name, true);
TextBox tb = (TextBox)c[0];
HtmlAttribute attr = document.CreateAttribute(displayname, tb.Text);
nd.Attributes.Add(attr);
}
}
Control[] ctl1 = ctl.Controls.Find("innerHtmlTextBox", true);
TextBox tb1 = (TextBox)ctl1[0];
Control[] ctl2 = ctl.Controls.Find("pathTextBox", true);
TextBox tb2 = (TextBox)ctl2[0];
nd.InnerHtml = tb1.Text;
XPathNavigator nav = document.DocumentNode.CreateRootNavigator();
if (tb2.Text != string.Empty)
{
try
{
var ob = (XPathNodeIterator)nav.Evaluate(tb2.Text);
if (ob.Count == 0) { MessageBox.Show("The path is not correct"); return; }
}
catch(XPathException) { MessageBox.Show("The Path is Not Correct"); return; }
HtmlNode node = document.DocumentNode.SelectSingleNode(tb2.Text);
node.AppendChild(nd);
document.Save(Application.StartupPath + "/tempHtml.txt");
}
else if (MessageBox.Show("Please Enter A Path") == DialogResult.OK)tb2.Select();
richTextBox1.LoadFile(Application.StartupPath + "/tempHtml.txt", RichTextBoxStreamType.PlainText);
toolStripStatusLabel1.Text = "Html Element <" + nd.Name + "> added";
string filePath = Application.StartupPath;
using (Document d = Document.FromString(richTextBox1.Text))
{
d.IndentBlockElements = AutoBool.Yes;
d.IndentSpaces = 2;
d.AddTidyMetaElement = false;
d.CleanAndRepair();
d.Save(filePath + "/tempHtml.txt");
}
document.LoadHtml(richTextBox1.Text);
richTextBox1.Clear();
richTextBox1.LoadFile(filePath + "/tempHtml.txt", RichTextBoxStreamType.PlainText);
}
CssBulderControl1 builder = new CssBulderControl1();
private void cSSBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
builder.Dock = DockStyle.Fill;
this.Controls.Add(builder);
builder.BringToFront();
cSSBlockToolStripMenuItem1.Enabled= true;
htmlElementToolStripMenuItem1.Enabled = false;
}
readonly StringBuilder sb = new StringBuilder();
private void cSSBlockToolStripMenuItem1_Click(object sender, EventArgs e)
{
Control[] ctl = builder.Controls.Find("listBox1", true);
Control[] ctl1 = builder.Controls.Find("checkedListBox2", true);
ListBox listBox1 = (ListBox)ctl[0];
if (listBox1.SelectedItem != null)
{
sb.AppendLine(listBox1.SelectedItem.ToString() + "\t" + "{");
}
CheckedListBox clb = (CheckedListBox)ctl1[0];
foreach (var item in clb.CheckedItems)
{
string displayname = item.ToString();
int index = clb.FindStringExact(displayname);
string name = "textBox" + (index + 1).ToString();
Control[] c = clb.Controls.Find(name, true);
TextBox tb = (TextBox)c[0];
sb.AppendLine(displayname + ":" + tb.Text + ";");
}
sb.AppendLine("}");
rtb1.AppendText(sb.ToString());
sb.Clear();
toolStripStatusLabel1.Text = "CSS For " + listBox1.SelectedItem.ToString() + " added";
cSSBlockToolStripMenuItem1.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.LoadFile(Application.StartupPath + "/tempHtml.txt", RichTextBoxStreamType.PlainText);
treeView1.Nodes.Clear();
document.Load(Application.StartupPath + "/tempHtml.txt");
HtmlNode node = document.DocumentNode;
treeView1.Nodes.Add(node.Name);
TreeViewMethods.BindTreeView(node, treeView1.Nodes[0]);
ColorHtmlTag();
HtmlNode nd = null;
XPathNavigator nav = document.DocumentNode.CreateRootNavigator();
if (nav.Evaluate("//head/link") != null)
{
nd = document.DocumentNode.SelectSingleNode("//head/link");
}
if (nd != null)
{
string cssfilePath = nd.GetAttributeValue("href", string.Empty);
rtb1.LoadFile(cssfilePath,RichTextBoxStreamType.PlainText);
}
else return;
}
private async void htmlToolStripMenuItem1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Html File(.html)|*.html";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
document.LoadHtml(richTextBox1.Text);
document.Save(Application.StartupPath + "/tempHtml.txt");
Thread.Sleep(1000);
this.ColorHtmlTag();
await view.EnsureCoreWebView2Async(null);
view.CoreWebView2.NavigateToString(richTextBox1.Text);
treeView1.Nodes.Clear();
treeView1.Nodes.Add(document.DocumentNode.Name);
TreeViewMethods.BindTreeView(document.DocumentNode, treeView1.Nodes[0]);
htmlElementToolStripMenuItem1.Enabled = false;
builder.Dispose();
builder = new CssBulderControl1();
}
}
private void htmlToolStripMenuItem2_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "Html File(.html)|*.html";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
htmlElementToolStripMenuItem1.Enabled = false;
}
private void cssToolStripMenuItem1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "CSS File(.css)|*.css";
saveFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
rtb1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
document.Save(Application.StartupPath + "/tempcss.txt");
}
htmlElementToolStripMenuItem1.Enabled = false;
}
private void cssToolStripMenuItem2_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "CSS File(.css)|*.css";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
rtb1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
htmlElementToolStripMenuItem1.Enabled = false;
}
private void GetHtmlTags(HtmlNode node, ref List<HtmlNode> taglist)
{
if (node.HasChildNodes)
{
foreach (HtmlNode n in node.ChildNodes)
{
if (n.Name == "#text" ) continue;
else taglist.Add(n);
this.GetHtmlTags(n, ref taglist);
}
}
}
List<HtmlNode> HtmlTagList = new List<HtmlNode>();
private void ColorHtmlTag()
{
FindAndHighLight("<", Color.Brown);
FindAndHighLight(">", Color.Brown);
FindAndHighLight("/", Color.Brown);
FindAndHighLight("!DOCTYPE", Color.Brown);
this.GetHtmlTags(document.DocumentNode, ref HtmlTagList);
foreach (HtmlNode node in HtmlTagList)
{
FindAndHighLight(node.Name, Color.Brown);
//FindAndHighLight(node.InnerHtml, Color.Olive);
}
this.GetAttributes(document.DocumentNode, ref attrList);
foreach (HtmlAttribute attr in attrList)
{
FindAndHighLight(attr.Name, Color.Red);
//FindAndHighLight(attr.Value, Color.Blue);
}
}
private void FindAndHighLight(string str,Color clr)
{
int index = richTextBox1.Text.IndexOf(str);
while (index != -1)
{
richTextBox1.SelectionStart = index;
richTextBox1.SelectionLength = str.Length;
richTextBox1.SelectionColor = clr;
index = richTextBox1.Text.IndexOf(str, index + str.Length);
}
}
private void GetAttributes(HtmlNode node, ref List<HtmlAttribute> collection)
{
if (node.HasChildNodes)
{
foreach (HtmlNode n in node.ChildNodes)
{
if (n.Name == "#text") continue;
else
{
foreach (HtmlAttribute attr in n.Attributes)
{
collection.Add(attr);
}
}
this.GetAttributes(n, ref collection);
}
}
}
List<HtmlAttribute> attrList = new List<HtmlAttribute>();
private void cssToolStripMenuItem_Click(object sender, EventArgs e)
{
htmlElementToolStripMenuItem1.Enabled = false;
rtb1.Clear();
}
}
}