Skip to content

Commit 5c657bf

Browse files
committed
WIP (45)
1 parent 5dcbe95 commit 5c657bf

File tree

2 files changed

+64
-44
lines changed

2 files changed

+64
-44
lines changed

src/WinWebDiff/WinWebDiff.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <CommCtrl.h>
77
#include <string>
88
#include <vector>
9+
#include <list>
910
#include <filesystem>
1011
#include <wrl.h>
1112

@@ -125,8 +126,8 @@ HWND m_hWnd;
125126
WCHAR m_szTitle[256] = L"WinWebDiff";
126127
WCHAR m_szWindowClass[256] = L"WINWEBDIFF";
127128
IWebDiffWindow* m_pWebDiffWindow = nullptr;
128-
std::vector<TempFile> m_tempFiles;
129-
std::vector<TempFolder> m_tempFolders;
129+
std::list<TempFile> m_tempFiles;
130+
std::list<TempFolder> m_tempFolders;
130131

131132
ATOM MyRegisterClass(HINSTANCE hInstance);
132133
BOOL InitInstance(HINSTANCE, int);

src/WinWebDiffLib/WebWindow.hpp

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,16 @@ class CWebWindow
8787

8888
wil::com_ptr_t<ICoreWebView2Settings> settings;
8989
m_webview->get_Settings(&settings);
90-
settings->put_IsScriptEnabled(TRUE);
91-
settings->put_AreDefaultScriptDialogsEnabled(TRUE);
92-
settings->put_IsWebMessageEnabled(TRUE);
93-
settings->put_AreDevToolsEnabled(TRUE);
94-
auto settings2 = settings.try_query<ICoreWebView2Settings2>();
95-
if (settings2)
96-
settings2->put_UserAgent(userAgent.c_str());
90+
if (settings)
91+
{
92+
settings->put_IsScriptEnabled(TRUE);
93+
settings->put_AreDefaultScriptDialogsEnabled(TRUE);
94+
settings->put_IsWebMessageEnabled(TRUE);
95+
settings->put_AreDevToolsEnabled(TRUE);
96+
auto settings2 = settings.try_query<ICoreWebView2Settings2>();
97+
if (settings2)
98+
settings2->put_UserAgent(userAgent.c_str());
99+
}
97100

98101
m_webview->add_NewWindowRequested(
99102
Callback<ICoreWebView2NewWindowRequestedEventHandler>(
@@ -140,6 +143,7 @@ class CWebWindow
140143
}).Get(), nullptr);
141144

142145
m_webview->CallDevToolsProtocolMethod(L"Page.enable", L"{}", nullptr);
146+
/*
143147
m_webview->CallDevToolsProtocolMethod(L"Network.enable", L"{}", nullptr);
144148
m_webview->CallDevToolsProtocolMethod(L"Log.enable", L"{}", nullptr);
145149
@@ -154,6 +158,7 @@ class CWebWindow
154158
return m_parent->OnDevToolsProtocolEventReceived(sender, args, event2);
155159
}).Get(), nullptr);
156160
}
161+
*/
157162

