[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/webroot/rsrc/externals/javelin/ext/view/ -> HTMLView.js (source)

   1  /**
   2   * Dumb HTML views. Mostly to demonstrate how the visitor pattern over these
   3   * views works, as driven by validation. I'm not convinced it's actually a good
   4   * idea to do validation.
   5   *
   6   * @provides javelin-view-html
   7   * @requires javelin-install
   8   *           javelin-dom
   9   *           javelin-view-visitor
  10   *           javelin-util
  11   */
  12  
  13  JX.install('HTMLView', {
  14    extend: 'View',
  15    members : {
  16      render: function(rendered_children) {
  17        return JX.$N(this.getName(), this.getAllAttributes(), rendered_children);
  18      },
  19      validate: function() {
  20        this.accept(JX.HTMLView.getValidatingVisitor());
  21      }
  22    },
  23  
  24    statics: {
  25      getValidatingVisitor: function() {
  26        return new JX.ViewVisitor(JX.HTMLView.validate);
  27      },
  28  
  29      validate: function(view, children) {
  30        var spec = this._getHTMLSpec();
  31        if (!(view.getName() in spec)) {
  32          throw new Error("invalid tag");
  33        }
  34  
  35        var tag_spec = spec[view.getName()];
  36  
  37        var attrs = view.getAllAttributes();
  38        for (var attr in attrs) {
  39          if (!(attr in tag_spec)) {
  40            throw new Error("invalid attr");
  41          }
  42  
  43          var validator = tag_spec[attr];
  44          if (typeof validator === "function") {
  45            return validator(attrs[attr]);
  46          }
  47        }
  48  
  49        return true;
  50      },
  51  
  52      _validateRel: function(target) {
  53        return target in {
  54          "_blank": 1,
  55          "_self": 1,
  56          "_parent": 1,
  57          "_top": 1
  58        };
  59      },
  60      _getHTMLSpec: function() {
  61        var attrs_any_can_have = {
  62          className: 1,
  63          id: 1,
  64          sigil: 1
  65        };
  66  
  67        var form_elem_attrs = {
  68          name: 1,
  69          value: 1
  70        };
  71  
  72        var spec = {
  73          a: { href: 1, target: JX.HTMLView._validateRel },
  74          b: {},
  75          blockquote: {},
  76          br: {},
  77          button: JX.copy({}, form_elem_attrs),
  78          canvas: {},
  79          code: {},
  80          dd: {},
  81          div: {},
  82          dl: {},
  83          dt: {},
  84          em: {},
  85          embed: {},
  86          fieldset: {},
  87          form: { type: 1 },
  88          h1: {},
  89          h2: {},
  90          h3: {},
  91          h4: {},
  92          h5: {},
  93          h6: {},
  94          hr: {},
  95          i: {},
  96          iframe: { src: 1 },
  97          img: { src: 1, alt: 1 },
  98          input: JX.copy({}, form_elem_attrs),
  99          label: {'for': 1},
 100          li: {},
 101          ol: {},
 102          optgroup: {},
 103          option: JX.copy({}, form_elem_attrs),
 104          p: {},
 105          pre: {},
 106          q: {},
 107          select: {},
 108          span: {},
 109          strong: {},
 110          sub: {},
 111          sup: {},
 112          table: {},
 113          tbody: {},
 114          td: {},
 115          textarea: {},
 116          tfoot: {},
 117          th: {},
 118          thead: {},
 119          tr: {},
 120          ul: {}
 121        };
 122  
 123        for (var k in spec) {
 124          JX.copy(spec[k], attrs_any_can_have);
 125        }
 126  
 127        return spec;
 128      },
 129      registerToInterpreter: function(view_interpreter) {
 130        var spec = this._getHTMLSpec();
 131        for (var tag in spec) {
 132          view_interpreter.register(tag, JX.HTMLView);
 133        }
 134        return view_interpreter;
 135      }
 136    }
 137  });


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1