summaryrefslogtreecommitdiffstats
path: root/toolkit/components/microformats/test/lib/parser-includes.js
blob: f0967710d09c86b04ec0efeba9f0f3deba3db020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*!
	Parser includes
	All the functions that deal with microformats v1 include rules

	Copyright (C) 2010 - 2015 Glenn Jones. All Rights Reserved.
	MIT License: https://raw.github.com/glennjones/microformat-shiv/master/license.txt
	Dependencies  dates.js, domutils.js, html.js, isodate,js, text.js, utilities.js
*/


var Modules = (function (modules) {

	// check parser module is loaded
	if(modules.Parser){


		/**
		 * appends clones of include Nodes into the DOM structure
		 *
		 * @param  {DOM node} rootNode
		 */
		modules.Parser.prototype.addIncludes = function(rootNode) {
			this.addAttributeIncludes(rootNode, 'itemref');
			this.addAttributeIncludes(rootNode, 'headers');
			this.addClassIncludes(rootNode);
		};


		/**
		 * appends clones of include Nodes into the DOM structure for attribute based includes
		 *
		 * @param  {DOM node} rootNode
		 * @param  {String} attributeName
		 */
		modules.Parser.prototype.addAttributeIncludes = function(rootNode, attributeName) {
			var arr,
				idList,
				i,
				x,
				z,
				y;

			arr = modules.domUtils.getNodesByAttribute(rootNode, attributeName);
			x = 0;
			i = arr.length;
			while(x < i) {
				idList = modules.domUtils.getAttributeList(arr[x], attributeName);
				if(idList) {
					z = 0;
					y = idList.length;
					while(z < y) {
						this.apppendInclude(arr[x], idList[z]);
						z++;
					}
				}
				x++;
			}
		};


		/**
		 * appends clones of include Nodes into the DOM structure for class based includes
		 *
		 * @param  {DOM node} rootNode
		 */
		modules.Parser.prototype.addClassIncludes = function(rootNode) {
			var id,
				arr,
				x = 0,
				i;

			arr = modules.domUtils.getNodesByAttributeValue(rootNode, 'class', 'include');
			i = arr.length;
			while(x < i) {
				id = modules.domUtils.getAttrValFromTagList(arr[x], ['a'], 'href');
				if(!id) {
					id = modules.domUtils.getAttrValFromTagList(arr[x], ['object'], 'data');
				}
				this.apppendInclude(arr[x], id);
				x++;
			}
		};


		/**
		 * appends a clone of an include into another Node using Id
		 *
		 * @param  {DOM node} rootNode
		 * @param  {Stringe} id
		 */
		modules.Parser.prototype.apppendInclude = function(node, id){
			var include,
				clone;

			id = modules.utils.trim(id.replace('#', ''));
			include = modules.domUtils.getElementById(this.document, id);
			if(include) {
				clone = modules.domUtils.clone(include);
				this.markIncludeChildren(clone);
				modules.domUtils.appendChild(node, clone);
			}
		};


		/**
		 * adds an attribute marker to all the child microformat roots
		 *
		 * @param  {DOM node} rootNode
		 */
		modules.Parser.prototype.markIncludeChildren = function(rootNode) {
			var arr,
				x,
				i;

			// loop the array and add the attribute
			arr = this.findRootNodes(rootNode);
			x = 0;
			i = arr.length;
			modules.domUtils.setAttribute(rootNode, 'data-include', 'true');
			modules.domUtils.setAttribute(rootNode, 'style', 'display:none');
			while(x < i) {
				modules.domUtils.setAttribute(arr[x], 'data-include', 'true');
				x++;
			}
		};


		/**
		 * removes all appended include clones from DOM
		 *
		 * @param  {DOM node} rootNode
		 */
		modules.Parser.prototype.removeIncludes = function(rootNode){
			var arr,
				i;

			// remove all the items that were added as includes
			arr = modules.domUtils.getNodesByAttribute(rootNode, 'data-include');
			i = arr.length;
			while(i--) {
				modules.domUtils.removeChild(rootNode,arr[i]);
			}
		};


	}

	return modules;

} (Modules || {}));