~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/htmlpurifier/HTMLPurifier/AttrDef/HTML/Nmtokens.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/**
4
 
 * Validates contents based on NMTOKENS attribute type.
5
 
 */
6
 
class HTMLPurifier_AttrDef_HTML_Nmtokens extends HTMLPurifier_AttrDef
7
 
{
8
 
 
9
 
    public function validate($string, $config, $context) {
10
 
 
11
 
        $string = trim($string);
12
 
 
13
 
        // early abort: '' and '0' (strings that convert to false) are invalid
14
 
        if (!$string) return false;
15
 
 
16
 
        $tokens = $this->split($string, $config, $context);
17
 
        $tokens = $this->filter($tokens, $config, $context);
18
 
        if (empty($tokens)) return false;
19
 
        return implode(' ', $tokens);
20
 
 
21
 
    }
22
 
 
23
 
    /**
24
 
     * Splits a space separated list of tokens into its constituent parts.
25
 
     */
26
 
    protected function split($string, $config, $context) {
27
 
        // OPTIMIZABLE!
28
 
        // do the preg_match, capture all subpatterns for reformulation
29
 
 
30
 
        // we don't support U+00A1 and up codepoints or
31
 
        // escaping because I don't know how to do that with regexps
32
 
        // and plus it would complicate optimization efforts (you never
33
 
        // see that anyway).
34
 
        $pattern = '/(?:(?<=\s)|\A)'. // look behind for space or string start
35
 
                   '((?:--|-?[A-Za-z_])[A-Za-z_\-0-9]*)'.
36
 
                   '(?:(?=\s)|\z)/'; // look ahead for space or string end
37
 
        preg_match_all($pattern, $string, $matches);
38
 
        return $matches[1];
39
 
    }
40
 
 
41
 
    /**
42
 
     * Template method for removing certain tokens based on arbitrary criteria.
43
 
     * @note If we wanted to be really functional, we'd do an array_filter
44
 
     *       with a callback. But... we're not.
45
 
     */
46
 
    protected function filter($tokens, $config, $context) {
47
 
        return $tokens;
48
 
    }
49
 
 
50
 
}
51
 
 
52
 
// vim: et sw=4 sts=4
 
1
<?php
 
2
 
 
3
/**
 
4
 * Validates contents based on NMTOKENS attribute type.
 
5
 */
 
6
class HTMLPurifier_AttrDef_HTML_Nmtokens extends HTMLPurifier_AttrDef
 
7
{
 
8
 
 
9
    /**
 
10
     * @param string $string
 
11
     * @param HTMLPurifier_Config $config
 
12
     * @param HTMLPurifier_Context $context
 
13
     * @return bool|string
 
14
     */
 
15
    public function validate($string, $config, $context)
 
16
    {
 
17
        $string = trim($string);
 
18
 
 
19
        // early abort: '' and '0' (strings that convert to false) are invalid
 
20
        if (!$string) {
 
21
            return false;
 
22
        }
 
23
 
 
24
        $tokens = $this->split($string, $config, $context);
 
25
        $tokens = $this->filter($tokens, $config, $context);
 
26
        if (empty($tokens)) {
 
27
            return false;
 
28
        }
 
29
        return implode(' ', $tokens);
 
30
    }
 
31
 
 
32
    /**
 
33
     * Splits a space separated list of tokens into its constituent parts.
 
34
     * @param string $string
 
35
     * @param HTMLPurifier_Config $config
 
36
     * @param HTMLPurifier_Context $context
 
37
     * @return array
 
38
     */
 
39
    protected function split($string, $config, $context)
 
40
    {
 
41
        // OPTIMIZABLE!
 
42
        // do the preg_match, capture all subpatterns for reformulation
 
43
 
 
44
        // we don't support U+00A1 and up codepoints or
 
45
        // escaping because I don't know how to do that with regexps
 
46
        // and plus it would complicate optimization efforts (you never
 
47
        // see that anyway).
 
48
        $pattern = '/(?:(?<=\s)|\A)' . // look behind for space or string start
 
49
            '((?:--|-?[A-Za-z_])[A-Za-z_\-0-9]*)' .
 
50
            '(?:(?=\s)|\z)/'; // look ahead for space or string end
 
51
        preg_match_all($pattern, $string, $matches);
 
52
        return $matches[1];
 
53
    }
 
54
 
 
55
    /**
 
56
     * Template method for removing certain tokens based on arbitrary criteria.
 
57
     * @note If we wanted to be really functional, we'd do an array_filter
 
58
     *       with a callback. But... we're not.
 
59
     * @param array $tokens
 
60
     * @param HTMLPurifier_Config $config
 
61
     * @param HTMLPurifier_Context $context
 
62
     * @return array
 
63
     */
 
64
    protected function filter($tokens, $config, $context)
 
65
    {
 
66
        return $tokens;
 
67
    }
 
68
}
 
69
 
 
70
// vim: et sw=4 sts=4