Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #16 from mobify/WEB-2162
Browse files Browse the repository at this point in the history
WEB-2162: Remove plaintext from SourceDoc upon Capture.init
  • Loading branch information
noahadams committed May 1, 2019
2 parents 179ae11 + dbb803c commit 5240c4d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 28 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG
@@ -1,3 +1,12 @@
1.0.7
- Upgrade dependencies (grunt-conbtrib-qunit and grunt-saucelabs).
- Make bower, browserify, and grunt-cli dependencies explicit.
- Update list of browsers to run saucelabs on.
- Remove plaintext from sourceDoc after captured doc elements have been
saved into the capturedDoc to prevent googlebot from picking up the
lingering plaintext as metadata.
- Add checks to tests to confirm that plaintext was added before
Capture.init and removed after the callback in Capture.init.
1.0.6
- Fixed issue where <!-- --> comments containing '<head>' created false
positives when looking for the <head> section in HTML
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "mobify-capturejs",
"version": "1.0.6",
"version": "1.0.7",
"devDependencies": {
"bower": "~1.8.8",
"browserify": "~3.46.1",
Expand Down
11 changes: 10 additions & 1 deletion src/capture.js
Expand Up @@ -128,7 +128,16 @@ Capture.init = Capture.initCapture = function(callback, doc, prefix) {
Utils.extend(capture, capturedStringFragments);
var capturedDOMFragments = capture.createDocumentFragments();
Utils.extend(capture, capturedDOMFragments);
callback(capture);
try {
callback(capture);
} finally {
// Remove plaintext element after Capture.initCapture() callback
// has been called, and be robust in case of exceptions
var plaintextEl = capture.sourceDoc.querySelector('plaintext')
if (plaintextEl) {
plaintextEl.parentNode.removeChild(plaintextEl)
}
}
};

if (Utils.domIsReady(doc)) {
Expand Down
111 changes: 85 additions & 26 deletions tests/capture-tests.js
Expand Up @@ -79,26 +79,48 @@ require(["saucelabs-config", "qunit", "mobifyjs/utils", "capture"], function(_,
// We remove the webdriver attribute set when running tests on selenium (typically done through SauceLabs)
var htmlEl = doc.getElementsByTagName("html")[0].removeAttribute("webdriver")

var expectedPlaintext =
" <link rel=stylesheet href=/path/to/stylesheet.css>" +
"</head>" +
"<body bar=baz>" +
" <!-- comment with <head> -->" +
" <p>Plaintext example page!</p>" +
" <script src=/path/to/script.js><\/script>" +
"</body>" +
"</html>";

var plaintextEl = doc.querySelector('plaintext')
// Firefox 4.0 does not support access to innerText
var plaintextText = plaintextEl.innerText || plaintextEl.innerHTML
ok(compareHTMLStrings(plaintextText, expectedPlaintext), "plaintext is added")

Capture.init(function(capture) {
var capturedDoc = capture.capturedDoc;

var expectedHtml =
"<!DOCTYPE HTML>" +
"<html class=testclass>" +
"<head foo=bar>" +
" <link rel=stylesheet href=/path/to/stylesheet.css>" +
"</head>" +
"<body bar=baz>" +
" <!-- comment with <head> -->" +
" <p>Plaintext example page!</p>" +
" <script src=/path/to/script.js><\/script>" +
"</body>" +
"</html>";

var html = capture.enabledHTMLString(capturedDoc);
ok(compareHTMLStrings(html, expectedHtml), "Passed!");
start();
// wait for a turn of the event loop to see the removed
// <plaintext> element
setTimeout(function() {
var capturedDoc = capture.capturedDoc;

var expectedHtml =
"<!DOCTYPE HTML>" +
"<html class=testclass>" +
"<head foo=bar>" +
" <link rel=stylesheet href=/path/to/stylesheet.css>" +
"</head>" +
"<body bar=baz>" +
" <!-- comment with <head> -->" +
" <p>Plaintext example page!</p>" +
" <script src=/path/to/script.js><\/script>" +
"</body>" +
"</html>";

var html = capture.enabledHTMLString(capturedDoc);
ok(doc.querySelector('plaintext') === null, "plaintext is removed")
ok(compareHTMLStrings(html, expectedHtml), "Passed!");
start();
}, 0);
}, doc);

ok(doc.querySelector('plaintext') === null, "plaintext is removed")
});
$("#qunit-fixture").append(iframe);
});
Expand Down Expand Up @@ -492,6 +514,15 @@ require(["saucelabs-config", "qunit", "mobifyjs/utils", "capture"], function(_,
});
var el = $iframe[0];

