html_aide 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d0c8ecb947f4e1433c02d4d3c66300908c3d49dc
4
+ data.tar.gz: 9865da568a9172ab2bbd31835e29dd2d582552ed
5
+ SHA512:
6
+ metadata.gz: 9a325f843ec18410222da043149acaa70b1bb2b7cf640184d09e2019f855bd1bf1375b21ed96db3c279af424666d6c5fac1ffadd039654fbecec96fc1c2b7d3b
7
+ data.tar.gz: 6e524d7d532fe5768bd4f5ca9cc890de01ca2e9f5e31d42e0de52560f1aa23ab3aa1cf3f02e036e222f693e73fa25a6eb324d6bb815263cc0564ba351ff38de4
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1 @@
1
+ html_aide
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in html_aide.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jeremy Woertink
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # WARNING:
2
+ this gem is in super early stages, and API may change many times without notice.
3
+
4
+ # HtmlAide
5
+
6
+ Use the HtmlAide to validate html snippets or entire page markup
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'html_aide'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install html_aide
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ snippet = '<div id="taco"><strong>Taco</strong> Meat</div>'
26
+ validator = HtmlAide::Validator.validate(snippet)
27
+ validator.valid? #=> true
28
+ validator.class #=> HtmlAide::SnippetValidator
29
+ validator.errors #=> []
30
+ tag = validator.element #=> HtmlAide::Element
31
+ tag.attributes #=> {:id => "taco"}
32
+ tag.name #= 'div'
33
+ tag.text #=> 'Taco Meat'
34
+ tag.children #=> [<#HtmlAide::Element>]
35
+ tag.children.first.name #=> 'strong'
36
+ tag.children.first.to_s #=> '<strong>Taco</strong>'
37
+ tag.to_s #=> '<div id="taco"><strong>Taco</strong> Meat</div>'
38
+ ```
39
+
40
+ ```ruby
41
+ snippet = '<div>Messed up </div'
42
+ validator = HtmlAide::Validator.validate(snippet)
43
+ validator.valid? #=> false
44
+ validator.class #=> HtmlAide::SnippetValidator
45
+ validator.errors #=> [<#HtmlAide::InvalidElementError>]
46
+ error = validator.errors.first #=> HtmlAide::InvalidElementError
47
+ error.message #=> 'Syntax Error in element'
48
+ validator.element #=> HtmlAide::NullElement
49
+ ```
50
+
51
+ possible helpers
52
+
53
+ ```ruby
54
+ snippet = '<div id="taco"><strong>Taco</strong> Meat</div>'
55
+ snippet.html? #=> true
56
+ validator = snippet.validate_html!
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it ( https://github.com/[my-github-username]/html_aide/fork )
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create a new Pull Request
66
+
67
+
68
+ ## Notes & thoughts
69
+ My current vision for this project is that you can pass in a snippet of html like `<a href="http://www.google.com">Google</a>` and it would return an object that knows the type of element along with the attributes as well as if it's valid or not. An invalid snippet would also return an object, but more of a `NullObject` or something like that which would act similar to a valid object, but would have `errors` method which would include any validation errors that occurred.
70
+
71
+ Instantiating this `HtmlAide::Validation` object would give you the option to choose page or snippet. The full page validation would look for things like doctype, proper head tags, etc... We could proxy this to `tidy` which would need to be required, or somehow included.
72
+
73
+ More advanced markup like `<ul><li>Item</li></ul>` could be passed where the return object (`HtmlAide::HTMLElement` ?) would have a `children` method that could return a collection of the nested elements.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'html_aide/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "html_aide"
8
+ spec.version = HtmlAide::VERSION
9
+ spec.authors = ["Jeremy Woertink"]
10
+ spec.email = ["jeremywoertink@gmail.com"]
11
+ spec.summary = %q{Validate HTML snippets and markup.}
12
+ spec.description = %q{Validate HTML snippets and markup.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_dependency "ox"
26
+ spec.add_dependency "nokogiri"
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'ox'
2
+ require 'nokogiri'
3
+ require 'html_aide/validator'
4
+ require 'html_aide/version'
5
+ require 'html_aide/invalid_element_error'
6
+ require 'html_aide/element'
7
+ require 'html_aide/null_element'
8
+ require 'html_aide/snippet_validator'
9
+
10
+ module HtmlAide
11
+ end
@@ -0,0 +1,34 @@
1
+ module HtmlAide
2
+ class Element
3
+ attr_reader :proxy
4
+
5
+ def initialize(node)
6
+ @proxy = node
7
+ @markup = Ox.to_xml(node).gsub("\n", '')
8
+ end
9
+
10
+ def name
11
+ proxy.name
12
+ end
13
+
14
+ def attributes
15
+ proxy.attributes
16
+ end
17
+
18
+ def text
19
+ @text ||= Nokogiri::HTML.parse(@markup).text.strip
20
+ end
21
+
22
+ def to_s
23
+ @markup
24
+ end
25
+
26
+ def children
27
+ @children ||= proxy.nodes.reject {|n|
28
+ String === n}.collect do |node|
29
+ Element.new(node)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ module HtmlAide
2
+ class InvalidElementError
3
+
4
+ def initialize(error)
5
+ @message = error.to_s
6
+ end
7
+
8
+ def message
9
+ @clean_message ||= @message.gsub(/\[\w+?.+\]/, '').strip
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module HtmlAide
2
+ class NullElement < Element
3
+
4
+ def initialize
5
+ node = Struct.new(:name, :attributes, :nodes).new('Null', {}, [])
6
+ super(node)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,38 @@
1
+ module HtmlAide
2
+ class SnippetValidator
3
+
4
+ def initialize(markup = '')
5
+ @markup = markup
6
+ @errors = []
7
+ create_node_and_set_element
8
+ end
9
+
10
+ # returns the parsed element
11
+ def element
12
+ @element
13
+ end
14
+
15
+ # returns true if the element and all children are valid
16
+ def valid?
17
+ @valid
18
+ end
19
+
20
+ def errors
21
+ @errors
22
+ end
23
+
24
+ private
25
+
26
+ def create_node_and_set_element
27
+ begin
28
+ node = Ox.parse(@markup)
29
+ @element = Element.new(node)
30
+ @valid = true
31
+ rescue Ox::ParseError => e
32
+ @element = NullElement.new
33
+ @errors << InvalidElementError.new(e)
34
+ @valid = false
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ module HtmlAide
2
+ class Validator
3
+ # validate markup and return the instance that validated the markup based on format
4
+ # possible future formats [:snippet, :document]
5
+ def self.validate(markup, format = :snippet)
6
+ @validator = HtmlAide.const_get("#{format.to_s.capitalize}Validator").new(markup)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module HtmlAide
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe HtmlAide::Element do
4
+ before do
5
+ @markup = '<div id="taco"><strong>Taco</strong> Meat</div>'
6
+ @node = Ox.parse(@markup)
7
+ end
8
+
9
+ subject { HtmlAide::Element.new(@node) }
10
+
11
+ describe '#name' do
12
+ it 'returns div' do
13
+ expect(subject.name).to eq 'div'
14
+ end
15
+ end
16
+
17
+ describe '#attributes' do
18
+ it 'returns a hash' do
19
+ expect(subject.attributes).to be_kind_of Hash
20
+ end
21
+
22
+ it 'has the key id with a value "taco"' do
23
+ expect(subject.attributes).to have_key :id
24
+ expect(subject.attributes[:id]).to eq 'taco'
25
+ end
26
+ end
27
+
28
+ describe '#text' do
29
+ it 'returns Taco Meat' do
30
+ expect(subject.text).to eq 'Taco Meat'
31
+ end
32
+ end
33
+
34
+ describe '#to_s' do
35
+ it 'returns the original markup' do
36
+ pending "Ox messes with the markup"
37
+ expect(subject.to_s).to eq @markup
38
+ end
39
+ end
40
+
41
+ describe '#children' do
42
+ it 'returns an array with 1 element' do
43
+ expect(subject.children.count).to eq 1
44
+ end
45
+
46
+ it 'contains a strong Element object' do
47
+ expect(subject.children.collect(&:name)).to include 'strong'
48
+ end
49
+
50
+ it 'has a child element with the text Taco' do
51
+ expect(subject.children.first.text).to eq 'Taco'
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe HtmlAide::InvalidElementError do
4
+
5
+ describe '#message' do
6
+ it 'returns the string "invalid format"' do
7
+ error = HtmlAide::InvalidElementError.new('invalid format')
8
+ expect(error.message).to eq 'invalid format'
9
+ end
10
+
11
+ it 'removes the cruft from Ox' do
12
+ error = HtmlAide::InvalidElementError.new('invalid format, document not terminated at line 1, column 5 [parse.c:785]')
13
+ expect(error.message).to eq 'invalid format, document not terminated at line 1, column 5'
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe HtmlAide::SnippetValidator do
4
+
5
+ context 'with good markup' do
6
+ before do
7
+ @valid_markup = '<div id="taco">Taco Meat</div>'
8
+ end
9
+
10
+ subject { HtmlAide::SnippetValidator.new(@valid_markup) }
11
+
12
+ describe '#element' do
13
+ it 'returns HtmlAide::Element' do
14
+ expect(subject.element).to be_kind_of HtmlAide::Element
15
+ end
16
+ end
17
+
18
+ describe '#valid?' do
19
+ it 'returns true' do
20
+ expect(subject).to be_valid
21
+ end
22
+ end
23
+ end
24
+
25
+ context 'with bad markup' do
26
+ before do
27
+ @invalid_markup = '<div>Messed up </div'
28
+ end
29
+
30
+ subject { HtmlAide::SnippetValidator.new(@invalid_markup) }
31
+
32
+ describe '#element' do
33
+ it 'returns HtmlAide::NullElement' do
34
+ expect(subject.element).to be_kind_of HtmlAide::NullElement
35
+ end
36
+ end
37
+
38
+ describe '#valid?' do
39
+ it 'returns false' do
40
+ expect(subject).to_not be_valid
41
+ end
42
+ end
43
+
44
+ describe '#errors' do
45
+ it 'is an array with 1 error object' do
46
+ expect(subject.errors.count).to eq 1
47
+ expect(subject.errors.first).to be_kind_of HtmlAide::InvalidElementError
48
+ end
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ $: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'html_aide'
6
+
7
+ RSpec.configure do |config|
8
+ config.filter_run focus: true
9
+ config.run_all_when_everything_filtered = true
10
+ config.order = "random"
11
+ config.color = true
12
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe HtmlAide::Validator do
4
+
5
+ describe '#initialize' do
6
+ it 'returns a SnippetValidator' do
7
+ v = HtmlAide::Validator.validate('')
8
+ expect(v).to be_kind_of HtmlAide::SnippetValidator
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_aide
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Woertink
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ox
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: nokogiri
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Validate HTML snippets and markup.
98
+ email:
99
+ - jeremywoertink@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".ruby-gemset"
106
+ - ".ruby-version"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - html_aide.gemspec
112
+ - lib/html_aide.rb
113
+ - lib/html_aide/element.rb
114
+ - lib/html_aide/invalid_element_error.rb
115
+ - lib/html_aide/null_element.rb
116
+ - lib/html_aide/snippet_validator.rb
117
+ - lib/html_aide/validator.rb
118
+ - lib/html_aide/version.rb
119
+ - spec/element_spec.rb
120
+ - spec/invalid_element_error_spec.rb
121
+ - spec/snippet_validator_spec.rb
122
+ - spec/spec_helper.rb
123
+ - spec/validator_spec.rb
124
+ homepage: ''
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.2.2
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Validate HTML snippets and markup.
148
+ test_files:
149
+ - spec/element_spec.rb
150
+ - spec/invalid_element_error_spec.rb
151
+ - spec/snippet_validator_spec.rb
152
+ - spec/spec_helper.rb
153
+ - spec/validator_spec.rb