| 3565 | } |
| 3566 | |
| 3567 | void CodeViewEditor::FormatCSSStyle(const QString &property_name, const QString &property_value) |
| 3568 | { |
| 3569 | if (property_name.isEmpty() || property_value.isEmpty()) { |
| 3570 | return; |
| 3571 | } |
| 3572 | |
| 3573 | // Emit a selection changed event, so we can make sure the style buttons are updated |
| 3574 | // to uncheck any buttons check states. |
| 3575 | emit selectionChanged(); |
| 3576 | // Going to assume that the user is allowed to click anywhere within or just after the block |
| 3577 | // Also makes assumptions about being well formed, or else crazy things may happen... |
| 3578 | int pos = textCursor().selectionStart(); |
| 3579 | QString text = toPlainText(); |
| 3580 | // Valid places to apply this modification are either if: |
| 3581 | // (a) the caret is on a line somewhere inside CSS {} parenthesis. |
| 3582 | // (b) the caret is on a line declaring a CSS style |
| 3583 | const QTextBlock block = textCursor().block(); |
| 3584 | int bracket_end = text.indexOf(QChar('}'), pos); |
| 3585 | |
| 3586 | if (bracket_end < 0) { |
| 3587 | return; |
| 3588 | } |
| 3589 | |
| 3590 | int bracket_start = text.lastIndexOf(QChar('{'), pos); |
| 3591 | |
| 3592 | if (bracket_start < 0 || text.lastIndexOf(QChar('}'), pos - 1) > bracket_start) { |
| 3593 | // The previous opening parenthesis we found belongs to another CSS style set |
| 3594 | // Look for another one after the current position on the same line. |
| 3595 | bracket_start = block.text().indexOf('{'); |
| 3596 | |
| 3597 | if (bracket_start >= 0) { |
| 3598 | bracket_start += block.position(); |
| 3599 | } else { |
| 3600 | // Some CSS stylesheets put the {} on their own line following the style name. |
| 3601 | // Look for a non-empty current line followed by a line starting with parenthesis |
| 3602 | const QTextBlock next_block = block.next(); |
| 3603 | |
| 3604 | if (block.text().trimmed().isEmpty() || !next_block.isValid() || |
| 3605 | !next_block.text().trimmed().startsWith(QChar('{'))) { |
| 3606 | return; |
| 3607 | } |
| 3608 | |
| 3609 | bracket_start = next_block.text().indexOf(QChar('{')) + next_block.position(); |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | if (bracket_start > bracket_end) { |
| 3614 | // Sanity check for some really weird bracketing (perhaps invalid css) |
| 3615 | return; |
| 3616 | } |
| 3617 | |
| 3618 | // Now parse the HTML style content |
| 3619 | QList<HTMLStyleInfo::CSSProperty> css_properties = HTMLStyleInfo::getCSSProperties(text, bracket_start + 1, bracket_end); |
| 3620 | // Apply our property value, adding if not present currently, toggling if it is. |
| 3621 | ApplyChangeToProperties(css_properties, property_name, property_value); |
| 3622 | // Figure out the formatting to be applied to these style properties to write prettily |
| 3623 | // preserving any multi-line/single line style the CSS had before we changed things. |
| 3624 | bool is_single_line_format = (block.position() < bracket_start) && (bracket_end <= (block.position() + block.length())); |
no test coverage detected