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

Commit 8d57af9

Browse files
committed
updated mf2 parser to latest version
1 parent f6d4b6c commit 8d57af9

File tree

1 file changed

+39
-75
lines changed

1 file changed

+39
-75
lines changed

includes/Mf2/Parser.php

100644100755
Lines changed: 39 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,6 @@ class Parser {
280280
/** @var boolean Whether to include experimental language parsing in the result */
281281
public $lang = false;
282282

283-
/** @var bool Whether to include alternates object (dropped from spec in favor of rel-urls) */
284-
public $enableAlternates = false;
285-
286283
/**
287284
* Elements upgraded to mf2 during backcompat
288285
* @var SplObjectStorage
@@ -820,7 +817,7 @@ public function parseE(\DOMElement $e) {
820817
if($this->lang) {
821818
// Language
822819
if ( $html_lang = $this->language($e) ) {
823-
$return['lang'] = $html_lang;
820+
$return['html-lang'] = $html_lang;
824821
}
825822
}
826823

@@ -849,10 +846,6 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
849846
// Get current µf name
850847
$mfTypes = mfNamesFromElement($e, 'h-');
851848

852-
if (!$mfTypes) {
853-
return null;
854-
}
855-
856849
// Initalise var to store the representation in
857850
$return = array();
858851
$children = array();
@@ -1057,7 +1050,6 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
10571050

10581051
// Check for u-url
10591052
if (!array_key_exists('url', $return) && !$is_backcompat) {
1060-
$url = null;
10611053
// Look for img @src
10621054
if ($e->tagName == 'a' or $e->tagName == 'area') {
10631055
$url = $e->getAttribute('href');
@@ -1081,11 +1073,18 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
10811073
}
10821074
}
10831075

1084-
if (!is_null($url)) {
1076+
if (!empty($url)) {
10851077
$return['url'][] = $this->resolveUrl($url);
10861078
}
10871079
}
10881080

1081+
if($this->lang) {
1082+
// Language
1083+
if ( $html_lang = $this->language($e) ) {
1084+
$return['html-lang'] = $html_lang;
1085+
}
1086+
}
1087+
10891088
// Make sure things are in alphabetical order
10901089
sort($mfTypes);
10911090

@@ -1095,13 +1094,6 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
10951094
'properties' => $return
10961095
);
10971096

1098-
if($this->lang) {
1099-
// Language
1100-
if ( $html_lang = $this->language($e) ) {
1101-
$parsed['lang'] = $html_lang;
1102-
}
1103-
}
1104-
11051097
if (!empty($shape)) {
11061098
$parsed['shape'] = $shape;
11071099
}
@@ -1161,89 +1153,62 @@ public function parseImpliedPhoto(\DOMElement $e) {
11611153
}
11621154

11631155
/**
1164-
* Parse rels and alternates
1156+
* Parse Rels and Alternatives
11651157
*
1166-
* Returns [$rels, $rel_urls, $alternates].
1167-
* For $rels and $rel_urls, if they are empty and $this->jsonMode = true, they will be returned as stdClass,
1168-
* optimizing for JSON serialization. Otherwise they will be returned as an empty array.
1169-
* Note that $alternates is deprecated in the microformats spec in favor of $rel_urls. $alternates only appears
1170-
* in parsed results if $this->enableAlternates = true.
1171-
* @return array|stdClass
1158+
* Returns [$rels, $alternatives]. If the $rels value is to be empty, i.e. there are no links on the page
1159+
* with a rel value *not* containing `alternate`, then the type of $rels depends on $this->jsonMode. If set
1160+
* to true, it will be a stdClass instance, optimising for JSON serialisation. Otherwise (the default case),
1161+
* it will be an empty array.
11721162
*/
11731163
public function parseRelsAndAlternates() {
11741164
$rels = array();
1175-
$rel_urls = array();
11761165
$alternates = array();
11771166

11781167
// Iterate through all a, area and link elements with rel attributes
11791168
foreach ($this->xpath->query('//a[@rel and @href] | //link[@rel and @href] | //area[@rel and @href]') as $hyperlink) {
1180-
if ($hyperlink->getAttribute('rel') == '') {
1169+
if ($hyperlink->getAttribute('rel') == '')
11811170
continue;
1182-
}
11831171

11841172
// Resolve the href
11851173
$href = $this->resolveUrl($hyperlink->getAttribute('href'));
11861174

11871175
// Split up the rel into space-separated values
11881176
$linkRels = array_filter(explode(' ', $hyperlink->getAttribute('rel')));
11891177

1190-
$rel_attributes = array();
1191-
1192-
if ($hyperlink->hasAttribute('media')) {
1193-
$rel_attributes['media'] = $hyperlink->getAttribute('media');
1194-
}
1178+
// If alternate in rels, create alternate structure, append
1179+
if (in_array('alternate', $linkRels)) {
1180+
$alt = array(
1181+
'url' => $href,
1182+
'rel' => implode(' ', array_diff($linkRels, array('alternate')))
1183+
);
1184+
if ($hyperlink->hasAttribute('media'))
1185+
$alt['media'] = $hyperlink->getAttribute('media');
11951186

1196-
if ($hyperlink->hasAttribute('hreflang')) {
1197-
$rel_attributes['hreflang'] = $hyperlink->getAttribute('hreflang');
1198-
}
1187+
if ($hyperlink->hasAttribute('hreflang'))
1188+
$alt['hreflang'] = $hyperlink->getAttribute('hreflang');
11991189

1200-
if ($hyperlink->hasAttribute('title')) {
1201-
$rel_attributes['title'] = $hyperlink->getAttribute('title');
1202-
}
1190+
if ($hyperlink->hasAttribute('title'))
1191+
$alt['title'] = $hyperlink->getAttribute('title');
12031192

1204-
if ($hyperlink->hasAttribute('type')) {
1205-
$rel_attributes['type'] = $hyperlink->getAttribute('type');
1206-
}
1193+
if ($hyperlink->hasAttribute('type'))
1194+
$alt['type'] = $hyperlink->getAttribute('type');
12071195

1208-
if ($hyperlink->nodeValue) {
1209-
$rel_attributes['text'] = $hyperlink->nodeValue;
1210-
}
1196+
if ($hyperlink->nodeValue)
1197+
$alt['text'] = $hyperlink->nodeValue;
12111198

1212-
if ($this->enableAlternates) {
1213-
// If 'alternate' in rels, create 'alternates' structure, append
1214-
if (in_array('alternate', $linkRels)) {
1215-
$alternates[] = array_merge(
1216-
$rel_attributes,
1217-
array(
1218-
'url' => $href,
1219-
'rel' => implode(' ', array_diff($linkRels, array('alternate')))
1220-
)
1221-
);
1199+
$alternates[] = $alt;
1200+
} else {
1201+
foreach ($linkRels as $rel) {
1202+
$rels[$rel][] = $href;
12221203
}
12231204
}
1224-
1225-
foreach ($linkRels as $rel) {
1226-
$rels[$rel][] = $href;
1227-
}
1228-
1229-
if (!in_array($href, $rel_urls)) {
1230-
$rel_urls[$href] = array_merge(
1231-
$rel_attributes,
1232-
array('rels' => $linkRels)
1233-
);
1234-
}
1235-
12361205
}
12371206

12381207
if (empty($rels) and $this->jsonMode) {
12391208
$rels = new stdClass();
12401209
}
12411210

1242-
if (empty($rel_urls) and $this->jsonMode) {
1243-
$rel_urls = new stdClass();
1244-
}
1245-
1246-
return array($rels, $rel_urls, $alternates);
1211+
return array($rels, $alternates);
12471212
}
12481213

12491214
/**
@@ -1274,15 +1239,14 @@ public function parse($convertClassic = true, DOMElement $context = null) {
12741239
}
12751240

12761241
// Parse rels
1277-
list($rels, $rel_urls, $alternates) = $this->parseRelsAndAlternates();
1242+
list($rels, $alternates) = $this->parseRelsAndAlternates();
12781243

12791244
$top = array(
12801245
'items' => array_values(array_filter($mfs)),
1281-
'rels' => $rels,
1282-
'rel-urls' => $rel_urls,
1246+
'rels' => $rels
12831247
);
12841248

1285-
if ($this->enableAlternates && count($alternates)) {
1249+
if (count($alternates)) {
12861250
$top['alternates'] = $alternates;
12871251
}
12881252

0 commit comments

Comments
 (0)