Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow Gecko FFI functions for color parsing to report errors #19490

Merged
merged 5 commits into from Dec 5, 2017

geckolib: Allow Servo_ComputeColor to report errors to the console.

  • Loading branch information
heycam committed Dec 5, 2017
commit 9a31d0d8d82ae907f29bdbfa131da7197472b5e9
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use cssparser::{Parser, ParserInput};
use cssparser::{ParseErrorKind, Parser, ParserInput};
use cssparser::ToCss as ParserToCss;
use env_logger::LogBuilder;
use malloc_size_of::MallocSizeOfOps;
@@ -23,7 +23,7 @@ use style::data::{ElementStyles, self};
use style::dom::{ShowSubtreeData, TDocument, TElement, TNode};
use style::driver;
use style::element_state::{DocumentState, ElementState};
use style::error_reporting::{NullReporter, ParseErrorReporter};
use style::error_reporting::{ContextualParseError, NullReporter, ParseErrorReporter};
use style::font_metrics::{FontMetricsProvider, get_metrics_provider_for_product};
use style::gecko::data::{GeckoStyleSheet, PerDocumentStyleData, PerDocumentStyleDataImpl};
use style::gecko::global_style_data::{GLOBAL_STYLE_DATA, GlobalStyleData, STYLE_THREAD_POOL};
@@ -153,7 +153,7 @@ use style::values::distance::ComputeSquaredDistance;
use style::values::specified;
use style::values::specified::gecko::IntersectionObserverRootMargin;
use style::values::specified::source_size_list::SourceSizeList;
use style_traits::{ParsingMode, ToCss};
use style_traits::{ParsingMode, StyleParseErrorKind, ToCss};
use super::error_reporter::ErrorReporter;
use super::stylesheet_loader::StylesheetLoader;

@@ -4557,18 +4557,39 @@ pub unsafe extern "C" fn Servo_SelectorList_Drop(list: RawServoSelectorListOwned
let _ = list.into_box::<::selectors::SelectorList<SelectorImpl>>();
}

fn parse_color(value: &str) -> Result<specified::Color, ()> {
fn parse_color(
value: &str,
error_reporter: Option<&ErrorReporter>,
) -> Result<specified::Color, ()> {
let mut input = ParserInput::new(value);
let mut parser = Parser::new(&mut input);
parser.parse_entirely(specified::Color::parse_color).map_err(|_| ())
let start_position = parser.position();
parser.parse_entirely(specified::Color::parse_color).map_err(|err| {
if let Some(error_reporter) = error_reporter {
match err.kind {
ParseErrorKind::Custom(StyleParseErrorKind::ValueError(..)) => {
let location = err.location.clone();
let error = ContextualParseError::UnsupportedValue(
parser.slice_from(start_position),
err,
);
error_reporter.report(location, error);
}
// Ignore other kinds of errors that might be reported, such as
// ParseErrorKind::Basic(BasicParseErrorKind::UnexpectedToken),
// since Gecko doesn't report those to the error console.
_ => {}
}
}
})
}

#[no_mangle]
pub extern "C" fn Servo_IsValidCSSColor(
value: *const nsAString,
) -> bool {
let value = unsafe { (*value).to_string() };
parse_color(&value).is_ok()
parse_color(&value, None).is_ok()
}

#[no_mangle]
@@ -4578,14 +4599,20 @@ pub extern "C" fn Servo_ComputeColor(
value: *const nsAString,
result_color: *mut structs::nscolor,
was_current_color: *mut bool,
loader: *mut Loader,
) -> bool {
use style::gecko;

let current_color = gecko::values::convert_nscolor_to_rgba(current_color);
let value = unsafe { (*value).to_string() };
let result_color = unsafe { result_color.as_mut().unwrap() };

match parse_color(&value) {
let reporter = unsafe { loader.as_mut() }.map(|loader| {
// Make an ErrorReporter that will report errors as being "from DOM".
ErrorReporter::new(ptr::null_mut(), loader, ptr::null_mut())
});

match parse_color(&value, reporter.as_ref()) {
Ok(specified_color) => {
let computed_color = match raw_data {
Some(raw_data) => {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.