Merge branch 'master' into develop

This commit is contained in:
Nabil Chatbi 2017-09-07 23:45:37 +02:00
commit 252d7acff6
13 changed files with 66 additions and 141 deletions

1
.swift-version Normal file
View File

@ -0,0 +1 @@
3.2

View File

@ -15,6 +15,7 @@ extension Character {
public static let BackslashN: Character = "\n"
public static let BackslashF: Character = Character(UnicodeScalar(12))
public static let BackslashR: Character = "\r"
public static let BackshashRBackslashN: Character = "\r\n"
//http://www.unicode.org/glossary/#supplementary_code_point
public static let MIN_SUPPLEMENTARY_CODE_POINT: UInt32 = 0x010000
@ -24,6 +25,7 @@ extension Character {
var isWhitespace: Bool {
switch self {
case Character.space, Character.BackslashT, Character.BackslashN,Character.BackslashF,Character.BackslashR: return true
case Character.BackshashRBackslashN: return true
default: return false
}

View File

@ -1288,9 +1288,10 @@ open class Element: Node {
}
override public var hashValue: Int {
var h = super.hashValue
h = Int.addWithOverflow(Int.multiplyWithOverflow(31, h).0, _tag.hashValue).0
return h
let prime = 31
var result = super.hashValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_tag.hashValue).partialValue
return result
}
}

View File

@ -771,8 +771,10 @@ open class Node: Equatable, Hashable {
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
public var hashValue: Int {
var result: Int = description.hashValue
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, baseUri != nil ? baseUri!.hashValue : 31).0
let prime = 31
var result = 1
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(description.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(baseUri?.hashValue ?? 31).partialValue
return result
}

View File

@ -399,31 +399,31 @@ public struct OrderedSetGenerator<T: Hashable>: IteratorProtocol {
extension OrderedSetGenerator where T: Comparable {}
public func +<T: Hashable, S: Sequence> (lhs: OrderedSet<T>, rhs: S) -> OrderedSet<T> where S.Iterator.Element == T {
public func +<T, S: Sequence> (lhs: OrderedSet<T>, rhs: S) -> OrderedSet<T> where S.Iterator.Element == T {
let joinedSet = lhs
joinedSet.append(contentsOf: rhs)
return joinedSet
}
public func +=<T: Hashable, S: Sequence> (lhs: inout OrderedSet<T>, rhs: S) where S.Iterator.Element == T {
public func +=<T, S: Sequence> (lhs: inout OrderedSet<T>, rhs: S) where S.Iterator.Element == T {
lhs.append(contentsOf: rhs)
}
public func -<T: Hashable, S: Sequence> (lhs: OrderedSet<T>, rhs: S) -> OrderedSet<T> where S.Iterator.Element == T {
public func -<T, S: Sequence> (lhs: OrderedSet<T>, rhs: S) -> OrderedSet<T> where S.Iterator.Element == T {
let purgedSet = lhs
purgedSet.remove(rhs)
return purgedSet
}
public func -=<T: Hashable, S: Sequence> (lhs: inout OrderedSet<T>, rhs: S) where S.Iterator.Element == T {
public func -=<T, S: Sequence> (lhs: inout OrderedSet<T>, rhs: S) where S.Iterator.Element == T {
lhs.remove(rhs)
}
extension OrderedSet: Equatable { }
public func ==<T: Hashable> (lhs: OrderedSet<T>, rhs: OrderedSet<T>) -> Bool {
public func ==<T> (lhs: OrderedSet<T>, rhs: OrderedSet<T>) -> Bool {
if lhs.count != rhs.count {
return false
}

View File

@ -8,14 +8,6 @@
import Foundation
#if !os(Linux)
extension NSTextCheckingResult {
func range(at idx: Int) -> NSRange {
return rangeAt(idx)
}
}
#endif
public struct Pattern {
public static let CASE_INSENSITIVE: Int = 0x02
let pattern: String
@ -76,9 +68,9 @@ public class Matcher {
public func group(_ i: Int) -> String? {
let b = matches[index]
let c = b.range(at:i)
let c = b.rangeAt(i)
if(c.location == NSNotFound) {return nil}
let result = string.substring(c.location, c.length)
let result = string.substring(c.location, c.length)
return result
}
public func group() -> String? {

View File

@ -235,16 +235,18 @@ open class Tag: Hashable {
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
public var hashValue: Int {
var result: Int = _tagName.hashValue
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, _isBlock ? 1 : 0).0
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, _formatAsBlock ? 1 : 0).0
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, _canContainBlock ? 1 : 0).0
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, _canContainInline ? 1 : 0).0
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, _empty ? 1 : 0).0
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, _selfClosing ? 1 : 0).0
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, _preserveWhitespace ? 1 : 0).0
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, _formList ? 1 : 0).0
result = Int.addWithOverflow(Int.multiplyWithOverflow(31, result).0, _formSubmit ? 1 : 0).0
let prime = 31
var result = 1
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_tagName.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_isBlock.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_formatAsBlock.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_canContainBlock.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_canContainInline.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_empty.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_selfClosing.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_preserveWhitespace.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_formList.hashValue).partialValue
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(_formSubmit.hashValue).partialValue
return result
}

View File

@ -15,7 +15,7 @@ open class Token {
}
func tokenType() -> String {
return String(describing: type(of: self))
return String(describing: Swift.type(of: self))
}
/**
@ -32,7 +32,7 @@ open class Token {
}
open func toString()throws->String {
return String(describing: type(of: self))
return String(describing: Swift.type(of: self))
}
final class Doctype: Token {

View File

@ -642,7 +642,7 @@ extension TypedValue: Hashable {
public var hashValue: Int {
let prime = 31
var result = 1
result = Int.addWithOverflow(Int.multiplyWithOverflow(prime, result).0, value.hash).0
result = prime.multipliedReportingOverflow(by: result).partialValue.addingReportingOverflow(value.hash).partialValue
return result
}
}

View File

@ -331,7 +331,9 @@
8CE418231DAA54A900240B42 /* Tests */,
8CE418171DAA54A900240B42 /* Products */,
);
indentWidth = 4;
sourceTree = "<group>";
tabWidth = 4;
};
8CE418171DAA54A900240B42 /* Products */ = {
isa = PBXGroup;
@ -476,18 +478,18 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0800;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Nabil Chatbi";
TargetAttributes = {
8CE418151DAA54A900240B42 = {
CreatedOnToolsVersion = 8.0;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
};
8CE4181E1DAA54A900240B42 = {
CreatedOnToolsVersion = 8.0;
DevelopmentTeam = 69CKJW5DBB;
LastSwiftMigration = 0820;
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
};
};
@ -644,7 +646,9 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
@ -652,7 +656,11 @@
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_SUSPICIOUS_MOVES = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
@ -683,6 +691,8 @@
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
@ -698,7 +708,9 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
@ -706,7 +718,11 @@
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_SUSPICIOUS_MOVES = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
@ -729,6 +745,8 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
@ -759,7 +777,6 @@
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
@ -785,7 +802,6 @@
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_VERSION = 3.0;
};
name = Release;
};
@ -805,7 +821,6 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
@ -824,7 +839,6 @@
PRODUCT_BUNDLE_IDENTIFIER = com.scinfu.SwiftSoupTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_VERSION = 3.0;
};
name = Release;
};

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0810"
LastUpgradeVersion = "0900"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
@ -55,6 +56,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"

