UNPKG

1.58 kBJavaScriptView Raw
1
2/*!
3 * Jade - nodes - Tag
4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
5 * MIT Licensed
6 */
7
8/**
9 * Module dependencies.
10 */
11
12var Node = require('./node'),
13 Block = require('./block');
14
15/**
16 * Initialize a `Tag` node with the given tag `name` and optional `block`.
17 *
18 * @param {String} name
19 * @param {Block} block
20 * @api public
21 */
22
23var Tag = module.exports = function Tag(name, block) {
24 this.name = name;
25 this.attrs = [];
26 this.block = block || new Block;
27};
28
29/**
30 * Inherit from `Node`.
31 */
32
33Tag.prototype.__proto__ = Node.prototype;
34
35/**
36 * Set attribute `name` to `val`, keep in mind these become
37 * part of a raw js object literal, so to quote a value you must
38 * '"quote me"', otherwise or example 'user.name' is literal JavaScript.
39 *
40 * @param {String} name
41 * @param {String} val
42 * @return {Tag} for chaining
43 * @api public
44 */
45
46Tag.prototype.setAttribute = function(name, val){
47 this.attrs.push({ name: name, val: val });
48 return this;
49};
50
51/**
52 * Remove attribute `name` when present.
53 *
54 * @param {String} name
55 * @api public
56 */
57
58Tag.prototype.removeAttribute = function(name){
59 for (var i = 0, len = this.attrs.length; i < len; ++i) {
60 if (this.attrs[i] && this.attrs[i].name == name) {
61 delete this.attrs[i];
62 }
63 }
64};
65
66/**
67 * Get attribute value by `name`.
68 *
69 * @param {String} name
70 * @return {String}
71 * @api public
72 */
73
74Tag.prototype.getAttribute = function(name){
75 for (var i = 0, len = this.attrs.length; i < len; ++i) {
76 if (this.attrs[i] && this.attrs[i].name == name) {
77 return this.attrs[i].val;
78 }
79 }
80};