Commit 67f6a555 authored by Luna Lu's avatar Luna Lu Committed by Commit Bot

Move the common parsing logic for intrinsicSize attribute to a shared namespace

The logic for parsing intrinsicSize attribute is shared among
HTML{Image,Video}Element and SVGImageElement. Moving it to
blink/core/html/media/media_element_parser_helpers to avoidredundant
code.

Bug: 874629
Change-Id: I20e1dc6b626eb86e8919c621b1c98401eaa9d201
Reviewed-on: https://chromium-review.googlesource.com/1217163Reviewed-by: default avatarSteve Kobes <skobes@chromium.org>
Commit-Queue: Luna Lu <loonybear@chromium.org>
Cr-Commit-Position: refs/heads/master@{#590008}
parent 7f63b1f8
...@@ -489,6 +489,8 @@ blink_core_sources("html") { ...@@ -489,6 +489,8 @@ blink_core_sources("html") {
"media/media_custom_controls_fullscreen_detector.h", "media/media_custom_controls_fullscreen_detector.h",
"media/media_document.cc", "media/media_document.cc",
"media/media_document.h", "media/media_document.h",
"media/media_element_parser_helpers.cc",
"media/media_element_parser_helpers.h",
"media/media_error.h", "media/media_error.h",
"media/media_fragment_uri_parser.cc", "media/media_fragment_uri_parser.cc",
"media/media_fragment_uri_parser.h", "media/media_fragment_uri_parser.h",
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include "third_party/blink/renderer/core/html/html_image_fallback_helper.h" #include "third_party/blink/renderer/core/html/html_image_fallback_helper.h"
#include "third_party/blink/renderer/core/html/html_picture_element.h" #include "third_party/blink/renderer/core/html/html_picture_element.h"
#include "third_party/blink/renderer/core/html/html_source_element.h" #include "third_party/blink/renderer/core/html/html_source_element.h"
#include "third_party/blink/renderer/core/html/media/media_element_parser_helpers.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h" #include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h"
#include "third_party/blink/renderer/core/html/parser/html_srcset_parser.h" #include "third_party/blink/renderer/core/html/parser/html_srcset_parser.h"
#include "third_party/blink/renderer/core/html_names.h" #include "third_party/blink/renderer/core/html_names.h"
...@@ -292,7 +293,18 @@ void HTMLImageElement::ParseAttribute( ...@@ -292,7 +293,18 @@ void HTMLImageElement::ParseAttribute(
} else if (name == intrinsicsizeAttr && } else if (name == intrinsicsizeAttr &&
RuntimeEnabledFeatures:: RuntimeEnabledFeatures::
ExperimentalProductivityFeaturesEnabled()) { ExperimentalProductivityFeaturesEnabled()) {
ParseIntrinsicSizeAttribute(params.new_value); String message;
bool intrinsic_size_changed =
MediaElementParserHelpers::ParseIntrinsicSizeAttribute(
params.new_value, &overridden_intrinsic_size_, &message);
if (!message.IsEmpty()) {
GetDocument().AddConsoleMessage(ConsoleMessage::Create(
kOtherMessageSource, kWarningMessageLevel, message));
}
if (intrinsic_size_changed && GetLayoutObject() &&
GetLayoutObject()->IsLayoutImage())
ToLayoutImage(GetLayoutObject())->IntrinsicSizeChanged();
} else { } else {
HTMLElement::ParseAttribute(params); HTMLElement::ParseAttribute(params);
} }
...@@ -589,31 +601,6 @@ IntSize HTMLImageElement::GetOverriddenIntrinsicSize() const { ...@@ -589,31 +601,6 @@ IntSize HTMLImageElement::GetOverriddenIntrinsicSize() const {
return overridden_intrinsic_size_; return overridden_intrinsic_size_;
} }
void HTMLImageElement::ParseIntrinsicSizeAttribute(const String& value) {
unsigned new_width = 0, new_height = 0;
Vector<String> size;
value.Split('x', size);
if (!value.IsEmpty() &&
(size.size() != 2 ||
!ParseHTMLNonNegativeInteger(size.at(0), new_width) ||
!ParseHTMLNonNegativeInteger(size.at(1), new_height))) {
GetDocument().AddConsoleMessage(
ConsoleMessage::Create(kOtherMessageSource, kWarningMessageLevel,
"Unable to parse intrinsicSize: expected "
"[unsigned] x [unsigned], got " +
value));
new_width = 0;
new_height = 0;
}
IntSize new_size(new_width, new_height);
if (overridden_intrinsic_size_ != new_size) {
overridden_intrinsic_size_ = new_size;
if (GetLayoutObject() && GetLayoutObject()->IsLayoutImage())
ToLayoutImage(GetLayoutObject())->IntrinsicSizeChanged();
}
}
KURL HTMLImageElement::Src() const { KURL HTMLImageElement::Src() const {
return GetDocument().CompleteURL(getAttribute(srcAttr)); return GetDocument().CompleteURL(getAttribute(srcAttr));
} }
......
...@@ -224,8 +224,6 @@ class CORE_EXPORT HTMLImageElement final ...@@ -224,8 +224,6 @@ class CORE_EXPORT HTMLImageElement final
void NotifyViewportChanged(); void NotifyViewportChanged();
void CreateMediaQueryListIfDoesNotExist(); void CreateMediaQueryListIfDoesNotExist();
void ParseIntrinsicSizeAttribute(const String& value);
Member<HTMLImageLoader> image_loader_; Member<HTMLImageLoader> image_loader_;
Member<ViewportChangeListener> listener_; Member<ViewportChangeListener> listener_;
Member<HTMLFormElement> form_; Member<HTMLFormElement> form_;
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include "third_party/blink/renderer/core/fullscreen/fullscreen.h" #include "third_party/blink/renderer/core/fullscreen/fullscreen.h"
#include "third_party/blink/renderer/core/fullscreen/fullscreen_options.h" #include "third_party/blink/renderer/core/fullscreen/fullscreen_options.h"
#include "third_party/blink/renderer/core/html/media/media_custom_controls_fullscreen_detector.h" #include "third_party/blink/renderer/core/html/media/media_custom_controls_fullscreen_detector.h"
#include "third_party/blink/renderer/core/html/media/media_element_parser_helpers.h"
#include "third_party/blink/renderer/core/html/media/media_remoting_interstitial.h" #include "third_party/blink/renderer/core/html/media/media_remoting_interstitial.h"
#include "third_party/blink/renderer/core/html/media/picture_in_picture_interstitial.h" #include "third_party/blink/renderer/core/html/media/picture_in_picture_interstitial.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h" #include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h"
...@@ -211,7 +212,18 @@ void HTMLVideoElement::ParseAttribute( ...@@ -211,7 +212,18 @@ void HTMLVideoElement::ParseAttribute(
} else if (params.name == intrinsicsizeAttr && } else if (params.name == intrinsicsizeAttr &&
RuntimeEnabledFeatures:: RuntimeEnabledFeatures::
ExperimentalProductivityFeaturesEnabled()) { ExperimentalProductivityFeaturesEnabled()) {
ParseIntrinsicSizeAttribute(params.new_value); String message;
bool intrinsic_size_changed =
MediaElementParserHelpers::ParseIntrinsicSizeAttribute(
params.new_value, &overridden_intrinsic_size_, &message);
if (!message.IsEmpty()) {
GetDocument().AddConsoleMessage(ConsoleMessage::Create(
kOtherMessageSource, kWarningMessageLevel, message));
}
if (intrinsic_size_changed && GetLayoutObject() &&
GetLayoutObject()->IsVideo())
ToLayoutVideo(GetLayoutObject())->IntrinsicSizeChanged();
} else { } else {
HTMLMediaElement::ParseAttribute(params); HTMLMediaElement::ParseAttribute(params);
} }
...@@ -242,31 +254,6 @@ IntSize HTMLVideoElement::GetOverriddenIntrinsicSize() const { ...@@ -242,31 +254,6 @@ IntSize HTMLVideoElement::GetOverriddenIntrinsicSize() const {
return overridden_intrinsic_size_; return overridden_intrinsic_size_;
} }
void HTMLVideoElement::ParseIntrinsicSizeAttribute(const String& value) {
unsigned new_width = 0, new_height = 0;
Vector<String> size;
value.Split('x', size);
if (!value.IsEmpty() &&
(size.size() != 2 ||
!ParseHTMLNonNegativeInteger(size.at(0), new_width) ||
!ParseHTMLNonNegativeInteger(size.at(1), new_height))) {
GetDocument().AddConsoleMessage(
ConsoleMessage::Create(kOtherMessageSource, kWarningMessageLevel,
"Unable to parse intrinsicSize: expected "
"[unsigned] x [unsigned], got " +
value));
new_width = 0;
new_height = 0;
}
IntSize new_size(new_width, new_height);
if (overridden_intrinsic_size_ != new_size) {
overridden_intrinsic_size_ = new_size;
if (GetLayoutObject() && GetLayoutObject()->IsVideo())
ToLayoutVideo(GetLayoutObject())->IntrinsicSizeChanged();
}
}
bool HTMLVideoElement::IsURLAttribute(const Attribute& attribute) const { bool HTMLVideoElement::IsURLAttribute(const Attribute& attribute) const {
return attribute.GetName() == posterAttr || return attribute.GetName() == posterAttr ||
HTMLMediaElement::IsURLAttribute(attribute); HTMLMediaElement::IsURLAttribute(attribute);
......
...@@ -220,8 +220,6 @@ class CORE_EXPORT HTMLVideoElement final : public HTMLMediaElement, ...@@ -220,8 +220,6 @@ class CORE_EXPORT HTMLVideoElement final : public HTMLMediaElement,
void DidMoveToNewDocument(Document& old_document) override; void DidMoveToNewDocument(Document& old_document) override;
void SetDisplayMode(DisplayMode) override; void SetDisplayMode(DisplayMode) override;
void ParseIntrinsicSizeAttribute(const String& value);
Member<HTMLImageLoader> image_loader_; Member<HTMLImageLoader> image_loader_;
Member<MediaCustomControlsFullscreenDetector> Member<MediaCustomControlsFullscreenDetector>
custom_controls_fullscreen_detector_; custom_controls_fullscreen_detector_;
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/html/media/media_element_parser_helpers.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
namespace blink {
namespace MediaElementParserHelpers {
bool ParseIntrinsicSizeAttribute(const String& value,
IntSize* intrinsic_size,
String* message) {
unsigned new_width = 0, new_height = 0;
Vector<String> size;
value.Split('x', size);
if (!value.IsEmpty() &&
(size.size() != 2 ||
!ParseHTMLNonNegativeInteger(size.at(0), new_width) ||
!ParseHTMLNonNegativeInteger(size.at(1), new_height))) {
*message =
"Unable to parse intrinsicSize: expected [unsigned] x [unsigned]"
", got " +
value;
new_width = 0;
new_height = 0;
}
IntSize new_size(new_width, new_height);
if (*intrinsic_size != new_size) {
*intrinsic_size = new_size;
return true;
}
return false;
}
} // namespace MediaElementParserHelpers
} // namespace blink
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_MEDIA_MEDIA_ELEMENT_PARSER_HELPERS_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_MEDIA_MEDIA_ELEMENT_PARSER_HELPERS_H_
#include "third_party/blink/renderer/platform/geometry/int_size.h"
namespace blink {
namespace MediaElementParserHelpers {
// Parses the intrinsicSize attribute of HTMLImageElement, HTMLVideoElement, and
// SVGImageElement. Returns true if the value is updated.
// https://github.com/ojanvafai/intrinsicsize-attribute/blob/master/README.md
bool ParseIntrinsicSizeAttribute(const String& value,
IntSize* intrinsic_size,
String* message);
} // namespace MediaElementParserHelpers
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_HTML_MEDIA_MEDIA_ELEMENT_PARSER_HELPERS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment