Skip to content

Commit

Permalink
chore(tree): fix tree production assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Feb 15, 2024
1 parent 8430c04 commit 3e92f17
Show file tree
Hide file tree
Showing 14 changed files with 966 additions and 85 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions accessibility-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "accessibility-rs"
version = "0.0.45"
version = "0.0.46"
authors = ["The A11yWatch Project Developers", "Jeff Mendez <jeff@a11ywatch.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -15,8 +15,8 @@ default = []

[dependencies]
lazy_static = { workspace = true }
accessibility-scraper = { version = "0.0.5", features = ["main"], default-features = false, path = "../accessibility-scraper" }
accessibility-tree = { version = "0.0.5", path = "../accessibility-tree/victor" }
accessibility-scraper = { version = "0.0.6", features = ["main"], default-features = false, path = "../accessibility-scraper" }
accessibility-tree = { version = "0.0.6", path = "../accessibility-tree/victor" }
getrandom = { version = "0.2", features = ["js"] }
taffy = { version = "0.3.13" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion accessibility-rs/src/engine/rules/techniques.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub enum Techniques {
/// <https://www.w3.org/TR/WCAG20-TECHS/F41>
F41,
/// <https://www.w3.org/TR/WCAG20-TECHS/F47>
F47
F47,
}

impl Techniques {
Expand Down
11 changes: 11 additions & 0 deletions accessibility-rs/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ fn _audit() {
));
println!("{:?}", report)
}

#[test]
fn _audit_large() {
let report = accessibility_rs::audit(AuditConfig::new(
mock::MOCK_HTML_LARGE_PAGE,
&mock::MOCK_CSS_RULES_LARGE,
true,
"en",
));
println!("{:?}", report)
}
859 changes: 859 additions & 0 deletions accessibility-rs/tests/mocks/mock.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion accessibility-rs/tests/unit/html.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
//! Test generic html elements
//! Test generic html elements
2 changes: 1 addition & 1 deletion accessibility-scraper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "accessibility-scraper"
version = "0.0.5"
version = "0.0.6"
edition = "2021"

description = "HTML parsing and querying with CSS selectors with CSS binding styles to elements."
Expand Down
34 changes: 19 additions & 15 deletions accessibility-tree/proc-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,26 @@ pub fn derive_sfnt_table(input: proc_macro::TokenStream) -> proc_macro::TokenStr
field.ty.clone().into_token_stream()
)
};
assert!(ty.qself.is_none());
let size = match &*ty.path.segments.last().unwrap().value().ident.to_string() {
"u16" | "i16" | "FWord" | "UFWord" | "FontDesignUnitsPerEmFactorU16" => 2,
"u32" | "FixedPoint" | "Tag" => 4,
"LongDateTime" => 8,
_ => panic!("The size of {} is unknown", ty.clone().into_token_stream()),
};
// The TrueType format seems to be designed so that this never happens:
let expected_align = std::cmp::min(size, 4);
assert_eq!(offset % expected_align, 0, "Field {} is misaligned", name);
methods.extend(quote! {
pub(in crate::fonts) fn #name(self) -> crate::fonts::parsing::Position<#ty> {
self.offset_bytes(#offset)

if ty.qself.is_none() {
let size = match &*ty.path.segments.last().unwrap().value().ident.to_string() {
"u16" | "i16" | "FWord" | "UFWord" | "FontDesignUnitsPerEmFactorU16" => 2,
"u32" | "FixedPoint" | "Tag" => 4,
"LongDateTime" => 8,
_ => panic!("The size of {} is unknown", ty.clone().into_token_stream()),
};
// The TrueType format seems to be designed so that this never happens:
let expected_align = std::cmp::min(size, 4);

if offset % expected_align == 0 {
methods.extend(quote! {
pub(in crate::fonts) fn #name(self) -> crate::fonts::parsing::Position<#ty> {
self.offset_bytes(#offset)
}
});
offset += size;
}
});
offset += size;
}
}
let size_of = offset as usize;

