File tree Expand file tree Collapse file tree 2 files changed +18
-4
lines changed Expand file tree Collapse file tree 2 files changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -897,10 +897,17 @@ public function parseE(\DOMElement $e) {
897
897
// TODO: as it is this is not relative to only children, make this .// and rerun tests
898
898
$ this ->resolveChildUrls ($ e );
899
899
900
- $ html = '' ;
901
- foreach ($ e ->childNodes as $ node ) {
902
- $ html .= $ node ->ownerDocument ->saveHTML ($ node );
903
- }
900
+ // Temporarily move all descendants into a separate DocumentFragment.
901
+ // This way we can DOMDocument::saveHTML on the entire collection at once.
902
+ // Running DOMDocument::saveHTML per node may add whitespace that isn't in source.
903
+ // See https://stackoverflow.com/q/38317903
904
+ $ innerNodes = $ e ->ownerDocument ->createDocumentFragment ();
905
+ while ($ e ->hasChildNodes ()) {
906
+ $ innerNodes ->appendChild ($ e ->firstChild );
907
+ }
908
+ $ html = $ e ->ownerDocument ->saveHtml ($ innerNodes );
909
+ // Put the nodes back in place.
910
+ $ e ->appendChild ($ innerNodes );
904
911
905
912
$ return = array (
906
913
'html ' => unicodeTrim ($ html ),
Original file line number Diff line number Diff line change @@ -696,6 +696,13 @@ public function testMultiLevelRecursion() {
696
696
$ this ->assertEquals ('Comment D ' , $ three ['children ' ][1 ]['properties ' ]['name ' ][0 ]);
697
697
$ this ->assertEquals ('Four ' , $ output ['items ' ][0 ]['children ' ][3 ]['properties ' ]['name ' ][0 ]);
698
698
}
699
+
700
+ public function testNoErrantWhitespaceOnEHtml ()
701
+ {
702
+ $ input = '<div class="h-entry"><div class="e-content"><p>1</p><p>2</p></div></div> ' ;
703
+ $ output = Mf2 \parse ($ input );
704
+ $ this ->assertEquals ('<p>1</p><p>2</p> ' , $ output ['items ' ][0 ]['properties ' ]['content ' ][0 ]['html ' ]);
705
+ }
699
706
700
707
/**
701
708
* @see https://github.com/indieweb/php-mf2/issues/158
You can’t perform that action at this time.
0 commit comments