Sunday 26 December 2021

Spot The Bug: The Case of the Unstyleable Text

Hello, bug-hunters!

This is yet another PHP-related bug we will be exploring today, though in another sense, it's a lot more general than that.
Good hunting, guys!

It all started when I was working on yet another PHP report, that was an open-source plugin to export to PDF format. One of the users had requested me to make the font of the italicized descriptions (outlined in red) bigger, and I thought, how difficult could this be?



Oh, poor naive fool. It could get plenty more difficult.

What went wrong

I added this code around the code block. Easy fix, right?
<tr>
    <td>
        <?php
            echo get_name($item);
        ?>
        <span style="font-size:14px">
        <?php
            echo get_desc($item);
        ?>
        </span>
    </td>
    <td class="spacer"></td>
    <td class="figure" valign="top">
        <?php
            echo get_qty($item);
        ?>
    </td>
    <td class="spacer"></td>
    <td class="figure" valign="top">
        <?php
            echo get_price($item);
        ?>
    </td>
    <td class="spacer"></td>
    <td class="figure" valign="top">
        <?php
            echo get_subtotal($item);
        ?>
    </td>
</tr>


Nope! The PDF font remained stubbornly small. So what now?!


Why it went wrong

After several hours of tearing my hair out, I noticed that the text was in italics, though there was nothing in the code that suggested that it would be italicized. With that in mind, I made changes to the code to see what was in the HTML.
<?php
    echo htmlspecialchars(get_desc($item));
?>


And viola! Apparently it didn't return just text! There was a span tag containing all that text. That was why the code I wrote had no effect!


How I fixed it

So now this was a problem for the attached CSS file. What I needed to do, was fix the CSS class info_box.
.info_box
{
    font-size: 14px;
    font-style: italic;
}


And now the font was larger!


Moral of the story

There's usually more than meets the eye, especially when it comes to HTML. Developers who find themselves in situations where they can't just view the source, need to adopt other measures to have some visibility as to the HTML that they are trying to fix.

Think out of the info_box!
T___T

No comments:

Post a Comment