Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
migrate_plus
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
migrate_plus
Commits
08096932
Commit
08096932
authored
2 years ago
by
Ivan Doroshenko
Committed by
Ivan Doroshenko
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3225457
by RichardGaunt: Add additional functionality to `dom_remove`...
parent
767b9810
No related branches found
No related tags found
1 merge request
!8
Issue #3229479: Entity_lookup taxonomy_term restricted by VID
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Plugin/migrate/process/DomRemove.php
+46
-5
46 additions, 5 deletions
src/Plugin/migrate/process/DomRemove.php
tests/src/Unit/process/DomRemoveTest.php
+30
-0
30 additions, 0 deletions
tests/src/Unit/process/DomRemoveTest.php
with
76 additions
and
5 deletions
src/Plugin/migrate/process/DomRemove.php
+
46
−
5
View file @
08096932
...
...
@@ -8,13 +8,17 @@ use Drupal\migrate\MigrateExecutableInterface;
use
Drupal\migrate\Row
;
/**
* Remove nodes from a DOMDocument object.
* Remove nodes
/ attributes of a node
from a DOMDocument object.
*
* Configuration:
* - selector: An XPath selector.
* - limit: (optional) The maximum number of nodes to remove.
* - limit: (optional) The maximum number of nodes / attributes to remove.
* - mode: (optional) What to remove. Possible values:
* - element: An element (default option).
* - attribute: An element's attribute.
* - attribute: An attribute name (required if mode is attribute)
*
*
Usage
:
*
Examples
:
*
* @code
* process:
...
...
@@ -33,7 +37,26 @@ use Drupal\migrate\Row;
* @endcode
*
* This example will remove the first two <img> elements from the source text
* (if there are that many). Omit 'limit: 2' to remove all <img> elements.
* (if there are that many). Omit 'limit: 2' to remove all <img> elements.
*
* @code
* process:
* bar:
* -
* plugin: dom
* method: import
* source: text_field
* -
* plugin: dom_remove
* mode: attribute
* selector: //*[@style]
* attribute: style
* -
* plugin: dom
* method: export
* @endcode
*
* This example will remove "style" attributes from all tags.
*
* @MigrateProcessPlugin(
* id = "dom_remove"
...
...
@@ -41,6 +64,17 @@ use Drupal\migrate\Row;
*/
class
DomRemove
extends
DomProcessBase
{
/**
* {@inheritdoc}
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
$this
->
configuration
[
'mode'
]
=
$this
->
configuration
[
'mode'
]
??
'element'
;
if
(
$this
->
configuration
[
'mode'
]
===
'attribute'
&&
!
isset
(
$this
->
configuration
[
'attribute'
]))
{
throw
new
\InvalidArgumentException
(
'The "attribute" must be set if "mode" is set to "attribute".'
);
}
}
/**
* {@inheritdoc}
*/
...
...
@@ -56,7 +90,14 @@ class DomRemove extends DomProcessBase {
$walking_dead
[]
=
$node
;
}
foreach
(
$walking_dead
as
$node
)
{
$node
->
parentNode
->
removeChild
(
$node
);
switch
(
$this
->
configuration
[
'mode'
])
{
case
'attribute'
:
$node
->
removeAttribute
(
$this
->
configuration
[
'attribute'
]);
break
;
case
'element'
:
$node
->
parentNode
->
removeChild
(
$node
);
break
;
}
}
return
$this
->
document
;
...
...
This diff is collapsed.
Click to expand it.
tests/src/Unit/process/DomRemoveTest.php
+
30
−
0
View file @
08096932
...
...
@@ -34,6 +34,7 @@ final class DomRemoveTest extends MigrateProcessTestCase {
*/
public
function
providerTestTransform
():
array
{
$input_string
=
'<ul><li>Item 1</li><li>Item 2</li><li><ul><li>Item 3.1</li><li>Item 3.2</li></ul></li><li>Item 4</li><li>Item 5</li></ul>'
;
$attribute_input_string
=
'<div style="font-size:15px;"><a class="btn-lg" href="#" style="padding: 10px;">Button</a><p class="lead-paragraph">Testing</p></div>'
;
$cases
=
[
'any li, no limit'
=>
[
$input_string
,
...
...
@@ -78,9 +79,38 @@ final class DomRemoveTest extends MigrateProcessTestCase {
[
'selector'
=>
'//li[./ul]'
],
'<ul><li>Item 1</li><li>Item 2</li><li>Item 4</li><li>Item 5</li></ul>'
,
],
'attribute, no limit'
=>
[
$attribute_input_string
,
[
'selector'
=>
'//*[@style]'
,
'mode'
=>
'attribute'
,
'attribute'
=>
'style'
],
'<div><a class="btn-lg" href="#">Button</a><p class="lead-paragraph">Testing</p></div>'
,
],
'attribute, limit 1'
=>
[
$attribute_input_string
,
[
'selector'
=>
'//*[@style]'
,
'mode'
=>
'attribute'
,
'attribute'
=>
'style'
,
'limit'
=>
1
],
'<div><a class="btn-lg" href="#" style="padding: 10px;">Button</a><p class="lead-paragraph">Testing</p></div>'
,
],
'attribute in specific tag'
=>
[
$attribute_input_string
,
[
'selector'
=>
'//p[@class]'
,
'mode'
=>
'attribute'
,
'attribute'
=>
'class'
],
'<div style="font-size:15px;"><a class="btn-lg" href="#" style="padding: 10px;">Button</a><p>Testing</p></div>'
,
],
'attribute not found'
=>
[
$attribute_input_string
,
[
'selector'
=>
'p[@class]'
,
'mode'
=>
'attribute'
,
'attribute'
=>
'data-test'
],
$attribute_input_string
,
],
];
return
$cases
;
}
/**
* Tests running remove attribute without specifying an attribute to remove.
*/
public
function
testMissingConfiguration
():
void
{
$this
->
expectException
(
\InvalidArgumentException
::
class
);
$this
->
expectExceptionMessage
(
'The "attribute" must be set if "mode" is set to "attribute".'
);
(
new
DomRemove
([
'selector'
=>
'p'
,
'mode'
=>
'attribute'
],
'dom_remove'
,
[]));
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment