Permalink
Browse files

released 0.0.4, fixes #4

  • Loading branch information...
1 parent 7d81000 commit 732dc068bb6ae7c5e5a98a7cd473f8b729158ccc @searls searls committed Apr 10, 2012
@@ -8,9 +8,7 @@ class app.dom.ReplacesWords
#private
textNodes: ->
- $('*:not(script)').contents().filter(->
- @nodeType == 3 and not _(@textContent.trim()).isEmpty()
- ).toArray()
+ $('*:not(script)').filter(@notIllegalIframe).contents().filter(@nonEmptyTextNode).toArray()
replaceEntry: (string, entry) =>
regex = ///
@@ -19,3 +17,9 @@ class app.dom.ReplacesWords
(\W|$)
///g
string.replace(regex, "$1#{entry.replacement}$3")
+
+ notIllegalIframe: ->
+ !($.nodeName(@, "iframe") && !@contentWindow)
+
+ nonEmptyTextNode: ->
+ @nodeType == 3 and not _(@textContent.trim()).isEmpty()
@@ -1,5 +1,12 @@
class app.initializers.TransformsContent
transform: ->
chrome.extension.sendRequest {type: "config"}, (config) ->
- $(document).ready ->
- new app.dom.ReplacesWords().replace(config.dictionary.entries)
+ replacesWords = new app.dom.ReplacesWords()
+
+ $(document).on
+ ready: ->
+ replacesWords.replace(config.dictionary.entries)
+
+ DOMNodeInserted: _.debounce( ->
+ replacesWords.replace(config.dictionary.entries)
+ , 300)
View
Binary file not shown.
@@ -2408,9 +2408,7 @@ Backbone.sync = function(method, model, options, error) {
};
ReplacesWords.prototype.textNodes = function() {
- return $('*:not(script)').contents().filter(function() {
- return this.nodeType === 3 && !_(this.textContent.trim()).isEmpty();
- }).toArray();
+ return $('*:not(script)').filter(this.notIllegalIframe).contents().filter(this.nonEmptyTextNode).toArray();
};
ReplacesWords.prototype.replaceEntry = function(string, entry) {
@@ -2419,6 +2417,14 @@ Backbone.sync = function(method, model, options, error) {
return string.replace(regex, "$1" + entry.replacement + "$3");
};
+ ReplacesWords.prototype.notIllegalIframe = function() {
+ return !($.nodeName(this, "iframe") && !this.contentWindow);
+ };
+
+ ReplacesWords.prototype.nonEmptyTextNode = function() {
+ return this.nodeType === 3 && !_(this.textContent.trim()).isEmpty();
+ };
+
return ReplacesWords;
})();
@@ -2486,8 +2492,15 @@ Backbone.sync = function(method, model, options, error) {
return chrome.extension.sendRequest({
type: "config"
}, function(config) {
- return $(document).ready(function() {
- return new app.dom.ReplacesWords().replace(config.dictionary.entries);
+ var replacesWords;
+ replacesWords = new app.dom.ReplacesWords();
+ return $(document).on({
+ ready: function() {
+ return replacesWords.replace(config.dictionary.entries);
+ },
+ DOMNodeInserted: _.debounce(function() {
+ return replacesWords.replace(config.dictionary.entries);
+ }, 300)
});
});
};
View
@@ -8,7 +8,8 @@
{
"matches": [
"http://*/*",
- "https://*/*"
+ "https://*/*",
+ "file://*/*"
],
"js": ["assets/application.js"],
"run_at": "document_start",
@@ -41,6 +41,13 @@ describe "app.dom.ReplacesWords", ->
When -> @subject.replace(@entries)
Then -> expect(@$mixed.text()).toContain("lol but also bar")
+ # ... no apparent good way to test the iframe issue.
+ # context "in a node with children & text elements", ->
+ # Given -> @$iframe = @$root.append('<iframe src=""><div>foo</div></iframe>')
+ # When -> @subject.replace(@entries)
+ # Then -> expect(@$iframe.text()).toContain("foo")
+
+
describe "language", ->
@@ -9,14 +9,20 @@ describe "app.initializers.TransformsContent", ->
Then -> expect(@sendRequest).toHaveBeenCalledWith({type: "config"}, jasmine.any(Function))
describe "~ after response", ->
- Given -> spyOn($.fn, "ready")
+ Given -> @replacesWords = fakeClass(app.dom, 'ReplacesWords', 'replace')
+ Given -> spyOn(_, "debounce").andCallFake (f) -> f()
+
Given -> @config = dictionary: { entries: 'entries!' }
When -> @sendRequest.mostRecentCall.args[1](@config)
describe "~ on domready", ->
- Given -> @replacesWords = fakeClass(app.dom, 'ReplacesWords', 'replace')
- When -> $.fn.ready.mostRecentCall.args[0]()
+ When -> $(document).trigger('ready')
+ Then -> expect(@replacesWords.replace).toHaveBeenCalledWith(@config.dictionary.entries)
+
+ describe "~ DOMNodeInserted", ->
+ When -> $(document).trigger('DOMNodeInserted')
Then -> expect(@replacesWords.replace).toHaveBeenCalledWith(@config.dictionary.entries)
+ Then -> expect(_.debounce).toHaveBeenCalledWith(jasmine.any(Function), 300)

0 comments on commit 732dc06

Please sign in to comment.