@@ -280,6 +280,9 @@ class Parser {
280
280
/** @var boolean Whether to include experimental language parsing in the result */
281
281
public $ lang = false ;
282
282
283
+ /** @var bool Whether to include alternates object (dropped from spec in favor of rel-urls) */
284
+ public $ enableAlternates = false ;
285
+
283
286
/**
284
287
* Elements upgraded to mf2 during backcompat
285
288
* @var SplObjectStorage
@@ -1153,62 +1156,89 @@ public function parseImpliedPhoto(\DOMElement $e) {
1153
1156
}
1154
1157
1155
1158
/**
1156
- * Parse Rels and Alternatives
1159
+ * Parse rels and alternates
1157
1160
*
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.
1161
+ * Returns [$rels, $rel_urls, $alternates].
1162
+ * For $rels and $rel_urls, if they are empty and $this->jsonMode = true, they will be returned as stdClass,
1163
+ * optimizing for JSON serialization. Otherwise they will be returned as an empty array.
1164
+ * Note that $alternates is deprecated in the microformats spec in favor of $rel_urls. $alternates only appears
1165
+ * in parsed results if $this->enableAlternates = true.
1166
+ * @return array|stdClass
1162
1167
*/
1163
1168
public function parseRelsAndAlternates () {
1164
1169
$ rels = array ();
1170
+ $ rel_urls = array ();
1165
1171
$ alternates = array ();
1166
1172
1167
1173
// Iterate through all a, area and link elements with rel attributes
1168
1174
foreach ($ this ->xpath ->query ('//a[@rel and @href] | //link[@rel and @href] | //area[@rel and @href] ' ) as $ hyperlink ) {
1169
- if ($ hyperlink ->getAttribute ('rel ' ) == '' )
1175
+ if ($ hyperlink ->getAttribute ('rel ' ) == '' ) {
1170
1176
continue ;
1177
+ }
1171
1178
1172
1179
// Resolve the href
1173
1180
$ href = $ this ->resolveUrl ($ hyperlink ->getAttribute ('href ' ));
1174
1181
1175
1182
// Split up the rel into space-separated values
1176
1183
$ linkRels = array_filter (explode (' ' , $ hyperlink ->getAttribute ('rel ' )));
1177
1184
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 ' );
1185
+ $ rel_attributes = array ();
1186
1186
1187
- if ($ hyperlink ->hasAttribute ('hreflang ' ))
1188
- $ alt ['hreflang ' ] = $ hyperlink ->getAttribute ('hreflang ' );
1187
+ if ($ hyperlink ->hasAttribute ('media ' )) {
1188
+ $ rel_attributes ['media ' ] = $ hyperlink ->getAttribute ('media ' );
1189
+ }
1189
1190
1190
- if ($ hyperlink ->hasAttribute ('title ' ))
1191
- $ alt ['title ' ] = $ hyperlink ->getAttribute ('title ' );
1191
+ if ($ hyperlink ->hasAttribute ('hreflang ' )) {
1192
+ $ rel_attributes ['hreflang ' ] = $ hyperlink ->getAttribute ('hreflang ' );
1193
+ }
1192
1194
1193
- if ($ hyperlink ->hasAttribute ('type ' ))
1194
- $ alt ['type ' ] = $ hyperlink ->getAttribute ('type ' );
1195
+ if ($ hyperlink ->hasAttribute ('title ' )) {
1196
+ $ rel_attributes ['title ' ] = $ hyperlink ->getAttribute ('title ' );
1197
+ }
1195
1198
1196
- if ($ hyperlink ->nodeValue )
1197
- $ alt ['text ' ] = $ hyperlink ->nodeValue ;
1199
+ if ($ hyperlink ->hasAttribute ('type ' )) {
1200
+ $ rel_attributes ['type ' ] = $ hyperlink ->getAttribute ('type ' );
1201
+ }
1198
1202
1199
- $ alternates [] = $ alt ;
1200
- } else {
1201
- foreach ($ linkRels as $ rel ) {
1202
- $ rels [$ rel ][] = $ href ;
1203
+ if ($ hyperlink ->nodeValue ) {
1204
+ $ rel_attributes ['text ' ] = $ hyperlink ->nodeValue ;
1205
+ }
1206
+
1207
+ if ($ this ->enableAlternates ) {
1208
+ // If 'alternate' in rels, create 'alternates' structure, append
1209
+ if (in_array ('alternate ' , $ linkRels )) {
1210
+ $ alternates [] = array_merge (
1211
+ $ rel_attributes ,
1212
+ array (
1213
+ 'url ' => $ href ,
1214
+ 'rel ' => implode (' ' , array_diff ($ linkRels , array ('alternate ' )))
1215
+ )
1216
+ );
1203
1217
}
1204
1218
}
1219
+
1220
+ foreach ($ linkRels as $ rel ) {
1221
+ $ rels [$ rel ][] = $ href ;
1222
+ }
1223
+
1224
+ if (!in_array ($ href , $ rel_urls )) {
1225
+ $ rel_urls [$ href ] = array_merge (
1226
+ $ rel_attributes ,
1227
+ array ('rels ' => $ linkRels )
1228
+ );
1229
+ }
1230
+
1205
1231
}
1206
1232
1207
1233
if (empty ($ rels ) and $ this ->jsonMode ) {
1208
1234
$ rels = new stdClass ();
1209
1235
}
1210
1236
1211
- return array ($ rels , $ alternates );
1237
+ if (empty ($ rel_urls ) and $ this ->jsonMode ) {
1238
+ $ rel_urls = new stdClass ();
1239
+ }
1240
+
1241
+ return array ($ rels , $ rel_urls , $ alternates );
1212
1242
}
1213
1243
1214
1244
/**
@@ -1239,14 +1269,15 @@ public function parse($convertClassic = true, DOMElement $context = null) {
1239
1269
}
1240
1270
1241
1271
// Parse rels
1242
- list ($ rels , $ alternates ) = $ this ->parseRelsAndAlternates ();
1272
+ list ($ rels , $ rel_urls , $ alternates ) = $ this ->parseRelsAndAlternates ();
1243
1273
1244
1274
$ top = array (
1245
1275
'items ' => array_values (array_filter ($ mfs )),
1246
- 'rels ' => $ rels
1276
+ 'rels ' => $ rels ,
1277
+ 'rel-urls ' => $ rel_urls ,
1247
1278
);
1248
1279
1249
- if (count ($ alternates )) {
1280
+ if ($ this -> enableAlternates && count ($ alternates )) {
1250
1281
$ top ['alternates ' ] = $ alternates ;
1251
1282
}
1252
1283
0 commit comments