Giter Site home page Giter Site logo

aura.html's Introduction

Aura.Html

Provides HTML escapers and helpers, including form input helpers, that can be used in any template, view, or presentation system.

Foreword

Installation

This library requires PHP 5.3 or later with mbstring and/or iconv installed; we recommend using the latest available version of PHP as a matter of principle. It has no userland dependencies.

It is installable and autoloadable via Composer as aura/html.

Alternatively, download a release or clone this repository, then require or include its autoload.php file.

Quality

Scrutinizer Code Quality codecov Continuous Integration

To run the unit tests at the command line, issue composer install and then vendor/bin/phpunit at the package root. This requires Composer to be available as composer.

This library attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Community

To ask questions, provide feedback, or otherwise communicate with the Aura community, please join our Google Group, follow @auraphp on Twitter, or chat with us on #auraphp on Freenode.

Getting Started

The easiest way to instantiate a HelperLocator with all the available helpers is to use the HelperLocatorFactory:

<?php
$factory = new \Aura\Html\HelperLocatorFactory;
$helper = $factory->newInstance();
?>

Built-In Helpers

Once you have a HelperLocator, you can then use the helpers by calling them as methods on the HelperLocator instance. See the tag helpers and form helpers pages for more information.

N.b.: All built-in helpers escape values appropriately; see the various helper class internals for more information.

Custom Helpers

There are two steps to adding your own custom helpers:

  1. Write a helper class

  2. Set a factory for that class into the HelperLocator under a service name

A helper class needs only to implement the __invoke() method. We suggest extending from AbstractHelper to get access to indenting, escaping, etc., but it's not required.

The following example helper class applies ROT-13 to a string.

<?php
namespace Vendor\Package;

use Aura\Html\Helper\AbstractHelper;

class Obfuscate extends AbstractHelper
{
    public function __invoke($string)
    {
        return $this->escaper->html(str_rot13($input));
    }
}
?>

Now that we have a helper class, we set a factory for it into the HelperLocator under a service name. Therein, we create and return the helper class.

<?php
$helper->set('obfuscate', function () {
    return new \Vendor\Package\Obfuscate;
});
?>

The service name in the HelperLocator doubles as a method name. This means we can call the helper via $this->obfuscate():

<?= $helper->obfuscate('plain text') ?>

Note that we can use any service name for the helper, although it is generally useful to name the service for the helper class, and for a word that can be called as a method.

Please examine the classes in Aura\Html\Helper for more complex and powerful examples.

Escaping

One of the important but menial tasks with PHP-based template systems is that of escaping output properly. Escaping output is absolutely necessary from a security perspective. This package comes with an escape() helper that has four escaping methods:

  • $this->escape()->html('foo') to escape HTML values
  • $this->escape()->attr('foo') to escape unquoted HTML attributes
  • $this->escape()->css('foo') to escape CSS values
  • $this->escape()->js('foo') to escape JavaScript values

Here is a contrived example of the various escape() helper methods:

<head>

    <style>
        body {
            color: <?= $this->escape()->css($theme->color) ?>;
            font-size: <?= $this->escape()->css($theme->font_size) ?>;
        }
    </style>

    <script language="javascript">
        var foo = "<?= $this->escape()->js($js->foo); ?>";
    </script>

</head>

<body>

    <h1><?= $this->escape()->html($blog->title) ?></h1>

    <p class="byline">
        by <?= $this->escape()->html($blog->author) ?>
        on <?= $this->escape()->html($blog->date) ?>
    </p>

    <div id="<?php $this->escape()->attr($blog->div_id) ?>">
        <?= $blog->raw_html ?>
    </div>

</body>

Unfortunately, escaper functionality is verbose, and can make the template code look cluttered. There are two ways to mitigate this.

The first is to assign the escape() helper to a variable, and then invoke it as a callable. Here is a contrived example of the various escaping methods as callables:

<?php
// assign the escaper helper properties to callable variables
$h = $this->escape()->html;
$a = $this->escape()->attr;
$c = $this->escape()->css;
$j = $this->escape()->js;
?>

<head>

    <style>
        body {
            color: <?= $c($theme->color) ?>;
            font-size: <?= $c($theme->font_size) ?>;
        }
    </style>

    <script language="javascript">
        var foo = "<?= $j($js->foo); ?>";
    </script>

</head>

<body>

    <h1><?= $h($blog->title) ?></h1>

    <p class="byline">
        by <?= $h($blog->author) ?>
        on <?= $h($blog->date) ?>
    </p>

    <div id="<?php $a($blog->div_id) ?>">
        <?= $blog->raw_html ?>
    </div>

</body>

Alternatively, the Escaper class used by the escape() helper comes with four static methods to reduce verbosity and clutter: h(), a(), c(), j(), and. These escape values for HTML content values, unquoted HTML attribute values, CSS values, and JavaScript values, respectively.

N.b.: In Aura, we generally avoid static methods. However, we feel the tradeoff of less-cluttered templates can be worth using static methods in this one case.

To call the static Escaper methods in a PHP-based template, use the Escaper as a short alias name, then call the static methods on the alias. (If you did not instantiate a HelperLocatorFactory, you will need to prepare the static escaper methods by calling Escaper::setStatic(new Escaper).)

Here is a contrived example of the various static methods:

<?php use Aura\Html\Escaper as e; ?>

<head>

    <style>
        body {
            color: <?= e::c($theme->color) ?>;
            font-size: <?= e::c($theme->font_size) ?>;
        }
    </style>

    <script language="javascript">
        var foo = "<?= e::j($js->foo); ?>";
    </script>

</head>

