nokogiri-tools 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.travis.yml +6 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +29 -0
- data/README.md +26 -0
- data/Rakefile +8 -0
- data/lib/nokogiri-tools.rb +50 -0
- data/lib/nokogiri-tools/version.rb +6 -0
- data/nokogiri_tools.gemspec +24 -0
- data/test/test-nokogiri-tools.rb +46 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 39d6df169e87c1926113baa48a1e2f9aca50add0
|
4
|
+
data.tar.gz: 8dde514294b266ce214fd1583ac91c505eaf1fdb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7903138ec772c0a9fc23c878532531959fb2fa8081730ba9b3a7b1d1351188a186ec8623e79017b036ffa0a8bf440b36a1534a5b65ceb9863260b801d71ca1e8
|
7
|
+
data.tar.gz: 54b8541845af02d83c733dc69e89a401165174fe3b1d0114954504eccd69dd22376000c21fdf74c873c0ebf5756e221ee9077f3c42a3c029a5624dd06dd627db
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nokogiri-tools (1.0.0)
|
5
|
+
nokogiri
|
6
|
+
unicode-tools (~> 1.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
mini_portile2 (2.1.0)
|
12
|
+
nokogiri (1.7.1)
|
13
|
+
mini_portile2 (~> 2.1.0)
|
14
|
+
power_assert (1.0.1)
|
15
|
+
rake (12.0.0)
|
16
|
+
test-unit (3.2.3)
|
17
|
+
power_assert
|
18
|
+
unicode-tools (1.0.2)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
nokogiri-tools!
|
25
|
+
rake (~> 12.0)
|
26
|
+
test-unit (~> 3.2)
|
27
|
+
|
28
|
+
BUNDLED WITH
|
29
|
+
1.14.3
|
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
## Extends Nokogiri with useful methods.
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/nokogiri-tools.svg)](https://badge.fury.io/rb/nokogiri-tools)
|
4
|
+
[![Build Status](https://travis-ci.org/yivo/nokogiri-tools.svg?branch=master)](https://travis-ci.org/yivo/nokogiri-tools)
|
5
|
+
|
6
|
+
## Installing gem
|
7
|
+
Add to your Gemfile:
|
8
|
+
```ruby
|
9
|
+
gem 'nokogiri-tools', '~> 1.0'
|
10
|
+
```
|
11
|
+
|
12
|
+
## Running Tests
|
13
|
+
Install bundler:
|
14
|
+
```bash
|
15
|
+
gem install bundler
|
16
|
+
```
|
17
|
+
|
18
|
+
Install dependencies:
|
19
|
+
```bash
|
20
|
+
cd nokogiri-tools && bundle
|
21
|
+
```
|
22
|
+
|
23
|
+
Run tests:
|
24
|
+
```bash
|
25
|
+
cd nokogiri-tools && bundle exec rake test
|
26
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module NokogiriTools
|
5
|
+
module CSS
|
6
|
+
def css_properties
|
7
|
+
"#{self['style']};".scan(/(\w[\w-]*\w):\s*([^;]+);/)
|
8
|
+
end
|
9
|
+
|
10
|
+
def css_properties=(properties)
|
11
|
+
style = properties.map { |x| "#{x[0]}: #{x[1]}" }
|
12
|
+
style.empty? ? delete('style') : self['style'] = style.join('; ')
|
13
|
+
end
|
14
|
+
|
15
|
+
def css_classes
|
16
|
+
self['class'].to_s.split(/[[:blank:]]+/)
|
17
|
+
end
|
18
|
+
|
19
|
+
def css_classes=(classes)
|
20
|
+
value = [classes]
|
21
|
+
value.flatten!
|
22
|
+
value.map!(&:squish)
|
23
|
+
value.empty? ? delete('class') : self['class'] = value.join(' ')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module Tree
|
28
|
+
def remove_keeping_children
|
29
|
+
children.each { |child| before(child) }
|
30
|
+
remove
|
31
|
+
end
|
32
|
+
|
33
|
+
def remove_with_parent
|
34
|
+
parent ? parent.remove : remove
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module Data
|
39
|
+
def remove_data_attributes
|
40
|
+
attributes.keys.each { |name| delete(name) if name.start_with?('data-') }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
require 'nokogiri'
|
46
|
+
require 'unicode-tools'
|
47
|
+
|
48
|
+
Nokogiri::XML::Node.include NokogiriTools::CSS
|
49
|
+
Nokogiri::XML::Node.include NokogiriTools::Tree
|
50
|
+
Nokogiri::XML::Node.include NokogiriTools::Data
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require File.expand_path('../lib/nokogiri-tools/version', __FILE__)
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'nokogiri-tools'
|
8
|
+
s.version = NokogiriTools::VERSION
|
9
|
+
s.author = 'Yaroslav Konoplov'
|
10
|
+
s.email = 'eahome00@gmail.com'
|
11
|
+
s.summary = 'Extends Nokogiri with useful methods.'
|
12
|
+
s.description = 'Extends Nokogiri with useful methods.'
|
13
|
+
s.homepage = 'https://github.com/yivo/nokogiri-tools'
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
16
|
+
s.files = `git ls-files -z`.split("\x0")
|
17
|
+
s.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.add_dependency 'nokogiri'
|
21
|
+
s.add_dependency 'unicode-tools', '~> 1.0'
|
22
|
+
s.add_development_dependency 'rake', '~> 12.0'
|
23
|
+
s.add_development_dependency 'test-unit', '~> 3.2'
|
24
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'test-unit'
|
5
|
+
require 'nokogiri-tools'
|
6
|
+
|
7
|
+
class NokogiriToolsTest < Test::Unit::TestCase
|
8
|
+
def test_css_classes
|
9
|
+
div = fragment('<div class="foo"></div>').children.first
|
10
|
+
assert_equal %w( foo ), div.css_classes
|
11
|
+
|
12
|
+
div.css_classes = %w( foo bar )
|
13
|
+
assert_equal %w( foo bar ), div.css_classes
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_css_styles
|
17
|
+
div = fragment('<div style="color: #000; width: 100%;"></div>').children.first
|
18
|
+
assert_equal [['color', '#000'], ['width', '100%']], div.css_properties
|
19
|
+
|
20
|
+
div.css_properties = [['height', '100px'], ['color', 'red']]
|
21
|
+
assert_equal [['height', '100px'], ['color', 'red']], div.css_properties
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_data
|
25
|
+
div = fragment('<div data-foo="foo" data-bar="bar">test</div>').children.first
|
26
|
+
div.remove_data_attributes
|
27
|
+
assert_equal '<div>test</div>', div.to_html
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_parent_removal
|
31
|
+
fragment = fragment('<div>1<div>2</div></div>')
|
32
|
+
fragment.children.first.remove_keeping_children
|
33
|
+
assert_equal '1<div>2</div>', fragment.to_html
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_removal_including_parent
|
37
|
+
fragment = fragment('<div><div>test</div></div>')
|
38
|
+
fragment.children.first.children.first.remove_with_parent
|
39
|
+
assert_equal '', fragment.to_html
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
def fragment(html)
|
44
|
+
Nokogiri::HTML.fragment(html) { |config| config.nonet.huge.nowarning.noerror }
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nokogiri-tools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yaroslav Konoplov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: unicode-tools
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '12.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '12.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: test-unit
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
69
|
+
description: Extends Nokogiri with useful methods.
|
70
|
+
email: eahome00@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- ".gitignore"
|
76
|
+
- ".travis.yml"
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/nokogiri-tools.rb
|
82
|
+
- lib/nokogiri-tools/version.rb
|
83
|
+
- nokogiri_tools.gemspec
|
84
|
+
- test/test-nokogiri-tools.rb
|
85
|
+
homepage: https://github.com/yivo/nokogiri-tools
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.6.8
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Extends Nokogiri with useful methods.
|
109
|
+
test_files:
|
110
|
+
- test/test-nokogiri-tools.rb
|