| 373 | } |
| 374 | |
| 375 | void Selection::update() |
| 376 | { |
| 377 | const auto addElement = [this](const Selection::Element &element, |
| 378 | const Selection::Element &end = {}) { |
| 379 | litehtml::tstring elemText; |
| 380 | element.element->get_text(elemText); |
| 381 | const QString textStr = QString::fromStdString(elemText); |
| 382 | if (!textStr.isEmpty()) { |
| 383 | QRect rect = toQRect(element.element->get_placement()).adjusted(-1, -1, 1, 1); |
| 384 | if (element.index < 0) { // fully selected |
| 385 | text += textStr; |
| 386 | } else if (end.element) { // select from element "to end" |
| 387 | if (element.element == end.element) { |
| 388 | // end.index is guaranteed to be >= element.index by caller, same for x |
| 389 | text += textStr.mid(element.index, end.index - element.index); |
| 390 | const int left = rect.left(); |
| 391 | rect.setLeft(left + element.x); |
| 392 | rect.setRight(left + end.x); |
| 393 | } else { |
| 394 | text += textStr.mid(element.index); |
| 395 | rect.setLeft(rect.left() + element.x); |
| 396 | } |
| 397 | } else { // select from start of element |
| 398 | text += textStr.left(element.index); |
| 399 | rect.setRight(rect.left() + element.x); |
| 400 | } |
| 401 | selection.append(rect); |
| 402 | } |
| 403 | }; |
| 404 | |
| 405 | if (startElem.element && endElem.element) { |
| 406 | // Edge cases: |
| 407 | // start and end elements could be reversed or children of each other |
| 408 | Selection::Element start; |
| 409 | Selection::Element end; |
| 410 | std::tie(start, end) = getStartAndEnd(startElem, endElem); |
| 411 | |
| 412 | selection.clear(); |
| 413 | text.clear(); |
| 414 | |
| 415 | // Treats start element as a leaf even if it isn't, because it already contains all its |
| 416 | // children |
| 417 | addElement(start, end); |
| 418 | if (start.element != end.element) { |
| 419 | litehtml::element::ptr current = start.element; |
| 420 | do { |
| 421 | current = nextLeaf(current, end.element); |
| 422 | if (current == end.element) |
| 423 | addElement(end); |
| 424 | else |
| 425 | addElement({current, -1, -1}); |
| 426 | } while (current != end.element); |
| 427 | } |
| 428 | } else { |
| 429 | selection = {}; |
| 430 | text.clear(); |
| 431 | } |
| 432 | QClipboard *cb = QGuiApplication::clipboard(); |
no test coverage detected