<body>

    <h1><?= e::h($blog->title) ?></h1>

    <p class="byline">
        by <?= e::h($blog->author) ?>
        on <?= e::h($blog->date) ?>
    </p>

    <div id="<?php e::a($blog->div_id) ?>">
        <?= $blog->raw_html ?>
    </div>

</body>

aura.html's People

Contributors

afilina avatar arokettu avatar avaq avatar dmolineus avatar harikt avatar iansltx avatar jakejohns avatar jelofson avatar koriym avatar mapthegod avatar mcordingley avatar rodsouto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aura.html's Issues

Better frontend integration

Is there any way to use decorators in forms? It would be nice to switch between default raw HTML, Foundation, Bootstrap and other CSS frameworks.

Way to make the with out

Hi Paul,

I forget there is a way to call h(), j() etc without making use of

<?php 
use Aura\Html\Escaper as e; 
e::h('Hello World');
?>

like adding a functions.php file and having something like

<?php
use Aura\Html\Escaper; 
function h($raw) 
{
    return Escaper::h($raw);
}

And this can be autoloaded by composer.

And I would like to alter the getStatic() to make sure the Escaper object is there.

    public static function getStatic()
    {
        if (! static::$escaper) {
            $html = new HtmlEscaper($this->flags, $this->encoding);
            $attr = new AttrEscaper($html, $this->encoding);
            $css = new CssEscaper($this->encoding);
            $js = new JsEscaper($this->encoding);
            static::$escaper = new self($html, $attr, $css, $js);
        }
        return static::$escaper;
    }

This make calling as h("Hello World") I guess ๐Ÿ‘ .

Not sure whether you like or not.

Continuation of the Numeric Keys for Options issue

I found where you fixed the Select issue for this, but the same issue can be found within Aura\Html\Helper\Input\AbstractChecked, which causes issues with the radio inputs (since they are set using the options array as well). Could you implement the same fix you did with the Select, or create a different fix?

JsEscaper duplicating json_encode() ?

Is JsEscaper duplicating the behavior of json_encode()?

Isn't this code essentially equivalent to, say, substr(json_encode($value), 1, -1) ?

Is there any benefit to re-implementing this?

Also, is it necessary to escape multi-byte UTF-8 characters in the first place? json_encode() lets you disable unnecessary unicode escapes with the JSON_UNESCAPED_UNICODE flag.

For non-UTF-8 (e.g. ASCII) output, unicode escaping is required - but the likely use-case is outputting UTF-8 strings, right? So why escape everything as ASCII?

Calling helpers inside helpers

I don't see a way to call the helpers inside the helpers.

Else we need to duplicate the code of the other html helpers.

Assign authority to @jakejohns

@jakejohns Since you are putting so much effort into this package, and since I am so slow to respond, how would you feel about becoming the package owner? Full authority and responsibility over it, etc. (And if you can't, no big deal.) Thanks, hope you are doing well!

Would like to have PHPdoc on HelperLocator for IDE

In IDE autocomplete for HelperLocator class doesn't have basic helpers like a, base etc

Would you like to add it there? It could be achieved by adding PHPdoc to HelperLocator:

/**
* @method Helper\Anchor a($href, $text, array $attr = array())
*/

Thoughts on supporting the "double_encode" flag?

see: http://php.net/manual/en/function.htmlspecialchars.php

I have a case where I would like to use the double_encode flag.

I understand why you probably don't want to in most cases.
As how would you deal with the following: The HTML entity for "&" is "&amp;".
see: http://stackoverflow.com/questions/16646577/

In my case though I have integrated the Foil view engine into a Wordpress theme.
Foil uses this library for all it's escaping and by default it auto escapes everything, which is great 95% of the time.

There are cases though where Wordpress has already escaped a value, say the "Post Title".
And then foil comes along and escapes again. The solution is to turn off auto escaping and manually escape where needed.

Then I saw the double_encode flag, and figured that would solve my issue.
Went looking for a way to set it through Foil, no dice, then I came here and looked through the source and noticed there is indeed no way to set it.

https://github.com/auraphp/Aura.Html/blob/2.x/src/Escaper/HtmlEscaper.php#L73

If you have strong opinions that the flag is a big NO NO, thats fine however if you think it is something that could be made configurable through your API I will create a pull request for the feature.

Cheers Brad

Issue then using numeric key for options

I have to fill my select widget with options, which contains numeric keys. Unfortunately the select helper does not detect the selected value because of the strict value checking in https://github.com/auraphp/Aura.Html/blob/develop-2/src/Helper/Input/Select.php#L257

// example with integer keys
$options = array('one', 'two');
$field->setOptions($options);

// example with string keys, but converted to integers by php
$options = array('0' => 'one', '1' => 'two');
$field->setOptions($options);

Both examples will have integer array keys.

There seems to be some issues with the automatically type convertion of PHP.

  1. PHP gets numbers as string in $_POST (I simply filled my form using the post array)
  2. PHP converts numeric keys set as string to integers in arrays.
  3. The select helper will fail because the post value is a string, the defined value is an integer.

Maybe something is wrong with my PHP setup but at the moment I can not use the select helpers because of the strict value checking.

About the new unit and integration testing

To run the unit tests at the command line, go to the tests/unit directory and issue ./phpunit.sh. (This requires [PHPUnit][] to be available as phpunit.)

I think to run the unit test you don't need users to force to go via ./phpunit.sh I also wonder whether this works in windows then .

Let them run unit tests via phpunit . In case of integration I am ok with ./phpunit.sh for it needs installing the aura/di . ( this gives another thought on windows also ) My suggestion is rather keeping it for all we need to write a page in the website with testing.

Which can mention the unit and integration testing.

Released 2.5.0!

@harikt I went ahead and released 2.5.0 as we talked about in #53.

Perhaps I was a bit hasty, since I just opened #55, but I got a little excited when you figured out the stuff with travis. :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.