forked from tpryan/Brackets-Reflow-Cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflowHTMLExtractor.js
121 lines (99 loc) · 3.5 KB
/
reflowHTMLExtractor.js
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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, document */
var ReflowHTMLExtractor = function (htmlcontent, jQuery) {
"use strict";
this.htmldoc = document.implementation.createHTMLDocument('');
this.jQuery = jQuery;
this.jQuery.fn.changeElementType = function (newType) {
var newElements = [];
jQuery(this).each(function () {
var attrs = {};
jQuery.each(this.attributes, function (idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
var newElement = jQuery("<" + newType + "/>", attrs).append(jQuery(this).contents());
jQuery(this).replaceWith(newElement);
newElements.push(newElement);
});
return jQuery(newElements);
};
this.htmldoc.open();
this.htmldoc.write(htmlcontent);
this.htmldoc.close();
this.removeClearFixes = function () {
jQuery(".clearfix", this.htmldoc).each(function (index) {
jQuery(this).removeClass("clearfix");
if (jQuery(this).attr("class").length === 0) {
jQuery(this).removeAttr("class");
}
});
};
this.removeTextSpans = function () {
jQuery("[id^=textspan]", this.htmldoc).each(function (index) {
jQuery(this).removeAttr("id");
});
};
this.trimWhitespace = function () {
jQuery("*", this.htmldoc).each(function (index) {
jQuery(this).innerhtml = jQuery.trim(jQuery(this).innerhtml);
});
};
this.classifyImagesAndRemoveID = function () {
this.jQuery("img", this.htmldoc).each(function (i, obj) {
var k = 0;
var $obj = jQuery(obj);
var classes = $obj.attr("id").split("_");
for (k = 1; k < classes.length; k++) {
if (classes[k].indexOf("png") !== 0 &&
classes[k].indexOf("jpg") !== 0 &&
classes[k].indexOf("gif") !== 0) {
$obj.addClass(classes[k]);
}
}
$obj.removeAttr("id");
$obj.removeClass("image");
if ($obj.attr("class").length === 0) {
$obj.removeAttr("class");
}
});
};
this.changeIDToElement = function (type) {
this.jQuery("[id^= " + type + "]", this.htmldoc).each(function (i, obj) {
var k = 0;
var $obj = jQuery(obj);
var original_id = $obj.attr("id");
//preserve classes
if (original_id.indexOf("_") > -1) {
var classes = original_id.split("_");
for (k = 1; k < classes.length; k++) {
$obj.addClass(classes[k]);
}
}
$obj.removeAttr("id");
$obj.changeElementType(type);
});
};
this.processHTML = function () {
this.changeIDToElement("header");
this.changeIDToElement("footer");
this.changeIDToElement("ul");
this.changeIDToElement("ol");
this.changeIDToElement("li");
this.changeIDToElement("h1");
this.changeIDToElement("h2");
this.changeIDToElement("h3");
this.changeIDToElement("h4");
this.changeIDToElement("h5");
this.changeIDToElement("h6");
this.changeIDToElement("article");
this.changeIDToElement("section");
this.changeIDToElement("time");
this.changeIDToElement("address");
this.changeIDToElement("div");
this.changeIDToElement("nav");
this.removeClearFixes();
this.trimWhitespace();
this.removeTextSpans();
this.classifyImagesAndRemoveID();
};
};