8000 Various optimizations by aehlke · Pull Request #304 · scinfu/SwiftSoup · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Various optimizations #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to 8000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Sources/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,22 @@ open class Attributes: NSCopying {
@return HTML
@throws SerializationException if the HTML representation of the attributes cannot be constructed.
*/
open func html()throws -> String {
open func html() throws -> String {
let accum = StringBuilder()
try html(accum: accum, out: Document([]).outputSettings()) // output settings a bit funky, but this html() seldom used
return accum.toString()
}

/**
Get the HTML representation of these attributes.
@return HTML
@throws SerializationException if the HTML representation of the attributes cannot be constructed.
*/
open func htmlUTF8() throws -> [UInt8] {
let accum = StringBuilder()
try html(accum: accum, out: Document([]).outputSettings()) // output settings a bit funky, but this html() seldom used
10000 return accum.buffer
}

@inlinable
public func html(accum: StringBuilder, out: OutputSettings ) throws {
Expand Down
52 changes: 47 additions & 5 deletions Sources/Element.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ open class Element: Node {
public init(_ tag: Tag, _ baseUri: [UInt8], _ attributes: Attributes) {
self._tag = tag
super.init(baseUri, attributes)
childNodes.reserveCapacity(8)
}
/**
* Create a new Element from a tag and a base URI.
Expand All @@ -48,6 +49,7 @@ open class Element: Node {
public init(_ tag: Tag, _ baseUri: [UInt8]) {
self._tag = tag
super.init(baseUri, Attributes())
childNodes.reserveCapacity(8)
}

public override func nodeNameUTF8() -> [UInt8] {
Expand Down Expand Up @@ -1015,6 +1017,16 @@ open class Element: Node {
}
return text
}

public func textUTF8(trimAndNormaliseWhitespace: Bool = true) throws -> [UInt8] {
let accum: StringBuilder = StringBuilder()
try NodeTraversor(TextNodeVisitor(accum, trimAndNormaliseWhitespace: trimAndNormaliseWhitespace)).traverse(self)
let text = accum.buffer
if trimAndNormaliseWhitespace {
return text.trim()
}
return text
}

/**
* Gets the text owned by this element only; does not get the combined text of all children.
Expand All @@ -1032,6 +1044,23 @@ open class Element: Node {
ownText(sb)
return sb.toString().trim()
}

/**
* Gets the text owned by this element only; does not get the combined text of all children.
* <p>
* For example, given HTML {@code <p>Hello <b>there</b> now!</p>}, {@code p.ownText()} returns {@code "Hello now!"},
* whereas {@code p.text()} returns {@code "Hello there now!"}.
* Note that the text within the {@code b} element is not returned, as it is not a direct child of the {@code p} element.
*
* @return unencoded text, or empty string if none.
* @see #text()
* @see #textNodes()
*/
public func ownTextUTF8() -> [UInt8] {
let sb: StringBuilder = StringBuilder()
ownText(sb)
return sb.buffer.trim()
}

private func ownText(_ accum: StringBuilder) {
for child: Node in childNodes {
Expand Down Expand Up @@ -1244,7 +1273,7 @@ open class Element: Node {
@return this element
*/
@discardableResult
public func addClass(_ className: String)throws->Element {
public func addClass(_ className: String) throws -> Element {
let classes: OrderedSet<String> = try classNames()
classes.append(className)
try classNames(classes)
Expand All @@ -1257,7 +1286,7 @@ open class Element: Node {
@return this element
*/
@discardableResult
public func removeClass(_ className: String)throws->Element {
public func removeClass(_ className: String) throws -> Element {
let classes: OrderedSet<String> = try classNames()
classes.remove(className)
try classNames(classes)
Expand All @@ -1270,7 +1299,7 @@ open class Element: Node {
@return this element
*/
@discardableResult
public func toggleClass(_ className: String)throws->Element {
public func toggleClass(_ className: String) throws -> Element {
let classes: OrderedSet<String> = try classNames()
if (classes.contains(className)) {classes.remove(className)
} else {
Expand Down Expand Up @@ -1354,6 +1383,19 @@ open class Element: Node {
try html2(accum)
return getOutputSettings().prettyPrint() ? accum.toString().trim() : accum.toString()
}

/**
* Retrieves the element's inner HTML. E.g. on a {@code <div>} with one empty {@code <p>}, would return
* {@code <p></p>}. (Whereas {@link #outerHtml()} would return {@code <div><p></p></div>}.)
*
* @return String of HTML.
* @see #outerHtml()
*/
public func htmlUTF8() throws -> [UInt8] {
let accum: StringBuilder = StringBuilder()
try html2(accum)
return getOutputSettings().prettyPrint() ? accum.buffer.trim() : accum.buffer
}

private func html2(_ accum: StringBuilder) throws {
for node in childNodes {
Expand All @@ -1364,7 +1406,7 @@ open class Element: Node {
/**
* {@inheritDoc}
*/
open override func html(_ appendable: StringBuilder)throws->StringBuilder {
open override func html(_ appendable: StringBuilder) throws -> StringBuilder {
for node in childNodes {
try node.outerHtml(appendable)
}
Expand All @@ -1378,7 +1420,7 @@ open class Element: Node {
* @see #append(String)
*/
@discardableResult
public func html(_ html: String)throws->Element {
public func html(_ html: String) throws -> Element {
empty()
try append(html)
return self
Expand Down
Loading
0