Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: Source/core/css/CSSComputedStyleDeclaration.cpp

Issue 13871003: Fixing getComputedStyle to return pixel values for left / right / top / bottom (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fixing a const issue. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « LayoutTests/fast/css/hover-affects-child.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004 Zack Rusin <zack@kde.org> 2 * Copyright (C) 2004 Zack Rusin <zack@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> 4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
5 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com> 5 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
6 * Copyright (C) 2011 Sencha, Inc. All rights reserved. 6 * Copyright (C) 2011 Sencha, Inc. All rights reserved.
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public 9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 ASSERT_UNUSED(propertyID, propertyID == CSSPropertyBackgroundPosition || propertyID == CSSPropertyWebkitMaskPosition); 616 ASSERT_UNUSED(propertyID, propertyID == CSSPropertyBackgroundPosition || propertyID == CSSPropertyWebkitMaskPosition);
617 positionList->append(cssValuePool().createValue(layer->backgroundXOrigin ())); 617 positionList->append(cssValuePool().createValue(layer->backgroundXOrigin ()));
618 } 618 }
619 positionList->append(zoomAdjustedPixelValueForLength(layer->xPosition(), sty le)); 619 positionList->append(zoomAdjustedPixelValueForLength(layer->xPosition(), sty le));
620 if (layer->isBackgroundOriginSet()) { 620 if (layer->isBackgroundOriginSet()) {
621 ASSERT(propertyID == CSSPropertyBackgroundPosition || propertyID == CSSP ropertyWebkitMaskPosition); 621 ASSERT(propertyID == CSSPropertyBackgroundPosition || propertyID == CSSP ropertyWebkitMaskPosition);
622 positionList->append(cssValuePool().createValue(layer->backgroundYOrigin ())); 622 positionList->append(cssValuePool().createValue(layer->backgroundYOrigin ()));
623 } 623 }
624 positionList->append(zoomAdjustedPixelValueForLength(layer->yPosition(), sty le)); 624 positionList->append(zoomAdjustedPixelValueForLength(layer->yPosition(), sty le));
625 return positionList.release(); 625 return positionList.release();
626
626 } 627 }
627 628
628 static PassRefPtr<CSSValue> getPositionOffsetValue(RenderStyle* style, CSSProper tyID propertyID, const RenderObject* renderer, RenderView* renderView) 629 static Length getOffsetComputedStyle(RenderStyle* style, CSSPropertyID propertyI D)
630 {
631 // If specified as a length, the corresponding absolute length; if specified as
632 // a percentage, the specified value; otherwise, 'auto'. Hence, we can just
633 // return the value in the style.
634 //
635 // See http://www.w3.org/TR/CSS21/cascade.html#computed-value
636 switch (propertyID) {
637 case CSSPropertyLeft:
638 return style->left();
639 case CSSPropertyRight:
640 return style->right();
641 case CSSPropertyTop:
642 return style->top();
643 case CSSPropertyBottom:
644 return style->bottom();
645 default:
646 ASSERT_NOT_REACHED();
647 }
648
649 return Length(0);
650 }
651
652 static LayoutUnit getOffsetUsedStyleRelative(RenderBox* box, RenderStyle* style, CSSPropertyID propertyID)
653 {
654 // For relatively positioned boxes, the offset is with respect to the top ed ges
655 // of the box itself. This ties together top/bottom and left/right to be
656 // opposites of each other.
657 //
658 // See http://www.w3.org/TR/CSS2/visuren.html#relative-positioning
659 //
660 // Specifically;
661 // Since boxes are not split or stretched as a result of 'left' or
662 // 'right', the used values are always: left = -right.
663 // and
664 // Since boxes are not split or stretched as a result of 'top' or
665 // 'bottom', the used values are always: top = -bottom.
666 switch (propertyID) {
667 case CSSPropertyTop:
668 return box->relativePositionOffset().height();
669 case CSSPropertyBottom:
670 return -(box->relativePositionOffset().height());
671 case CSSPropertyLeft:
672 return box->relativePositionOffset().width();
673 case CSSPropertyRight:
674 return -(box->relativePositionOffset().width());
675 default:
676 ASSERT_NOT_REACHED();
677 }
678
679 return 0;
680 }
681
682 static LayoutUnit getOffsetUsedStyleAbsolute(RenderBlock* container, RenderBox* box, RenderStyle* style, CSSPropertyID propertyID)
683 {
684 // For absoultely positioned boxes, the offset is how far an box's margin
685 // edge is offset below the edge of the box's containing block.
686 // See http://www.w3.org/TR/CSS2/visuren.html#position-props
687
688 // Margins are included in Webkit's offsetTop/offsetLeft so we need to
689 // remove them here.
690 switch (propertyID) {
691 case CSSPropertyTop:
692 return box->offsetTop() - box->marginTop();
693 case CSSPropertyBottom:
694 return container->clientHeight() - (box->offsetTop() + box->offsetHeight ()) - box->marginBottom();
695 case CSSPropertyLeft:
696 return box->offsetLeft() - box->marginLeft();
697 case CSSPropertyRight:
698 return container->clientWidth() - (box->offsetLeft() + box->offsetWidth( )) - box->marginRight();
699 default:
700 ASSERT_NOT_REACHED();
701 }
702
703 return 0;
704 }
705
706 static PassRefPtr<CSSValue> getPositionOffsetValue(RenderStyle* style, CSSProper tyID propertyID, RenderObject* renderer, RenderView* renderView)
629 { 707 {
630 if (!style) 708 if (!style)
631 return 0; 709 return 0;
632 710
633 Length l; 711 // If the element is not displayed; return the "computed value".
634 switch (propertyID) { 712 if (!renderView || !renderer || !renderer->isBox()) {
635 case CSSPropertyLeft: 713 return zoomAdjustedPixelValueForLength(getOffsetComputedStyle(style, pro pertyID), style);
636 l = style->left(); 714
637 break; 715 // We should return the "used value".
638 case CSSPropertyRight: 716 } else {
639 l = style->right(); 717 LayoutUnit length = 0;
640 break; 718 RenderBox* box = toRenderBox(renderer);
641 case CSSPropertyTop: 719 RenderBlock* containingBlock = renderer->containingBlock();
642 l = style->top(); 720 if (box->isRelPositioned() || !containingBlock) {
643 break; 721 length = getOffsetUsedStyleRelative(box, style, propertyID);
644 case CSSPropertyBottom: 722 } else {
645 l = style->bottom(); 723 length = getOffsetUsedStyleAbsolute(containingBlock, box, style, pro pertyID);
646 break; 724 }
647 default: 725 return zoomAdjustedPixelValue(length, style);
648 return 0;
649 } 726 }
650
651 if (l.isPercent() && renderer && renderer->isBox()) {
652 LayoutUnit containingBlockSize = (propertyID == CSSPropertyLeft || prope rtyID == CSSPropertyRight) ?
653 toRenderBox(renderer)->containingBlockLogicalWidthForContent() :
654 toRenderBox(renderer)->containingBlockLogicalHeightForContent(Exclud eMarginBorderPadding);
655 return zoomAdjustedPixelValue(valueForLength(l, containingBlockSize, 0), style);
656 } if (l.isViewportPercentage())
657 return zoomAdjustedPixelValue(valueForLength(l, 0, renderView), style);
658 if (l.isAuto()) {
659 // FIXME: It's not enough to simply return "auto" values for one offset if the other side is defined.
660 // In other words if left is auto and right is not auto, then left's com puted value is negative right().
661 // So we should get the opposite length unit and see if it is auto.
662 return cssValuePool().createValue(l);
663 }
664
665 return zoomAdjustedPixelValueForLength(l, style);
666 } 727 }
667 728
668 PassRefPtr<CSSPrimitiveValue> CSSComputedStyleDeclaration::currentColorOrValidCo lor(RenderStyle* style, const Color& color) const 729 PassRefPtr<CSSPrimitiveValue> CSSComputedStyleDeclaration::currentColorOrValidCo lor(RenderStyle* style, const Color& color) const
669 { 730 {
670 // This function does NOT look at visited information, so that computed styl e doesn't expose that. 731 // This function does NOT look at visited information, so that computed styl e doesn't expose that.
671 if (!color.isValid()) 732 if (!color.isValid())
672 return cssValuePool().createColorValue(style->color().rgb()); 733 return cssValuePool().createColorValue(style->color().rgb());
673 return cssValuePool().createColorValue(color.rgb()); 734 return cssValuePool().createColorValue(color.rgb());
674 } 735 }
675 736
(...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after
1501 { 1562 {
1502 if (!m_node) 1563 if (!m_node)
1503 return 0; 1564 return 0;
1504 if (m_node->isElementNode()) { 1565 if (m_node->isElementNode()) {
1505 if (PseudoElement* element = toElement(m_node.get())->pseudoElement(m_ps eudoElementSpecifier)) 1566 if (PseudoElement* element = toElement(m_node.get())->pseudoElement(m_ps eudoElementSpecifier))
1506 return element; 1567 return element;
1507 } 1568 }
1508 return m_node.get(); 1569 return m_node.get();
1509 } 1570 }
1510 1571
1572 // In CSS 2.1 the returned object should actually contain the "used values"
1573 // rather then the "computed values" (despite the name saying otherwise).
1574 //
1575 // See;
1576 // http://www.w3.org/TR/CSS21/cascade.html#used-value
1577 // http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration
1578 // https://developer.mozilla.org/en-US/docs/DOM/window.getComputedStyle
1511 PassRefPtr<CSSValue> CSSComputedStyleDeclaration::getPropertyCSSValue(CSSPropert yID propertyID, EUpdateLayout updateLayout) const 1579 PassRefPtr<CSSValue> CSSComputedStyleDeclaration::getPropertyCSSValue(CSSPropert yID propertyID, EUpdateLayout updateLayout) const
1512 { 1580 {
1513 Node* styledNode = this->styledNode(); 1581 Node* styledNode = this->styledNode();
1514 if (!styledNode) 1582 if (!styledNode)
1515 return 0; 1583 return 0;
1516 1584
1517 if (updateLayout) { 1585 if (updateLayout) {
1518 Document* document = styledNode->document(); 1586 Document* document = styledNode->document();
1519 // FIXME: Some of these cases could be narrowed down or optimized better . 1587 // FIXME: Some of these cases could be narrowed down or optimized better .
1520 bool forceFullLayout = isLayoutDependentProperty(propertyID) 1588 bool forceFullLayout = isLayoutDependentProperty(propertyID)
(...skipping 1469 matching lines...) Expand 10 before | Expand all | Expand 10 after
2990 static const CSSPropertyID propertiesAfterSlashSeperator[3] = { CSSPropertyB ackgroundSize, CSSPropertyBackgroundOrigin, 3058 static const CSSPropertyID propertiesAfterSlashSeperator[3] = { CSSPropertyB ackgroundSize, CSSPropertyBackgroundOrigin,
2991 CSSPropertyB ackgroundClip }; 3059 CSSPropertyB ackgroundClip };
2992 3060
2993 RefPtr<CSSValueList> list = CSSValueList::createSlashSeparated(); 3061 RefPtr<CSSValueList> list = CSSValueList::createSlashSeparated();
2994 list->append(getCSSPropertyValuesForShorthandProperties(StylePropertyShortha nd(propertiesBeforeSlashSeperator, WTF_ARRAY_LENGTH(propertiesBeforeSlashSeperat or)))); 3062 list->append(getCSSPropertyValuesForShorthandProperties(StylePropertyShortha nd(propertiesBeforeSlashSeperator, WTF_ARRAY_LENGTH(propertiesBeforeSlashSeperat or))));
2995 list->append(getCSSPropertyValuesForShorthandProperties(StylePropertyShortha nd(propertiesAfterSlashSeperator, WTF_ARRAY_LENGTH(propertiesAfterSlashSeperator )))); 3063 list->append(getCSSPropertyValuesForShorthandProperties(StylePropertyShortha nd(propertiesAfterSlashSeperator, WTF_ARRAY_LENGTH(propertiesAfterSlashSeperator ))));
2996 return list.release(); 3064 return list.release();
2997 } 3065 }
2998 3066
2999 } // namespace WebCore 3067 } // namespace WebCore
OLDNEW
« no previous file with comments | « LayoutTests/fast/css/hover-affects-child.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698