Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
sg_jobs
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
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
TYPO3
sg_jobs
Commits
5403d523
Commit
5403d523
authored
1 month ago
by
Kevin Kleiber
Browse files
Options
Downloads
Patches
Plain Diff
[TASK] Unify SVG ViewHelper
parent
14c56dd8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!51
[TASK] Unify SVG ViewHelper
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Classes/ViewHelpers/RenderSvgViewHelper.php
+60
-0
60 additions, 0 deletions
Classes/ViewHelpers/RenderSvgViewHelper.php
with
60 additions
and
0 deletions
Classes/ViewHelpers/RenderSvgViewHelper.php
+
60
−
0
View file @
5403d523
...
...
@@ -41,6 +41,9 @@ class RenderSvgViewHelper extends AbstractTagBasedViewHelper {
* @api
*/
protected
$tagName
=
'svg'
;
/**
* @var string
*/
protected
$xmlInfo
=
'<?xml version="1.0"?>'
;
/**
...
...
@@ -106,6 +109,11 @@ class RenderSvgViewHelper extends AbstractTagBasedViewHelper {
FALSE
,
FALSE
);
$this
->
registerArgument
(
'aria-label'
,
'string'
,
'The HTML aria-label attribute'
);
$this
->
registerArgument
(
'aria-labelledby'
,
'string'
,
'The HTML aria-labelledby attribute'
);
$this
->
registerArgument
(
'role'
,
'string'
,
'The HTML role attribute'
);
$this
->
registerArgument
(
'aria-hidden'
,
'string'
,
'The HTML aria-hidden attribute'
);
$this
->
registerArgument
(
'focusable'
,
'string'
,
'The HTML focusable attribute'
);
}
/**
...
...
@@ -131,6 +139,11 @@ class RenderSvgViewHelper extends AbstractTagBasedViewHelper {
$createNewInstance
=
$this
->
arguments
[
'createNewInstance'
];
$preserveColors
=
$this
->
arguments
[
'preserveColors'
];
$createColorAttribute
=
$this
->
arguments
[
'createColorAttribute'
];
$ariaLabel
=
$this
->
arguments
[
'aria-label'
];
$ariaLabelledby
=
$this
->
arguments
[
'aria-labelledby'
];
$role
=
$this
->
arguments
[
'role'
];
$ariaHidden
=
$this
->
arguments
[
'aria-hidden'
];
$focusable
=
$this
->
arguments
[
'focusable'
];
$src
=
$path
??
$this
->
directoryPath
.
'/'
.
$name
.
'.svg'
;
...
...
@@ -142,9 +155,13 @@ class RenderSvgViewHelper extends AbstractTagBasedViewHelper {
$id
=
'svg-'
.
md5
(
$src
.
$width
.
$height
.
$color
);
}
$clipPathId
=
'clipPath-'
.
md5
(
$src
.
$width
.
$height
.
$class
.
$id
);
// Load the SVG into a SimpleXMLElement object
$svg
=
new
SimpleXMLElement
(
$content
);
$this
->
makeClipPathIdUnique
(
$svg
,
$clipPathId
);
// Set the attributes of the SVG element
if
(
$width
>
0
)
{
$this
->
addOrReplaceAttribute
(
$svg
,
'width'
,
$width
);
...
...
@@ -196,6 +213,26 @@ class RenderSvgViewHelper extends AbstractTagBasedViewHelper {
return
str_replace
(
$this
->
xmlInfo
,
''
,
$svg
->
asXML
());
}
if
(
$ariaLabel
)
{
$this
->
addOrReplaceAttribute
(
$svg
,
'aria-label'
,
$ariaLabel
);
}
if
(
$ariaLabelledby
)
{
$this
->
addOrReplaceAttribute
(
$svg
,
'aria-labelledby'
,
$ariaLabelledby
);
}
if
(
$role
)
{
$this
->
addOrReplaceAttribute
(
$svg
,
'role'
,
$role
);
}
if
(
$ariaHidden
)
{
$this
->
addOrReplaceAttribute
(
$svg
,
'aria-hidden'
,
$ariaHidden
);
}
if
(
$focusable
)
{
$this
->
addOrReplaceAttribute
(
$svg
,
'focusable'
,
$focusable
);
}
// Extract the SVG contents
$contents
=
$this
->
getContents
(
$svg
,
TRUE
);
...
...
@@ -340,4 +377,27 @@ class RenderSvgViewHelper extends AbstractTagBasedViewHelper {
$element
->
addAttribute
(
$attributeName
,
$attributeValue
);
}
/**
* Makes the clipPath ID unique
*/
private
function
makeClipPathIdUnique
(
SimpleXMLElement
$xml
,
$newId
):
void
{
// Loop through the children to find the <g> element and update the clip-path attribute
foreach
(
$xml
->
children
()
as
$child
)
{
if
(
$child
->
getName
()
===
'g'
)
{
$this
->
addOrReplaceAttribute
(
$child
,
'clip-path'
,
"url(#
$newId
)"
);
}
}
// Now loop through <defs> to find <clipPath> and update its id
foreach
(
$xml
->
children
()
as
$child
)
{
if
(
$child
->
getName
()
===
'defs'
)
{
foreach
(
$child
->
children
()
as
$defChild
)
{
if
(
$defChild
->
getName
()
===
'clipPath'
)
{
$this
->
addOrReplaceAttribute
(
$defChild
,
'id'
,
$newId
);
}
}
}
}
}
}
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