Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function parse($input, $url = null, $convertClassic = true) {
* @param &array $curlInfo (optional) the results of curl_getinfo will be placed in this variable for debugging
* @return array|null canonical microformats2 array structure on success, null on failure
*/
function fetch($url, $convertClassic = true, &$curlInfo=null) {
function fetch($url, $convertClassic = true, &$curlInfo = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand Down Expand Up @@ -127,7 +127,7 @@ function unicodeTrim($str) {
* @param string $prefix The prefix to look for
* @return string|array The prefixed name of the first microfomats class found or false
*/
function mfNamesFromClass($class, $prefix='h-') {
function mfNamesFromClass($class, $prefix = 'h-') {
$class = str_replace(array(' ', ' ', "\n"), ' ', $class);
$classes = explode(' ', $class);
$classes = preg_grep('#^(h|p|u|dt|e)-([a-z0-9]+-)?[a-z]+(-[a-z]+)*$#', $classes);
Expand Down Expand Up @@ -366,7 +366,7 @@ public function __construct($input, $url = null, $jsonMode = false) {
if (empty($input)) {
$input = $emptyDocDefault;
}

if (class_exists('Masterminds\\HTML5')) {
$doc = new \Masterminds\HTML5(array('disable_html_ns' => true));
$doc = $doc->loadHTML($input);
Expand Down Expand Up @@ -482,15 +482,15 @@ private function resolveChildUrls(DOMElement $el) {
* @param bool $implied
* @see https://wiki.zegnat.net/media/textparsing.html
**/
public function textContent(DOMElement $element, $implied=false)
public function textContent(DOMElement $element, $implied = false)
{
return preg_replace(
'/(^[\t\n\f\r ]+| +(?=\n)|(?<=\n) +| +(?= )|[\t\n\f\r ]+$)/',
'',
$this->elementToString($element, $implied)
);
}
private function elementToString(DOMElement $input, $implied=false)
private function elementToString(DOMElement $input, $implied = false)
{
$output = '';
foreach ($input->childNodes as $child) {
Expand Down Expand Up @@ -1179,7 +1179,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
);

if(trim($e->getAttribute('id')) !== '') {
$parsed['id'] = trim($e->getAttribute("id"));
$parsed['id'] = trim($e->getAttribute('id'));
}

if($this->lang) {
Expand Down Expand Up @@ -1385,7 +1385,7 @@ public function upgradeRelTagToCategory(DOMElement $el) {
*/
public function parse($convertClassic = true, DOMElement $context = null) {
$this->convertClassic = $convertClassic;
$mfs = $this->parse_recursive($context);
$mfs = $this->parseRecursive($context);

// Parse rels
list($rels, $rel_urls, $alternates) = $this->parseRelsAndAlternates();
Expand All @@ -1411,7 +1411,7 @@ public function parse($convertClassic = true, DOMElement $context = null) {
* @param int $depth: recursion depth
* @return array
*/
public function parse_recursive(DOMElement $context = null, $depth = 0) {
public function parseRecursive(DOMElement $context = null, $depth = 0) {
$mfs = array();
$mfElements = $this->getRootMF($context);

Expand All @@ -1422,7 +1422,7 @@ public function parse_recursive(DOMElement $context = null, $depth = 0) {
$this->backcompat($node);
}

$recurse = $this->parse_recursive($node, $depth + 1);
$recurse = $this->parseRecursive($node, $depth + 1);

// set bool flag for nested mf
$has_nested_mf = (bool) $recurse;
Expand Down Expand Up @@ -1486,6 +1486,18 @@ public function parse_recursive(DOMElement $context = null, $depth = 0) {
return $mfs;
}

/**
* Parse microformats recursively (deprecated)
* @param DOMElement $context: node to start with
* @param int $depth: recursion depth
* @return array
* @deprecated Use parseRecursive() instead
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function parse_recursive(DOMElement $context = null, $depth = 0) {
return $this->parseRecursive($context, $depth);
}


/**
* Parse From ID
Expand All @@ -1502,7 +1514,7 @@ public function parse_recursive(DOMElement $context = null, $depth = 0) {
* @param bool $htmlSafe = false whether or not to HTML-encode angle brackets in non e-* properties
* @return array
*/
public function parseFromId($id, $convertClassic=true) {
public function parseFromId($id, $convertClassic = true) {
$matches = $this->xpath->query("//*[@id='{$id}']");

if (empty($matches))
Expand Down
44 changes: 43 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,49 @@
<?xml version="1.0"?>
<ruleset name="PHP-MF2">
<description>PHP-MF2 Standards</description>
<description>PHP-MF2 Coding Standards</description>

<file>./Mf2/Parser.php</file>
<file>./tests/</file>

<!-- PHP Compatibility -->
<rule ref="PHPCompatibility"/>
<config name="testVersion" value="5.6-"/>

<!-- Tabs for indentation -->
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>

<!-- PSR-1 naming conventions -->
<rule ref="PSR1.Classes.ClassDeclaration"/>
<rule ref="PSR1.Methods.CamelCapsMethodName">
<!-- Allow date format test methods (e.g., testYYYY_MM_DD__HH_MM) -->
<exclude-pattern>tests/Mf2/ParseDTTest.php</exclude-pattern>
</rule>

<!-- No trailing whitespace -->
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/>

<!-- Single space after cast -->
<rule ref="Generic.Formatting.SpaceAfterCast"/>

<!-- PHP opening tag -->
<rule ref="Generic.PHP.DisallowShortOpenTag"/>

<!-- Use lowercase for PHP keywords -->
<rule ref="Generic.PHP.LowerCaseKeyword"/>
<rule ref="Generic.PHP.LowerCaseConstant"/>

<!-- Spaces around equals in function declarations -->
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
<properties>
<property name="equalsSpacing" value="1"/>
</properties>
</rule>

<!-- Single quotes when no variable substitution needed -->
<rule ref="Squiz.Strings.DoubleQuoteUsage.NotRequired"/>

<!-- Allow multiple classes in test files (helper classes) -->
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
</ruleset>
17 changes: 9 additions & 8 deletions tests/Mf2/ClassicMicroformatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Mainly based off BC tables on https://microformats.org/wiki/microformats2#v2_vocabularies
*/
class ClassicMicroformatsTest extends TestCase {
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
protected function set_up() {
date_default_timezone_set('Europe/London');
}
Expand Down Expand Up @@ -147,7 +148,7 @@ public function testParsesHProduct() {
/**
* @see https://github.com/indieweb/php-mf2/issues/81
*/
public function test_vevent() {
public function testVevent() {
$input = <<< EOT
<div class="vevent">
<h3 class="summary">XYZ Project Review</h3>
Expand Down Expand Up @@ -896,21 +897,21 @@ public function testHEntryRelTag() {
}

public function testHEntryRelTagInContent() {
$input = <<< END
$input = <<< END
<article class="hentry">
<div class="entry-content">
Entry content should not include the generated <code>data</code> element for rel tag backcompat <a href="/tag/test" rel="tag">test</a>
</div>
</article>
END;

$parser = new Parser($input);
$output = $parser->parse();
$item = $output['items'][0];
$parser = new Parser($input);
$output = $parser->parse();
$item = $output['items'][0];

$this->assertEquals(['test'], $item['properties']['category']);
$this->assertEquals('Entry content should not include the generated data element for rel tag backcompat test', $item['properties']['content'][0]['value']);
$this->assertEquals('Entry content should not include the generated <code>data</code> element for rel tag backcompat <a href="/tag/test" rel="tag">test</a>', $item['properties']['content'][0]['html']);
$this->assertEquals(['test'], $item['properties']['category']);
$this->assertEquals('Entry content should not include the generated data element for rel tag backcompat test', $item['properties']['content'][0]['value']);
$this->assertEquals('Entry content should not include the generated <code>data</code> element for rel tag backcompat <a href="/tag/test" rel="tag">test</a>', $item['properties']['content'][0]['html']);
}

/**
Expand Down
17 changes: 9 additions & 8 deletions tests/Mf2/CombinedMicroformatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class CombinedMicroformatsTest extends TestCase {
use AssertIsType;

// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
protected function set_up() {
date_default_timezone_set('Europe/London');
}
Expand Down Expand Up @@ -294,7 +295,7 @@ public function testMicroformatsNestedUnderUPropertyClassnamesDeriveValueFromURL
"rels":[],
"rel-urls": []
}';
$mf = Mf2\parse($input);
$mf = Mf2\parse($input);

$this->assertJsonStringEqualsJsonString(json_encode($mf), $expected);
$this->assertEquals($mf['items'][0]['properties']['comment'][0]['value'], 'https://example.com/post1234');
Expand Down Expand Up @@ -345,18 +346,18 @@ public function testNestedMf1() {
}

public function testNoUrlFromRelOnMf2() {
$input = <<< END
$input = <<< END
<div class="h-entry">
<p> <a href="/article" rel="bookmark" class="p-name">Title of Post</a> </p>
<div class="e-content"><p> This is the post </p> </div>
</div>
END;
$parser = new Parser($input);
$output = $parser->parse();
$parser = new Parser($input);
$output = $parser->parse();

$this->assertArrayHasKey('name', $output['items'][0]['properties']);
$this->assertArrayHasKey('content', $output['items'][0]['properties']);
$this->assertArrayNotHasKey('url', $output['items'][0]['properties']);
$this->assertArrayHasKey('name', $output['items'][0]['properties']);
$this->assertArrayHasKey('content', $output['items'][0]['properties']);
$this->assertArrayNotHasKey('url', $output['items'][0]['properties']);
}

/**
Expand All @@ -378,7 +379,7 @@ public function testNestedValuePProperty() {
$output = $parser->parse();

$this->assertArrayHasKey('value', $output['items'][0]['properties']['location'][0]);
$this->assertEquals("Portland, Oregon • 44°F", $output['items'][0]['properties']['location'][0]['value']);
$this->assertEquals('Portland, Oregon • 44°F', $output['items'][0]['properties']['location'][0]['value']);
}

/**
Expand Down
Loading