aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dom_utils.coffee
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dom_utils.coffee')
-rw-r--r--lib/dom_utils.coffee29
1 files changed, 24 insertions, 5 deletions
diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee
index 9658df2b..ee7d415f 100644
--- a/lib/dom_utils.coffee
+++ b/lib/dom_utils.coffee
@@ -8,12 +8,25 @@ DomUtils =
else
func()
+ createElement: (tagName) ->
+ element = document.createElement tagName
+ if element instanceof HTMLElement
+ # The document namespace provides (X)HTML elements, so we can use them directly.
+ @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 = (tagName) ->
+ document.createElementNS "http://www.w3.org/1999/xhtml", tagName
+ @createElement(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 = @createElement "div"
parent.id = overlayOptions.id if overlayOptions.id?
parent.className = overlayOptions.className if overlayOptions.className?
parent.appendChild(el) for el in els
@@ -82,7 +95,7 @@ DomUtils =
# NOTE(mrmr1993): This ignores floated/absolutely positioned descendants nested within inline
# children.
continue if (computedStyle.getPropertyValue("float") == "none" and
- computedStyle.getPropertyValue("position") != "absolute" and
+ not (computedStyle.getPropertyValue("position") in ["absolute", "fixed"]) and
not (clientRect.height == 0 and isInlineZeroHeight() and
0 == computedStyle.getPropertyValue("display").indexOf "inline"))
childClientRect = @getVisibleClientRect child, true
@@ -236,7 +249,7 @@ DomUtils =
# momentarily flash a rectangular border to give user some visual feedback
flashRect: (rect) ->
- flashEl = document.createElement("div")
+ flashEl = @createElement "div"
flashEl.id = "vimiumFlash"
flashEl.className = "vimiumReset"
flashEl.style.left = rect.left + window.scrollX + "px"
@@ -297,7 +310,7 @@ DomUtils =
'letterSpacing', 'wordSpacing' ]
(element, position) ->
- div = document.createElement "div"
+ div = @createElement "div"
div.id = "vimium-input-textarea-caret-position-mirror-div"
document.body.appendChild div
@@ -315,7 +328,7 @@ DomUtils =
if element.nodeName.toLowerCase() == "input"
div.textContent = div.textContent.replace /\s/g, "\u00a0"
- span = document.createElement "span"
+ span = @createElement "span"
span.textContent = element.value.substring(position) || "."
div.appendChild span
@@ -357,5 +370,11 @@ DomUtils =
text
texts.join " "
+ # Get the element in the DOM hierachy that contains `element`.
+ # If the element is rendered in a shadow DOM via a <content> element, the <content> element will be
+ # returned, so the shadow DOM is traversed rather than passed over.
+ getContainingElement: (element) ->
+ element.getDestinationInsertionPoints()[0] or element.parentElement
+
root = exports ? window
root.DomUtils = DomUtils