Skip to content

Commit

Permalink
Adding UFIXIT for pNotUsedAsHeader Issue #60
Browse files Browse the repository at this point in the history
  • Loading branch information
emilysachs committed May 2, 2017
1 parent e3950a4 commit ecef527
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
22 changes: 22 additions & 0 deletions lib/Ufixit.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,28 @@ public function fixHeading($error_html, $new_content, $submitting_again = false)
return $fixed_heading;
}

/**
* Makes stylized p elements into headings
* @param string $error_html - The bad html that needs to be fixed
* @param string|array $new_content - The chosen heading level from the user
* @param bool $submitting_again - If the user is resubmitting their error fix
* @return string $fixed_heading - The html with corrected Heading
*/
public function makeHeading($error_html, $new_content, $submitting_again = false)
{
$fixed_heading = '';
if ($new_content == '') {
return $fixed_heading;
}

$fixed_heading = $error_html;

$fixed_heading = preg_replace('/<p/', '<' . $new_content, $fixed_heading, 1);
$fixed_heading = preg_replace('/<\/p>(?!.*<\/p>)/', '</' . $new_content . '>', $fixed_heading);

return $fixed_heading;
}

/**
* Fixes table headers by changing td elements to th (and adding proper scope, of course)
* @param string $error_html - The unmodified table without proper headings
Expand Down
2 changes: 1 addition & 1 deletion public/parseResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
'report_groups' => $udoit_report->content,
'post_path' => $post_input['path'],
'fixable_error_types' => ["cssTextHasContrast", "imgNonDecorativeHasAlt", "tableDataShouldHaveTh", "tableThShouldHaveScope", "headersHaveText", "aMustContainText", "imgAltIsDifferent", "imgAltIsTooLong"],
'fixable_suggestions' => ["aSuspiciousLinkText", "imgHasAlt", "aLinkTextDoesNotBeginWithRedundantWord", "cssTextStyleEmphasize"]
'fixable_suggestions' => ["aSuspiciousLinkText", "imgHasAlt", "aLinkTextDoesNotBeginWithRedundantWord", "cssTextStyleEmphasize", "pNotUsedAsHeader"]

];

Expand Down
7 changes: 6 additions & 1 deletion public/process.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@
$remove_attr = preg_replace("/ data-api-endpoint.*$/s", "", $data['error_html']);
$data['error_html'] = $remove_attr;
break;


case 'pNotUsedAsHeader':
$new_content = filter_input(INPUT_POST, 'newcontent', FILTER_SANITIZE_STRING);
$corrected_error = $ufixit->makeHeading($data['error_html'], $new_content);
break;

case 'tableDataShouldHaveTh':
// fixing table headers is a special case...
$new_content = filter_input(INPUT_POST, 'newcontent', FILTER_SANITIZE_STRING);
Expand Down
31 changes: 31 additions & 0 deletions templates/partials/result_item/make_heading.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright (C) 2014 University of Central Florida, created by Jacob Bates, Eric Colon, Fenel Joseph, and Emily Sachs.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Primary Author Contact: Jacob Bates <[email protected]>
*/
?>
<div class="input-group">
<label for="<?= $this->e($item_id); ?>-input" class="control-label sr-only">Select which heading the paragraph should be</label>
<select class="form-control" name="newcontent" id="<?= $this->e($item_id); ?>-input">
<option value="h2">h2</option>
<option value="h3">h3</option>
<option value="h4">h4</option>
</select>
<span class="input-group-btn">
<button class="submit-content inactive btn btn-default" type="submit">Submit</button>
</span>
</div>
4 changes: 4 additions & 0 deletions templates/partials/results_items.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@
$result_template = 'image_alt';
break;

case "pNotUsedAsHeader":
$result_template = 'make_heading';
break;

case "tableDataShouldHaveTh":
$result_template = 'table_header';
break;
Expand Down
13 changes: 13 additions & 0 deletions tests/UfixitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ public function testFixHeadingDeleteHeading() {
$this->checkOutputBuffer();
}

public function testMakeHeading() {
$error_html = '<p><strong>Paragraph text.</strong></p>';
$new_content = 'h2';
$expected = '<h2><strong>Paragraph text.</strong></h2>';

ob_start();
$temp = new Ufixit($this->data);
$output = $temp->makeHeading($error_html, $new_content);

$this->assertEquals($expected, $output);
$this->checkOutputBuffer();
}

public function testFixTableHeadersRow() {
$error_html = '<table><tbody><tr><td>Header One</td><td>Header Two</td></tr><tr><td>1.30</td><td>4.50</td></tr></tbody></table>';
$sel_header = 'row';
Expand Down

0 comments on commit ecef527

Please sign in to comment.