var expectedPlaintext =
'</head>' +
'<body bar="baz">' +
' <!-- comment with <head> -->' +
' <p>Plaintext example page!</p>' +
' <script src="/path/to/script.js"><\/script>' +
'</body>' +
'</html>';

var expectedHTML =
'<!DOCTYPE HTML>' +
'<html class="testclass">' +
Expand Down Expand Up @@ -523,9 +554,19 @@ require(["saucelabs-config", "qunit", "mobifyjs/utils", "capture"], function(_,
// We remove the webdriver attribute set when running tests on selenium (typically done through SauceLabs)
var htmlEl = doc.getElementsByTagName("html")[0].removeAttribute("webdriver")

var plaintextEl = doc.querySelector('plaintext')
// Firefox 4.0 does not support access to innerText
var plaintextText = plaintextEl.innerText || plaintextEl.innerHTML
ok(compareHTMLStrings(plaintextText, expectedPlaintext), 'plaintext added')
Capture.init(function(capture) {
capture.restore('<script>parent.postMessage("done!", "*");<\/script>');
// wait for a turn of the event loop to see the removed
// <plaintext> element
setTimeout(function() {
ok(doc.querySelector('plaintext') === null, "plaintext is removed")
capture.restore('<script>parent.postMessage("done!", "*");<\/script>');
}, 0);
}, doc);
ok(doc.querySelector('plaintext') === null, 'plaintext is removed')
});
$("#qunit-fixture").append($iframe);
});
Expand All @@ -542,15 +583,33 @@ require(["saucelabs-config", "qunit", "mobifyjs/utils", "capture"], function(_,
iframe.one('load', function() {
var doc = this.contentDocument;

var expectedPlaintext =
'</head>' +
'<body>' +
' <form><\/form>' +
' <form><\/form>' +
'</body>' +
'</html>';

var plaintextEl = doc.querySelector('plaintext')
// Firefox 4.0 does not support access to innerText
var plaintextText = plaintextEl.innerText || plaintextEl.innerHTML
ok(compareHTMLStrings(plaintextText, expectedPlaintext), "plaintext added")
Capture.init(function(capture) {
var capturedDoc = capture.capturedDoc;

var bodyChildren = capturedDoc.body.children;
equal(bodyChildren.length, 2);
equal(bodyChildren[0].nodeName, 'FORM', 'first child element is a form');
equal(bodyChildren[1].nodeName, 'FORM', 'second child element is a form');
start();
// wait for a turn of the event loop to see the removed
// <plaintext> element
setTimeout(function() {
var capturedDoc = capture.capturedDoc;

var bodyChildren = capturedDoc.body.children;
ok(doc.querySelector('plaintext') === null, 'plaintext is removed')
equal(bodyChildren.length, 2);
equal(bodyChildren[0].nodeName, 'FORM', 'first child element is a form');
equal(bodyChildren[1].nodeName, 'FORM', 'second child element is a form');
start();
}, 0);
}, doc);
ok(doc.querySelector('plaintext') === null, 'plaintext is removed')
});
$("#qunit-fixture").append(iframe);
});
Expand Down

0 comments on commit 5240c4d

Please sign in to comment.