Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit dea83c2

Browse files
committed
increased Shoop integration
1 parent 5838b22 commit dea83c2

File tree

13 files changed

+150
-152
lines changed

13 files changed

+150
-152
lines changed

composer.json

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
{
2-
"name": "8fold/php-markup",
3-
"description": "8fold Markup seeks to make writing semi-structured data easier while allowing for more dynamism.",
4-
"type": "library",
5-
"license": "MIT",
6-
"authors": [
7-
{
8-
"name": "Josh Bruce",
9-
"email": "[email protected]"
10-
}
11-
],
12-
"require": {
13-
"php": ">=7.0",
14-
"8fold/php-shoop": "0.3.*",
15-
"nesbot/carbon": "2.35.*",
16-
"league/commonmark": "1.4.*"
17-
},
18-
"require-dev": {
19-
"phpunit/phpunit": "9.1.*",
20-
"phpunit/php-code-coverage": "8.0.*"
21-
},
22-
"autoload": {
23-
"psr-4": {
24-
"Eightfold\\Markup\\": "./src/"
25-
}
26-
},
27-
"autoload-dev": {
28-
"psr-4": {
29-
"Eightfold\\Markup\\Tests\\": "./tests/"
30-
}
31-
},
32-
"config": {
33-
"preferred-install": "dist"
34-
},
2+
"name": "8fold/php-markup",
3+
"description": "8fold Markup seeks to make writing semi-structured data easier while allowing for more dynamism.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Josh Bruce",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.0",
14+
"8fold/php-shoop": "0.4.*",
15+
"nesbot/carbon": "2.35.*",
16+
"league/commonmark": "1.4.*"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "9.1.*",
20+
"phpunit/php-code-coverage": "8.0.*"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Eightfold\\Markup\\": "./src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Eightfold\\Markup\\Tests\\": "./tests/"
30+
}
31+
},
32+
"config": {
33+
"preferred-install": "dist"
34+
},
3535
"prefer-stable": true
3636
}

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Element.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Element
1616

1717
protected $content;
1818

19-
protected $extends;
19+
// protected $extends;
2020

2121
protected $attributes;
2222

@@ -35,30 +35,42 @@ public function __construct($element, ...$content)
3535
$this->attributes = Shoop::dictionary([]);
3636
}
3737

