Skip to content

Commit

Permalink
Merge pull request #150 from dravenk/fix-148-fix-spaces-encode
Browse files Browse the repository at this point in the history
Fixed #148 fix space convert for outlook and office365
  • Loading branch information
lptn authored Jun 9, 2022
2 parents 3c14064 + 2f72285 commit bb96f1d
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 56 deletions.
27 changes: 3 additions & 24 deletions src/Generators/WebOffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,16 @@ public function generate(Link $link): string
$url .= '&allday=true';
}

$url .= '&subject='.urlencode($this->sanitizeText($link->title));
$url .= '&subject='.rawurlencode($link->title);

if ($link->description) {
$url .= '&body='.urlencode($this->sanitizeText($link->description));
$url .= '&body='.rawurlencode($link->description);
}

if ($link->address) {
// The Location field is not HTML code
$url .= '&location='.urlencode($link->address);
$url .= '&location='.rawurlencode($link->address);
}

return $url;
}

/**
* Generate a text without html entity code and hexadecimal code instead spaces.
* @param string $text
* @return string
*/
private function sanitizeText(string $text): string
{
$replaceList = [
'/\s/' => ' ',
];

$resultText = html_entity_decode($text);

foreach ($replaceList as $pattern => $newValue) {
$resultText = preg_replace($pattern, $newValue, $resultText);
}

return $resultText;
}
}
27 changes: 3 additions & 24 deletions src/Generators/WebOutlook.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,16 @@ public function generate(Link $link): string
$url .= '&allday=true';
}

$url .= '&subject='.urlencode($this->sanitizeText($link->title));
$url .= '&subject='.rawurlencode($link->title);

if ($link->description) {
$url .= '&body='.urlencode($this->sanitizeText($link->description));
$url .= '&body='.rawurlencode($link->description);
}

if ($link->address) {
// The Location field is not HTML code
$url .= '&location='.urlencode($link->address);
$url .= '&location='.rawurlencode($link->address);
}

return $url;
}

/**
* Generate a text without html entity code and hexadecimal code instead spaces.
* @param string $text
* @return string
*/
private function sanitizeText(string $text): string
{
$replaceList = [
'/\s/' => ' ',
];

$resultText = html_entity_decode($text);

foreach ($replaceList as $pattern => $newValue) {
$resultText = preg_replace($pattern, $newValue, $resultText);
}

return $resultText;
}
}
13 changes: 13 additions & 0 deletions tests/Generators/GeneratorTestContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ public function it_can_generate_a_multiple_days_event_link_with_allday_flag()
$this->generator()->generate($this->createMultipleDaysAllDayEventLink(true))
);
}

/** @test */
public function it_can_generate_a_description_is_html_code_event_link_with_allday_flag()
{
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createDescriptionIsHTMLcodeEventLink())
);

$this->assertSame(
$this->generator()->generate($this->createDescriptionIsHTMLcodeEventLink(false)),
$this->generator()->generate($this->createDescriptionIsHTMLcodeEventLink(true))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://calendar.google.com/calendar/render?action=TEMPLATE&dates=20180201T090000Z/20180201T180000Z&ctz=UTC&text=Birthday+Party+%2B1&details=With+balloons%2C+clowns+and+stuff%0ABring+a+dog%2C+bring+a+frog.%0AThere+will+be+line+breaks+on+it.%0AProject+link+%3Ca+href%3D%22https%3A%2F%2Fgithub.com%2Fspatie%2Fcalendar-links%22%3Ecalendar-links%3C%2Fa%3E%0A%3Cimg+src%3D%22https%3A%2F%2Fgithub-ads.s3.eu-central-1.amazonaws.com%2Fcalendar-links.jpg%3Ft%3D1%22+width%3D%22419px%22+%2F%3E%0A%3Cbr%3E%0AThank+you.%0A&location=Party+Lane+1A%2C+1337+Funtown
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:9fc99e4f3dcee13f23355d63918b9a0c
SUMMARY:Birthday Party +1
DTSTART;TZID=UTC:20180201T090000
DTEND;TZID=UTC:20180201T180000
DESCRIPTION:With balloons\, clowns and stuff\nBring a dog\, bring a frog.\nThere will be line breaks on it.\nProject link <a href="https://github.com/spatie/calendar-links">calendar-links</a>\n<img src="https://github-ads.s3.eu-central-1.amazonaws.com/calendar-links.jpg?t=1" width="419px" />\n<br>\nThank you.\n
LOCATION:Party Lane 1A\, 1337 Funtown
END:VEVENT
END:VCALENDAR
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday%20Party%20%2B1&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog.%0AThere%20will%20be%20line%20breaks%20on%20it.%0AProject%20link%20%3Ca%20href%3D%22https%3A%2F%2Fgithub.com%2Fspatie%2Fcalendar-links%22%3Ecalendar-links%3C%2Fa%3E%0A%3Cimg%20src%3D%22https%3A%2F%2Fgithub-ads.s3.eu-central-1.amazonaws.com%2Fcalendar-links.jpg%3Ft%3D1%22%20width%3D%22419px%22%20%2F%3E%0A%3Cbr%3E%0AThank%20you.%0A&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2019-12-31T23:00:00Z&enddt=2020-01-01T01:00:00Z&subject=New%26%2332%3BYear&body=With%26%2332%3Bballoons%2C%26%2332%3Bclowns%26%2332%3Band%26%2332%3Bstuff%26%2332%3BBring%26%2332%3Ba%26%2332%3Bdog%2C%26%2332%3Bbring%26%2332%3Ba%26%2332%3Bfrog&location=Party+Lane+1A%2C+1337+Funtown
https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2019-12-31T23:00:00Z&enddt=2020-01-01T01:00:00Z&subject=New%20Year&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01&enddt=2018-02-06&allday=true&subject=Birthday&body=With%26%2332%3Bballoons%2C%26%2332%3Bclowns%26%2332%3Band%26%2332%3Bstuff%26%2332%3BBring%26%2332%3Ba%26%2332%3Bdog%2C%26%2332%3Bbring%26%2332%3Ba%26%2332%3Bfrog&location=Party+Lane+1A%2C+1337+Funtown
https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01&enddt=2018-02-06&allday=true&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%26%2332%3Bballoons%2C%26%2332%3Bclowns%26%2332%3Band%26%2332%3Bstuff%26%2332%3BBring%26%2332%3Ba%26%2332%3Bdog%2C%26%2332%3Bbring%26%2332%3Ba%26%2332%3Bfrog&location=Party+Lane+1A%2C+1337+Funtown
https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01&enddt=2018-02-02&allday=true&subject=Birthday&body=With%26%2332%3Bballoons%2C%26%2332%3Bclowns%26%2332%3Band%26%2332%3Bstuff%26%2332%3BBring%26%2332%3Ba%26%2332%3Bdog%2C%26%2332%3Bbring%26%2332%3Ba%26%2332%3Bfrog&location=Party+Lane+1A%2C+1337+Funtown
https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01&enddt=2018-02-02&allday=true&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday%20Party%20%2B1&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog.%0AThere%20will%20be%20line%20breaks%20on%20it.%0AProject%20link%20%3Ca%20href%3D%22https%3A%2F%2Fgithub.com%2Fspatie%2Fcalendar-links%22%3Ecalendar-links%3C%2Fa%3E%0A%3Cimg%20src%3D%22https%3A%2F%2Fgithub-ads.s3.eu-central-1.amazonaws.com%2Fcalendar-links.jpg%3Ft%3D1%22%20width%3D%22419px%22%20%2F%3E%0A%3Cbr%3E%0AThank%20you.%0A&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2019-12-31T23:00:00Z&enddt=2020-01-01T01:00:00Z&subject=New%26%2332%3BYear&body=With%26%2332%3Bballoons%2C%26%2332%3Bclowns%26%2332%3Band%26%2332%3Bstuff%26%2332%3BBring%26%2332%3Ba%26%2332%3Bdog%2C%26%2332%3Bbring%26%2332%3Ba%26%2332%3Bfrog&location=Party+Lane+1A%2C+1337+Funtown
https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2019-12-31T23:00:00Z&enddt=2020-01-01T01:00:00Z&subject=New%20Year&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01&enddt=2018-02-06&allday=true&subject=Birthday&body=With%26%2332%3Bballoons%2C%26%2332%3Bclowns%26%2332%3Band%26%2332%3Bstuff%26%2332%3BBring%26%2332%3Ba%26%2332%3Bdog%2C%26%2332%3Bbring%26%2332%3Ba%26%2332%3Bfrog&location=Party+Lane+1A%2C+1337+Funtown
https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01&enddt=2018-02-06&allday=true&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%26%2332%3Bballoons%2C%26%2332%3Bclowns%26%2332%3Band%26%2332%3Bstuff%26%2332%3BBring%26%2332%3Ba%26%2332%3Bdog%2C%26%2332%3Bbring%26%2332%3Ba%26%2332%3Bfrog&location=Party+Lane+1A%2C+1337+Funtown
https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01&enddt=2018-02-02&allday=true&subject=Birthday&body=With%26%2332%3Bballoons%2C%26%2332%3Bclowns%26%2332%3Band%26%2332%3Bstuff%26%2332%3BBring%26%2332%3Ba%26%2332%3Bdog%2C%26%2332%3Bbring%26%2332%3Ba%26%2332%3Bfrog&location=Party+Lane+1A%2C+1337+Funtown
https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01&enddt=2018-02-02&allday=true&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://calendar.yahoo.com/?v=60&view=d&type=20&ST=20180201T090000Z&ET=20180201T180000Z&TITLE=Birthday%20Party%20%2B1&DESC=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog.%0AThere%20will%20be%20line%20breaks%20on%20it.%0AProject%20link%20%3Ca%20href%3D%22https%3A%2F%2Fgithub.com%2Fspatie%2Fcalendar-links%22%3Ecalendar-links%3C%2Fa%3E%0A%3Cimg%20src%3D%22https%3A%2F%2Fgithub-ads.s3.eu-central-1.amazonaws.com%2Fcalendar-links.jpg%3Ft%3D1%22%20width%3D%22419px%22%20%2F%3E%0A%3Cbr%3E%0AThank%20you.%0A&in_loc=Party%20Lane%201A%2C%201337%20Funtown
21 changes: 21 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,25 @@ protected function createMultipleDaysAllDayEventLink(bool $immutable = false): L
5
)->description($description)->address('Party Lane 1A, 1337 Funtown');
}

protected function createDescriptionIsHTMLcodeEventLink(bool $immutable = false): Link
{
$description = 'With balloons, clowns and stuff
Bring a dog, bring a frog.
There will be line breaks on it.
Project link <a href="https://github.com/spatie/calendar-links">calendar-links</a>
<img src="https://github-ads.s3.eu-central-1.amazonaws.com/calendar-links.jpg?t=1" width="419px" />
<br>
Thank you.
';

/** @var \DateTimeInterface $dateTimeClass */
$dateTimeClass = $immutable ? DateTimeImmutable::class : DateTime::class;

return Link::create(
'Birthday Party +1',
$dateTimeClass::createFromFormat('Y-m-d H:i', '2018-02-01 09:00', new DateTimeZone('UTC')),
$dateTimeClass::createFromFormat('Y-m-d H:i', '2018-02-01 18:00', new DateTimeZone('UTC'))
)->description($description)->address('Party Lane 1A, 1337 Funtown');
}
}

0 comments on commit bb96f1d

Please sign in to comment.