html_aide 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0c8ecb947f4e1433c02d4d3c66300908c3d49dc
4
- data.tar.gz: 9865da568a9172ab2bbd31835e29dd2d582552ed
3
+ metadata.gz: 84a4214a1dd1e8ca6c2cd56a857f5b05258e2749
4
+ data.tar.gz: f1b8d75bdbbc117dcc8ad0789298caa02e0cfdb8
5
5
  SHA512:
6
- metadata.gz: 9a325f843ec18410222da043149acaa70b1bb2b7cf640184d09e2019f855bd1bf1375b21ed96db3c279af424666d6c5fac1ffadd039654fbecec96fc1c2b7d3b
7
- data.tar.gz: 6e524d7d532fe5768bd4f5ca9cc890de01ca2e9f5e31d42e0de52560f1aa23ab3aa1cf3f02e036e222f693e73fa25a6eb324d6bb815263cc0564ba351ff38de4
6
+ metadata.gz: ed7d0580d08699bebe5ba7c8c8d2a689e56ca7805b3db0ea44d5beeb646277070ef19998d5f72e4681ec3abca53c2fc2701b07cea4f395a9bfafb15bf13539e9
7
+ data.tar.gz: 35096bd9be92d694f7ab1106a9b59100d46afd5067a108a414c992f2d9e8e6a6f9f93d889447684d3503d1ea9ebbf581a68fe0d1762a47542a33aa159318e472
data/README.md CHANGED
@@ -21,6 +21,8 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
+ _With valid markup_
25
+
24
26
  ```ruby
25
27
  snippet = '<div id="taco"><strong>Taco</strong> Meat</div>'
26
28
  validator = HtmlAide::Validator.validate(snippet)
@@ -37,6 +39,8 @@ tag.children.first.to_s #=> '<strong>Taco</strong>'
37
39
  tag.to_s #=> '<div id="taco"><strong>Taco</strong> Meat</div>'
38
40
  ```
39
41
 
42
+ _With invalid markup_
43
+
40
44
  ```ruby
41
45
  snippet = '<div>Messed up </div'
42
46
  validator = HtmlAide::Validator.validate(snippet)
@@ -44,11 +48,20 @@ validator.valid? #=> false
44
48
  validator.class #=> HtmlAide::SnippetValidator
45
49
  validator.errors #=> [<#HtmlAide::InvalidElementError>]
46
50
  error = validator.errors.first #=> HtmlAide::InvalidElementError
47
- error.message #=> 'Syntax Error in element'
51
+ error.message #=> 'invalid format, document not terminated at line 1, column 21'
48
52
  validator.element #=> HtmlAide::NullElement
49
53
  ```
50
54
 
51
- possible helpers
55
+ _Also validates invalid HTML 5 tags_
56
+
57
+ ```ruby
58
+ snippet = '<taco>Chicken Soft</taco>'
59
+ validator = HtmlAide::Validator.validate(snippet)
60
+ validator.valid? #=> false
61
+ validator.errors.first.message #=> 'taco is not a valid HTML tag'
62
+ ```
63
+
64
+ ## Future implementations
52
65
 