Expand Down
4 changes: 2 additions & 2 deletions accessibility-tree/victor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "accessibility-tree"
version = "0.0.5"
version = "0.0.6"
authors = ["Jeff Mendez <jeff@a11ywatch.com"]
license = "MIT OR Apache-2.0"
description = "Accessibility tree binding CSS styles and vectors to elements. Used mainly for accessibility-rs crate."
Expand All @@ -22,7 +22,7 @@ lock_api = "0.1"
num-traits = "0.2"
rayon = "1"
rayon_croissant = "0.1.1"
accessibility-scraper = { version = "0.0.5", features = ["main"], default-features = false, path = "../../accessibility-scraper" }
accessibility-scraper = { version = "0.0.6", features = ["main"], default-features = false, path = "../../accessibility-scraper" }
selectors = { workspace = true }
smallbitvec = "2.4"
smallvec = { workspace = true }
Expand Down
35 changes: 20 additions & 15 deletions accessibility-tree/victor/src/dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,30 @@ impl Document {

pub fn root_element(&self) -> NodeId {
let document_node = &self[Document::document_node_id()];
assert!(matches!(document_node.data, NodeData::Document));
assert!(document_node.parent.is_none());
assert!(document_node.next_sibling.is_none());
assert!(document_node.previous_sibling.is_none());
let mut root = None;
for child in self.node_and_following_siblings(document_node.first_child.unwrap()) {
match &self[child].data {
NodeData::Doctype { .. }
| NodeData::Comment { .. }
| NodeData::ProcessingInstruction { .. } => {}
NodeData::Document | NodeData::Text { .. } => {
panic!("Unexpected node type under document node")
}
NodeData::Element(_) => {
assert!(root.is_none(), "Found two root elements");
root = Some(child)

if matches!(document_node.data, NodeData::Document)
&& document_node.parent.is_none()
&& document_node.next_sibling.is_none()
&& document_node.previous_sibling.is_none()
{
for child in self.node_and_following_siblings(document_node.first_child.unwrap()) {
match &self[child].data {
NodeData::Doctype { .. }
| NodeData::Comment { .. }
| NodeData::ProcessingInstruction { .. } => {}
NodeData::Document | NodeData::Text { .. } => {
println!("Unexpected node type under document node")
}
NodeData::Element(_) => {
if root.is_none() {
root = Some(child)
}
}
}
}
}

root.unwrap()
}

Expand Down
4 changes: 3 additions & 1 deletion accessibility-tree/victor/src/layout/dom_traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ impl<'dom> BoxSlot<'dom> {
impl Drop for BoxSlot<'_> {
fn drop(&mut self) {
if let Some(slot) = &mut self.slot {
assert!(slot.is_some(), "failed to set a layout box")
if !slot.is_some() {
println!("failed to set a layout box");
}
}
}
}
Expand Down
47 changes: 20 additions & 27 deletions accessibility-tree/victor/src/layout/flow/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,35 +470,28 @@ impl<'a> BlockContainerBuilder<'a> {
}

fn end_ongoing_inline_formatting_context(&mut self) {
assert!(
self.ongoing_inline_boxes_stack.is_empty(),
"there should be no ongoing inline level boxes",
);

if self
.ongoing_inline_formatting_context
.inline_level_boxes
.is_empty()
if self.ongoing_inline_boxes_stack.is_empty()
&& !self
.ongoing_inline_formatting_context
.inline_level_boxes
.is_empty()
{
// There should never be an empty inline formatting context.
return;
let block_container_style = self.block_container_style;
let anonymous_style = self.anonymous_style.get_or_insert_with(|| {
// If parent_style is None, the parent is the document node,
// in which case anonymous inline boxes should inherit their
// styles from initial values.
ComputedValues::anonymous_inheriting_from(Some(block_container_style))
});

let box_ = IntermediateBlockLevelBox::SameFormattingContextBlock {
style: anonymous_style.clone(),
contents: IntermediateBlockContainer::InlineFormattingContext(take(
&mut self.ongoing_inline_formatting_context,
)),
};
self.block_level_boxes.push((box_, BoxSlot::dummy()))
}

let block_container_style = self.block_container_style;
let anonymous_style = self.anonymous_style.get_or_insert_with(|| {
// If parent_style is None, the parent is the document node,
// in which case anonymous inline boxes should inherit their
// styles from initial values.
ComputedValues::anonymous_inheriting_from(Some(block_container_style))
});

let box_ = IntermediateBlockLevelBox::SameFormattingContextBlock {
style: anonymous_style.clone(),
contents: IntermediateBlockContainer::InlineFormattingContext(take(
&mut self.ongoing_inline_formatting_context,
)),
};
self.block_level_boxes.push((box_, BoxSlot::dummy()))
}

fn current_inline_level_boxes(&mut self) -> &mut Vec<Arc<InlineLevelBox>> {
Expand Down
10 changes: 5 additions & 5 deletions accessibility-tree/victor/src/layout/flow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ fn layout_block_level_children<'a>(
Fragment::Anonymous(fragment) => {
// FIXME(nox): Margin collapsing for hypothetical boxes of
// abspos elements is probably wrong.
assert!(fragment.children.is_empty());
assert_eq!(fragment.rect.size.block, Length::zero());
fragment.rect.start_corner.block +=
placement_state.current_block_direction_position;
if fragment.children.is_empty() && fragment.rect.size.block == Length::zero() {
fragment.rect.start_corner.block +=
placement_state.current_block_direction_position;
}
}
_ => unreachable!(),
_ => (),
}
}

Expand Down
29 changes: 18 additions & 11 deletions accessibility-tree/victor/src/style/declaration_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ impl DeclarationBlock {
match result {
Ok(()) => {}
Err(_) => {
assert!(iter.parser.block.declarations.len() == previous_len);
// FIXME error reporting
// we may not want to break the loop - look into repaairing the parser.
if iter.parser.block.declarations.len() == previous_len {
// println!("Parse error prior block length exceeded.");
break;
}
}
}
debug_assert_eq!(
Expand Down Expand Up @@ -107,16 +110,20 @@ impl<'i> DeclarationParser<'i> for LonghandDeclarationParser {
}
let important = parser.r#try(cssparser::parse_important).is_ok();
let count = self.block.declarations.len() - previous_len;
assert!(count > 0);
self.block.important.extend(repeat(important).take(count));
let any = if important {
&mut self.block.any_important

if count > 0 {
self.block.important.extend(repeat(important).take(count));
let any = if important {
&mut self.block.any_important
} else {
&mut self.block.any_normal
};
any.early |= parsed.early;
any.late |= parsed.late;
Ok(())
} else {
&mut self.block.any_normal
};
any.early |= parsed.early;
any.late |= parsed.late;
Ok(())
Err(parser.new_custom_error(PropertyParseErrorKind::UnknownUnit(name)))
}
} else {
Err(parser.new_custom_error(PropertyParseErrorKind::UnknownProperty(name)))
}
Expand Down

0 comments on commit 3e92f17

Please sign in to comment.