Class: Dommy::Range
- Inherits:
-
Object
- Object
- Dommy::Range
- Includes:
- Bridge::Methods
- Defined in:
- lib/dommy/range.rb
Overview
Range — a span between two boundary points in the DOM, used by
text-editing / highlighting / selection logic.
Dommy has no layout, so methods that return pixel rectangles
(getBoundingClientRect, getClientRects) return zeroed values.
All non-layout operations (selectNode, extractContents,
cloneContents, surroundContents, deleteContents, toString,
collapse, compareBoundaryPoints, intersectsNode, containsNode)
work against the actual DOM tree.
Constant Summary collapse
- START_TO_START =
compareBoundaryPoints
howconstants. 0- START_TO_END =
1- END_TO_END =
2- END_TO_START =
3
Instance Attribute Summary collapse
-
#end_container ⇒ Object
readonly
Returns the value of attribute end_container.
-
#end_offset ⇒ Object
readonly
Returns the value of attribute end_offset.
-
#start_container ⇒ Object
readonly
Returns the value of attribute start_container.
-
#start_offset ⇒ Object
readonly
Returns the value of attribute start_offset.
Instance Method Summary collapse
- #__js_call__(method, args) ⇒ Object
-
#__js_get__(key) ⇒ Object
--- JS bridge -------------------------------------------------.
-
#clone_contents ⇒ Object
cloneContents — returns a DocumentFragment with a deep clone of the range contents.
-
#clone_range ⇒ Object
--- Cloning ---------------------------------------------------.
- #collapse(to_start = false) ⇒ Object
- #collapsed? ⇒ Boolean (also: #collapsed)
- #common_ancestor_container ⇒ Object
-
#compare_boundary_points(how, other) ⇒ Object
--- Ordering / containment ------------------------------------.
-
#compare_point(node, offset) ⇒ Object
WHATWG Range.comparePoint(node, offset): -1 if (node, offset) is before the range, 0 if inside, 1 if after.
- #contains_node(node, partial = false) ⇒ Object
-
#create_contextual_fragment(html) ⇒ Object
createContextualFragment(html)— parse an HTML string into a DocumentFragment using the range's start node as the parsing context (DOM Parsing & Serialization). -
#delete_contents ⇒ Object
WHATWG Range.deleteContents.
-
#deletion_collapse_point(sc, so, ec, eo) ⇒ Object
The (node, offset) the range collapses to after deletion (WHATWG step 5): the start boundary if it contains the end, else the start's highest ancestor that doesn't contain the end, positioned just after it.
-
#extract_contents ⇒ Object
extractContents — like cloneContents but also removes the extracted nodes from the document.
-
#get_bounding_client_rect ⇒ Object
--- Layout stubs ---------------------------------------------- No layout engine; return zeroed rects so callers don't crash.
- #get_client_rects ⇒ Object
- #inclusive_ancestor?(maybe_ancestor, node) ⇒ Boolean
-
#initialize(document) ⇒ Range
constructor
A new instance of Range.
- #insert_node(node) ⇒ Object
- #intersects_node(node) ⇒ Object
-
#is_point_in_range(node, offset) ⇒ Object
WHATWG Range.isPointInRange(node, offset): true iff the point lies within the range (inclusive).
-
#node_fully_contained?(node) ⇒ Boolean
A node is contained in the range when its start position is at or after the range start and its end position is at or before the range end.
-
#nodes_to_remove ⇒ Object
Top-level nodes fully contained in the range (their parent isn't), removed whole.
- #select_node(node) ⇒ Object
- #select_node_contents(node) ⇒ Object
- #set_end(node, offset) ⇒ Object
- #set_end_after(node) ⇒ Object
- #set_end_before(node) ⇒ Object
-
#set_start(node, offset) ⇒ Object
--- Boundary setters --------------------------------------------.
- #set_start_after(node) ⇒ Object
- #set_start_before(node) ⇒ Object
-
#surround_contents(new_parent) ⇒ Object
surroundContents(newParent) — wraps the range contents in newParent (which must be an element).
-
#to_s ⇒ Object
--- Content extraction ----------------------------------------.
Methods included from Bridge::Methods
Constructor Details
#initialize(document) ⇒ Range
Returns a new instance of Range.
24 25 26 27 28 29 30 31 |
# File 'lib/dommy/range.rb', line 24 def initialize(document) @document = document # Default: collapsed at start of the document @start_container = document @start_offset = 0 @end_container = document @end_offset = 0 end |
Instance Attribute Details
#end_container ⇒ Object (readonly)
Returns the value of attribute end_container.
22 23 24 |
# File 'lib/dommy/range.rb', line 22 def end_container @end_container end |
#end_offset ⇒ Object (readonly)
Returns the value of attribute end_offset.
22 23 24 |
# File 'lib/dommy/range.rb', line 22 def end_offset @end_offset end |
#start_container ⇒ Object (readonly)
Returns the value of attribute start_container.
22 23 24 |
# File 'lib/dommy/range.rb', line 22 def start_container @start_container end |
#start_offset ⇒ Object (readonly)
Returns the value of attribute start_offset.
22 23 24 |
# File 'lib/dommy/range.rb', line 22 def start_offset @start_offset end |
Instance Method Details
#__js_call__(method, args) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/dommy/range.rb', line 406 def __js_call__(method, args) case method when "setStart" set_start(args[0], args[1]) when "setEnd" set_end(args[0], args[1]) when "setStartBefore" set_start_before(args[0]) when "setStartAfter" set_start_after(args[0]) when "setEndBefore" set_end_before(args[0]) when "setEndAfter" set_end_after(args[0]) when "collapse" collapse(args[0]) when "selectNode" select_node(args[0]) when "selectNodeContents" select_node_contents(args[0]) when "createContextualFragment" # WebIDL required argument: a bare call (0 args) throws TypeError. raise Bridge::TypeError, "createContextualFragment requires 1 argument, but only 0 present" if args.empty? create_contextual_fragment(args[0]) when "toString" to_s when "cloneContents" clone_contents when "extractContents" extract_contents when "deleteContents" delete_contents when "surroundContents" surround_contents(args[0]) when "insertNode" insert_node(args[0]) when "compareBoundaryPoints" compare_boundary_points(args[0], args[1]) when "intersectsNode" intersects_node(args[0]) when "comparePoint" compare_point(args[0], args[1]) when "isPointInRange" is_point_in_range(args[0], args[1]) when "containsNode" contains_node(args[0], args[1]) when "cloneRange" clone_range when "detach" nil when "getBoundingClientRect" get_bounding_client_rect when "getClientRects" get_client_rects end end |
#__js_get__(key) ⇒ Object
--- JS bridge -------------------------------------------------
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/dommy/range.rb', line 372 def __js_get__(key) case key when "startContainer" @start_container when "startOffset" @start_offset when "endContainer" @end_container when "endOffset" @end_offset when "collapsed" collapsed? when "commonAncestorContainer" common_ancestor_container when "START_TO_START" START_TO_START when "START_TO_END" START_TO_END when "END_TO_END" END_TO_END when "END_TO_START" END_TO_START else Bridge::ABSENT end end |
#clone_contents ⇒ Object
cloneContents — returns a DocumentFragment with a deep clone of the range contents. Range is left unchanged.
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/dommy/range.rb', line 136 def clone_contents fragment = @document.create_document_fragment contents = collect_nodes_in_range contents.each do |node| clone = clone_wrapped(node) fragment.append_child(clone) if clone end fragment end |
#clone_range ⇒ Object
--- Cloning ---------------------------------------------------
352 353 354 355 356 357 |
# File 'lib/dommy/range.rb', line 352 def clone_range r = Range.new(@document) r.set_start(@start_container, @start_offset) r.set_end(@end_container, @end_offset) r end |
#collapse(to_start = false) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dommy/range.rb', line 84 def collapse(to_start = false) if to_start @end_container = @start_container @end_offset = @start_offset else @start_container = @end_container @start_offset = @end_offset end nil end |
#collapsed? ⇒ Boolean Also known as: collapsed
33 34 35 |
# File 'lib/dommy/range.rb', line 33 def collapsed? @start_container.equal?(@end_container) && @start_offset == @end_offset end |
#common_ancestor_container ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/dommy/range.rb', line 39 def common_ancestor_container # Find the lowest (deepest) common ancestor of start_container # and end_container. Walk from start_container up and return the # first node also present in end_container's ancestor chain. starts = ancestor_chain(@start_container) ends_set = ancestor_chain(@end_container) starts.find { |a| ends_set.any? { |e| e.equal?(a) } } || @document end |
#compare_boundary_points(how, other) ⇒ Object
--- Ordering / containment ------------------------------------
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/dommy/range.rb', line 272 def compare_boundary_points(how, other) # `how` is a WebIDL `unsigned short`: coerce first (NaN/±0/±Infinity → 0, # otherwise truncate toward zero and take modulo 2^16), then require one of # the four named constants, else NotSupportedError. how = to_unsigned_short(how) unless [START_TO_START, START_TO_END, END_TO_END, END_TO_START].include?(how) raise DOMException::NotSupportedError, "invalid comparison type: #{how}" end # The two ranges must share a root. unless same_root?(other.start_container) raise DOMException::WrongDocumentError, "the two Ranges are in different trees" end case how when START_TO_START compare_points(@start_container, @start_offset, other.start_container, other.start_offset) when START_TO_END compare_points(@end_container, @end_offset, other.start_container, other.start_offset) when END_TO_END compare_points(@end_container, @end_offset, other.end_container, other.end_offset) when END_TO_START compare_points(@start_container, @start_offset, other.end_container, other.end_offset) end end |
#compare_point(node, offset) ⇒ Object
WHATWG Range.comparePoint(node, offset): -1 if (node, offset) is before the range, 0 if inside, 1 if after. offset is a WebIDL unsigned long (so -1 wraps to a huge value > length → IndexSizeError).
314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/dommy/range.rb', line 314 def compare_point(node, offset) raise Bridge::TypeError, "argument is not a Node" unless node.is_a?(Dommy::Node) off = unsigned_long(offset) raise DOMException::WrongDocumentError, "node is in a different tree" unless same_root?(node) raise DOMException::InvalidNodeTypeError, "node is a doctype" if doctype?(node) raise DOMException::IndexSizeError, "offset is greater than node length" if off > length_of(node) return -1 if compare_points(node, off, @start_container, @start_offset) < 0 return 1 if compare_points(node, off, @end_container, @end_offset) > 0 0 end |
#contains_node(node, partial = false) ⇒ Object
341 342 343 344 345 346 347 348 |
# File 'lib/dommy/range.rb', line 341 def contains_node(node, partial = false) if partial intersects_node(node) else # node must be wholly inside the range !before?(node) && !after?(node) && fully_inside?(node) end end |
#create_contextual_fragment(html) ⇒ Object
createContextualFragment(html) — parse an HTML string into a
DocumentFragment using the range's start node as the parsing context
(DOM Parsing & Serialization). Frameworks use it to turn an HTML string
into nodes (Nuxt's DOM hydration / <slot> helpers call it via a Range).
118 119 120 121 122 123 124 125 126 |
# File 'lib/dommy/range.rb', line 118 def create_contextual_fragment(html) context = @document.create_element(contextual_local_name) # WebIDL DOMString coercion: JS null stringifies to "null" (no # [LegacyNullToEmptyString] here), the UNDEFINED sentinel to "undefined". context.inner_html = html.nil? ? "null" : html.to_s # fragment-parses in the context element fragment = @document.create_document_fragment context.child_nodes.to_a.each { |child| fragment.append_child(child) } fragment end |
#delete_contents ⇒ Object
WHATWG Range.deleteContents. Removes the fully-contained nodes (childList
records) and trims the partially-contained boundary CharacterData nodes
(characterData records via data=), rather than removing boundary text
nodes whole — so a deletion like "abc"[1]…"def"[1] leaves "a"…"ef".
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/dommy/range.rb', line 159 def delete_contents return nil if collapsed? sc = @start_container so = @start_offset ec = @end_container eo = @end_offset # Both boundaries in the same text node: just delete the substring. if sc.equal?(ec) && text_node?(sc) d = sc.data.to_s sc.data = d[0, so].to_s + (d[eo..] || "") return nil end to_remove = nodes_to_remove new_node, new_offset = deletion_collapse_point(sc, so, ec, eo) # Trim the start boundary text node's tail, remove the contained nodes, # then trim the end boundary text node's head (order matters for records). sc.data = sc.data.to_s[0, so].to_s if text_node?(sc) to_remove.each do |node| if node.respond_to?(:__dommy_backend_node__) @document.remove_node_with_notify(node.__dommy_backend_node__) elsif node.respond_to?(:remove) node.remove end end ec.data = (ec.data.to_s[eo..] || "") if text_node?(ec) @start_container = @end_container = new_node @start_offset = @end_offset = new_offset nil end |
#deletion_collapse_point(sc, so, ec, eo) ⇒ Object
The (node, offset) the range collapses to after deletion (WHATWG step 5): the start boundary if it contains the end, else the start's highest ancestor that doesn't contain the end, positioned just after it.
217 218 219 220 221 222 223 224 |
# File 'lib/dommy/range.rb', line 217 def deletion_collapse_point(sc, so, ec, eo) return [sc, so] if inclusive_ancestor?(sc, ec) ref = sc ref = parent_of(ref) while (p = parent_of(ref)) && !inclusive_ancestor?(p, ec) parent = parent_of(ref) [parent, child_index_of(parent, ref) + 1] end |
#extract_contents ⇒ Object
extractContents — like cloneContents but also removes the extracted nodes from the document.
149 150 151 152 153 |
# File 'lib/dommy/range.rb', line 149 def extract_contents fragment = clone_contents delete_contents fragment end |
#get_bounding_client_rect ⇒ Object
--- Layout stubs ---------------------------------------------- No layout engine; return zeroed rects so callers don't crash.
362 363 364 |
# File 'lib/dommy/range.rb', line 362 def get_bounding_client_rect DOMRect.new(x: 0, y: 0, width: 0, height: 0) end |
#get_client_rects ⇒ Object
366 367 368 |
# File 'lib/dommy/range.rb', line 366 def get_client_rects [] end |
#inclusive_ancestor?(maybe_ancestor, node) ⇒ Boolean
226 227 228 229 230 231 232 233 234 |
# File 'lib/dommy/range.rb', line 226 def inclusive_ancestor?(maybe_ancestor, node) current = node while current return true if current.equal?(maybe_ancestor) current = parent_of(current) end false end |
#insert_node(node) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/dommy/range.rb', line 247 def insert_node(node) # Insert at the range start. For a text container, split it at the offset # (when interior) and insert before the split-off tail — matching the spec, # which produces a childList record for the split plus one for the insert. sc = @start_container if text_node?(sc) parent = parent_of(sc) idx = child_index_of(parent, sc) if @start_offset.zero? insert_into_parent_at(parent, idx, node) elsif @start_offset >= length_of(sc) insert_into_parent_at(parent, idx + 1, node) else tail = sc.split_text(@start_offset) parent.insert_before(node, tail) end else insert_into_parent_at(sc, @start_offset, node) end nil end |
#intersects_node(node) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/dommy/range.rb', line 297 def intersects_node(node) # WHATWG Range.intersectsNode: a node in a different tree never intersects; # a node with no parent (a tree root, e.g. the document) always does. return false unless same_root?(node) return true if parent_of(node).nil? # Otherwise range and node intersect iff node's position relative to the # range is not entirely before or entirely after it. return false if before?(node) return false if after?(node) true end |
#is_point_in_range(node, offset) ⇒ Object
WHATWG Range.isPointInRange(node, offset): true iff the point lies within the range (inclusive). A different root returns false (no throw).
330 331 332 333 334 335 336 337 338 339 |
# File 'lib/dommy/range.rb', line 330 def is_point_in_range(node, offset) return false unless same_root?(node) off = unsigned_long(offset) raise DOMException::InvalidNodeTypeError, "node is a doctype" if doctype?(node) raise DOMException::IndexSizeError, "offset is greater than node length" if off > length_of(node) compare_points(node, off, @start_container, @start_offset) >= 0 && compare_points(node, off, @end_container, @end_offset) <= 0 end |
#node_fully_contained?(node) ⇒ Boolean
A node is contained in the range when its start position is at or after the range start and its end position is at or before the range end.
205 206 207 208 209 210 211 212 |
# File 'lib/dommy/range.rb', line 205 def node_fully_contained?(node) parent = parent_of(node) return false unless parent idx = child_index_of(parent, node) compare_points(parent, idx, @start_container, @start_offset) >= 0 && compare_points(parent, idx + 1, @end_container, @end_offset) <= 0 end |
#nodes_to_remove ⇒ Object
Top-level nodes fully contained in the range (their parent isn't), removed whole. Deeper contained nodes go with their removed ancestor.
196 197 198 199 200 201 |
# File 'lib/dommy/range.rb', line 196 def nodes_to_remove ancestor = common_ancestor_container return [] unless ancestor.respond_to?(:child_nodes) ancestor.child_nodes.to_a.select { |child| node_fully_contained?(child) } end |
#select_node(node) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/dommy/range.rb', line 96 def select_node(node) parent = parent_of(node) idx = child_index_of(parent, node) @start_container = parent @start_offset = idx @end_container = parent @end_offset = idx + 1 nil end |
#select_node_contents(node) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/dommy/range.rb', line 106 def select_node_contents(node) @start_container = node @start_offset = 0 @end_container = node @end_offset = length_of(node) nil end |
#set_end(node, offset) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/dommy/range.rb', line 57 def set_end(node, offset) @end_container = node @end_offset = offset.to_i collapse_to_end if compare_points(@start_container, @start_offset, @end_container, @end_offset) > 0 nil end |
#set_end_after(node) ⇒ Object
79 80 81 82 |
# File 'lib/dommy/range.rb', line 79 def set_end_after(node) parent = parent_of(node) set_end(parent, child_index_of(parent, node) + 1) end |
#set_end_before(node) ⇒ Object
74 75 76 77 |
# File 'lib/dommy/range.rb', line 74 def set_end_before(node) parent = parent_of(node) set_end(parent, child_index_of(parent, node)) end |
#set_start(node, offset) ⇒ Object
--- Boundary setters --------------------------------------------
50 51 52 53 54 55 |
# File 'lib/dommy/range.rb', line 50 def set_start(node, offset) @start_container = node @start_offset = offset.to_i collapse_to_start if compare_points(@start_container, @start_offset, @end_container, @end_offset) > 0 nil end |
#set_start_after(node) ⇒ Object
69 70 71 72 |
# File 'lib/dommy/range.rb', line 69 def set_start_after(node) parent = parent_of(node) set_start(parent, child_index_of(parent, node) + 1) end |
#set_start_before(node) ⇒ Object
64 65 66 67 |
# File 'lib/dommy/range.rb', line 64 def set_start_before(node) parent = parent_of(node) set_start(parent, child_index_of(parent, node)) end |
#surround_contents(new_parent) ⇒ Object
surroundContents(newParent) — wraps the range contents in newParent (which must be an element).
238 239 240 241 242 243 244 245 |
# File 'lib/dommy/range.rb', line 238 def surround_contents(new_parent) contents = extract_contents new_parent.append_child(contents) # Insert new_parent at the (now-collapsed) range start. insert_node(new_parent) select_node(new_parent) nil end |
#to_s ⇒ Object
--- Content extraction ----------------------------------------
130 131 132 |
# File 'lib/dommy/range.rb', line 130 def to_s Internal::RangeTextSerializer.new(self).serialize end |