From 46fe5cffc952b80371f839fce0aa2fc8ded27f50 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 26 Aug 2015 18:21:27 +0100 Subject: Use createElementNS for XML documents and remove XML specific codepaths This implements @gdh1995's idea from #1796. --- lib/clipboard.coffee | 2 +- lib/dom_utils.coffee | 18 ++++++++++++++---- lib/utils.coffee | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/clipboard.coffee b/lib/clipboard.coffee index 2b28df70..26cd2fad 100644 --- a/lib/clipboard.coffee +++ b/lib/clipboard.coffee @@ -1,6 +1,6 @@ Clipboard = _createTextArea: -> - textArea = document.createElement("textarea") + textArea = DomUtils.createElement("textarea") textArea.style.position = "absolute" textArea.style.left = "-100%" textArea diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index d4f5953d..8a3969b1 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -8,12 +8,22 @@ DomUtils = else func() + createElement: (tagName) -> + element = document.createElement tagName + if element.style + # The document namespace provides (X)HTML elements, so we can use them directly. + element + else + # The document namespace doesn't give (X)HTML elements, so we create them with the correct namespace + # manually. + document.createElementNS "http://www.w3.org/1999/xhtml", tagName + # # Adds a list of elements to a page. # Note that adding these nodes all at once (via the parent div) is significantly faster than one-by-one. # addElementList: (els, overlayOptions) -> - parent = document.createElement("div") + parent = DomUtils.createElement("div") parent.id = overlayOptions.id if overlayOptions.id? parent.className = overlayOptions.className if overlayOptions.className? parent.appendChild(el) for el in els @@ -236,7 +246,7 @@ DomUtils = # momentarily flash a rectangular border to give user some visual feedback flashRect: (rect) -> - flashEl = document.createElement("div") + flashEl = DomUtils.createElement("div") flashEl.id = "vimiumFlash" flashEl.className = "vimiumReset" flashEl.style.left = rect.left + window.scrollX + "px" @@ -297,7 +307,7 @@ DomUtils = 'letterSpacing', 'wordSpacing' ] (element, position) -> - div = document.createElement "div" + div = DomUtils.createElement "div" div.id = "vimium-input-textarea-caret-position-mirror-div" document.body.appendChild div @@ -315,7 +325,7 @@ DomUtils = if element.nodeName.toLowerCase() == "input" div.textContent = div.textContent.replace /\s/g, "\u00a0" - span = document.createElement "span" + span = DomUtils.createElement "span" span.textContent = element.value.substring(position) || "." div.appendChild span diff --git a/lib/utils.coffee b/lib/utils.coffee index d4beff03..c0da6fb3 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -21,7 +21,7 @@ Utils = # Creates a single DOM element from :html createElementFromHtml: (html) -> - tmp = document.createElement("div") + tmp = DomUtils.createElement("div") tmp.innerHTML = html tmp.firstChild -- cgit v1.2.3 From 6125394ba844f4082803366ab9956dec13b547e2 Mon Sep 17 00:00:00 2001 From: gdh1995 Date: Fri, 28 Aug 2015 21:08:19 +0800 Subject: fix a bug that Utils using DomUtils remove Utils.createElementFromHtml since it's not in use --- lib/utils.coffee | 6 ------ 1 file changed, 6 deletions(-) (limited to 'lib') diff --git a/lib/utils.coffee b/lib/utils.coffee index c0da6fb3..90469fad 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -19,12 +19,6 @@ Utils = func = obj[components.pop()] func.apply(obj, argArray) - # Creates a single DOM element from :html - createElementFromHtml: (html) -> - tmp = DomUtils.createElement("div") - tmp.innerHTML = html - tmp.firstChild - escapeHtml: (string) -> string.replace(//g, ">") # Generates a unique ID -- cgit v1.2.3 From 034e2515a0a23cc02046bc761adf950232715885 Mon Sep 17 00:00:00 2001 From: gdh1995 Date: Fri, 28 Aug 2015 21:08:57 +0800 Subject: fix a bug that Clipboard fails without DomUtils --- lib/clipboard.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/clipboard.coffee b/lib/clipboard.coffee index 26cd2fad..47fa3cb8 100644 --- a/lib/clipboard.coffee +++ b/lib/clipboard.coffee @@ -1,6 +1,6 @@ Clipboard = _createTextArea: -> - textArea = DomUtils.createElement("textarea") + textArea = document.createElement "textarea" textArea.style.position = "absolute" textArea.style.left = "-100%" textArea -- cgit v1.2.3 From da8653f303015f0780fe3270a2ec8db60316d29e Mon Sep 17 00:00:00 2001 From: gdh1995 Date: Fri, 28 Aug 2015 21:10:04 +0800 Subject: clean code --- lib/dom_utils.coffee | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index 8a3969b1..cd9e0405 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -12,18 +12,20 @@ DomUtils = element = document.createElement tagName if element.style # The document namespace provides (X)HTML elements, so we can use them directly. + @createElement = document.createElement.bind document element else # The document namespace doesn't give (X)HTML elements, so we create them with the correct namespace # manually. - document.createElementNS "http://www.w3.org/1999/xhtml", tagName + @createElement = document.createElementNS.bind document, "http://www.w3.org/1999/xhtml" + @createElement() # # Adds a list of elements to a page. # Note that adding these nodes all at once (via the parent div) is significantly faster than one-by-one. # addElementList: (els, overlayOptions) -> - parent = DomUtils.createElement("div") + parent = @createElement "div" parent.id = overlayOptions.id if overlayOptions.id? parent.className = overlayOptions.className if overlayOptions.className? parent.appendChild(el) for el in els @@ -246,7 +248,7 @@ DomUtils = # momentarily flash a rectangular border to give user some visual feedback flashRect: (rect) -> - flashEl = DomUtils.createElement("div") + flashEl = @createElement "div" flashEl.id = "vimiumFlash" flashEl.className = "vimiumReset" flashEl.style.left = rect.left + window.scrollX + "px" @@ -307,7 +309,7 @@ DomUtils = 'letterSpacing', 'wordSpacing' ] (element, position) -> - div = DomUtils.createElement "div" + div = @createElement "div" div.id = "vimium-input-textarea-caret-position-mirror-div" document.body.appendChild div @@ -325,7 +327,7 @@ DomUtils = if element.nodeName.toLowerCase() == "input" div.textContent = div.textContent.replace /\s/g, "\u00a0" - span = DomUtils.createElement "span" + span = @createElement "span" span.textContent = element.value.substring(position) || "." div.appendChild span -- cgit v1.2.3 From 854a7972c21e5fedfbca6c06e2671d7466369919 Mon Sep 17 00:00:00 2001 From: gdh1995 Date: Fri, 28 Aug 2015 21:51:22 +0800 Subject: fix a typo in PR #1798 --- lib/dom_utils.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index cd9e0405..ef34207e 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -18,7 +18,7 @@ DomUtils = # The document namespace doesn't give (X)HTML elements, so we create them with the correct namespace # manually. @createElement = document.createElementNS.bind document, "http://www.w3.org/1999/xhtml" - @createElement() + @createElement(tagName) # # Adds a list of elements to a page. -- cgit v1.2.3 From 83aee937126727228a58a85a0ba1a8c5cab03010 Mon Sep 17 00:00:00 2001 From: gdh1995 Date: Fri, 28 Aug 2015 22:26:05 +0800 Subject: remove `.bind` imported in da8653f because dom tests fail if bind exists --- lib/dom_utils.coffee | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index ef34207e..47ee6518 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -12,12 +12,13 @@ DomUtils = element = document.createElement tagName if element.style # The document namespace provides (X)HTML elements, so we can use them directly. - @createElement = document.createElement.bind document + @createElement = (tagName) -> document.createElement tagName element else # The document namespace doesn't give (X)HTML elements, so we create them with the correct namespace # manually. - @createElement = document.createElementNS.bind document, "http://www.w3.org/1999/xhtml" + @createElement = (tagName) -> + document.createElementNS document, "http://www.w3.org/1999/xhtml", tagName @createElement(tagName) # -- cgit v1.2.3 From b771f2201c244d7b80bed337e37210faccf954d1 Mon Sep 17 00:00:00 2001 From: gdh1995 Date: Fri, 28 Aug 2015 22:37:00 +0800 Subject: fix a new typo oh my god --- lib/dom_utils.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index 47ee6518..0ea3e93d 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -18,7 +18,7 @@ DomUtils = # The document namespace doesn't give (X)HTML elements, so we create them with the correct namespace # manually. @createElement = (tagName) -> - document.createElementNS document, "http://www.w3.org/1999/xhtml", tagName + document.createElementNS "http://www.w3.org/1999/xhtml", tagName @createElement(tagName) # -- cgit v1.2.3 From 684d14c9ccfbb48f040d238f1564825d11f3551a Mon Sep 17 00:00:00 2001 From: gdh1995 Date: Fri, 28 Aug 2015 22:42:23 +0800 Subject: a better way to detect document types On a XML view page, createElement will create an "Element" object Otherwise, createElement('div') will get an object : HTMLDivElement >> HTMLElement >> Element --- lib/dom_utils.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index 0ea3e93d..ad88deae 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -10,7 +10,7 @@ DomUtils = createElement: (tagName) -> element = document.createElement tagName - if element.style + if element instanceof HTMLElement # The document namespace provides (X)HTML elements, so we can use them directly. @createElement = (tagName) -> document.createElement tagName element -- cgit v1.2.3