Merge pull request #926 from n1k0/bug-178-plain-text-fallback
Refs #178 - plain text version for non-html contents.
Showing
2 changed files
with
5 additions
and
33 deletions
... | @@ -233,11 +233,9 @@ utils.inherits(Casper, events.EventEmitter); | ... | @@ -233,11 +233,9 @@ utils.inherits(Casper, events.EventEmitter); |
233 | Casper.prototype.back = function back() { | 233 | Casper.prototype.back = function back() { |
234 | "use strict"; | 234 | "use strict"; |
235 | this.checkStarted(); | 235 | this.checkStarted(); |
236 | return this.then(function _step() { | 236 | return this.then(function() { |
237 | this.emit('back'); | 237 | this.emit('back'); |
238 | this.evaluate(function _evaluate() { | 238 | this.page.goBack(); |
239 | history.back(); | ||
240 | }); | ||
241 | }); | 239 | }); |
242 | }; | 240 | }; |
243 | 241 | ||
... | @@ -943,36 +941,10 @@ Casper.prototype.getPageContent = function getPageContent() { | ... | @@ -943,36 +941,10 @@ Casper.prototype.getPageContent = function getPageContent() { |
943 | "use strict"; | 941 | "use strict"; |
944 | this.checkStarted(); | 942 | this.checkStarted(); |
945 | var contentType = utils.getPropertyPath(this, 'currentResponse.contentType'); | 943 | var contentType = utils.getPropertyPath(this, 'currentResponse.contentType'); |
946 | if (!utils.isString(contentType)) { | 944 | if (!utils.isString(contentType) || contentType.indexOf("text/html") !== -1) { |
947 | return this.page.frameContent; | 945 | return this.page.frameContent; |
948 | } | 946 | } |
949 | // for some reason (qt)webkit/Gecko will always enclose non text/html body contents within an html | 947 | return this.page.framePlainText; |
950 | // structure like this: | ||
951 | // webkit: <html><head></head><body><pre style="(...)">content</pre></body></html> | ||
952 | // gecko: <html><head><link rel="alternate stylesheet" type="text/css" href="resource://gre-resources/plaintext.css" title="..."></head><body><pre>document.write('foo');\n</pre></body></html> | ||
953 | var sanitizedHtml = this.evaluate(function checkHtml() { | ||
954 | var head = __utils__.findOne('head'), | ||
955 | body = __utils__.findOne('body'); | ||
956 | if (!head || !body) { | ||
957 | return null; | ||
958 | } | ||
959 | // for content in Webkit | ||
960 | if (head.childNodes.length === 0 && | ||
961 | body.childNodes.length === 1 && | ||
962 | __utils__.findOne('body pre[style]')) { | ||
963 | return __utils__.findOne('body pre').textContent.trim(); | ||
964 | } | ||
965 | // for content in Gecko | ||
966 | if (head.childNodes.length === 1 && | ||
967 | body.childNodes.length === 1 && | ||
968 | head.childNodes[0].localName === 'link' && | ||
969 | head.childNodes[0].getAttribute('href') === 'resource://gre-resources/plaintext.css' && | ||
970 | body.childNodes[0].localName === 'pre' ) { | ||
971 | return body.childNodes[0].textContent.trim(); | ||
972 | } | ||
973 | return null; | ||
974 | }); | ||
975 | return sanitizedHtml ? sanitizedHtml : this.page.frameContent; | ||
976 | }; | 948 | }; |
977 | 949 | ||
978 | /** | 950 | /** | ... | ... |
... | @@ -13,7 +13,7 @@ casper.test.begin("Casper.getPageContent() text/html content", 1, function(test) | ... | @@ -13,7 +13,7 @@ casper.test.begin("Casper.getPageContent() text/html content", 1, function(test) |
13 | 13 | ||
14 | casper.test.begin("Casper.getPageContent() non text/html content", 1, function(test) { | 14 | casper.test.begin("Casper.getPageContent() non text/html content", 1, function(test) { |
15 | casper.start("tests/site/dummy.js", function() { | 15 | casper.start("tests/site/dummy.js", function() { |
16 | test.assertEquals(this.getPageContent(), "document.write('foo');", | 16 | test.assertEquals(this.getPageContent(), "document.write('foo');\n", |
17 | "Casper.getPageContent() retrieves non text/html content"); | 17 | "Casper.getPageContent() retrieves non text/html content"); |
18 | }).run(function() { | 18 | }).run(function() { |
19 | test.done(); | 19 | test.done(); | ... | ... |
-
Please register or sign in to post a comment