Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74e31628d0 | ||
|
|
ed149868fa | ||
|
|
3e11d3b1a6 | ||
|
|
1c3e758e66 | ||
|
|
6e2c7f6f56 | ||
|
|
6b71cfa931 | ||
|
|
3dad76a6cc | ||
|
|
a9974e706d | ||
|
|
99ed0dc05e | ||
|
|
8dc01b9156 | ||
|
|
bc8eb4cda6 | ||
|
|
fe0918b88f | ||
|
|
89fa0b8601 | ||
|
|
046856f5eb | ||
|
|
cae8f6da0a | ||
|
|
db34356431 | ||
|
|
09593b7a92 | ||
|
|
8e5263131f | ||
|
|
6eca7f6e5c | ||
|
|
52a051450d | ||
|
|
f9a084323d | ||
|
|
ca93c08d53 | ||
|
|
a5d3d8ae37 | ||
|
|
1932f6ac83 |
No files matched your search
@@ -0,0 +1,62 @@
|
||||
on:
|
||||
workflow_dispatch:
|
||||
name: Update package dependencies
|
||||
jobs:
|
||||
dep_update:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 10
|
||||
|
||||
steps:
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.20.x
|
||||
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Update minor and patch-level dependencies
|
||||
run: go get -t -u ./...
|
||||
|
||||
- name: Tidy
|
||||
run: go mod tidy
|
||||
|
||||
- name: Create pull request
|
||||
uses: peter-evans/create-pull-request@v4
|
||||
with:
|
||||
title: "Update package dependencies + tidy"
|
||||
body: |
|
||||
This is a change initiated automatically on a weekly basis by a
|
||||
GitHub Action that updates the projects dependencies to their latest
|
||||
minor and patch-level versions. This lets us stay up to date
|
||||
incrementally so that updates are less effort to get merged compared
|
||||
to large monolithic updates, and gets us security updates more
|
||||
expediently.
|
||||
|
||||
If the build passes, you are probably A-OK to merge and deploy this.
|
||||
If not, try to dig into what's not working and see if you can fix it
|
||||
so that the dep train stays on its rails.
|
||||
|
||||
Note that although minor/patch level changes are handled
|
||||
automatically, notably major version changes like you'd find in
|
||||
stripe-go are not and those upgrades need to be performed manually.
|
||||
That should theoretically not be a problem if fixes are backported
|
||||
to all previous majors, but in practice they are often not, so it's
|
||||
worthwhile to occasionally look for new majors and integrate them.
|
||||
branch: "autodep"
|
||||
branch-suffix: "timestamp"
|
||||
commit-message: |
|
||||
Update package dependencies + tidy
|
||||
|
||||
Weekly update to the project's package dependencies initiated by an
|
||||
automatic GitHub Action running on cron. Keeps upgrades less of a
|
||||
monolithic task and lets security-related patches trickle in more
|
||||
quickly.
|
||||
author: "Bot <bot@iandavis.com>"
|
||||
committer: "Bot <bot@iandavis.com>"
|
||||
delete-branch: true
|
||||
draft: true
|
||||
reviewers: |
|
||||
iand
|
||||
assignees: |
|
||||
iand
|
||||
@@ -0,0 +1,45 @@
|
||||
on:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
types: [opened, reopened, ready_for_review, synchronize]
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
name: Run checks
|
||||
jobs:
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.20.x
|
||||
- name: Get StaticCheck
|
||||
run: go install honnef.co/go/tools/cmd/staticcheck@v0.4.2 # Version 2023.1.2 (v0.4.2)
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: recursive
|
||||
- name: Gomod
|
||||
run: |
|
||||
go mod tidy
|
||||
if [[ -n $(git ls-files --other --exclude-standard --directory -- go.sum) ]]; then
|
||||
echo "go.sum was added by go mod tidy"
|
||||
exit 1
|
||||
fi
|
||||
git diff --exit-code -- go.sum go.mod
|
||||
- name: Gofmt
|
||||
if: ${{ success() || failure() }} # run this step even if the previous one failed
|
||||
run: |
|
||||
out=$(gofmt -s -l .)
|
||||
if [[ -n "$out" ]]; then
|
||||
echo $out | awk '{print "::error file=" $0 ",line=0,col=0::File is not gofmt-ed."}'
|
||||
exit 1
|
||||
fi
|
||||
- name: Vet
|
||||
if: ${{ success() || failure() }} # run this step even if the previous one failed
|
||||
run: go vet ./...
|
||||
- name: StaticCheck
|
||||
if: ${{ success() || failure() }} # run this step even if the previous one failed
|
||||
run: staticcheck ./...
|
||||
@@ -0,0 +1,46 @@
|
||||
on:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
types: [opened, reopened, ready_for_review, synchronize]
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
name: Test Go tip
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.20.x
|
||||
- name: Install Go tip
|
||||
run: |
|
||||
go install golang.org/dl/gotip@latest
|
||||
gotip download
|
||||
gotip version
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: recursive
|
||||
- id: Cache
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: |
|
||||
~/go/pkg/mod # Module download cache
|
||||
~/.cache/go-build # Build cache (Linux)
|
||||
key: ubuntu-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
ubuntu-go-
|
||||
- name: Dependencies
|
||||
run: gotip mod download
|
||||
if: steps.cache.outputs.cache-hit != 'true'
|
||||
- name: Test
|
||||
run: gotip test ./...
|
||||
- name: Test 32 bit
|
||||
env:
|
||||
GOARCH: 386
|
||||
run: gotip test ./...
|
||||
- name: Test with race detector
|
||||
run: gotip test -race ./...
|
||||
@@ -0,0 +1,49 @@
|
||||
on:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
types: [opened, reopened, ready_for_review, synchronize]
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
name: Run tests
|
||||
jobs:
|
||||
test:
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [1.19.x, 1.20.x]
|
||||
os: ["ubuntu", "windows", "macos"]
|
||||
runs-on: ${{ matrix.os }}-latest
|
||||
steps:
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: recursive
|
||||
- id: Cache
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: |
|
||||
~/go/pkg/mod # Module download cache
|
||||
~/.cache/go-build # Build cache (Linux)
|
||||
~/Library/Caches/go-build # Build cache (Mac)
|
||||
'%LocalAppData%\go-build' # Build cache (Windows)
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-
|
||||
- name: Dependencies
|
||||
run: go mod download
|
||||
if: steps.cache.outputs.cache-hit != 'true'
|
||||
- name: Test
|
||||
run: go test ./...
|
||||
- name: Test 32 bit
|
||||
if: ${{ matrix.os != 'macos' }} # can't run 32 bit tests on OSX.
|
||||
env:
|
||||
GOARCH: 386
|
||||
run: go test ./...
|
||||
- name: Test with race detector
|
||||
if: ${{ matrix.os == 'ubuntu' }} # speed things up. Windows and OSX VMs are slow
|
||||
run: go test -race ./...
|
||||
@@ -0,0 +1,17 @@
|
||||
name: Bump version
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Bump version and push tag
|
||||
id: tag_version
|
||||
uses: mathieudutour/github-tag-action@v6.1
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- "1.14.x"
|
||||
- "1.15.x"
|
||||
- tip
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- go: tip
|
||||
fast_finish: true
|
||||
|
||||
before_script:
|
||||
- GO_FILES=$(find . -iname '*.go' | grep -v /vendor/)
|
||||
- PKGS=$(go list ./... | grep -v /vendor/)
|
||||
|
||||
script:
|
||||
- test -z $(gofmt -s -l $GO_FILES)
|
||||
- go test -v -race $PKGS
|
||||
- go vet $PKGS
|
||||
@@ -1,3 +1,5 @@
|
||||
module github.com/iand/microdata
|
||||
|
||||
go 1.16
|
||||
|
||||
require golang.org/x/net v0.11.0
|
||||
@@ -0,0 +1,40 @@
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU=
|
||||
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
+65
-83
@@ -121,8 +121,18 @@ func (p *Parser) Parse() (*Microdata, error) {
|
||||
})
|
||||
|
||||
for _, node := range topLevelItemNodes {
|
||||
item := NewItem()
|
||||
p.data.Items = append(p.data.Items, item)
|
||||
p.data.Items = append(p.data.Items, p.readItem(nil, node))
|
||||
}
|
||||
|
||||
return p.data, nil
|
||||
}
|
||||
|
||||
func (p *Parser) readItem(item *Item, node *html.Node) *Item {
|
||||
var parent *Item
|
||||
|
||||
if _, exists := getAttr("itemscope", node); exists {
|
||||
parent, item = item, NewItem()
|
||||
|
||||
if itemtypes, exists := getAttr("itemtype", node); exists {
|
||||
for _, itemtype := range strings.Split(strings.TrimSpace(itemtypes), " ") {
|
||||
itemtype = strings.TrimSpace(itemtype)
|
||||
@@ -143,105 +153,76 @@ func (p *Parser) Parse() (*Microdata, error) {
|
||||
itemref = strings.TrimSpace(itemref)
|
||||
|
||||
if refnode, exists := p.identifiedNodes[itemref]; exists {
|
||||
p.readItem(item, refnode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for child := node.FirstChild; child != nil; {
|
||||
p.readItem(item, child)
|
||||
child = child.NextSibling
|
||||
}
|
||||
}
|
||||
|
||||
return p.data, nil
|
||||
}
|
||||
|
||||
func (p *Parser) readItem(item *Item, node *html.Node) {
|
||||
if itemprop, exists := getAttr("itemprop", node); exists {
|
||||
if _, exists := getAttr("itemscope", node); exists {
|
||||
subitem := NewItem()
|
||||
|
||||
if itemrefs, exists := getAttr("itemref", node); exists {
|
||||
for _, itemref := range strings.Split(strings.TrimSpace(itemrefs), " ") {
|
||||
itemref = strings.TrimSpace(itemref)
|
||||
|
||||
if refnode, exists := p.identifiedNodes[itemref]; exists {
|
||||
if refnode != node {
|
||||
p.readItem(subitem, refnode)
|
||||
}
|
||||
if refnode != node {
|
||||
p.readItem(item, refnode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for child := node.FirstChild; child != nil; {
|
||||
p.readItem(subitem, child)
|
||||
child = child.NextSibling
|
||||
}
|
||||
|
||||
if itemprop, exists := getAttr("itemprop", node); exists {
|
||||
if parent != nil {
|
||||
// an itemprop on an itemscope has value of the item created by the itemscope
|
||||
for _, propertyName := range strings.Split(strings.TrimSpace(itemprop), " ") {
|
||||
propertyName = strings.TrimSpace(propertyName)
|
||||
if propertyName != "" {
|
||||
item.AddItem(propertyName, subitem)
|
||||
parent.AddItem(propertyName, item)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var propertyValue string
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
var propertyValue string
|
||||
|
||||
switch node.DataAtom {
|
||||
case atom.Meta:
|
||||
if val, exists := getAttr("content", node); exists {
|
||||
propertyValue = val
|
||||
}
|
||||
case atom.Audio, atom.Embed, atom.Iframe, atom.Img, atom.Source, atom.Track, atom.Video:
|
||||
if urlValue, exists := getAttr("src", node); exists {
|
||||
if parsedURL, err := p.base.Parse(urlValue); err == nil {
|
||||
propertyValue = parsedURL.String()
|
||||
switch node.DataAtom {
|
||||
case atom.Meta:
|
||||
if val, exists := getAttr("content", node); exists {
|
||||
propertyValue = val
|
||||
}
|
||||
}
|
||||
case atom.A, atom.Area, atom.Link:
|
||||
if urlValue, exists := getAttr("href", node); exists {
|
||||
if parsedURL, err := p.base.Parse(urlValue); err == nil {
|
||||
propertyValue = parsedURL.String()
|
||||
case atom.Audio, atom.Embed, atom.Iframe, atom.Img, atom.Source, atom.Track, atom.Video:
|
||||
if urlValue, exists := getAttr("src", node); exists {
|
||||
if parsedURL, err := p.base.Parse(urlValue); err == nil {
|
||||
propertyValue = parsedURL.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
case atom.Object:
|
||||
if urlValue, exists := getAttr("data", node); exists {
|
||||
propertyValue = urlValue
|
||||
}
|
||||
case atom.Data, atom.Meter:
|
||||
if urlValue, exists := getAttr("value", node); exists {
|
||||
propertyValue = urlValue
|
||||
}
|
||||
case atom.Time:
|
||||
if urlValue, exists := getAttr("datetime", node); exists {
|
||||
propertyValue = urlValue
|
||||
}
|
||||
|
||||
default:
|
||||
var text bytes.Buffer
|
||||
walk(node, func(n *html.Node) {
|
||||
if n.Type == html.TextNode {
|
||||
text.WriteString(n.Data)
|
||||
case atom.A, atom.Area, atom.Link:
|
||||
if urlValue, exists := getAttr("href", node); exists {
|
||||
if parsedURL, err := p.base.Parse(urlValue); err == nil {
|
||||
propertyValue = parsedURL.String()
|
||||
}
|
||||
}
|
||||
case atom.Object:
|
||||
if urlValue, exists := getAttr("data", node); exists {
|
||||
propertyValue = urlValue
|
||||
}
|
||||
case atom.Data, atom.Meter:
|
||||
if urlValue, exists := getAttr("value", node); exists {
|
||||
propertyValue = urlValue
|
||||
}
|
||||
case atom.Time:
|
||||
if urlValue, exists := getAttr("datetime", node); exists {
|
||||
propertyValue = urlValue
|
||||
}
|
||||
|
||||
})
|
||||
propertyValue = text.String()
|
||||
}
|
||||
default:
|
||||
var text bytes.Buffer
|
||||
walk(node, func(n *html.Node) {
|
||||
if n.Type == html.TextNode {
|
||||
text.WriteString(n.Data)
|
||||
}
|
||||
|
||||
if len(propertyValue) > 0 {
|
||||
for _, propertyName := range strings.Split(strings.TrimSpace(itemprop), " ") {
|
||||
propertyName = strings.TrimSpace(propertyName)
|
||||
if propertyName != "" {
|
||||
item.AddString(propertyName, propertyValue)
|
||||
})
|
||||
propertyValue = text.String()
|
||||
}
|
||||
|
||||
if len(propertyValue) > 0 {
|
||||
for _, propertyName := range strings.Split(strings.TrimSpace(itemprop), " ") {
|
||||
propertyName = strings.TrimSpace(propertyName)
|
||||
if propertyName != "" {
|
||||
item.AddString(propertyName, propertyValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for child := node.FirstChild; child != nil; {
|
||||
@@ -249,6 +230,7 @@ func (p *Parser) readItem(item *Item, node *html.Node) {
|
||||
child = child.NextSibling
|
||||
}
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
func getAttr(name string, node *html.Node) (string, bool) {
|
||||
|
||||
+69
-2
@@ -583,8 +583,9 @@ func TestSkipSelfReferencingItemref(t *testing.T) {
|
||||
actual := ParseData(html, t)
|
||||
|
||||
child := NewItem()
|
||||
child.AddString("title", "Foo")
|
||||
child.AddType("http://data-vocabulary.org/Breadcrumb")
|
||||
child.AddString("url", "http://example.com/foo/bar")
|
||||
child.AddString("title", "Foo")
|
||||
|
||||
item := NewItem()
|
||||
item.AddType("http://schema.org/WebPage")
|
||||
@@ -594,6 +595,72 @@ func TestSkipSelfReferencingItemref(t *testing.T) {
|
||||
expected.AddItem(item)
|
||||
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("Expecting %s but got %s", expected, actual)
|
||||
t.Errorf("Expecting %v but got %v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
// This test validates that properties within an itemscope'd element remain
|
||||
// with that contained item even if it is not, via itemprop, made an explicit child
|
||||
// of its container item.
|
||||
func TestPropertiesInContainedItem(t *testing.T) {
|
||||
html := `
|
||||
<body itemscope itemtype="http://schema.org/WebPage">
|
||||
<meta itemprop="foo" content="foo value">
|
||||
|
||||
<div itemscope itemtype="http://schema.org/Person">
|
||||
<meta itemprop="bar" content="bar value">
|
||||
</div>
|
||||
<div itemscope itemtype="http://schema.org/Person" itemprop="author">
|
||||
<meta itemprop="baz" content="baz value">
|
||||
</div>
|
||||
</body>`
|
||||
|
||||
actual := ParseData(html, t)
|
||||
|
||||
// There should be two top-level items: WebPage and the Person that isn't an author.
|
||||
if len(actual.Items) != 2 {
|
||||
t.Fatalf("expected 2 top-level items, got %d", len(actual.Items))
|
||||
}
|
||||
|
||||
outer := actual.Items[0]
|
||||
inner := actual.Items[1]
|
||||
|
||||
// The first item should be a WebPage. The properties in its contained items should
|
||||
// not be properties of the containing item.
|
||||
if len(outer.Types) != 1 || outer.Types[0] != "http://schema.org/WebPage" {
|
||||
t.Fatalf("expected outer to be http://schema.org/WebPage, got %v", outer.Types)
|
||||
}
|
||||
if _, present := outer.Properties["bar"]; present {
|
||||
t.Errorf("outer should not have a 'bar' property, got %v", outer.Properties["bar"])
|
||||
}
|
||||
if _, present := outer.Properties["baz"]; present {
|
||||
t.Errorf("outer should not have a 'baz' property, got %v", outer.Properties["baz"])
|
||||
}
|
||||
|
||||
// The second item should be the non-author child element of outer. Since there is
|
||||
// no itemprop attribute, it's not a child *item* of outer but it should stand as its
|
||||
// own item.
|
||||
if len(inner.Types) != 1 || inner.Types[0] != "http://schema.org/Person" {
|
||||
t.Fatalf("expected inner to be http://schema.org/Person, got %v", inner.Types)
|
||||
}
|
||||
if _, present := inner.Properties["bar"]; !present {
|
||||
t.Errorf("inner should have a 'bar' property")
|
||||
}
|
||||
|
||||
// The third item is the author, which should be a child item (via the 'author' itemprop)
|
||||
// of outer. It, too, should have its own discrete type and property.
|
||||
if list := outer.Properties["author"]; len(list) == 1 {
|
||||
if author, ok := list[0].(*Item); ok {
|
||||
if len(author.Types) != 1 || author.Types[0] != "http://schema.org/Person" {
|
||||
t.Fatalf("expected author to be http://schema.org/Person, got %v", author.Types)
|
||||
}
|
||||
if _, present := author.Properties["baz"]; !present {
|
||||
t.Errorf("inner should have a 'baz' property")
|
||||
}
|
||||
} else {
|
||||
t.Errorf("expected author item, got %v", list)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("expected outer to have a child of author, got %v", outer.Properties["author"])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user