Only strings with single top level element (root) works correctly:

  • <div>content</div>
  • Simple string

Following strings didn't work before, only some beginning part of data was inserted

  • Hello, <span>World</span> – only Hello, here
  • <p>first</p><p>second</p> – only <p>first</p> here

So I propose to insert all DOMNodes from the string coming to DomHelperTrait::replaceNodeContent().

Comments

AndreyMaximov created an issue. See original summary.

andreymaximov’s picture

andreymaximov’s picture

Status: Active » Needs review
wim leers’s picture

StatusFileSize
new3.44 KB
new1.38 KB

First, let's show that this patch's new test coverage indeed fails without the other changes.

I didn't change anything, I only rerolled it.

The last submitted patch, 4: embed-replaceNodeContent-2602316-4-FAIL-test-only.patch, failed testing.

The last submitted patch, 4: embed-replaceNodeContent-2602316-4-FAIL-test-only.patch, failed testing.

The last submitted patch, 4: embed-replaceNodeContent-2602316-4-FAIL-test-only.patch, failed testing.

wim leers’s picture

Category: Feature request » Bug report
Status: Needs review » Postponed
Related issues: +#2567561: Captioned elements and their children are removed when theme debugging on

This is basically exactly the same problem as #2567561: Captioned elements and their children are removed when theme debugging on. We should use the same solution as Drupal core uses.

(The current code assumes only one DOM node will be present:

      // Load the contents into a new DOMDocument and retrieve the element.
      $replacement_node = Html::load($content)->getElementsByTagName('body')
        ->item(0)
        ->childNodes
        ->item(0);

This is identical to FilterCaption's buggy logic and what #2567561: Captioned elements and their children are removed when theme debugging on is fixing.

wim leers’s picture

A duplicate of this bug was reported against the Entity Embed module: #2541984: <drupal-entity>is not replaced when twig debugging is enabled. It's a duplicate, because Entity Embed uses Embed's DomHelperTrait.

wim leers’s picture

This is now blocking #2547085: Remove the <drupal-entity>/<div> placeholder instead of embedding the entity as a child, which is blocking #2628358: Disallow <div>, only allow <drupal-entity>, which again blocks its parent issue.

But… HAH! That won't actually work, because embed's replaceNodeContent() is broken: see #2602316: Allow any HTML as content for replaceNodeContent. It will only actually import the first DOM node. And the first DOM node when rendering an article template happens to be… a text DOM node with a newline in it. The second DOM node is the <article class="type-node" …> HTML that we expect.

So, with the above, tests actually fail for both patches because embed is broken…

Now working on #2602316: Allow any HTML as content for replaceNodeContent.

Hence, marking this critical, since it blocks so much.

wim leers’s picture

Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new1.67 KB
+++ b/src/DomHelperTrait.php
@@ -85,25 +85,35 @@ trait DomHelperTrait {
+   * @return array
+   *   Array of DOMNode objects that replaced a node.
...
+    return $replacement_nodes_array;

This is an unnecessary API change.

So, instead of changing an API, let's just use the same solution that will land in Drupal 8 core: the one at #2567561: Captioned elements and their children are removed when theme debugging on.


Tests still needed!

Status: Needs review » Needs work

The last submitted patch, 11: 2602316-11.patch, failed testing.

The last submitted patch, 11: 2602316-11.patch, failed testing.

The last submitted patch, 11: 2602316-11.patch, failed testing.

wim leers’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new2.2 KB
new3.83 KB

Transplanted the tests written by @AndreyMaximov: modified them so they don't expect that API change anymore. In fact, having done that, I'm 90% certain he introduced that API change to simplify testability.

Fortunately, once you start using a data provider, that's not really necessary anymore :)

The last submitted patch, 15: 2602316-12-FAIL-test-only.patch, failed testing.

The last submitted patch, 15: 2602316-12-FAIL-test-only.patch, failed testing.

The last submitted patch, 15: 2602316-12-FAIL-test-only.patch, failed testing.

wim leers’s picture

StatusFileSize
new3.83 KB

Oops. I unchecked the Display box for the non-FAIL patch. Hence it wasn't tested. Re-uploading that one.

wim leers’s picture

There we go. Green :)

dave reid’s picture

@WimLeers Should setNodeContent() also be fixed in the same way?

dave reid’s picture

D'oh, no of course setNodeContent() doesn't need the same fix.

slashrsm’s picture

Issue tags: +Zurich media sprint 2015
StatusFileSize
new5.74 KB
new2.37 KB

setNodeContent() apparently can handle string that consist of multiple nodes. Uploading updated test that proves that.

slashrsm’s picture

Status: Needs review » Fixed

I will go ahead and fix this as there are patches that are waiting for this. Thanks!

The last submitted patch, 2: embed-replaceNodeContent-2602316-1.patch, failed testing.

The last submitted patch, 4: embed-replaceNodeContent-2602316-4.patch, failed testing.

The last submitted patch, 4: embed-replaceNodeContent-2602316-4-FAIL-test-only.patch, failed testing.

The last submitted patch, 15: 2602316-12-FAIL-test-only.patch, failed testing.

The last submitted patch, 19: 2602316-12.patch, failed testing.

Status: Fixed » Needs work

The last submitted patch, 23: 2602316_22.patch, failed testing.

The last submitted patch, 11: 2602316-11.patch, failed testing.

dave reid’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.