158163
if (args && deferral)
159164
{
@@ -533,30 +538,40 @@ class CWebWindow
533538

534539
HRESULT SaveText(FILE *fp, const WValue& value)
535540
{
536-
HRESULT hr = S_OK;
541+
const int nodeType = value[L"nodeType"].GetInt();
542+
const auto* nodeName = value[L"nodeName"].GetString();
543+
const bool fInline = IsInlineElement(nodeName);
544+
537545
if (value[L"nodeType"].GetInt() == 3 /* #text */)
538546
{
539547
if (fwprintf(fp, L"%s", value[L"nodeValue"].GetString()) < 0)
540-
hr = HRESULT_FROM_WIN32(GetLastError());
541-
if (!IsInlineElement(value[L"nodeName"].GetString()))
542-
fwprintf(fp, L"\n");
548+
return HRESULT_FROM_WIN32(GetLastError());
543549
}
544-
const auto& nodeName = value[L"nodeName"].GetString();
545-
if (wcscmp(nodeName, L"SCRIPT") != 0 && wcscmp(nodeName, L"STYLE") != 0)
550+
if (value.HasMember(L"children") && value[L"children"].IsArray())
546551
{
547-
if (value.HasMember(L"children") && value[L"children"].IsArray())
552+
if (wcscmp(nodeName, L"SCRIPT") != 0 && wcscmp(nodeName, L"STYLE") != 0)
548553
{
554+
int textCount = 0;
549555
for (const auto& child : value[L"children"].GetArray())
550556
{
551-
HRESULT hr2 = SaveText(fp, child);
552-
if (FAILED(hr2))
553-
hr = hr2;
557+
int childNodeType = child[L"nodeType"].GetInt();
558+
if (childNodeType == 3)
559+
textCount++;
560+
HRESULT hr = SaveText(fp, child);
561+
if (FAILED(hr))
562+
return hr;
554563
}
564+
if ((!fInline && textCount > 0) || wcscmp(nodeName, L"BR") == 0 || wcscmp(nodeName, L"HR") == 0)
565+
fwprintf(fp, L"\n");
555566
}
556-
if (value.HasMember(L"contentDocument"))
557-
hr = SaveText(fp, value[L"contentDocument"]);
558567
}
559-
return hr;
568+
if (value.HasMember(L"contentDocument"))
569+
{
570+
HRESULT hr = SaveText(fp, value[L"contentDocument"]);
571+
if (FAILED(hr))
572+
return hr;
573+
}
574+
return S_OK;
560575
}
561576

562577
HRESULT SaveText(const std::wstring& filename, IWebDiffCallback* callback)
@@ -1079,48 +1094,52 @@ class CWebWindow
10791094
}
10801095
}
10811096

1097+
static int cmp(const void* a, const void* b)
1098+
{
1099+
const wchar_t* const* pa = reinterpret_cast<const wchar_t* const*>(a);
1100+
const wchar_t* const* pb = reinterpret_cast<const wchar_t* const*>(b);
1101+
return wcscmp(*pa, *pb);
1102+
}
1103+
10821104
static bool IsInlineElement(const wchar_t* name)
10831105
{
10841106
static const wchar_t* inlineElements[] =
10851107
{
1086-
L"B",
1087-
L"BIG",
1088-
L"I",
1089-
L"SMALL",
1090-
L"TT",
1108+
L"A",
10911109
L"ABBR",
10921110
L"ACRONYM",
1111+
L"B",
1112+
L"BDO",
1113+
L"BIG",
1114+
L"BR",
1115+
L"BUTTON",
10931116
L"CITE",
10941117
L"CODE",
10951118
L"DFN",
10961119
L"EM",
1097-
L"KBD",
1098-
L"STRONG",
1099-
L"SAMP",
1100-
L"VAR",
1101-
L"A",
1102-
L"BDO",
1103-
L"BR",
1120+
L"I",
11041121
L"IMG",
1122+
L"INPUT",
1123+
L"KBD",
1124+
L"LABEL",
11051125
L"MAP",
11061126
L"OBJECT",
11071127
L"Q",
1128+
L"SAMP",
11081129
L"SCRIPT",
1130+
L"SELECT",
1131+
L"SMALL",
11091132
L"SPAN",
1133+
L"STRONG",
11101134
L"SUB",
11111135
L"SUP",
1112-
L"BUTTON",
1113-
L"INPUT",
1114-
L"LABEL",
1115-
L"SELECT",
11161136
L"TEXTAREA",
1137+
L"TT",
1138+
L"VAR",
11171139
};
1118-
for (const auto* inlineName: inlineElements)
1119-
{
1120-
if (wcscmp(name, inlineName) == 0)
1121-
return true;
1122-
}
1123-
return false;
1140+
return bsearch(&name, inlineElements,
1141+
sizeof(inlineElements) / sizeof(inlineElements[0]),
1142+
sizeof(inlineElements[0]), cmp);
11241143
}
11251144

11261145
static std::wstring Escape(const std::wstring& text)

0 commit comments

Comments
 (0)