53
66
  ```ruby
54
67
  snippet = '<div id="taco"><strong>Taco</strong> Meat</div>'
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["jeremywoertink@gmail.com"]
11
11
  spec.summary = %q{Validate HTML snippets and markup.}
12
12
  spec.description = %q{Validate HTML snippets and markup.}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/devpointlabs/html_aide"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -1,5 +1,7 @@
1
1
  require 'ox'
2
2
  require 'nokogiri'
3
+ require 'html_aide/tag'
4
+ require 'html_aide/markup_parser'
3
5
  require 'html_aide/validator'
4
6
  require 'html_aide/version'
5
7
  require 'html_aide/invalid_element_error'
@@ -0,0 +1,15 @@
1
+ module HtmlAide
2
+ class MarkupParser
3
+
4
+ def self.parse(markup)
5
+ node = Ox.parse(markup)
6
+ node = Tag.parse(node)
7
+ rescue Ox::ParseError => e
8
+ raise ParseError, e.to_s
9
+ rescue Tag::ParseError => e
10
+ raise ParseError, e.to_s
11
+ end
12
+
13
+ class ParseError < StandardError; end
14
+ end
15
+ end
@@ -25,10 +25,10 @@ module HtmlAide
25
25
 
26
26
  def create_node_and_set_element
27
27
  begin
28
- node = Ox.parse(@markup)
28
+ node = MarkupParser.parse(@markup)
29
29
  @element = Element.new(node)
30
30
  @valid = true
31
- rescue Ox::ParseError => e
31
+ rescue MarkupParser::ParseError => e
32
32
  @element = NullElement.new
33
33
  @errors << InvalidElementError.new(e)
34
34
  @valid = false
@@ -0,0 +1,38 @@
1
+ module HtmlAide
2
+ class Tag
3
+ VALID_TAGS = %w(
4
+ a abbr address area article aside audio
5
+ b base bdi bdo blockquote body br button
6
+ canvas caption cite code col colgroup
7
+ datalist dd del details dfn div dl dt
8
+ em embed
9
+ fieldset figcaption figure footer form
10
+ h1 h2 h3 h4 h5 h6 head header hgroup hr html
11
+ i iframe img input ins
12
+ kbd keygen
13
+ label legend li link
14
+ map mark menu menuitem meta meter
15
+ nav noscript
16
+ object ol optgroup option output
17
+ p param pre progress
18
+ q
19
+ rp rt ruby
20
+ s samp script section select small source span strong summary sup
21
+ table tbody td textarea tfoot th thead time title tr track
22
+ ul
23
+ var video
24
+ wbr
25
+ ).freeze
26
+
27
+ def self.parse(ox_node)
28
+ raise ParseError, "#{ox_node.name} is not a valid HTML tag." unless valid_tag?(ox_node.name)
29
+ ox_node
30
+ end
31
+
32
+ def self.valid_tag?(tag_name)
33
+ VALID_TAGS.include?(tag_name)
34
+ end
35
+
36
+ class ParseError < StandardError; end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module HtmlAide
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe HtmlAide::Tag do
4
+
5
+ describe '.parse' do
6
+ let(:bad_node) { Ox.parse('<taco></taco>') }
7
+ let(:good_node) { Ox.parse('<p></p>') }
8
+ it 'takes an ox node and raises an error for an invalid tag' do
9
+ expect { HtmlAide::Tag.parse(bad_node) }.to raise_error 'taco is not a valid HTML tag.'
10
+ end
11
+
12
+ it 'returns the original node when all is good' do
13
+ expect(HtmlAide::Tag.parse(good_node)).to eq good_node
14
+ end
15
+ end
16
+
17
+ describe '.valid_tag?' do
18
+ it 'takes the tag name section and returns true' do
19
+ expect(HtmlAide::Tag.valid_tag?('section')).to eq true
20
+ end
21
+ it 'takes the tag name blink and returns false' do
22
+ expect(HtmlAide::Tag.valid_tag?('blink')).to eq false
23
+ end
24
+ end
25
+ end
@@ -2,10 +2,44 @@ require 'spec_helper'
2
2
 
3
3
  describe HtmlAide::Validator do
4
4
 
5
- describe '#initialize' do
5
+ describe '.validate' do
6
6
  it 'returns a SnippetValidator' do
7
- v = HtmlAide::Validator.validate('')
7
+ v = HtmlAide::Validator.validate('<a></a>')
8
8
  expect(v).to be_kind_of HtmlAide::SnippetValidator
9
9
  end
10
10
  end
11
+
12
+ describe '#valid?' do
13
+ context 'when the markup is valid' do
14
+ it 'returns true for a normal snippet' do
15
+ validator = HtmlAide::Validator.validate('<div id="taco">Taco</div>')
16
+ expect(validator).to be_valid
17
+ end
18
+
19
+ it 'returns true for a nested snippet' do
20
+ validator = HtmlAide::Validator.validate('<div id="taco">Taco <strong>Meat</strong></div>')
21
+ expect(validator).to be_valid
22
+ end
23
+
24
+ it 'returns true for valid html tags' do
25
+ validator = HtmlAide::Validator.validate('<input />')
26
+ expect(validator).to be_valid
27
+ end
28
+ end
29
+
30
+ context 'when the markup is not valid' do
31
+ it 'returns false for a broken tag' do
32
+ validator = HtmlAide::Validator.validate('<div id="taco">Taco</div')
33
+ expect(validator).to_not be_valid
34
+ end
35
+ it 'returns false for a nested broken tag' do
36
+ validator = HtmlAide::Validator.validate('<div id="taco">Taco <strong>Meat</strong</div>')
37
+ expect(validator).to_not be_valid
38
+ end
39
+ it 'returns false for invalid tags' do
40
+ validator = HtmlAide::Validator.validate('<taco>Meat</taco>')
41
+ expect(validator).to_not be_valid
42
+ end
43
+ end
44
+ end
11
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_aide
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Woertink
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-25 00:00:00.000000000 Z
11
+ date: 2014-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -112,16 +112,19 @@ files:
112
112
  - lib/html_aide.rb
113
113
  - lib/html_aide/element.rb
114
114
  - lib/html_aide/invalid_element_error.rb
115
+ - lib/html_aide/markup_parser.rb
115
116
  - lib/html_aide/null_element.rb
116
117
  - lib/html_aide/snippet_validator.rb
118
+ - lib/html_aide/tag.rb
117
119
  - lib/html_aide/validator.rb
118
120
  - lib/html_aide/version.rb
119
121
  - spec/element_spec.rb
120
122
  - spec/invalid_element_error_spec.rb
121
123
  - spec/snippet_validator_spec.rb
122
124
  - spec/spec_helper.rb
125
+ - spec/tag_spec.rb
123
126
  - spec/validator_spec.rb
124
- homepage: ''
127
+ homepage: https://github.com/devpointlabs/html_aide
125
128
  licenses:
126
129
  - MIT
127
130
  metadata: {}
@@ -150,4 +153,5 @@ test_files:
150
153
  - spec/invalid_element_error_spec.rb
151
154
  - spec/snippet_validator_spec.rb
152
155
  - spec/spec_helper.rb
156
+ - spec/tag_spec.rb
153
157
  - spec/validator_spec.rb