@@ -878,7 +878,15 @@ class Highlighter
878
878
}
879
879
}
880
880
881
- static std::wstring modifiedNodesToHTMLs (const WValue& tree, std::list<ModifiedNode>& nodes)
881
+ struct Context
882
+ {
883
+ explicit struct Context (bool inPreElement, std::list<ModifiedNode>& nodes)
884
+ : inPreElement(inPreElement), nodes(nodes) { }
885
+ bool inPreElement;
886
+ std::list<ModifiedNode>& nodes;
887
+ };
888
+
889
+ static std::wstring modifiedNodesToHTMLsInternal (Context& ctxt, const WValue& tree)
882
890
{
883
891
std::wstring html;
884
892
NodeType nodeType = static_cast <NodeType>(tree[L" nodeType" ].GetInt ());
@@ -896,7 +904,7 @@ class Highlighter
896
904
if (tree.HasMember (L" children" ))
897
905
{
898
906
for (const auto & child : tree[L" children" ].GetArray ())
899
- html += modifiedNodesToHTMLs (child, nodes );
907
+ html += modifiedNodesToHTMLsInternal (ctxt, child );
900
908
}
901
909
break ;
902
910
}
@@ -912,10 +920,10 @@ class Highlighter
912
920
if (tree.HasMember (L" insertedNodes" ))
913
921
{
914
922
for (const auto & child : tree[L" insertedNodes" ].GetArray ())
915
- html += modifiedNodesToHTMLs (child, nodes );
923
+ html += modifiedNodesToHTMLsInternal (ctxt, child );
916
924
}
917
925
std::wstring h = utils::EncodeHTMLEntities (tree[L" nodeValue" ].GetString ());
918
- if (!h.empty () && std::all_of (h.begin (), h.end (), [](wchar_t ch) { return iswspace (ch); }))
926
+ if (!ctxt. inPreElement && ! h.empty () && std::all_of (h.begin (), h.end (), [](wchar_t ch) { return iswspace (ch); }))
919
927
{
920
928
h.pop_back ();
921
929
h += L" ​" ;
@@ -924,14 +932,14 @@ class Highlighter
924
932
if (tree.HasMember (L" appendedNodes" ))
925
933
{
926
934
for (const auto & child : tree[L" appendedNodes" ].GetArray ())
927
- html += modifiedNodesToHTMLs (child, nodes );
935
+ html += modifiedNodesToHTMLsInternal (ctxt, child );
928
936
}
929
937
if (tree.HasMember (L" modified" ))
930
938
{
931
939
ModifiedNode node;
932
940
node.nodeId = tree[L" nodeId" ].GetInt ();
933
941
node.outerHTML = html;
934
- nodes.emplace_back (std::move (node));
942
+ ctxt. nodes .emplace_back (std::move (node));
935
943
}
936
944
break ;
937
945
}
@@ -940,10 +948,11 @@ class Highlighter
940
948
if (tree.HasMember (L" insertedNodes" ))
941
949
{
942
950
for (const auto & child : tree[L" insertedNodes" ].GetArray ())
943
- html += modifiedNodesToHTMLs (child, nodes );
951
+ html += modifiedNodesToHTMLsInternal (ctxt, child );
944
952
}
953
+ const std::wstring nodeName = tree[L" nodeName" ].GetString ();
945
954
html += L' <' ;
946
- html += tree[ L" nodeName" ]. GetString () ;
955
+ html += nodeName;
947
956
if (tree.HasMember (L" attributes" ))
948
957
{
949
958
const auto & attributes = tree[L" attributes" ].GetArray ();
@@ -960,18 +969,21 @@ class Highlighter
960
969
html += L' >' ;
961
970
if (tree.HasMember (L" children" ))
962
971
{
972
+ const bool oldInPreElement = ctxt.inPreElement ;
973
+ ctxt.inPreElement = ctxt.inPreElement || (nodeName == L" PRE" );
963
974
for (const auto & child : tree[L" children" ].GetArray ())
964
- html += modifiedNodesToHTMLs (child, nodes);
975
+ html += modifiedNodesToHTMLsInternal (ctxt, child);
976
+ ctxt.inPreElement = oldInPreElement;
965
977
}
966
978
if (tree.HasMember (L" appendedNodes" ))
967
979
{
968
980
for (const auto & child : tree[L" appendedNodes" ].GetArray ())
969
- html += modifiedNodesToHTMLs (child, nodes );
981
+ html += modifiedNodesToHTMLsInternal (ctxt, child );
970
982
}
971
983
if (tree.HasMember (L" contentDocument" ))
972
984
{
973
985
for (const auto & child : tree[L" contentDocument" ][L" children" ].GetArray ())
974
- modifiedNodesToHTMLs (child, nodes );
986
+ modifiedNodesToHTMLsInternal (ctxt, child );
975
987
}
976
988
if (!utils::IsVoidElement (tree[L" nodeName" ].GetString ()))
977
989
{
@@ -984,14 +996,20 @@ class Highlighter
984
996
ModifiedNode node;
985
997
node.nodeId = tree[L" nodeId" ].GetInt ();
986
998
node.outerHTML = html;
987
- nodes.emplace_back (std::move (node));
999
+ ctxt. nodes .emplace_back (std::move (node));
988
1000
}
989
1001
break ;
990
1002
}
991
1003
}
992
1004
return html;
993
1005
}
994
1006
1007
+ static std::wstring modifiedNodesToHTMLs (const WValue& tree, std::list<ModifiedNode>& nodes)
1008
+ {
1009
+ Context ctxt (false , nodes);
1010
+ return modifiedNodesToHTMLsInternal (ctxt, tree);
1011
+ }
1012
+
995
1013
static void getDiffNodes (WValue& tree, std::map<int , int >& nodes)
996
1014
{
997
1015
NodeType nodeType = static_cast <NodeType>(tree[L" nodeType" ].GetInt ());
0 commit comments