Skip to content

Commit

Permalink
Add html-validation crate
Browse files Browse the repository at this point in the history
  • Loading branch information
chinedufn committed Mar 16, 2019
1 parent e4d86f3 commit 6462688
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 28 deletions.
16 changes: 8 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jobs:
keys:
# Checksum ensures that when we update versions for our `html-macro` dependencies we clear our cache, otherwise
# compiletest-rs can error when the target directory has multiple versions of a crate.
- v6-cargo-cache-test-{{ arch }}-{{ .Branch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
- v6-cargo-cache-test-{{ arch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
- v7-cargo-cache-test-{{ arch }}-{{ .Branch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
- v7-cargo-cache-test-{{ arch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}

# Install nightly & wasm
- run:
Expand Down Expand Up @@ -65,12 +65,12 @@ jobs:

# Save cache
- save_cache:
key: v6-cargo-cache-test-{{ arch }}-{{ .Branch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
key: v7-cargo-cache-test-{{ arch }}-{{ .Branch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
paths:
- target
- /usr/local/cargo
- save_cache:
key: v6-cargo-cache-test-{{ arch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
key: v7-cargo-cache-test-{{ arch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
paths:
- target
- /usr/local/cargo
Expand All @@ -84,8 +84,8 @@ jobs:
# Multiple caches are used to increase the chance of a cache hit.
- restore_cache:
keys:
- v6-cargo-cache-docs-{{ arch }}-{{ .Branch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
- v6-cargo-cache-docs-{{ arch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
- v7-cargo-cache-docs-{{ arch }}-{{ .Branch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
- v7-cargo-cache-docs-{{ arch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}

# Install nightly
- run:
Expand Down Expand Up @@ -120,12 +120,12 @@ jobs:

# Save cache
- save_cache:
key: v6-cargo-cache-docs-{{ arch }}-{{ .Branch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
key: v7-cargo-cache-docs-{{ arch }}-{{ .Branch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
paths:
- target
- /usr/local/cargo
- save_cache:
key: v6-cargo-cache-docs-{{ arch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
key: v7-cargo-cache-docs-{{ arch }}-{{ checksum "./crates/html-macro/Cargo.toml" }}-{{ checksum "./crates/virtual-dom-rs/Cargo.toml" }}
paths:
- target
- /usr/local/cargo
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"crates/html-macro",
"crates/html-macro-test",
"crates/html-macro-ui",
"crates/html-validation",
"crates/router-rs",
"crates/router-rs-macro",
"crates/router-rs-macro-test",
Expand Down
5 changes: 2 additions & 3 deletions crates/html-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "html-macro"
version = "0.1.2"
version = "0.1.3"
description = "html macro"
authors = ["Chinedu Francis Nwafili <[email protected]>"]
keywords = ["virtual", "dom", "wasm", "assembly", "webassembly"]
Expand All @@ -16,5 +16,4 @@ proc-macro = true
proc-macro2 = { version = "0.4", features = ["span-locations"] }
quote = "0.6.11"
syn = { version = "0.15", features = ["full", "extra-traits"] }
# TODO: Remove this in favor of crates/html-validation
virtual-node = { path = "../virtual-node", version = "0.2.3" }
html-validation = {path = "../html-validation", version = "0.1.0"}
3 changes: 1 addition & 2 deletions crates/html-macro/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ struct RecentSpanLocations {
most_recent_block_start: Option<Span>,
}

// TODO: Cache this as a HashSet inside of our parser
fn is_self_closing(tag: &str) -> bool {
virtual_node::SELF_CLOSING_TAGS.contains(tag)
html_validation::is_self_closing(tag)
}
12 changes: 12 additions & 0 deletions crates/html-validation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "html-validation"
version = "0.1.0"
authors = ["Chinedu Francis Nwafili <[email protected]>"]
description = "Validation for HTML elements and attributes"
keywords = ["html", "validation", "valid", "dom", "virtual"]
license = "MIT/Apache-2.0"
repository = "https://github.com/chinedufn/percy"
edition = "2018"

[dependencies]
lazy_static = "1.0"
33 changes: 33 additions & 0 deletions crates/html-validation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Validation for html attributes and elements
#![deny(missing_docs)]

use lazy_static::lazy_static;
use std::collections::hash_set::HashSet;

// Used to uniquely identify elements that contain closures so that the DomUpdater can
// look them up by their unique id.
// When the DomUpdater sees that the element no longer exists it will drop all of it's
// Rc'd Closures for those events.
lazy_static! {
static ref SELF_CLOSING_TAGS: HashSet<&'static str> = [
"area", "base", "br", "col", "hr", "img", "input", "link", "meta", "param", "command",
"keygen", "source",
]
.iter()
.cloned()
.collect();
}

/// Whether or not this tag is self closing
///
/// ```
/// use html_validation::is_self_closing;
///
/// assert_eq!(is_self_closing("br"), true);
///
/// assert_eq!(is_self_closing("div"), false);
/// ```
pub fn is_self_closing(tag: &str) -> bool {
SELF_CLOSING_TAGS.contains(tag)
}
1 change: 1 addition & 0 deletions crates/virtual-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2018"
[dependencies]
js-sys = "0.3"
wasm-bindgen = {version = "0.2.33", features = ["default", "nightly"]}
html-validation = {path = "../html-validation", version = "0.1.0"}
lazy_static = "1.0"

[dependencies.web-sys]
Expand Down
17 changes: 2 additions & 15 deletions crates/virtual-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use web_sys::{self, Element, EventTarget, Node, Text};
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;

use lazy_static::lazy_static;

use std::iter::FromIterator;
use std::ops::Deref;
use std::option::Iter;
Expand All @@ -31,15 +29,9 @@ use std::sync::Mutex;
// look them up by their unique id.
// When the DomUpdater sees that the element no longer exists it will drop all of it's
// Rc'd Closures for those events.
use lazy_static::lazy_static;
lazy_static! {
static ref ELEM_UNIQUE_ID: Mutex<u32> = Mutex::new(0);
pub static ref SELF_CLOSING_TAGS: HashSet<&'static str> = [
"area", "base", "br", "col", "hr", "img", "input", "link", "meta", "param", "command",
"keygen", "source",
]
.iter()
.cloned()
.collect();
}

/// When building your views you'll typically use the `html!` macro to generate
Expand Down Expand Up @@ -220,11 +212,6 @@ impl VElement {
}
}

/// Whether or not this is a self closing tag such as <br> or <img />
pub fn is_self_closing(&self) -> bool {
SELF_CLOSING_TAGS.contains(self.tag.as_str())
}

/// Build a DOM element by recursively creating DOM nodes for this element and it's
/// children, it's children's children, etc.
pub fn create_element_node(&self) -> CreatedNode<Element> {
Expand Down Expand Up @@ -525,7 +512,7 @@ impl fmt::Display for VElement {
write!(f, "{}", child.to_string())?;
}

if !self.is_self_closing() {
if !html_validation::is_self_closing(&self.tag) {
write!(f, "</{}>", self.tag)?;
}

Expand Down

1 comment on commit 6462688

@chinedufn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivanceras heads up - thanks!

Please sign in to comment.