@@ -144,6 +144,15 @@ function mfNamesFromClass($class, $prefix='h-') {
144
144
return $ matches ;
145
145
}
146
146
147
+ /**
148
+ * Registered with the XPath object and used within XPaths for finding root elements.
149
+ * @param string $class
150
+ * @return bool
151
+ */
152
+ function classHasMf2RootClassname ($ class ) {
153
+ return count (mfNamesFromClass ($ class , 'h- ' )) > 0 ;
154
+ }
155
+
147
156
/**
148
157
* Get Nested µf Property Name From Class
149
158
*
@@ -371,7 +380,10 @@ public function __construct($input, $url = null, $jsonMode = false) {
371
380
@$ doc ->loadHTML ('' );
372
381
}
373
382
383
+ // Create an XPath object and allow some PHP functions to be used within XPath queries.
374
384
$ this ->xpath = new DOMXPath ($ doc );
385
+ $ this ->xpath ->registerNamespace ('php ' , 'http://php.net/xpath ' );
386
+ $ this ->xpath ->registerPhpFunctions ('\\Mf2 \\classHasMf2RootClassname ' );
375
387
376
388
$ baseurl = $ url ;
377
389
foreach ($ this ->xpath ->query ('//base[@href] ' ) as $ base ) {
@@ -1164,7 +1176,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
1164
1176
'type ' => $ mfTypes ,
1165
1177
'properties ' => $ return
1166
1178
);
1167
-
1179
+
1168
1180
if (trim ($ e ->getAttribute ('id ' )) !== '' ) {
1169
1181
$ parsed ['id ' ] = trim ($ e ->getAttribute ("id " ));
1170
1182
}
@@ -1506,7 +1518,7 @@ public function parseFromId($id, $convertClassic=true) {
1506
1518
public function getRootMF (DOMElement $ context = null ) {
1507
1519
// start with mf2 root class name xpath
1508
1520
$ xpaths = array (
1509
- 'contains(concat(" ", normalize-space(@class)), " h-" ) '
1521
+ '(php:function(" \\ Mf2 \\ classHasMf2RootClassname", normalize-space(@class))) '
1510
1522
);
1511
1523
1512
1524
// add mf1 root class names
@@ -1687,15 +1699,9 @@ public function addMfClasses(DOMElement $el, $classes) {
1687
1699
*/
1688
1700
public function hasRootMf2 (\DOMElement $ el ) {
1689
1701
$ class = str_replace (array ("\t" , "\n" ), ' ' , $ el ->getAttribute ('class ' ));
1690
- $ classes = array_filter (explode (' ' , $ class ));
1691
1702
1692
- foreach ( $ classes as $ classname ) {
1693
- if ( strpos ($ classname , 'h- ' ) === 0 ) {
1694
- return true ;
1695
- }
1696
- }
1697
-
1698
- return false ;
1703
+ // Check for valid mf2 root classnames, not just any classname with a h- prefix.
1704
+ return count (mfNamesFromClass ($ class , 'h- ' )) > 0 ;
1699
1705
}
1700
1706
1701
1707
/**
0 commit comments