View File

@ -469,6 +469,14 @@ class DocumentTest: XCTestCase {
// assertTrue("Should have contained a '&#xa0;' or a '&nbsp;'.",
// output.contains("&#xa0;") || output.contains("&nbsp;"));
// }
func testNewLine(){
let h = "<html><body><div>\r\n<div dir=\"ltr\">\r\n<div id=\"divtagdefaultwrapper\"><font face=\"Calibri,Helvetica,sans-serif\" size=\"3\" color=\"black\"><span style=\"font-size:12pt;\" id=\"divtagdefaultwrapper\">\r\n<div style=\"margin-top:0;margin-bottom:0;\">&nbsp;TEST</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\">TEST</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\">TEST</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\">TEST</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\">TEST</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\">TEST</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\">TEST</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\">TEST</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\">TEST</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\"><br>\r\n\r\n</div>\r\n<div style=\"margin-top:0;margin-bottom:0;\">TEST</div>\r\n</span></font></div>\r\n</div>\r\n</div>\r\n</body></html>"
let doc: Document = try! SwiftSoup.parse(h)
let text = try! doc.text()
try! XCTAssertEqual(text, "TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST")
}
static var allTests = {
return [

View File

@ -10,108 +10,9 @@ import XCTest
@testable import SwiftSoup
class SwiftSoupTests: XCTestCase {
var html : String!
override func setUp() {
super.setUp()
let myURLString = "http://www.pointlesssites.com"
guard let myURL = URL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}
html = try! String(contentsOf: myURL, encoding: .utf8)
}
private func getCustomWhiteList() -> Whitelist {
var whiteList: Whitelist?
do {
whiteList = try Whitelist.relaxed().preserveRelativeLinks(true)
.addTags("style")
.addTags("font")
.addTags("center")
.addTags("input")
.addTags("hr")
.addTags("title")
.addTags("iframe")
.addTags("map")
.addTags("area")
.addAttributes("font", "style", "color", "class", "face", "size")
.addAttributes("a", "style", "class")
.addAttributes("b", "style", "class")
.addAttributes("blockquote", "style", "class")
.addAttributes("br", "style", "class")
.addAttributes("caption", "style", "class")
.addAttributes("cite", "style", "class")
.addAttributes("code", "style", "class")
.addAttributes("col", "style", "class")
.addAttributes("colgroup", "style", "class")
.addAttributes("div", "style", "color", "class", "align")
.addAttributes("dl", "style", "class")
.addAttributes("dt", "style", "class")
.addAttributes("em", "style", "class")
.addAttributes("h1", "style", "class")
.addAttributes("h2", "style", "class")
.addAttributes("h3", "style", "class")
.addAttributes("h4", "style", "class")
.addAttributes("h5", "style", "class")
.addAttributes("h6", "style", "class")
.addAttributes("i", "style", "class")
.addAttributes("img", "style", "class", "usemap", "border")
.addAttributes("li", "style", "class")
.addAttributes("ol", "style", "class")
.addAttributes("p", "style", "class", "align")
.addAttributes("pre", "style", "class")
.addAttributes("q", "style", "class")
.addAttributes("small", "style", "class")
.addAttributes("span", "style", "class")
.addAttributes("strike", "style", "class")
.addAttributes("strong", "style", "class")
.addAttributes("sub", "style", "class")
.addAttributes("sup", "style", "class")
.addAttributes("table", "style", "class", "bgcolor", "align", "cellpadding",
"cellspacing", "border", "height", "align", "role", "dir")
.addAttributes("tbody", "style", "class")
.addAttributes("td", "style", "class", "bgcolor", "align", "valign", "height", "tabindex")
.addAttributes("tfoot", "style", "class")
.addAttributes("th", "style", "class", "align")
.addAttributes("thead", "style", "class")
.addAttributes("tr", "style", "class", "valign", "align")
.addAttributes("u", "style", "class")
.addAttributes("ul", "style", "class")
.addAttributes("center", "style", "class")
.addAttributes("style", "type")
.addAttributes("input", "type", "class", "disabled")
.addAttributes("iframe", "src", "style", "class")
.addAttributes("map", "name")
.addAttributes("area", "shape", "coords", "id", "href", "alt")
.addProtocols("img", "src", "cid", "data", "http", "https")
.addProtocols("a", "href", "travelertodo")
.addProtocols("a", "href", "ibmscp")
.addProtocols("a", "href", "sametime")
.addProtocols("a", "href", "stmeetings")
} catch Exception.Error (let type, let message) {
} catch {
}
return whiteList!
}
func testClean()
{
let customWhitelist = getCustomWhiteList()
var cleanHTML: String
do{
let noPrettyPrint = OutputSettings().prettyPrint(pretty: false)
let outputDocument = try SwiftSoup.parse(html.replaceAll(of: "\r\n", with: "\n"))
let bodyElement = outputDocument.body()
outputDocument.outputSettings(noPrettyPrint)
cleanHTML = try SwiftSoup.clean(outputDocument.html(), customWhitelist)!
}catch{
print("")
}
}