38-
public function unfold(string ...$attributes)
38+
public function unfold()
3939
{
40-
$this->attr(...$attributes);
41-
$elem = $this->compiledElement();
42-
$attr = $this->compiledAttributes();
43-
$cont = $this->compiledContent();
44-
return Shoop::string($elem)->start("<")->plus($attr)->end(">")
45-
->plus($cont)->plus(
40+
return Shoop::string($this->element)
41+
->start("<")->plus($this->compiledAttributes())->end(">")
42+
->plus($this->compiledContent())->plus(
4643
($this->omitEndTag)
4744
? ""
48-
: Shoop::string($elem)->start("</")->end(">")
45+
: Shoop::string($this->element)->start("</")->end(">")
4946
)->unfold();
5047
}
5148

5249
public function attr(string ...$attributes): Element
5350
{
51+
if ($this->attributes === null) {
52+
$this->attributes = Shoop::dictionary([]);
53+
}
54+
5455
Shoop::array($attributes)->each(function($string) {
55-
list($attribute, $value) = Shoop::string($string)
56+
list($attr, $value) = Shoop::string($string)
5657
->divide(" ", false, 2);
57-
$this->attributes = $this->attributes->plus($value, $attribute);
58+
$this->attributes = $this->attributes->plus($value, $attr);
5859
});
60+
5961
return $this;
6062
}
6163

64+
protected function attributes(): ESArray
65+
{
66+
if ($this->attributes === null) {
67+
return Shoop::array([]);
68+
}
69+
return $this->attributes->each(function($value, $attr) {
70+
return "{$attr} {$value}";
71+
});
72+
}
73+
6274
public function extends($extends): Element
6375
{
6476
$extends = Type::sanitizeType($extends, ESString::class);
@@ -78,12 +90,7 @@ public function omitEndTag(bool $omit = true): Element
7890

7991
protected function compiledElement()
8092
{
81-
$elem = $this->element;
82-
if (strlen($this->extends) > 0) {
83-
$elem = $this->extends;
84-
$this->attributes[] = "is {$this->element}";
85-
}
86-
return $elem;
93+
return $this->element;
8794
}
8895

8996
protected function compiledAttributes(): string

src/Html/Data/Attributes/Aria.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
namespace Eightfold\Markup\Html\Data\Attributes;
44

5-
/**
6-
* @version 1.0.0
7-
*
8-
* Aria attributes
9-
*/
105
abstract class Aria
116
{
127
static public function globals()

src/Html/Elements/Forms/Button.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111

1212
use Eightfold\Markup\Html\Data\Attributes\Content;
1313

14-
/**
15-
* @version 1.0.0
16-
*
17-
* Button
18-
*
19-
*
20-
*/
2114
class Button extends HtmlElement implements HtmlElementInterface
2215
{
2316
static public function elementName(): string

src/Html/Elements/HtmlElement.php

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Eightfold\Markup\Html\Elements;
44

55
use Eightfold\Shoop\Shoop;
6+
use Eightfold\Shoop\ESDictionary;
7+
use Eightfold\Shoop\ESArray;
68

79
use Eightfold\Markup\Element;
810

@@ -37,75 +39,79 @@ protected function compiledElement(): string
3739

3840
protected function compiledAttributes(): string
3941
{
40-
// TODO: Test beyond ordered
41-
$ordered = [];
42-
$events = [];
43-
$aria = [];
44-
$data = [];
45-
$other = [];
46-
$boolean = [];
47-
$leftovers = [];
48-
Shoop::array($this->attributes)->each(function($attribute) use
42+
$ordered = Shoop::dictionary([]);
43+
$events = Shoop::dictionary([]);
44+
$aria = Shoop::dictionary([]);
45+
$data = Shoop::dictionary([]);
46+
$other = Shoop::dictionary([]);
47+
$boolean = Shoop::dictionary([]);
48+
$leftovers = Shoop::dictionary([]);
49+
50+
$this->attributes->each(function($value, $member) use
4951
(&$ordered, &$events, &$aria, &$data, &$other, &$boolean, &$leftovers) {
50-
list($member, $value) = explode(" ", $attribute, 2);
52+
53+
$inAriaRoles = in_array($value, static::allAriaRoles()->unfold());
54+
$inOrdered = in_array($member, Ordered::order());
55+
$inEvents = in_array($member, static::optionalEventAttributes());
56+
$inAria = in_array($member, static::optionalAriaAttributes());
57+
$inData = Shoop::string($member)->startsWithUnfolded("data-");
58+
$inOther = in_array($member, array_merge(static::requiredAttributes(), static::optionalAttributes()));
59+
$inBoolean = in_array($member, Content::booleans());
5160

5261
if ($member === "role") {
53-
if (in_array($value, static::allAriaRoles()) && $value !== static::defaultAriaRole()) {
54-
$ordered[$member] = $value;
62+
if ($inAriaRoles and $value !== static::defaultAriaRole()) {
63+
$ordered = $ordered->plus($value, $member);
5564
}
5665

57-
} elseif (in_array($member, Ordered::order()) && in_array($member, array_merge(static::requiredAttributes(), static::optionalAttributes()))) {
58-
$ordered[$member] = $value;
66+
} elseif ($inOrdered and
67+
in_array($member, array_merge(static::requiredAttributes(), static::optionalAttributes()))
68+
) {
69+
$ordered = $ordered->plus($value, $member);
5970

60-
} elseif (in_array($member, static::optionalEventAttributes())) {
61-
$events[$member] = $value;
71+
} elseif ($inOptioneEventAttributes) {
72+
$events = $events->plus($value, $member);
6273

63-
} elseif (in_array($member, static::optionalAriaAttributes())) {
64-
$aria[$member] = $value;
74+
} elseif ($inAria) {
75+
$aria = $aria->plus($value, $member);
6576

66-
} elseif (Shoop::string($member)->startsWithUnfolded("data-")) {
67-
$data[$member] = $value;
77+
} elseif ($inData) {
78+
$data = $data->plus($value, $member);
6879

69-
} elseif (in_array($member, array_merge(static::requiredAttributes(), static::optionalAttributes()))) {
70-
$other[$member] = $value;
80+
} elseif ($inOther) {
81+
$other = $other->plus($value, $member);
7182

72-
} elseif (in_array($member, Content::booleans())) {
73-
$boolean[$member] = $value;
83+
} elseif ($inBoolean) {
84+
$boolean = $boolean->plus($value, $member);
7485

75-
} else {
76-
$leftovers[$member] = $value;
86+
// } else {
87+
// if ($this->isKnownElement) {
88+
// $errorComment = "<!-- The {$member} attribute is not valid for the {$this->element} element -->";
89+
// }
7790

78-
}
79-
});
91+
// $leftovers = $leftovers->plus($value, $member);
8092

81-
$order = [];
82-
Shoop::array(Ordered::order())->each(function($value) use (&$ordered, &$order) {
83-
if (array_key_exists($value, $ordered)) {
84-
$order[$value] = $ordered[$value];
8593
}
8694
});
8795

88-
ksort($events, SORT_NATURAL | SORT_FLAG_CASE);
89-
ksort($aria, SORT_NATURAL | SORT_FLAG_CASE);
90-
ksort($data, SORT_NATURAL | SORT_FLAG_CASE);
91-
ksort($other, SORT_NATURAL | SORT_FLAG_CASE);
92-
ksort($boolean, SORT_NATURAL | SORT_FLAG_CASE);
93-
94-
$compiled = Shoop::dictionary(array_merge($order, $events, $aria, $data, $other, $boolean))
95-
->each(function($value, $member) {
96-
if ($member === $value) {
97-
return $member;
98-
99-
} else {
100-
return "{$member}=\"{$value}\"";
101-
96+
$order = Shoop::dictionary([]);
97+
Shoop::array(Ordered::order())->each(
98+
function($member) use (&$ordered, &$order) {
99+
if ($ordered->hasMemberUnfolded($member)) {
100+
$order = $order->plus($ordered->{$member}, $member);
102101
}
103102
});
104103

105-
if ($compiled->int()->isGreaterThanUnfolded(0)) {
106-
return $compiled->join(" ")->start(" ");
107-
}
108-
return "";
104+
$this->attributes = Shoop::dictionary([])
105+
->plus(...$order->interleave())
106+
->plus(...$events->sortMembers()->interleave())
107+
->plus(...$aria->sortMembers()->interleave())
108+
->plus(...$data->sortMembers()->interleave())
109+
->plus(...$other->sortMembers()->interleave())
110+
->plus(...$boolean->sortMembers()->interleave())
111+
// ->plus(...$leftovers->sortMembers()->interleave())
112+
->noEmpties();
113+
114+
return parent::compiledAttributes();
109115
}
110116

111117
private function isKnownElement()
@@ -118,10 +124,11 @@ private function isKnownElement()
118124

119125
private function allAriaRoles()
120126
{
121-
return array_merge([static::defaultAriaRole()], static::optionalAriaRoles());
127+
return Shoop::array([static::defaultAriaRole()])
128+
->plus(...static::optionalAriaRoles());
122129
}
123130

124-
public function unfold()
131+
public function unfold(): string
125132
{
126133
$this->compiledElement();
127134
$this->isKnownElement = $this->isKnownElement();
@@ -131,9 +138,12 @@ public function unfold()
131138
return $this->prefix . parent::unfold();
132139
}
133140

134-
protected function getAttr(): array
141+
/**
142+
* @deprecated
143+
*/
144+
protected function getAttr(): ESArray
135145
{
136-
return $this->attributes;
146+
return $this->attributes();
137147
}
138148

139149
/** HTML specification-related */

0 commit comments

Comments
 (0)