Skip to content

Commit f4f73d3

Browse files
feat: Turtle.get/set_Title ( Fixes #285 )
1 parent 8396a4b commit f4f73d3

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

Types/Turtle/get_PathElement.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ foreach ($collection in $this.SVGAttribute, $this.Attribute) {
4545
" $attributeName='$($coreAttributes[$attributeName])'"
4646
}
4747
)>"
48+
if ($this.Title) { "<title>$([Security.SecurityElement]::Escape($this.Title))</title>" }
4849
if ($this.PathAnimation) {$this.PathAnimation}
4950
"</path>"
5051
)

Types/Turtle/get_TextElement.ps1

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,73 @@
1111
https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/text
1212
.EXAMPLE
1313
turtle text "hello world" textElement
14+
.EXAMPLE
15+
turtle text "hello world" title "Hi!" textElement
1416
#>
1517
[OutputType([xml])]
1618
param()
1719

20+
# If there is no text, there's no text element
1821
if (-not $this.Text) { return }
1922

20-
$textAttributes = [Ordered]@{}
23+
# Collect all of our text attributes
24+
$textAttributes = [Ordered]@{
25+
id="$($this.ID)-text"
26+
}
2127

28+
# If there are no steps
2229
if (-not $this.Steps) {
30+
# default the text to the middle
2331
$textAttributes['dominant-baseline'] = 'middle'
2432
$textAttributes['text-anchor'] = 'middle'
2533
$textAttributes['x'] = '50%'
2634
$textAttributes['y'] = '50%'
2735
}
2836

29-
foreach ($collection in 'SVGAttribute','Attribute') {
37+
# Text Attributes can exist in Attribute or SVGAttribute, as long as they are prefixed.
38+
$prefix = '^/?text/'
39+
foreach ($collection in 'Attribute','SVGAttribute') {
40+
if (-not $this.$Collection.Count) { continue }
3041
foreach ($key in $this.$collection.Keys) {
31-
if ($key -match '^text/') {
32-
$textAttributes[$key -replace '^text/'] = $this.$collection[$key]
42+
if ($key -match $prefix) {
43+
$textAttributes[$key -replace $prefix] = $this.$collection[$key]
3344
}
3445
}
3546
}
3647

48+
# Explicit text attributes will be copied last, so they take precedent.
3749
foreach ($key in $this.TextAttribute.Keys) {
3850
$textAttributes[$key] = $this.TextAttribute[$key]
3951
}
4052

53+
# Return a constructed element
4154
return [xml]@(
42-
"<text id='$($this.ID)-text' $(
55+
# Create the text element
56+
"<text$(
4357
foreach ($TextAttributeName in $TextAttributes.Keys) {
4458
" $TextAttributeName='$($TextAttributes[$TextAttributeName])'"
4559
}
4660
)>"
61+
62+
# If there is a title
63+
if ($this.Title) {
64+
# embed it here (so that the text is accessible).
65+
"<title>$([Security.SecurityElement]::Escape($this.Title))</title>"
66+
}
67+
68+
# If there are any text animations, include them here.
4769
if ($this.TextAnimation) {$this.TextAnimation}
70+
71+
# Escape our text
72+
$escapedText = [Security.SecurityElement]::Escape($this.Text)
73+
# If we have steps,
4874
if ($this.Steps) {
49-
"<textPath href='#$($this.id)-path'>$([Security.SecurityElement]::Escape($this.Text))</textPath>"
75+
# put the escaped text within a `<textPath>`.
76+
"<textPath href='#$($this.id)-path'>$escapedText</textPath>"
5077
} else {
51-
$([Security.SecurityElement]::Escape($this.Text))
78+
# otherwise, include the escaped text as the content
79+
$escapedText
5280
}
81+
# close the element and return our XML.
5382
"</text>"
54-
)
55-
56-
83+
)

Types/Turtle/get_Title.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<#
2+
.SYNOPSIS
3+
Gets a Turtle's title
4+
.DESCRIPTION
5+
Gets the title assigned to a Turtle.
6+
7+
A title will provide alternate text for the image that should be visible on hover, and should be available to screen readers.
8+
.EXAMPLE
9+
turtle square 42 title "It's Hip To Be Square"
10+
#>
11+
return $this.'.Title'

Types/Turtle/set_Title.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<#
2+
.SYNOPSIS
3+
Sets a Turtle's title
4+
.DESCRIPTION
5+
Sets the title assigned to a Turtle.
6+
7+
A title will provide alternate text for the image that should be visible on hover, and should be available to screen readers.
8+
.EXAMPLE
9+
turtle square 42 title "It's Hip To Be Square"
10+
#>
11+
param(
12+
# The title
13+
[string]
14+
$Title
15+
)
16+
17+
$this | Add-Member NoteProperty '.Title' $title -Force

0 commit comments

Comments
 (0)