mirror of
https://github.com/servo/servo.git
synced 2025-10-02 09:39:14 +01:00
Auto merge of #14535 - servo:property-id, r=mbrubeck
Introduce a PropertyId enum and use it instead of strings of property names <!-- Please describe your changes on the following line: --> * `LonghandId` and `ShorthandId` are C-like enums * `Atom` is used for the name of custom properties. * `PropertyDeclarationId` is the identifier for `PropertyDeclaration`, after parsing and shorthand expansion. (Longhand or custom property.) * `PropertyId` represents any CSS property, e.g. in CSSOM. (Longhand, shorthand, or custom.) CC @upsuper --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14535) <!-- Reviewable:end -->
This commit is contained in:
commit
6dd4b4822f
27 changed files with 601 additions and 459 deletions
|
@ -49,7 +49,7 @@ use style::gecko_bindings::sugar::refptr::{GeckoArcPrincipal, GeckoArcURI};
|
|||
use style::parallel;
|
||||
use style::parser::{ParserContext, ParserContextExtraData};
|
||||
use style::properties::{CascadeFlags, ComputedValues, Importance, PropertyDeclaration};
|
||||
use style::properties::{PropertyDeclarationParseResult, PropertyDeclarationBlock};
|
||||
use style::properties::{PropertyDeclarationParseResult, PropertyDeclarationBlock, PropertyId};
|
||||
use style::properties::{apply_declarations, parse_one_declaration};
|
||||
use style::restyle_hints::RestyleHint;
|
||||
use style::selector_parser::PseudoElementCascadeType;
|
||||
|
@ -534,6 +534,11 @@ pub extern "C" fn Servo_ParseProperty(property: *const nsACString, value: *const
|
|||
principal: *mut ThreadSafePrincipalHolder)
|
||||
-> RawServoDeclarationBlockStrong {
|
||||
let name = unsafe { property.as_ref().unwrap().as_str_unchecked() };
|
||||
let id = if let Ok(id) = PropertyId::parse(name.into()) {
|
||||
id
|
||||
} else {
|
||||
return RawServoDeclarationBlockStrong::null()
|
||||
};
|
||||
let value = unsafe { value.as_ref().unwrap().as_str_unchecked() };
|
||||
let base_str = unsafe { base_url.as_ref().unwrap().as_str_unchecked() };
|
||||
let base_url = ServoUrl::parse(base_str).unwrap();
|
||||
|
@ -548,7 +553,7 @@ pub extern "C" fn Servo_ParseProperty(property: *const nsACString, value: *const
|
|||
extra_data);
|
||||
|
||||
let mut results = vec![];
|
||||
match PropertyDeclaration::parse(name, &context, &mut Parser::new(value),
|
||||
match PropertyDeclaration::parse(id, &context, &mut Parser::new(value),
|
||||
&mut results, false) {
|
||||
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration => {},
|
||||
_ => return RawServoDeclarationBlockStrong::null(),
|
||||
|
@ -611,7 +616,7 @@ pub extern "C" fn Servo_DeclarationBlock_SerializeOneValue(
|
|||
buffer: *mut nsAString)
|
||||
{
|
||||
let declarations = RwLock::<PropertyDeclarationBlock>::as_arc(&declarations);
|
||||
let property = get_property_name_from_atom(property, is_custom);
|
||||
let property = get_property_id_from_atom(property, is_custom);
|
||||
let mut string = String::new();
|
||||
let rv = declarations.read().single_value_to_css(&property, &mut string);
|
||||
debug_assert!(rv.is_ok());
|
||||
|
@ -631,23 +636,21 @@ pub extern "C" fn Servo_DeclarationBlock_GetNthProperty(declarations: RawServoDe
|
|||
let declarations = RwLock::<PropertyDeclarationBlock>::as_arc(&declarations);
|
||||
if let Some(&(ref decl, _)) = declarations.read().declarations.get(index as usize) {
|
||||
let result = unsafe { result.as_mut().unwrap() };
|
||||
write!(result, "{}", decl.name()).unwrap();
|
||||
decl.id().to_css(result).unwrap();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME Methods of PropertyDeclarationBlock should take atoms directly.
|
||||
// This function is just a temporary workaround before that finishes.
|
||||
fn get_property_name_from_atom(atom: *mut nsIAtom, is_custom: bool) -> String {
|
||||
fn get_property_id_from_atom(atom: *mut nsIAtom, is_custom: bool) -> PropertyId {
|
||||
let atom = Atom::from(atom);
|
||||
if !is_custom {
|
||||
atom.to_string()
|
||||
// FIXME: can we do this mapping without going through a UTF-8 string?
|
||||
// Maybe even from nsCSSPropertyID directly?
|
||||
PropertyId::parse(atom.to_string().into()).expect("got unknown property name from Gecko")
|
||||
} else {
|
||||
let mut result = String::with_capacity(atom.len() as usize + 2);
|
||||
write!(result, "--{}", atom).unwrap();
|
||||
result
|
||||
PropertyId::Custom(atom)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -656,7 +659,7 @@ pub extern "C" fn Servo_DeclarationBlock_GetPropertyValue(declarations: RawServo
|
|||
property: *mut nsIAtom, is_custom: bool,
|
||||
value: *mut nsAString) {
|
||||
let declarations = RwLock::<PropertyDeclarationBlock>::as_arc(&declarations);
|
||||
let property = get_property_name_from_atom(property, is_custom);
|
||||
let property = get_property_id_from_atom(property, is_custom);
|
||||
declarations.read().property_value_to_css(&property, unsafe { value.as_mut().unwrap() }).unwrap();
|
||||
}
|
||||
|
||||
|
@ -664,7 +667,7 @@ pub extern "C" fn Servo_DeclarationBlock_GetPropertyValue(declarations: RawServo
|
|||
pub extern "C" fn Servo_DeclarationBlock_GetPropertyIsImportant(declarations: RawServoDeclarationBlockBorrowed,
|
||||
property: *mut nsIAtom, is_custom: bool) -> bool {
|
||||
let declarations = RwLock::<PropertyDeclarationBlock>::as_arc(&declarations);
|
||||
let property = get_property_name_from_atom(property, is_custom);
|
||||
let property = get_property_id_from_atom(property, is_custom);
|
||||
declarations.read().property_priority(&property).important()
|
||||
}
|
||||
|
||||
|
@ -672,12 +675,12 @@ pub extern "C" fn Servo_DeclarationBlock_GetPropertyIsImportant(declarations: Ra
|
|||
pub extern "C" fn Servo_DeclarationBlock_SetProperty(declarations: RawServoDeclarationBlockBorrowed,
|
||||
property: *mut nsIAtom, is_custom: bool,
|
||||
value: *mut nsACString, is_important: bool) -> bool {
|
||||
let property = get_property_name_from_atom(property, is_custom);
|
||||
let property = get_property_id_from_atom(property, is_custom);
|
||||
let value = unsafe { value.as_ref().unwrap().as_str_unchecked() };
|
||||
// FIXME Needs real URL and ParserContextExtraData.
|
||||
let base_url = &*DUMMY_BASE_URL;
|
||||
let extra_data = ParserContextExtraData::default();
|
||||
if let Ok(decls) = parse_one_declaration(&property, value, &base_url,
|
||||
if let Ok(decls) = parse_one_declaration(property, value, &base_url,
|
||||
Box::new(StdoutErrorReporter), extra_data) {
|
||||
let mut declarations = RwLock::<PropertyDeclarationBlock>::as_arc(&declarations).write();
|
||||
let importance = if is_important { Importance::Important } else { Importance::Normal };
|
||||
|
@ -694,19 +697,24 @@ pub extern "C" fn Servo_DeclarationBlock_SetProperty(declarations: RawServoDecla
|
|||
pub extern "C" fn Servo_DeclarationBlock_RemoveProperty(declarations: RawServoDeclarationBlockBorrowed,
|
||||
property: *mut nsIAtom, is_custom: bool) {
|
||||
let declarations = RwLock::<PropertyDeclarationBlock>::as_arc(&declarations);
|
||||
let property = get_property_name_from_atom(property, is_custom);
|
||||
let property = get_property_id_from_atom(property, is_custom);
|
||||
declarations.write().remove_property(&property);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn Servo_CSSSupports(property: *const nsACString, value: *const nsACString) -> bool {
|
||||
let property = unsafe { property.as_ref().unwrap().as_str_unchecked() };
|
||||
let id = if let Ok(id) = PropertyId::parse(property.into()) {
|
||||
id
|
||||
} else {
|
||||
return false
|
||||
};
|
||||
let value = unsafe { value.as_ref().unwrap().as_str_unchecked() };
|
||||
|
||||
let base_url = &*DUMMY_BASE_URL;
|
||||
let extra_data = ParserContextExtraData::default();
|
||||
|
||||
match parse_one_declaration(&property, &value, &base_url, Box::new(StdoutErrorReporter), extra_data) {
|
||||
match parse_one_declaration(id, &value, &base_url, Box::new(StdoutErrorReporter), extra_data) {
|
||||
Ok(decls) => !decls.is_empty(),
|
||||
Err(()) => false,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue