PageSourceSearch

https://comocayogoni.com/includes/rich/html2xhtml.js

js comocayogoni.com collected 2026-10-03 18:23:09 UTC 5,527 bytes, 178 lines download raw bytes

1var need_nl_before = '|div|p|table|tbody|tr|td|th|title|head|body|script|comment|li|meta|h1|h2|h3|h4|h5|h6|hr|ul|ol|option|';
2var need_nl_after = '|html|head|body|p|th|style|';
3var re_comment = new RegExp();
4re_comment.compile("^<!--(.*)-->$");
5var re_hyphen = new RegExp();
6re_hyphen.compile("-$");
7function get_xhtml(node, lang, encoding, need_nl, inside_pre) {
8	var i;
9	var text = '';
10	var children = node.childNodes;
11	var child_length = children.length;
12	var tag_name;
13	var do_nl = need_nl ? true : false;
14	var page_mode = true;
15	for (i = 0; i < child_length; i++) {
16		var child = children[i];
17		switch (child.nodeType) {
18			case 1: {
19				var tag_name = String(child.tagName).toLowerCase();
20				if (tag_name == '') break;
21				if (tag_name == 'meta') {
22					var meta_name = String(child.name).toLowerCase();
23					if (meta_name == 'generator') break;
24				}
25				if (!need_nl && tag_name == 'body') {
26					page_mode = false;
27				}
28				if (tag_name == '!') {
29					var parts = re_comment.exec(child.text);
30					if (parts) {
31						var inner_text = parts[1];
32						text += fix_comment(inner_text);
33					}
34				} else {
35					if (tag_name == 'html') {
36						text = '<?xml version="1.0" encoding="'+encoding+'"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n';
37					}
38					if (need_nl_before.indexOf('|'+tag_name+'|') != -1) {
39						if ((do_nl || text != '') && !inside_pre) text += '\n';
40					} else {
41						do_nl = true;
42					}
43					text += '<'+tag_name;
44					var attr = child.attributes;
45					var attr_length = attr.length;
46					var attr_value;
47					var attr_lang = false;
48					var attr_xml_lang = false;
49					var attr_xmlns = false;
50					var is_alt_attr = false;
51					for (j = 0; j < attr_length; j++) {
52						var attr_name = attr[j].nodeName.toLowerCase();
53						if (!attr[j].specified &&
54							(attr_name != 'selected' || !child.selected) &&
55							(attr_name != 'style' || child.style.cssText == '') &&
56							attr_name != 'value') continue;
57						if (attr_name == '_moz_dirty' ||
58							attr_name == '_moz_resizing' ||
59							tag_name == 'br' &&
60							attr_name == 'type' &&
61							child.getAttribute('type') == '_moz') continue;
62
63						var valid_attr = true;
64
65						switch (attr_name) {
66							case "style":
67								attr_value = child.style.cssText;
68								break;
69							case "class":
70								attr_value = child.className;
71								break;
72							case "http-equiv":
73								attr_value = child.httpEquiv;
74								break;
75							case "noshade": break;
76							case "checked": break;
77							case "selected": break;
78							case "multiple": break;
79							case "nowrap": break;
80							case "disabled": break;
81								attr_value = attr_name;
82								break;
83							default:
84								try {
85									attr_value = child.getAttribute(attr_name, 2);
86								} catch (e) {
87									valid_attr = false;
88								}
89								break;
90						}
91						if (attr_name == 'lang') {
92							attr_lang = true;
93							attr_value = lang;
94						}
95						if (attr_name == 'xml:lang') {
96							attr_xml_lang = true;
97							attr_value = lang;
98						}
99						if (attr_name == 'xmlns') attr_xmlns = true;
100						if (valid_attr) {
101							if (!(tag_name == 'li' && attr_name == 'value')) {
102								text += ' '+attr_name+'="'+fix_attribute(attr_value)+'"';
103							}
104						}
105						if (attr_name == 'alt') is_alt_attr = true;
106					}
107					if (tag_name == 'img' && !is_alt_attr) {
108						text += ' alt=""';
109					}
110					if (tag_name == 'html') {
111						if (!attr_lang) text += ' lang="'+lang+'"';
112						if (!attr_xml_lang) text += ' xml:lang="'+lang+'"';
113						if (!attr_xmlns) text += ' xmlns="http://www.w3.org/1999/xhtml"';
114					}
115					if (child.canHaveChildren || child.hasChildNodes()){
116						text += '>';
117						text += get_xhtml(child, lang, encoding, true, inside_pre || tag_name == 'pre' ? true : false);
118						text += '</'+tag_name+'>';
119					} else {
120						if (tag_name == 'style' || tag_name == 'title' || tag_name == 'script') {
121							text += '>';
122							var inner_text;
123							if (tag_name == 'script') {
124								inner_text = child.text;
125							} else {
126								inner_text = child.innerHTML;
127							}
128
129							if (tag_name == 'style') {
130								inner_text = String(inner_text).replace(/[\n]+/g,'\n');
131							}
132
133							text += inner_text+'</'+tag_name+'>';
134						} else {
135							text += ' />';
136						}
137					}
138				}
139				break;
140			}
141			case 3: {
142				if (!inside_pre) {
143					if (child.nodeValue != '\n') {
144						text += fix_text(child.nodeValue);
145					}
146				} else {
147					text += child.nodeValue;
148				}
149				break;
150			}
151			case 8: {
152				text += fix_comment(child.nodeValue);
153				break;
154			}
155			default:
156				break;
157		}
158	}
159	if (!need_nl && !page_mode) { //delete head and body tags from html fragment
160		text = text.replace(/<\/?head>[\n]*/gi, "");
161		text = text.replace(/<head \/>[\n]*/gi, "");
162		text = text.replace(/<\/?body>[\n]*/gi, "");
163	}
164	return text;
165}
166function fix_comment(text) {
167	text = text.replace(/--/g, "__");
168	if(re_hyphen.exec(text)) {
169		text += " ";
170	}
171	return "<!--"+text+"-->";
172}
173function fix_text(text) {
174	return String(text).replace(/\n{2,}/g, "\n").replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\u00A0/g, "&nbsp;");
175}
176function fix_attribute(text) {
177	return String(text).replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;");
178}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.